summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/filters.jsm
blob: 09abd448e516ec6f1841b2bddafc27dc306cdeac (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
 * 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 = ["EnigmailFilters"];

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

const lazy = {};
XPCOMUtils.defineLazyModuleGetters(lazy, {
  EnigmailCore: "chrome://openpgp/content/modules/core.jsm",
  EnigmailData: "chrome://openpgp/content/modules/data.jsm",
  EnigmailFuncs: "chrome://openpgp/content/modules/funcs.jsm",
  EnigmailKeyRing: "chrome://openpgp/content/modules/keyRing.jsm",
  EnigmailLog: "chrome://openpgp/content/modules/log.jsm",
  EnigmailMime: "chrome://openpgp/content/modules/mime.jsm",

  EnigmailPersistentCrypto:
    "chrome://openpgp/content/modules/persistentCrypto.jsm",

  EnigmailStreams: "chrome://openpgp/content/modules/streams.jsm",
  EnigmailDialog: "chrome://openpgp/content/modules/dialog.jsm",
  jsmime: "resource:///modules/jsmime.jsm",
  NetUtil: "resource://gre/modules/NetUtil.jsm",
});

let l10n = new Localization(["messenger/openpgp/openpgp.ftl"], true);

var gNewMailListenerInitiated = false;

/**
 * filter action for creating a decrypted version of the mail and
 * deleting the original mail at the same time
 */

const filterActionMoveDecrypt = {
  async applyAction(aMsgHdrs, aActionValue, aListener, aType, aMsgWindow) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: filterActionMoveDecrypt: Move to: " + aActionValue + "\n"
    );

    for (let msgHdr of aMsgHdrs) {
      await lazy.EnigmailPersistentCrypto.cryptMessage(
        msgHdr,
        aActionValue,
        true,
        null
      );
    }
  },

  isValidForType(type, scope) {
    return true;
  },

  validateActionValue(value, folder, type) {
    l10n.formatValue("filter-decrypt-move-warn-experimental").then(value => {
      lazy.EnigmailDialog.alert(null, value);
    });

    if (value === "") {
      return l10n.formatValueSync("filter-folder-required");
    }

    return null;
  },
};

/**
 * filter action for creating a decrypted copy of the mail, leaving the original
 * message untouched
 */
const filterActionCopyDecrypt = {
  async applyAction(aMsgHdrs, aActionValue, aListener, aType, aMsgWindow) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: filterActionCopyDecrypt: Copy to: " + aActionValue + "\n"
    );

    for (let msgHdr of aMsgHdrs) {
      await lazy.EnigmailPersistentCrypto.cryptMessage(
        msgHdr,
        aActionValue,
        false,
        null
      );
    }
  },

  isValidForType(type, scope) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: filterActionCopyDecrypt.isValidForType(" + type + ")\n"
    );

    let r = true;
    return r;
  },

  validateActionValue(value, folder, type) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: filterActionCopyDecrypt.validateActionValue(" +
        value +
        ")\n"
    );

    if (value === "") {
      return l10n.formatValueSync("filter-folder-required");
    }

    return null;
  },
};

/**
 * filter action for to encrypt a mail to a specific key
 */
