summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/ui/composeKeyStatus.js
blob: a6320072ab7fc53b85dc23aa60221ee7896ed621 (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
/* 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/. */

var { EnigmailFuncs } = ChromeUtils.import(
  "chrome://openpgp/content/modules/funcs.jsm"
);
var EnigmailKeyRing = ChromeUtils.import(
  "chrome://openpgp/content/modules/keyRing.jsm"
).EnigmailKeyRing;
var { EnigmailWindows } = ChromeUtils.import(
  "chrome://openpgp/content/modules/windows.jsm"
);
var { EnigmailKey } = ChromeUtils.import(
  "chrome://openpgp/content/modules/key.jsm"
);
const { OpenPGPAlias } = ChromeUtils.import(
  "chrome://openpgp/content/modules/OpenPGPAlias.jsm"
);
const { PgpSqliteDb2 } = ChromeUtils.import(
  "chrome://openpgp/content/modules/sqliteDb.jsm"
);

var gListBox;
var gViewButton;

var gEmailAddresses = [];
var gRowToEmail = [];

// One boolean entry per row. True means it is an alias row.
// This allows us to use different dialog behavior for alias entries.
var gAliasRows = [];

var gMapAddressToKeyObjs = null;

function addRecipients(toAddrList, recList) {
  for (var i = 0; i < recList.length; i++) {
    try {
      let entry = EnigmailFuncs.stripEmail(recList[i].replace(/[",]/g, ""));
      toAddrList.push(entry);
    } catch (ex) {
      console.debug(ex);
    }
  }
}

async function setListEntries() {
  gMapAddressToKeyObjs = new Map();

  for (let addr of gEmailAddresses) {
    addr = addr.toLowerCase();

    let statusStringID = null;
    let statusStringDirect = "";

    let aliasKeyList = EnigmailKeyRing.getAliasKeyList(addr);
    let isAlias = !!aliasKeyList;

    if (isAlias) {
      let aliasKeys = EnigmailKeyRing.getAliasKeys(aliasKeyList);
      if (!aliasKeys.length) {
        // failure, at least one alias key is unusable/unavailable
        statusStringDirect = await document.l10n.formatValue(
          "openpgp-compose-alias-status-error"
        );
      } else {
        statusStringDirect = await document.l10n.formatValue(
          "openpgp-compose-alias-status-direct",
          {
            count: aliasKeys.length,
          }
        );
      }
    } else {
      // We ask to include keys which are expired, because that's what
      // our sub dialog oneRecipientStatus needs. This is for
      // efficiency - because otherwise the sub dialog would have to
      // query all keys again.
      // The consequence is, we need to later call isValidForEncryption
      // for the keys we have obtained, to confirm they are really valid.
      let foundKeys = await EnigmailKeyRing.getMultValidKeysForOneRecipient(
        addr,
        true
      );
      if (!foundKeys || !foundKeys.length) {
        statusStringID = "openpgp-recip-missing";
      } else {
        gMapAddressToKeyObjs.set(addr, foundKeys);
        for (let keyObj of foundKeys) {
          let goodPersonal = false;
          if (keyObj.secretAvailable) {
            goodPersonal = await PgpSqliteDb2.isAcceptedAsPersonalKey(
              keyObj.fpr
            );
          }
          if (
            goodPersonal ||
            (EnigmailKeyRing.isValidForEncryption(keyObj) &&
              (keyObj.acceptance == "verified" ||
                keyObj.acceptance == "unverified"))
          ) {
            statusStringID = "openpgp-recip-good";
            break;
          }
        }
        if (!statusStringID) {
          statusStringID = "openpgp-recip-none-accepted";
        }
      }
    }

    let listitem = document.createXULElement("richlistitem");

    let emailItem = document.createXULElement("label");
    emailItem.setAttribute("value", addr);
    emailItem.setAttribute("crop", "end");
    emailItem.setAttribute("style", "width: var(--recipientWidth)");
    listitem.appendChild(emailItem);

    let status = document.createXULElement("label");

    if (statusStringID) {
      document.l10n.setAttributes(status, statusStringID);
    } else {
      status.setAttribute("value", statusStringDirect);
    }

    status.setAttribute("crop", "end");
    status.setAttribute("style", "width: var(--statusWidth)");
    listitem.appendChild(status);

    gListBox.appendChild(listitem);

    gRowToEmail.push(addr);
    gAliasRows.push(isAlias);
  }
}

async function onLoad() {
  let params = window.arguments[0];
  if (!params) {
    return;
  }

  try {
    await OpenPGPAlias.load();
  } catch (ex) {
    console.log("failed to load OpenPGP alias file: " + ex);
  }

  gListBox = document.getElementById("infolist");
  gViewButton = document.getElementById("detailsButton");

  var arrLen = {};
  var recList;

  if (params.compFields.to) {
    recList = params.compFields.splitRecipients(
      params.compFields.to,
      true,
      arrLen
    );
    addRecipients(gEmailAddresses, recList);
  }
  if (params.compFields.cc) {
    recList = params.compFields.splitRecipients(
      params.compFields.cc,
      true,
      arrLen
    );
    addRecipients(gEmailAddresses, recList);
  }
  if (params.compFields.bcc) {
    recList = params.compFields.splitRecipients(
      params.compFields.bcc,
      true,
      arrLen
    );
    addRecipients(gEmailAddresses, recList);
  }

  await setListEntries();
}

async function reloadAndReselect(selIndex = -1) {
  while (true) {
    let child = gListBox.lastChild;
    // keep first child, which is the header
    if (child == gListBox.firstChild) {
      break;
    }
    gListBox.removeChild(child);
  }
  gRowToEmail = [];
  await setListEntries();
  gListBox.selectedIndex = selIndex;
}

function onSelectionChange(event) {
  // We don't offer detail management/discovery for email addresses
  // that match an alias rule.
  gViewButton.disabled =
    !gListBox.selectedItems.length || gAliasRows[gListBox.selectedIndex];
}

function viewSelectedEmail() {
  let selIndex = gListBox.selectedIndex;
  if (gViewButton.disabled || selIndex == -1) {
    return;
  }
  let email = gRowToEmail[selIndex];
  window.openDialog(
    "chrome://openpgp/content/ui/oneRecipientStatus.xhtml",
    "",
    "chrome,modal,resizable,centerscreen",
    {
      email,
      keys: gMapAddressToKeyObjs.get(email),
    }
  );
  reloadAndReselect(selIndex);
}