summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/windows.jsm
blob: baf2e1e5f03c8f57354b20137c3bf71095271ace (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
/*
 * 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";

const EXPORTED_SYMBOLS = ["EnigmailWindows"];

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

const lazy = {};

XPCOMUtils.defineLazyModuleGetters(lazy, {
  EnigmailCore: "chrome://openpgp/content/modules/core.jsm",
  EnigmailDialog: "chrome://openpgp/content/modules/dialog.jsm",
  EnigmailKeyRing: "chrome://openpgp/content/modules/keyRing.jsm",
  EnigmailLog: "chrome://openpgp/content/modules/log.jsm",
});

XPCOMUtils.defineLazyGetter(lazy, "l10n", () => {
  return new Localization(["messenger/openpgp/openpgp.ftl"], true);
});

var EnigmailWindows = {
  /**
   * Open a window, or focus it if it is already open
   *
   * @winName   : String - name of the window; used to identify if it is already open
   * @spec      : String - window URL (e.g. chrome://openpgp/content/ui/test.xhtml)
   * @winOptions: String - window options as defined in nsIWindow.open
   * @optObj    : any    - an Object, Array, String, etc. that is passed as parameter
   *                       to the window
   */
  openWin(winName, spec, winOptions, optObj) {
    var windowManager = Services.wm;

    var recentWin = null;
    for (let win of windowManager.getEnumerator(null)) {
      if (win.location.href == spec) {
        recentWin = win;
        break;
      }
      if (winName && win.name && win.name == winName) {
        win.focus();
        break;
      }
    }

    if (recentWin) {
      recentWin.focus();
    } else {
      var appShellSvc = Services.appShell;
      var domWin = appShellSvc.hiddenDOMWindow;
      try {
        domWin.open(spec, winName, "chrome," + winOptions, optObj);
      } catch (ex) {
        domWin = windowManager.getMostRecentWindow(null);
        domWin.open(spec, winName, "chrome," + winOptions, optObj);
      }
    }
  },

  /**
   * Determine the best possible window to serve as parent window for dialogs.
   *
   * @return: nsIWindow object
   */
  getBestParentWin() {
    var windowManager = Services.wm;

    var bestFit = null;

    for (let win of windowManager.getEnumerator(null)) {
      if (win.location.href.search(/\/messenger.xhtml$/) > 0) {
        bestFit = win;
      }
      if (
        !bestFit &&
        win.location.href.search(/\/messengercompose.xhtml$/) > 0
      ) {
        bestFit = win;
      }
    }

    if (!bestFit) {
      var winEnum = windowManager.getEnumerator(null);
      bestFit = winEnum.getNext();
    }

    return bestFit;
  },

  /**
   * Iterate through the frames of a window and return the first frame with a
   * matching name.
   *
   * @win:       nsIWindow - XUL window to search
   * @frameName: String    - name of the frame to search
   *
   * @return:    the frame object or null if not found
   */
  getFrame(win, frameName) {
    lazy.EnigmailLog.DEBUG("windows.jsm: getFrame: name=" + frameName + "\n");
    for (var j = 0; j < win.frames.length; j++) {
      if (win.frames[j].name == frameName) {
        return win.frames[j];
      }
    }
    return null;
  },

  getMostRecentWindow() {
    var windowManager = Services.wm;
    return windowManager.getMostRecentWindow(null);
  },

  /**
   * Display the key help window
   *
   * @source - |string| containing the name of the file to display
   *
   * no return value
   */

  openHelpWindow(source) {
    EnigmailWindows.openWin(
      "enigmail:help",
      "chrome://openpgp/content/ui/enigmailHelp.xhtml?src=" + source,
      "centerscreen,resizable"
    );
  },

  /**
   * Open the Enigmail Documentation page in a new window
   *
   * no return value
   */
  openEnigmailDocu(parent) {
    if (!parent) {
      parent = this.getMostRecentWindow();
    }

    parent.open(
      "https://doesnotexist-openpgp-integration.thunderbird/faq/docu.php",
      "",
      "chrome,width=600,height=500,resizable"
    );
  },

  /**
   * Display the OpenPGP key manager window
   *
   * no return value
   */
  openKeyManager(win) {
    lazy.EnigmailCore.getService(win);

    EnigmailWindows.openWin(
      "enigmail:KeyManager",
      "chrome://openpgp/content/ui/enigmailKeyManager.xhtml",
      "resizable"
    );
  },

  /**
   * Display the OpenPGP key manager window
   *
   * no return value
   */
  openImportSettings(win) {
    lazy.EnigmailCore.getService(win);

    EnigmailWindows.openWin(
      "",
      "chrome://openpgp/content/ui/importSettings.xhtml",
      "chrome,dialog,centerscreen,resizable,modal"
    );
  },

  /**
   * If the Key Manager is open, dispatch an event to tell the key
   * manager to refresh the displayed keys
   */
  keyManReloadKeys() {
    for (let thisWin of Services.wm.getEnumerator(null)) {
      if (thisWin.name && thisWin.name == "enigmail:KeyManager") {
        let evt = new thisWin.Event("reload-keycache", {
          bubbles: true,
          cancelable: false,
        });
        thisWin.dispatchEvent(evt);
        break;
      }
    }
  },

  /**
   * Display the card details window
   *
   * no return value
   */
  openCardDetails() {
    EnigmailWindows.openWin(
      "enigmail:cardDetails",
      "chrome://openpgp/content/ui/enigmailCardDetails.xhtml",
      "centerscreen"
    );
  },

  /**
   * Display the console log window
   *
   * @win       - |object| holding the parent window for the dialog
   *
   * no return value
   */
  openConsoleWindow() {
    EnigmailWindows.openWin(
      "enigmail:console",
      "chrome://openpgp/content/ui/enigmailConsole.xhtml",
      "resizable,centerscreen"
    );
  },

  /**
   * Display the window for the debug log file
   *
   * @win       - |object| holding the parent window for the dialog
   *
   * no return value
   */
  openDebugLog(win) {
    EnigmailWindows.openWin(
      "enigmail:logFile",
      "chrome://openpgp/content/ui/enigmailViewFile.xhtml?viewLog=1&title=" +
        escape(lazy.l10n.formatValueSync("debug-log-title")),
      "centerscreen"
    );
  },

  /**
   * Display the dialog for changing the expiry date of one or several keys
   *
   * @win        - |object| holding the parent window for the dialog
   * @userIdArr  - |array| of |strings| containing the User IDs
   * @keyIdArr   - |array| of |strings| containing the key IDs (eg. "0x12345678") to change
   *
   * @returns Boolean - true if expiry date was changed; false otherwise
   */
  editKeyExpiry(win, userIdArr, keyIdArr) {
    const inputObj = {
      keyId: keyIdArr,
      userId: userIdArr,
    };
    const resultObj = {
      refresh: false,
    };
    win.openDialog(
      "chrome://openpgp/content/ui/enigmailEditKeyExpiryDlg.xhtml",
      "",
      "dialog,modal,centerscreen,resizable",
      inputObj,
      resultObj
    );
    return resultObj.refresh;
  },

  /**
   * Display the dialog for changing key trust of one or several keys
   *
   * @win        - |object| holding the parent window for the dialog
   * @userIdArr  - |array| of |strings| containing the User IDs
   * @keyIdArr   - |array| of |strings| containing the key IDs (eg. "0x12345678") to change
   *
   * @returns Boolean - true if key trust was changed; false otherwise
   */
  editKeyTrust(win, userIdArr, keyIdArr) {
    const inputObj = {
      keyId: keyIdArr,
      userId: userIdArr,
    };
    const resultObj = {
      refresh: false,
    };
    win.openDialog(
      "chrome://openpgp/content/ui/enigmailEditKeyTrustDlg.xhtml",
      "",
      "dialog,modal,centerscreen,resizable",
      inputObj,
      resultObj
    );
    return resultObj.refresh;
  },

  /**
   * Display the dialog for signing a key
   *
   * @win        - |object| holding the parent window for the dialog
   * @userId     - |string| containing the User ID (for displaing in the dialog only)
   * @keyId      - |string| containing the key ID (eg. "0x12345678")
   *
   * @returns Boolean - true if key was signed; false otherwise
   */
  signKey(win, userId, keyId) {
    const inputObj = {
      keyId,
      userId,
    };
    const resultObj = {
      refresh: false,
    };
    win.openDialog(
      "chrome://openpgp/content/ui/enigmailSignKeyDlg.xhtml",
      "",
      "dialog,modal,centerscreen,resizable",
      inputObj,
      resultObj
    );
    return resultObj.refresh;
  },

  /**
   * Display the OpenPGP Key Details window
   *
   * @win        - |object| holding the parent window for the dialog
   * @keyId      - |string| containing the key ID (eg. "0x12345678")
   * @refresh    - |boolean| if true, cache is cleared and the key data is loaded from GnuPG
   *
   * @returns Boolean - true:  keylist needs to be refreshed
   *                  - false: no need to refresh keylist
   */
  async openKeyDetails(win, keyId, refresh) {
    if (!win) {
      win = this.getBestParentWin();
    }

    keyId = keyId.replace(/^0x/, "");

    if (refresh) {
      lazy.EnigmailKeyRing.clearCache();
    }

    const resultObj = {
      refresh: false,
    };
    win.openDialog(
      "chrome://openpgp/content/ui/keyDetailsDlg.xhtml",
      "KeyDetailsDialog",
      "dialog,modal,centerscreen,resizable",
      { keyId, modified: lazy.EnigmailKeyRing.clearCache },
      resultObj
    );

    return resultObj.refresh;
  },

  /**
   * Display the dialog to search and/or download key(s) from a keyserver
   *
   * @win        - |object| holding the parent window for the dialog
   * @inputObj   - |object| with member searchList (|string| containing the keys to search)
   * @resultObj  - |object| with member importedKeys (|number| containing the number of imporeted keys)
   *
   * no return value
   */
  downloadKeys(win, inputObj, resultObj) {
    lazy.EnigmailLog.DEBUG(
      "windows.jsm: downloadKeys: searchList=" + inputObj.searchList + "\n"
    );

    resultObj.importedKeys = 0;

    const ioService = Services.io;
    if (ioService && ioService.offline) {
      lazy.l10n.formatValue("need-online").then(value => {
        lazy.EnigmailDialog.alert(win, value);
      });
      return;
    }

    let valueObj = {};
    if (inputObj.searchList) {
      valueObj = {
        keyId: "<" + inputObj.searchList.join("> <") + ">",
      };
    }

    const keysrvObj = {};

    if (inputObj.searchList && inputObj.autoKeyServer) {
      keysrvObj.value = inputObj.autoKeyServer;
    } else {
      win.openDialog(
        "chrome://openpgp/content/ui/enigmailKeyserverDlg.xhtml",
        "",
        "dialog,modal,centerscreen",
        valueObj,
        keysrvObj
      );
    }

    if (!keysrvObj.value) {
      return;
    }

    inputObj.keyserver = keysrvObj.value;

    if (!inputObj.searchList) {
      const searchval = keysrvObj.email
        .replace(/^(\s*)(.*)/, "$2")
        .replace(/\s+$/, ""); // trim spaces
      // special handling to convert fingerprints with spaces into fingerprint without spaces
      if (
        searchval.length == 49 &&
        searchval.match(/^[0-9a-fA-F ]*$/) &&
        searchval[4] == " " &&
        searchval[9] == " " &&
        searchval[14] == " " &&
        searchval[19] == " " &&
        searchval[24] == " " &&
        searchval[29] == " " &&
        searchval[34] == " " &&
        searchval[39] == " " &&
        searchval[44] == " "
      ) {
        inputObj.searchList = ["0x" + searchval.replace(/ /g, "")];
      } else if (searchval.length == 40 && searchval.match(/^[0-9a-fA-F ]*$/)) {
        inputObj.searchList = ["0x" + searchval];
      } else if (searchval.length == 8 && searchval.match(/^[0-9a-fA-F]*$/)) {
        // special handling to add the required leading 0x when searching for keys
        inputObj.searchList = ["0x" + searchval];
      } else if (searchval.length == 16 && searchval.match(/^[0-9a-fA-F]*$/)) {
        inputObj.searchList = ["0x" + searchval];
      } else {
        inputObj.searchList = searchval.split(/[,; ]+/);
      }
    }

    win.openDialog(
      "chrome://openpgp/content/ui/enigmailSearchKey.xhtml",
      "",
      "dialog,modal,centerscreen",
      inputObj,
      resultObj
    );
  },

  /**
   * Display Autocrypt Setup Passwd dialog.
   *
   * @param dlgMode:       String - dialog mode: "input" / "display"
   * @param passwdType:    String - type of password ("numeric9x4" / "generic")
   * @param password:      String - password or initial two digits of password
   *
   * @returns String entered password (in input mode) or NULL
   */
  autocryptSetupPasswd(window, dlgMode, passwdType = "numeric9x4", password) {
    if (!window) {
      window = this.getBestParentWin();
    }

    let inputObj = {
      password: null,
      passwdType,
      dlgMode,
    };

    if (password) {
      inputObj.initialPasswd = password;
    }

    window.openDialog(
      "chrome://openpgp/content/ui/autocryptSetupPasswd.xhtml",
      "",
      "dialog,modal,centerscreen",
      inputObj
    );

    return inputObj.password;
  },

  /**
   * Display dialog to initiate the Autocrypt Setup Message.
   *
   */
  inititateAcSetupMessage(window) {
    if (!window) {
      window = this.getBestParentWin();
    }

    window.openDialog(
      "chrome://openpgp/content/ui/autocryptInitiateBackup.xhtml",
      "",
      "dialog,centerscreen"
    );
  },

  shutdown(reason) {
    lazy.EnigmailLog.DEBUG("windows.jsm: shutdown()\n");

    let tabs = Services.wm
      .getMostRecentWindow("mail:3pane")
      .document.getElementById("tabmail");

    for (let i = tabs.tabInfo.length - 1; i >= 0; i--) {
      if (
        "openedUrl" in tabs.tabInfo[i] &&
        tabs.tabInfo[i].openedUrl.startsWith("chrome://openpgp/")
      ) {
        tabs.closeTab(tabs.tabInfo[i]);
      }
    }
  },
};