summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/modules/ChatEncryption.sys.mjs
blob: 4206b3397d9178336e35c696cc7f04e429436467 (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
/* 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/. */

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  OTRUI: "resource:///modules/OTRUI.sys.mjs",
});

XPCOMUtils.defineLazyGetter(
  lazy,
  "l10n",
  () => new Localization(["messenger/otr/otrUI.ftl"], true)
);

function _str(id) {
  return lazy.l10n.formatValueSync(id);
}

const STATE_STRING = {
  [Ci.prplIConversation.ENCRYPTION_AVAILABLE]: "not-private",
  [Ci.prplIConversation.ENCRYPTION_ENABLED]: "unverified",
  [Ci.prplIConversation.ENCRYPTION_TRUSTED]: "private",
};

export const ChatEncryption = {
  /**
   * If OTR is enabled.
   *
   * @type {boolean}
   */
  get otrEnabled() {
    if (!this.hasOwnProperty("_otrEnabled")) {
      this._otrEnabled = Services.prefs.getBoolPref("chat.otr.enable");
    }
    return this._otrEnabled;
  },
  /**
   * Check if the given protocol has encryption settings for accounts.
   *
   * @param {prplIProtocol} protocol - Protocol to check against.
   * @returns {boolean} If encryption can be configured.
   */
  canConfigureEncryption(protocol) {
    if (this.otrEnabled && lazy.OTRUI.enabled) {
      return true;
    }
    return protocol.canEncrypt;
  },
  /**
   * Check if the conversation should offer encryption settings.
   *
   * @param {prplIConversation} conversation
   * @returns {boolean}
   */
  hasEncryptionActions(conversation) {
    if (!conversation.isChat && this.otrEnabled && lazy.OTRUI.enabled) {
      return true;
    }
    return (
      conversation.encryptionState !==
      Ci.prplIConversation.ENCRYPTION_NOT_SUPPORTED
    );
  },
  /**
   * Show and initialize the encryption selector in the conversation UI for the
   * given conversation, if encryption is available.
   *
   * @param {DOMDocument} document
   * @param {imIConversation} conversation
   */
  updateEncryptionButton(document, conversation) {
    if (!this.hasEncryptionActions(conversation)) {
      this.hideEncryptionButton(document);
    }
    if (
      conversation.encryptionState !==
      Ci.prplIConversation.ENCRYPTION_NOT_SUPPORTED
    ) {
      // OTR is not available if the conversation can natively encrypt
      document.querySelector(".otr-start").hidden = true;
      document.querySelector(".otr-end").hidden = true;
      document.querySelector(".otr-auth").hidden = true;
      lazy.OTRUI.hideAllOTRNotifications();

      const actionsAvailable =
        conversation.encryptionState !==
        Ci.prplIConversation.ENCRYPTION_AVAILABLE;

      document.querySelector(".protocol-encrypt").hidden = false;
      document.querySelector(".protocol-encrypt").disabled = actionsAvailable;
      document.querySelector(".encryption-container").hidden = false;

      const trustStringLevel = STATE_STRING[conversation.encryptionState];
      const otrButton = document.querySelector(".encryption-button");
      otrButton.setAttribute(
        "tooltiptext",
        _str("state-generic-" + trustStringLevel)
      );
      otrButton.setAttribute(
        "label",
        _str("state-" + trustStringLevel + "-label")
      );
      otrButton.className = "encryption-button encryption-" + trustStringLevel;
    } else if (!conversation.isChat && lazy.OTRUI.enabled) {
      document.querySelector(".otr-start").hidden = false;
      document.querySelector(".otr-end").hidden = false;
      document.querySelector(".otr-auth").hidden = false;
      lazy.OTRUI.updateOTRButton(conversation);
      document.querySelector(".protocol-encrypt").hidden = true;
    } else {
      this.hideEncryptionButton(document);
    }
  },
  /**
   * Hide the encryption selector in the converstaion UI.
   *
   * @param {DOMDocument} document
   */
  hideEncryptionButton(document) {
    document.querySelector(".encryption-container").hidden = true;
    if (this.otrEnabled) {
      lazy.OTRUI.hideOTRButton();
    }
  },
  /**
   * Verify identity of a participant of buddy.
   *
   * @param {DOMWindow} window - Window that the verification dialog attaches to.
   * @param {prplIAccountBuddy|prplIConvChatBuddy} buddy - Buddy to verify.
   */
  verifyIdentity(window, buddy) {
    if (!buddy.canVerifyIdentity) {
      Promise.resolve();
    }
    buddy
      .verifyIdentity()
      .then(sessionVerification => {
        window.openDialog(
          "chrome://messenger/content/chat/verify.xhtml",
          "",
          "chrome,modal,titlebar,centerscreen",
          sessionVerification
        );
      })
      .catch(error => {
        // Only prplIAccountBuddy has a reference to the owner account.
        if (buddy.account) {
          buddy.account.prplAccount.wrappedJSObject.ERROR(error);
        } else {
          console.error(error);
        }
      });
  },
};