const filterActionEncrypt = {
  async applyAction(aMsgHdrs, aActionValue, aListener, aType, aMsgWindow) {
    // Ensure KeyRing is loaded.
    if (aMsgWindow) {
      lazy.EnigmailCore.getService(aMsgWindow.domWindow);
    } else {
      lazy.EnigmailCore.getService();
    }
    lazy.EnigmailKeyRing.getAllKeys();

    lazy.EnigmailLog.DEBUG(
      "filters.jsm: filterActionEncrypt: Encrypt to: " + aActionValue + "\n"
    );
    let keyObj = lazy.EnigmailKeyRing.getKeyById(aActionValue);

    if (keyObj === null) {
      lazy.EnigmailLog.DEBUG(
        "filters.jsm: failed to find key by id: " + aActionValue + "\n"
      );
      let keyId = lazy.EnigmailKeyRing.getValidKeyForRecipient(aActionValue);
      if (keyId) {
        keyObj = lazy.EnigmailKeyRing.getKeyById(keyId);
      }
    }

    if (keyObj === null && aListener) {
      lazy.EnigmailLog.DEBUG("filters.jsm: no valid key - aborting\n");

      aListener.OnStartCopy();
      aListener.OnStopCopy(1);

      return;
    }

    lazy.EnigmailLog.DEBUG(
      "filters.jsm: key to encrypt to: " +
        JSON.stringify(keyObj) +
        ", userId: " +
        keyObj.userId +
        "\n"
    );

    // Maybe skip messages here if they are already encrypted to
    // the target key? There might be some use case for unconditionally
    // encrypting here. E.g. to use the local preferences and remove all
    // other recipients.
    // Also not encrypting to already encrypted messages would make the
    // behavior less transparent as it's not obvious.

    for (let msgHdr of aMsgHdrs) {
      await lazy.EnigmailPersistentCrypto.cryptMessage(
        msgHdr,
        null /* same folder */,
        true /* move */,
        keyObj /* target key */
      );
    }
  },

  isValidForType(type, scope) {
    return true;
  },

  validateActionValue(value, folder, type) {
    // Initialize KeyRing. Ugly as it blocks the GUI but
    // we need it.
    lazy.EnigmailCore.getService();
    lazy.EnigmailKeyRing.getAllKeys();

    lazy.EnigmailLog.DEBUG(
      "filters.jsm: validateActionValue: Encrypt to: " + value + "\n"
    );
    if (value === "") {
      return l10n.formatValueSync("filter-key-required");
    }

    let keyObj = lazy.EnigmailKeyRing.getKeyById(value);

    if (keyObj === null) {
      lazy.EnigmailLog.DEBUG(
        "filters.jsm: failed to find key by id. Looking for uid.\n"
      );
      let keyId = lazy.EnigmailKeyRing.getValidKeyForRecipient(value);
      if (keyId) {
        keyObj = lazy.EnigmailKeyRing.getKeyById(keyId);
      }
    }

    if (keyObj === null) {
      return l10n.formatValueSync("filter-key-not-found", {
        desc: value,
      });
    }

    if (!keyObj.secretAvailable) {
      // We warn but we allow it. There might be use cases where
      // thunderbird + enigmail is used as a gateway filter with
      // the secret not available on one machine and the decryption
      // is intended to happen on different systems.
      l10n
        .formatValue("filter-warn-key-not-secret", {
          desc: value,
        })
        .then(value => {
          lazy.EnigmailDialog.alert(null, value);
        });
    }

    return null;
  },
};

function isPGPEncrypted(data) {
  // We only check the first mime subpart for application/pgp-encrypted.
  // If it is text/plain or text/html we look into that for the
  // message marker.
  // If there are no subparts we just look in the body.
  //
  // This intentionally does not match more complex cases
  // with sub parts being encrypted etc. as auto processing
  // these kinds of mails will be error prone and better not
  // done through a filter

  var mimeTree = lazy.EnigmailMime.getMimeTree(data, true);
  if (!mimeTree.subParts.length) {
    // No subParts. Check for PGP Marker in Body
    return mimeTree.body.includes("-----BEGIN PGP MESSAGE-----");
  }

  // Check the type of the first subpart.
  var firstPart = mimeTree.subParts[0];
  var ct = firstPart.fullContentType;
  if (typeof ct == "string") {
    ct = ct.replace(/[\r\n]/g, " ");
    // Proper PGP/MIME ?
    if (ct.search(/application\/pgp-encrypted/i) >= 0) {
      return true;
    }
    // Look into text/plain pgp messages and text/html messages.
    if (ct.search(/text\/plain/i) >= 0 || ct.search(/text\/html/i) >= 0) {
      return firstPart.body.includes("-----BEGIN PGP MESSAGE-----");
    }
  }
  return false;
}

/**
 * filter term for OpenPGP Encrypted mail
 */
