summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/msgRead.jsm
blob: 04f38bf60245c7f49ff316118d8e7415b04c7e7a (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * 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 https://mozilla.org/MPL/2.0/.
 */

"use strict";

var EXPORTED_SYMBOLS = ["EnigmailMsgRead"];

/**
 * Message-reading related functions
 */

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

const lazy = {};

XPCOMUtils.defineLazyModuleGetters(lazy, {
  EnigmailFuncs: "chrome://openpgp/content/modules/funcs.jsm",
  EnigmailKeyRing: "chrome://openpgp/content/modules/keyRing.jsm",
});

var EnigmailMsgRead = {
  /**
   * Ensure that Thunderbird prepares certain headers during message reading
   */
  ensureExtraAddonHeaders() {
    let hdr = Services.prefs.getCharPref("mailnews.headers.extraAddonHeaders");

    if (hdr !== "*") {
      // do nothing if extraAddonHeaders is "*" (all headers)
      for (let h of ["autocrypt", "openpgp"]) {
        if (hdr.search(h) < 0) {
          if (hdr.length > 0) {
            hdr += " ";
          }
          hdr += h;
        }
      }
      Services.prefs.setCharPref("mailnews.headers.extraAddonHeaders", hdr);
    }
  },

  /**
   * Get a mail URL from a uriSpec
   *
   * @param uriSpec: String - URI of the desired message
   *
   * @returns Object: nsIURL or nsIMsgMailNewsUrl object
   */
  getUrlFromUriSpec(uriSpec) {
    return lazy.EnigmailFuncs.getUrlFromUriSpec(uriSpec);
  },

  /**
   * Determine if an attachment is possibly signed
   */
  checkSignedAttachment(attachmentObj, index, currentAttachments) {
    function escapeRegex(string) {
      return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
    }

    var attachmentList;
    if (index !== null) {
      attachmentList = attachmentObj;
    } else {
      attachmentList = currentAttachments;
      for (let i = 0; i < attachmentList.length; i++) {
        if (attachmentList[i].url == attachmentObj.url) {
          index = i;
          break;
        }
      }
      if (index === null) {
        return false;
      }
    }

    var signed = false;
    var findFile;

    var attName = this.getAttachmentName(attachmentList[index])
      .toLowerCase()
      .replace(/\+/g, "\\+");

    // check if filename is a signature
    if (
      this.getAttachmentName(attachmentList[index]).search(/\.(sig|asc)$/i) >
        0 ||
      attachmentList[index].contentType.match(/^application\/pgp-signature/i)
    ) {
      findFile = new RegExp(escapeRegex(attName.replace(/\.(sig|asc)$/, "")));
    } else if (attName.search(/\.pgp$/i) > 0) {
      findFile = new RegExp(
        escapeRegex(attName.replace(/\.pgp$/, "")) + "(\\.pgp)?\\.(sig|asc)$"
      );
    } else {
      findFile = new RegExp(escapeRegex(attName) + "\\.(sig|asc)$");
    }

    for (let i in attachmentList) {
      if (
        i != index &&
        this.getAttachmentName(attachmentList[i])
          .toLowerCase()
          .search(findFile) === 0
      ) {
        signed = true;
      }
    }

    return signed;
  },

  /**
   * Get the name of an attachment from the attachment object
   */
  getAttachmentName(attachment) {
    return attachment.name;
  },

  /**
   * Escape text such that it can be used as HTML text
   */
  escapeTextForHTML(text, hyperlink) {
    // Escape special characters
    if (text.indexOf("&") > -1) {
      text = text.replace(/&/g, "&amp;");
    }

    if (text.indexOf("<") > -1) {
      text = text.replace(/</g, "&lt;");
    }

    if (text.indexOf(">") > -1) {
      text = text.replace(/>/g, "&gt;");
    }

    if (text.indexOf('"') > -1) {
      text = text.replace(/"/g, "&quot;");
    }

    if (!hyperlink) {
      return text;
    }

    // Hyperlink email addresses (we accept at most 1024 characters before and after the @)
    var addrs = text.match(
      /\b[A-Za-z0-9_+.-]{1,1024}@[A-Za-z0-9.-]{1,1024}\b/g
    );

    var newText, offset, loc;
    if (addrs && addrs.length) {
      newText = "";
      offset = 0;

      for (var j = 0; j < addrs.length; j++) {
        var addr = addrs[j];

        loc = text.indexOf(addr, offset);
        if (loc < offset) {
          break;
        }

        if (loc > offset) {
          newText += text.substr(offset, loc - offset);
        }

        // Strip any period off the end of address
        addr = addr.replace(/[.]$/, "");

        if (!addr.length) {
          continue;
        }

        newText += '<a href="mailto:' + addr + '">' + addr + "</a>";

        offset = loc + addr.length;
      }

      newText += text.substr(offset, text.length - offset);

      text = newText;
    }

    // Hyperlink URLs (we don't accept URLS or more than 1024 characters length)
    var urls = text.match(/\b(http|https|ftp):\S{1,1024}\s/g);

    if (urls && urls.length) {
      newText = "";
      offset = 0;

      for (var k = 0; k < urls.length; k++) {
        var url = urls[k];

        loc = text.indexOf(url, offset);
        if (loc < offset) {
          break;
        }

        if (loc > offset) {
          newText += text.substr(offset, loc - offset);
        }

        // Strip delimiters off the end of URL
        url = url.replace(/\s$/, "");
        url = url.replace(/([),.']|&gt;|&quot;)$/, "");

        if (!url.length) {
          continue;
        }

        newText += '<a href="' + url + '">' + url + "</a>";

        offset = loc + url.length;
      }

      newText += text.substr(offset, text.length - offset);

      text = newText;
    }

    return text;
  },

  /**
   * Match the key to the sender's from address
   *
   * @param {string}  keyId:    signing key ID
   * @param {string}  fromAddr: sender's email address
   *
   * @returns Promise<String>: matching email address
   */
  matchUidToSender(keyId, fromAddr) {
    if (!fromAddr || !keyId) {
      return null;
    }

    try {
      fromAddr = lazy.EnigmailFuncs.stripEmail(fromAddr).toLowerCase();
    } catch (ex) {
      console.debug(ex);
    }

    let keyObj = lazy.EnigmailKeyRing.getKeyById(keyId);
    if (!keyObj) {
      return null;
    }

    let userIdList = keyObj.userIds;

    try {
      for (let i = 0; i < userIdList.length; i++) {
        if (
          fromAddr ==
          lazy.EnigmailFuncs.stripEmail(userIdList[i].userId).toLowerCase()
        ) {
          let result = lazy.EnigmailFuncs.stripEmail(userIdList[i].userId);
          return result;
        }
      }
    } catch (ex) {
      console.debug(ex);
    }
    return null;
  },

  searchQuotedPgp(node) {
    if (
      node.nodeName.toLowerCase() === "blockquote" &&
      node.textContent.includes("-----BEGIN PGP ")
    ) {
      return true;
    }

    if (node.firstChild && this.searchQuotedPgp(node.firstChild)) {
      return true;
    }

    if (node.nextSibling && this.searchQuotedPgp(node.nextSibling)) {
      return true;
    }

    return false;
  },
};