summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/im/content/joinchat.js
blob: ae4029eb5a495caed6f29478776ca21c9d1d3ca0 (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
/* 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 autoJoinPref = "autoJoin";

var joinChat = {
  onload() {
    var accountList = document.getElementById("accountlist");
    for (let 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;
  },

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

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

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

    for (let field of acc.getChatRoomFields()) {
      let div1 = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "div"
      );
      let label = document.createXULElement("label");
      let text = field.label;
      let 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);

      let div2 = document.createElementNS(
        "http://www.w3.org/1999/xhtml",
        "div"
      );
      let 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"
      );
      let 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);

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

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

    window.sizeToContent();
  },

  join() {
    let values = joinChat._values;
    for (let field of joinChat._fields) {
      let 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);
      }
    }
    let account = joinChat._account;
    account.joinChat(values);

    let 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");
    }

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

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

      let prefBranch = Services.prefs.getBranch(
        "messenger.account." + account.id + "."
      );
      let autojoin = [];
      if (prefBranch.prefHasUserValue(autoJoinPref)) {
        let 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", event => {
  joinChat.onload();
});
window.addEventListener("load", event => {
  window.sizeToContent();
});