/
my.js
180 lines (155 loc) · 5.13 KB
/
my.js
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
/* fxos-irc, an IRC client application for Mozilla's Firefox OS
* Copyright (C) 2013 Nick Desaulniers <nick@mozilla.com>
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (c) 2010, Martyn Smith
* Copyright (c) 2011-2013, Fabien Cazenave
* Copyright (c) 2013, Yusuke Kamiyamane
* Copyright (c) 2013, Mark James
* Copyright (c) 2013, Mozilla Corporation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var clients = {};
var privMSG = {};
var $ = document.getElementById.bind(document);
function joinChans (channels, client, username, host) {
channels.split(/\s*,\s*/).forEach(function (chan) {
chan = chan.trim();
if (chan.length === 0) {
return;
}
if (chan[0] !== "#") {
chan = "#" + chan;
}
var chanAndPass = chan.split(" ");
var password = null;
if (chanAndPass.length > 1) {
chan = chanAndPass[0];
password = chanAndPass[1];
chanAndPass.length = 2;
}
client.join(chanAndPass.join(" "), function () {
console.log("Joined ", chan);
new Tab({
chan: chan,
password: password,
client: client,
nick: username,
host: host,
});
});
});
};
document.addEventListener("DOMContentLoaded", function () {
var $advanced = $("advanced");
$("advanced-link").onclick = function () {
if ($advanced.style.display === "block") {
$advanced.style.display = "none";
} else {
$advanced.style.display = "block";
}
}
if (localStorage.nick) {
$("username").value = localStorage.nick;
}
$("connect").addEventListener("click", function () {
var hostEle = $("host");
var userEle = $("username");
var channelsEle = $("channels");
var host = hostEle.value;
var username = userEle.value;
var channels = channelsEle.value;
var port = $("port").value;
var secure = $("secure").checked;
localStorage.nick = username;
hostEle.value = null;
channelsEle.value = null;
if (!host || !username) {
console.error("No username or host");
return;
}
// cache clients by host
var client = clients[host];
if (client) {
joinChans(channels, client, username, host);
} else {
client = clients[host] = new Client(host, username, {
stripColors: true,
autoConnect: false,
secure: secure,
port: port || (secure ? 6697 : 6667),
//debug: true,
});
$("loading").style.display = "block";
client.connect(function () {
console.log("client connected");
$("loading").style.display = "none";
var div = document.createElement("div");
div.id = "__" + host;
var disconnect = document.createElement("img");
disconnect.src = "disconnect.png";
disconnect.className = "moz-button";
disconnect.onclick = function () {
client.disconnect(function () {
console.log("client disconnected");
var listing = $("__" + host);
delete clients[host];
listing.parentElement.removeChild(listing);
// remove tabs and cards for all chans on that host
var nodes = document.querySelectorAll("." + host.replace(/\./g, "-"));
for (var i = 0; i < nodes.length; ++i) {
var el = nodes[i];
el.parentNode.removeChild(el)
}
for (var nick in privMSG) {
if (host === privMSG[nick].host) {
delete privMSG[nick];
}
}
});
};
div.appendChild(disconnect);
div.appendChild(document.createTextNode(host));
div.onclick = function () {
hostEle.value = host;
};
$("hostlist").appendChild(div);
joinChans(channels, client, username, host);
});
client.addListener("pm", function (from, text, message) {
var msg = privMSG[from];
if (!msg) {
msg = privMSG[from] = new Tab({
chan: from,
client: client,
nick: username,
host: host,
});
}
msg.addText(from, text);
var img = "https://raw.github.com/nickdesaulniers/fxos-irc/master/128.png";
if ($("container").selectedCard.id !== msg.card.id) {
// This hack is because origin is not supported in manifests for < 1.1.
sendNotification(from, { body: text, icon: img });
if (!msg.tab.classList.contains("glow")) {
msg.tab.classList.add("glow");
}
}
});
}
});
});