summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/local/src/Pop3Channel.jsm
blob: 4fae72894b817a6b61541701c8523c35da9f62bb (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
/* 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/. */

const EXPORTED_SYMBOLS = ["Pop3Channel"];

const { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

const lazy = {};

XPCOMUtils.defineLazyModuleGetters(lazy, {
  Pop3Client: "resource:///modules/Pop3Client.jsm",
});

/**
 * A channel to interact with POP3 server.
 *
 * @implements {nsIChannel}
 * @implements {nsIRequest}
 */
class Pop3Channel {
  QueryInterface = ChromeUtils.generateQI(["nsIChannel", "nsIRequest"]);

  _logger = console.createInstance({
    prefix: "mailnews.pop3",
    maxLogLevel: "Warn",
    maxLogLevelPref: "mailnews.pop3.loglevel",
  });

  /**
   * @param {nsIURI} uri - The uri to construct the channel from.
   * @param {nsILoadInfo} loadInfo - The loadInfo associated with the channel.
   */
  constructor(uri, loadInfo) {
    this._server = MailServices.accounts
      .findServerByURI(uri)
      .QueryInterface(Ci.nsIPop3IncomingServer);

    // nsIChannel attributes.
    this.originalURI = uri;
    this.URI = uri;
    this.loadInfo = loadInfo;
    this.contentLength = 0;
  }

  /**
   * @see nsIRequest
   */
  get status() {
    return Cr.NS_OK;
  }

  /**
   * @see nsIChannel
   */
  get contentType() {
    return this._contentType || "message/rfc822";
  }

  set contentType(value) {
    this._contentType = value;
  }

  get isDocument() {
    return true;
  }

  open() {
    throw Components.Exception(
      "Pop3Channel.open() not implemented",
      Cr.NS_ERROR_NOT_IMPLEMENTED
    );
  }

  asyncOpen(listener) {
    this._logger.debug(`asyncOpen ${this.URI.spec}`);
    let match = this.URI.spec.match(/pop3?:\/\/.+\/(?:\?|&)uidl=([^&]+)/);
    let uidl = decodeURIComponent(match?.[1] || "");
    if (!uidl) {
      throw Components.Exception(
        `Unrecognized url=${this.URI.spec}`,
        Cr.NS_ERROR_ILLEGAL_VALUE
      );
    }

    let client = new lazy.Pop3Client(this._server);
    client.runningUri = this.URI;
    client.connect();
    client.onOpen = () => {
      listener.onStartRequest(this);
      client.fetchBodyForUidl(
        this.URI.QueryInterface(Ci.nsIPop3URL).pop3Sink,
        uidl
      );
    };
    client.onDone = status => {
      listener.onStopRequest(this, status);
    };
  }
}