File: joinchat.js

package info (click to toggle)
thunderbird 1%3A143.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,703,968 kB
  • sloc: cpp: 7,770,492; javascript: 5,943,842; ansic: 3,918,754; python: 1,418,263; xml: 653,354; asm: 474,045; java: 183,079; sh: 111,238; makefile: 20,410; perl: 14,359; objc: 13,059; yacc: 4,583; pascal: 3,405; lex: 1,720; ruby: 999; exp: 762; sql: 715; awk: 580; php: 436; lisp: 430; sed: 69; csh: 10
file content (199 lines) | stat: -rw-r--r-- 6,077 bytes parent folder | download | duplicates (4)
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
/* 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 { IMServices } = ChromeUtils.importESModule(
  "resource:///modules/IMServices.sys.mjs"
);
var { ChatIcons } = ChromeUtils.importESModule(
  "resource:///modules/chatIcons.sys.mjs"
);
var { UIFontSize } = ChromeUtils.importESModule(
  "resource:///modules/UIFontSize.sys.mjs"
);

var autoJoinPref = "autoJoin";

var joinChat = {
  onload() {
    var accountList = document.getElementById("accountlist");
    for (const acc of IMServices.accounts.getAccounts()) {
      if (!acc.connected || !acc.canJoinChat) {
        continue;
      }
      var proto = acc.protocol;
      var item = accountList.appendItem(acc.name, acc.id, proto.name);
      item.setAttribute("image", ChatIcons.getProtocolIconURI(proto));
      item.setAttribute("class", "menuitem-iconic");
      item.account = acc;
    }
    if (!accountList.itemCount) {
      document
        .getElementById("joinChatDialog")
        .querySelector("dialog")
        .cancelDialog();
      throw new Error("No connected MUC enabled account!");
    }
    accountList.selectedIndex = 0;
    UIFontSize.registerWindow(window);
  },

  onAccountSelect() {
    const joinChatGrid = document.getElementById("joinChatGrid");
    while (joinChatGrid.children.length > 3) {
      // leave the first 3 cols
      joinChatGrid.lastChild.remove();
    }

    const acc = document.getElementById("accountlist").selectedItem.account;
    const defaultValues = acc.getChatRoomDefaultFieldValues();
    joinChat._values = defaultValues;
    joinChat._fields = [];
    joinChat._account = acc;

    const protoId = acc.protocol.id;
    document.getElementById("autojoin").hidden = !(
      protoId == "prpl-irc" ||
      protoId == "prpl-jabber" ||
      protoId == "prpl-gtalk"
    );

    for (const field of acc.getChatRoomFields()) {
      const div1 = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "div"
      );
      const label = document.createXULElement("label");
      let text = field.label;
      const match = /_(.)/.exec(text);
      if (match) {
        label.setAttribute("accesskey", match[1]);
        text = text.replace(/_/, "");
      }
      label.setAttribute("value", text);
      label.setAttribute("control", "field-" + field.identifier);
      label.setAttribute("id", "field-" + field.identifier + "-label");
      div1.appendChild(label);
      joinChatGrid.appendChild(div1);

      const div2 = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "div"
      );
      const input = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "input"
      );
      input.classList.add("input-inline");
      input.setAttribute("id", "field-" + field.identifier);
      input.setAttribute(
        "aria-labelledby",
        "field-" + field.identifier + "-label"
      );
      const val = defaultValues.getValue(field.identifier);
      if (val) {
        input.setAttribute("value", val);
      }
      if (field.type == Ci.prplIChatRoomField.TYPE_PASSWORD) {
        input.setAttribute("type", "password");
      } else if (field.type == Ci.prplIChatRoomField.TYPE_INT) {
        input.setAttribute("type", "number");
        input.setAttribute("min", field.min);
        input.setAttribute("max", field.max);
      } else {
        input.setAttribute("type", "text");
      }
      div2.appendChild(input);
      joinChatGrid.appendChild(div2);

      const div3 = document.querySelector(".optional-col").cloneNode(true);
      div3.classList.toggle("required", field.required);
      joinChatGrid.appendChild(div3);

      joinChat._fields.push({ field, input });
    }

    window.sizeToContent();
  },

  join() {
    const values = joinChat._values;
    for (const field of joinChat._fields) {
      const val = field.input.value.trim();
      if (!val && field.field.required) {
        field.input.focus();
        // FIXME: why isn't the return false enough?
        throw new Error("Some required fields are empty!");
        // return false;
      }
      if (val) {
        values.setValue(field.field.identifier, val);
      }
    }
    const account = joinChat._account;
    account.joinChat(values);

    const protoId = account.protocol.id;
    if (
      protoId != "prpl-irc" &&
      protoId != "prpl-jabber" &&
      protoId != "prpl-gtalk"
    ) {
      return;
    }

    let name;
    if (protoId == "prpl-irc") {
      name = values.getValue("channel");
    } else {
      name = values.getValue("room") + "@" + values.getValue("server");
    }

    const conv = IMServices.conversations.getConversationByNameAndAccount(
      name,
      account,
      true
    );
    if (conv) {
      const mailWindow = Services.wm.getMostRecentWindow("mail:3pane");
      if (mailWindow) {
        mailWindow.focus();
        const tabmail = mailWindow.document.getElementById("tabmail");
        tabmail.openTab("chat", { convType: "focus", conv });
      }
    }

    if (document.getElementById("autojoin").checked) {
      // "nick" for JS-XMPP, "handle" for libpurple prpls.
      const nick = values.getValue("nick") || values.getValue("handle");
      if (nick) {
        name += "/" + nick;
      }

      const prefBranch = Services.prefs.getBranch(
        "messenger.account." + account.id + "."
      );
      let autojoin = [];
      if (prefBranch.prefHasUserValue(autoJoinPref)) {
        const prefValue = prefBranch.getStringPref(autoJoinPref);
        if (prefValue) {
          autojoin = prefValue.split(",");
        }
      }

      if (!autojoin.includes(name)) {
        autojoin.push(name);
        prefBranch.setStringPref(autoJoinPref, autojoin.join(","));
      }
    }
  },
};

document.addEventListener("dialogaccept", joinChat.join);

window.addEventListener("DOMContentLoaded", () => {
  joinChat.onload();
});
window.addEventListener("load", () => {
  window.sizeToContent();
});