summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/shared-modules/NNTPHelpers.jsm
blob: 71c785c02c125a8b9b26a087d3d7b2168ca755ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const EXPORTED_SYMBOLS = [
  "setupNNTPDaemon",
  "NNTP_PORT",
  "setupLocalServer",
  "startupNNTPServer",
  "shutdownNNTPServer",
];

var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var { NewsArticle, NNTP_RFC977_handler, NntpDaemon } = ChromeUtils.import(
  "resource://testing-common/mailnews/Nntpd.jsm"
);
var { nsMailServer } = ChromeUtils.import(
  "resource://testing-common/mailnews/Maild.jsm"
);

var kSimpleNewsArticle =
  "From: John Doe <john.doe@example.com>\n" +
  "Date: Sat, 24 Mar 1990 10:59:24 -0500\n" +
  "Newsgroups: test.subscribe.simple\n" +
  "Subject: H2G2 -- What does it mean?\n" +
  "Message-ID: <TSS1@nntp.invalid>\n" +
  "\n" +
  "What does the acronym H2G2 stand for? I've seen it before...\n";

// The groups to set up on the fake server.
// It is an array of tuples, where the first element is the group name and the
// second element is whether or not we should subscribe to it.
var groups = [
  ["test.empty", false],
  ["test.subscribe.empty", true],
  ["test.subscribe.simple", true],
  ["test.filter", true],
];

// Sets up the NNTP daemon object for use in fake server
function setupNNTPDaemon() {
  var daemon = new NntpDaemon();

  groups.forEach(function (element) {
    daemon.addGroup(element[0]);
  });

  var article = new NewsArticle(kSimpleNewsArticle);
  daemon.addArticleToGroup(article, "test.subscribe.simple", 1);

  return daemon;
}

// Startup server
function startupNNTPServer(daemon, port) {
  var handler = NNTP_RFC977_handler;

  function createHandler(daemon) {
    return new handler(daemon);
  }

  var server = new nsMailServer(createHandler, daemon);
  server.start(port);
  return server;
}

// Shutdown server
function shutdownNNTPServer(server) {
  server.stop();
}

// Enable strict threading
Services.prefs.setBoolPref("mail.strict_threading", true);

// Make sure we don't try to use a protected port. I like adding 1024 to the
// default port when doing so...
var NNTP_PORT = 1024 + 119;

var _server = null;

function subscribeServer(incomingServer) {
  // Subscribe to newsgroups
  incomingServer.QueryInterface(Ci.nsINntpIncomingServer);
  groups.forEach(function (element) {
    if (element[1]) {
      incomingServer.subscribeToNewsgroup(element[0]);
    }
  });
  // Only allow one connection
  incomingServer.maximumConnectionsNumber = 1;
}

// Sets up the client-side portion of fakeserver
function setupLocalServer(port) {
  if (_server != null) {
    return _server;
  }

  var server = MailServices.accounts.createIncomingServer(
    null,
    "localhost",
    "nntp"
  );
  server.port = port;
  server.valid = false;

  var account = MailServices.accounts.createAccount();
  account.incomingServer = server;
  server.valid = true;
  // hack to cause an account loaded notification now the server is valid
  // (see also Bug 903804)
  account.incomingServer = account.incomingServer; // eslint-disable-line no-self-assign

  subscribeServer(server);

  _server = server;

  return server;
}