const filterTermPGPEncrypted = {
  id: EnigmailConstants.FILTER_TERM_PGP_ENCRYPTED,
  name: l10n.formatValueSync("filter-term-pgpencrypted-label"),
  needsBody: true,
  match(aMsgHdr, searchValue, searchOp) {
    var folder = aMsgHdr.folder;
    var stream = folder.getMsgInputStream(aMsgHdr, {});

    var messageSize = folder.hasMsgOffline(aMsgHdr.messageKey)
      ? aMsgHdr.offlineMessageSize
      : aMsgHdr.messageSize;
    var data;
    try {
      data = lazy.NetUtil.readInputStreamToString(stream, messageSize);
    } catch (ex) {
      lazy.EnigmailLog.DEBUG(
        "filters.jsm: filterTermPGPEncrypted: failed to get data.\n"
      );
      // If we don't know better to return false.
      stream.close();
      return false;
    }

    var isPGP = isPGPEncrypted(data);

    stream.close();

    return (
      (searchOp == Ci.nsMsgSearchOp.Is && isPGP) ||
      (searchOp == Ci.nsMsgSearchOp.Isnt && !isPGP)
    );
  },

  getEnabled(scope, op) {
    return true;
  },

  getAvailable(scope, op) {
    return true;
  },

  getAvailableOperators(scope, length) {
    length.value = 2;
    return [Ci.nsMsgSearchOp.Is, Ci.nsMsgSearchOp.Isnt];
  },
};

function initNewMailListener() {
  lazy.EnigmailLog.DEBUG("filters.jsm: initNewMailListener()\n");

  if (!gNewMailListenerInitiated) {
    let notificationService = Cc[
      "@mozilla.org/messenger/msgnotificationservice;1"
    ].getService(Ci.nsIMsgFolderNotificationService);
    notificationService.addListener(
      newMailListener,
      notificationService.msgAdded
    );
  }
  gNewMailListenerInitiated = true;
}

function shutdownNewMailListener() {
  lazy.EnigmailLog.DEBUG("filters.jsm: shutdownNewMailListener()\n");

  if (gNewMailListenerInitiated) {
    let notificationService = Cc[
      "@mozilla.org/messenger/msgnotificationservice;1"
    ].getService(Ci.nsIMsgFolderNotificationService);
    notificationService.removeListener(newMailListener);
    gNewMailListenerInitiated = false;
  }
}

function getIdentityForSender(senderEmail, msgServer) {
  let identities = MailServices.accounts.getIdentitiesForServer(msgServer);
  return identities.find(
    id => id.email.toLowerCase() === senderEmail.toLowerCase()
  );
}

var consumerList = [];

function JsmimeEmitter(requireBody) {
  this.requireBody = requireBody;
  this.mimeTree = {
    partNum: "",
    headers: null,
    body: "",
    parent: null,
    subParts: [],
  };
  this.stack = [];
  this.currPartNum = "";
}

JsmimeEmitter.prototype = {
  createPartObj(partNum, headers, parent) {
    return {
      partNum,
      headers,
      body: "",
      parent,
      subParts: [],
    };
  },

  getMimeTree() {
    return this.mimeTree.subParts[0];
  },

  /** JSMime API */
  startMessage() {
    this.currentPart = this.mimeTree;
  },
  endMessage() {},

  startPart(partNum, headers) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: JsmimeEmitter.startPart: partNum=" + partNum + "\n"
    );
    //this.stack.push(partNum);
    let newPart = this.createPartObj(partNum, headers, this.currentPart);

    if (partNum.indexOf(this.currPartNum) === 0) {
      // found sub-part
      this.currentPart.subParts.push(newPart);
    } else {
      // found same or higher level
      this.currentPart.subParts.push(newPart);
    }
    this.currPartNum = partNum;
    this.currentPart = newPart;
  },

  endPart(partNum) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: JsmimeEmitter.startPart: partNum=" + partNum + "\n"
    );
    this.currentPart = this.currentPart.parent;
  },

  deliverPartData(partNum, data) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: JsmimeEmitter.deliverPartData: partNum=" + partNum + "\n"
    );
    if (this.requireBody) {
      if (typeof data === "string") {
        this.currentPart.body += data;
      } else {
        this.currentPart.body += lazy.EnigmailData.arrayBufferToString(data);
      }
    }
  },
};

function processIncomingMail(url, requireBody, aMsgHdr) {
  lazy.EnigmailLog.DEBUG("filters.jsm: processIncomingMail()\n");

  let inputStream = lazy.EnigmailStreams.newStringStreamListener(msgData => {
    let opt = {
      strformat: "unicode",
      bodyformat: "decode",
    };

    try {
      let e = new JsmimeEmitter(requireBody);
      let p = new lazy.jsmime.MimeParser(e, opt);
      p.deliverData(msgData);

      for (let c of consumerList) {
        try {
          c.consumeMessage(e.getMimeTree(), msgData, aMsgHdr);
        } catch (ex) {
          lazy.EnigmailLog.DEBUG(
            "filters.jsm: processIncomingMail: exception: " +
              ex.toString() +
              "\n"
          );
        }
      }
    } catch (ex) {}
  });

  try {
    let channel = lazy.EnigmailStreams.createChannel(url);
    channel.asyncOpen(inputStream, null);
  } catch (e) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: processIncomingMail: open stream exception " +
        e.toString() +
        "\n"
    );
  }
}

