summaryrefslogtreecommitdiffstats
path: root/comm/chat/content/otr-add-fingerprint.js
blob: fb6d6c037d92d200375e3d73176d1cceaca1cf26 (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
/* 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 { l10nHelper } = ChromeUtils.importESModule(
  "resource:///modules/imXPCOMUtils.sys.mjs"
);
var { OTR } = ChromeUtils.importESModule("resource:///modules/OTR.sys.mjs");

window.addEventListener("DOMContentLoaded", () => {
  otrAddFinger.onload();
});

var otrAddFinger = {
  onload() {
    let args = window.arguments[0].wrappedJSObject;

    this.fingerWarning = document.getElementById("fingerWarning");
    this.fingerError = document.getElementById("fingerError");
    this.keyCount = document.getElementById("keyCount");

    document.l10n.setAttributes(
      document.getElementById("otrDescription"),
      "otr-add-finger-description",
      {
        name: args.screenname,
      }
    );

    document.addEventListener("dialogaccept", event => {
      let hex = document.getElementById("fingerprint").value;
      let context = OTR.getContextFromRecipient(
        args.account,
        args.protocol,
        args.screenname
      );
      let finger = OTR.addFingerprint(context, hex);
      if (finger.isNull()) {
        event.preventDefault();
        return;
      }
      try {
        // Ignore the return, this is just a test.
        OTR.getUIConvFromContext(context);
      } catch (error) {
        // We expect that a conversation may not have been started.
        context = null;
      }
      OTR.setTrust(finger, true, context);
    });
  },

  addBlankSpace(value) {
    return value
      .replace(/\s/g, "")
      .trim()
      .replace(/(.{8})/g, "$1 ")
      .trim();
  },

  oninput(input) {
    let hex = input.value.replace(/\s/g, "");

    if (/[^0-9A-F]/gi.test(hex)) {
      this.keyCount.hidden = true;
      this.fingerWarning.hidden = false;
      this.fingerError.hidden = false;
    } else {
      this.keyCount.hidden = false;
      this.fingerWarning.hidden = true;
      this.fingerError.hidden = true;
    }

    document.querySelector("dialog").getButton("accept").disabled =
      input.value && !input.validity.valid;

    this.keyCount.value = `${hex.length}/40`;
    input.value = this.addBlankSpace(input.value);
  },

  onblur(input) {
    input.value = this.addBlankSpace(input.value);
  },
};