function getRequireMessageProcessing(aMsgHdr) {
  let isInbox =
    aMsgHdr.folder.getFlag(Ci.nsMsgFolderFlags.CheckNew) ||
    aMsgHdr.folder.getFlag(Ci.nsMsgFolderFlags.Inbox);
  let requireBody = false;
  let inboxOnly = true;
  let selfSentOnly = false;
  let processReadMail = false;

  for (let c of consumerList) {
    if (!c.incomingMailOnly) {
      inboxOnly = false;
    }
    if (!c.unreadOnly) {
      processReadMail = true;
    }
    if (!c.headersOnly) {
      requireBody = true;
    }
    if (c.selfSentOnly) {
      selfSentOnly = true;
    }
  }

  if (!processReadMail && aMsgHdr.isRead) {
    return null;
  }
  if (inboxOnly && !isInbox) {
    return null;
  }
  if (selfSentOnly) {
    let sender = lazy.EnigmailFuncs.parseEmails(aMsgHdr.author, true);
    let id = null;
    if (sender && sender[0]) {
      id = getIdentityForSender(sender[0].email, aMsgHdr.folder.server);
    }

    if (!id) {
      return null;
    }
  }

  lazy.EnigmailLog.DEBUG(
    "filters.jsm: getRequireMessageProcessing: author: " + aMsgHdr.author + "\n"
  );

  let u = lazy.EnigmailFuncs.getUrlFromUriSpec(
    aMsgHdr.folder.getUriForMsg(aMsgHdr)
  );

  if (!u) {
    return null;
  }

  let op = u.spec.indexOf("?") > 0 ? "&" : "?";
  let url = u.spec + op + "header=enigmailFilter";

  return {
    url,
    requireBody,
  };
}

const newMailListener = {
  msgAdded(aMsgHdr) {
    lazy.EnigmailLog.DEBUG(
      "filters.jsm: newMailListener.msgAdded() - got new mail in " +
        aMsgHdr.folder.prettiestName +
        "\n"
    );

    if (consumerList.length === 0) {
      return;
    }

    let ret = getRequireMessageProcessing(aMsgHdr);
    if (ret) {
      processIncomingMail(ret.url, ret.requireBody, aMsgHdr);
    }
  },
};

/**
  messageStructure - Object:
    - partNum: String                       - MIME part number
    - headers: Object(nsIStructuredHeaders) - MIME part headers
    - body: String or typedarray            - the body part
    - parent: Object(messageStructure)      - link to the parent part
    - subParts: Array of Object(messageStructure) - array of the sub-parts
 */

var EnigmailFilters = {
  onStartup() {
    let filterService = Cc[
      "@mozilla.org/messenger/services/filters;1"
    ].getService(Ci.nsIMsgFilterService);
    filterService.addCustomTerm(filterTermPGPEncrypted);
    initNewMailListener();
  },

  onShutdown() {
    shutdownNewMailListener();
  },

  /**
   * add a new consumer to listen to new mails
   *
   * @param consumer - Object
   *   - headersOnly:      Boolean - needs full message body? [FUTURE]
   *   - incomingMailOnly: Boolean - only work on folder(s) that obtain new mail
   *                                  (Inbox and folders that listen to new mail)
   *   - unreadOnly:       Boolean - only process unread mails
   *   - selfSentOnly:     Boolean - only process mails with sender Email == Account Email
   *  - consumeMessage: function(messageStructure, rawMessageData, nsIMsgHdr)
   */
  addNewMailConsumer(consumer) {
    lazy.EnigmailLog.DEBUG("filters.jsm: addNewMailConsumer()\n");
    consumerList.push(consumer);
  },

  removeNewMailConsumer(consumer) {},

  moveDecrypt: filterActionMoveDecrypt,
  copyDecrypt: filterActionCopyDecrypt,
  encrypt: filterActionEncrypt,
};