]>
jfr.im git - irc/quakenet/qwebirc.git/blob - static/js/irc/ircclient.js
16e2b727d9bbfeb20fd969380a2429ad5241561a
1 function IRCClient(nickname
, ui
, autojoin
) {
4 this.modeprefixes
= "ov";
7 var newLine = function(window
, type
, data
) {
11 var w
= self
.getWindow(window
);
13 w
.addLine(type
, data
);
15 self
.statusWindow
.addLine(type
, data
);
18 this.newLine
= newLine
;
20 var newChanLine = function(channel
, type
, user
, extra
) {
24 extra
["n"] = hosttonick(user
);
25 extra
["h"] = hosttohost(user
);
27 extra
["-"] = self
.nickname
;
29 newLine(channel
, type
, extra
);
32 var newServerLine = function(type
, data
) {
33 self
.statusWindow
.addLine(type
, data
);
36 var newActiveLine = function(type
, data
) {
37 ui
.getActiveWindow().addLine(type
, data
);
40 this.rawNumeric = function(numeric
, prefix
, params
) {
41 newServerLine("RAW", {"n": "numeric", "m": params
.slice(1).join(" ")});
44 this.signedOn = function(nickname
) {
45 self
.tracker
= new IRCTracker();
46 self
.nickname
= nickname
;
47 newServerLine("SIGNON");
50 self
.__send("JOIN " + autojoin
);
53 this.updateNickList = function(channel
) {
54 var n1
= self
.tracker
.getChannel(channel
);
55 var names
= new Array();
56 var tff
= String
.fromCharCode(255);
64 if(nc
.prefixes
.length
> 0) {
65 var c
= nc
.prefixes
.charAt(0);
66 nx
= String
.fromCharCode(self
.prefixes
.indexOf(c
)) + toIRCLower(n
);
69 nx
= tff
+ toIRCLower(n
);
77 var sortednames
= new Array();
78 forEach(names
, function(name
) {
79 sortednames
.push(nh
[name
]);
82 var w
= self
.getWindow(channel
);
84 w
.updateNickList(sortednames
);
87 this.getWindow = function(name
) {
88 return self
.windows
[name
];
91 this.newWindow = function(name
, type
, select
) {
92 var w
= self
.getWindow(name
);
94 w
= self
.windows
[name
] = ui
.newWindow(self
, type
, name
);
96 w
.addEvent("close", function(w
) {
97 delete self
.windows
[name
];
107 this.userJoined = function(user
, channel
) {
108 var nick
= hosttonick(user
);
109 var host
= hosttohost(user
);
111 if((nick
== self
.nickname
) && !self
.getWindow(channel
))
112 self
.newWindow(channel
, WINDOW_CHANNEL
, true);
113 self
.tracker
.addNickToChannel(nick
, channel
);
115 newChanLine(channel
, "JOIN", user
);
117 self
.updateNickList(channel
);
120 this.userPart = function(user
, channel
, message
) {
121 var nick
= hosttonick(user
);
122 var host
= hosttohost(user
);
124 if(nick
== self
.nickname
) {
125 self
.tracker
.removeChannel(channel
);
127 self
.tracker
.removeNickFromChannel(nick
, channel
);
128 newChanLine(channel
, "PART", user
, {"m": message
});
131 self
.updateNickList(channel
);
133 var w
= self
.getWindow(channel
)
138 this.userKicked = function(kicker
, channel
, kickee
, message
) {
139 if(kickee
== self
.nickname
) {
140 self
.tracker
.removeChannel(channel
);
141 self
.getWindow(channel
).close();
143 self
.tracker
.removeNickFromChannel(kickee
, channel
);
144 self
.updateNickList(channel
);
147 newChanLine(channel
, "KICK", kicker
, {"v": kickee
, "m": message
});
150 this.channelMode = function(user
, channel
, modes
, raw
) {
151 forEach(modes
, function(mo
) {
152 var direction
= mo
[0];
155 var prefixindex
= self
.modeprefixes
.indexOf(mode
);
156 if(prefixindex
== -1)
160 var prefixchar
= self
.prefixes
.charAt(prefixindex
);
162 var nc
= self
.tracker
.getOrCreateNickOnChannel(nick
, channel
);
163 if(direction
== "-") {
164 self
.removePrefix(nc
, prefixchar
);
166 self
.addPrefix(nc
, prefixchar
);
170 newChanLine(channel
, "MODE", user
, {"m": raw
.join(" ")});
172 self
.updateNickList(channel
);
175 this.userQuit = function(user
, message
) {
176 var nick
= hosttonick(user
);
178 var channels
= self
.tracker
.getNick(nick
);
181 for(var c
in channels
) {
183 newChanLine(c
, "QUIT", user
, {"m": message
});
186 self
.tracker
.removeNick(nick
);
188 forEach(clist
, function(cli
) {
189 self
.updateNickList(cli
);
193 this.nickChanged = function(user
, newnick
) {
194 var oldnick
= hosttonick(user
);
196 if(oldnick
== self
.nickname
)
197 self
.nickname
= newnick
;
199 self
.tracker
.renameNick(oldnick
, newnick
);
201 var channels
= self
.tracker
.getNick(newnick
);
203 for(var c
in channels
) {
204 newChanLine(c
, "NICK", user
, {"w": newnick
});
205 /* TODO: rename queries */
206 self
.updateNickList(c
);
210 this.channelTopic = function(user
, channel
, topic
) {
211 newChanLine(channel
, "TOPIC", user
, {"m": topic
});
212 self
.getWindow(channel
).updateTopic(topic
);
215 this.initialTopic = function(channel
, topic
) {
216 self
.getWindow(channel
).updateTopic(topic
);
219 this.channelCTCP = function(user
, channel
, type
, args
) {
220 if(args
== undefined)
223 if(type
== "ACTION") {
224 newChanLine(channel
, "CHANACTION", user
, {"m": args
, "c": channel
});
228 newChanLine(channel
, "CHANCTCP", user
, {"x": type
, "m": args
, "c": channel
});
231 this.userCTCP = function(user
, type
, args
) {
232 var nick
= hosttonick(user
);
233 var host
= hosttohost(user
);
234 if(args
== undefined)
237 if(type
== "ACTION") {
238 self
.newWindow(nick
, WINDOW_QUERY
);
239 newLine(nick
, "PRIVACTION", {"m": args
, "x": type
, "h": host
, "n": nick
});
243 if(self
.getWindow(nick
)) {
244 newLine(nick
, "PRIVCTCP", {"m": args
, "x": type
, "h": host
, "n": nick
, "-": self
.nickname
});
246 newActiveLine("PRIVCTCP", {"m": args
, "x": type
, "h": host
, "n": nick
, "-": self
.nickname
});
250 this.userCTCPReply = function(user
, type
, args
) {
251 var nick
= hosttonick(user
);
252 var host
= hosttohost(user
);
253 if(args
== undefined)
256 if(self
.getWindow(nick
)) {
257 newLine(nick
, "CTCPREPLY", {"m": args
, "x": type
, "h": host
, "n": nick
, "-": self
.nickname
});
259 newActiveLine("CTCPREPLY", {"m": args
, "x": type
, "h": host
, "n": nick
, "-": self
.nickname
});
263 this.channelPrivmsg = function(user
, channel
, message
) {
264 newChanLine(channel
, "CHANMSG", user
, {"m": message
});
267 this.channelNotice = function(user
, channel
, message
) {
268 newChanLine(channel
, "CHANNOTICE", user
, {"m": message
});
271 this.userPrivmsg = function(user
, message
) {
272 var nick
= hosttonick(user
);
273 var host
= hosttohost(user
);
275 self
.newWindow(nick
, WINDOW_QUERY
);
277 newLine(nick
, "PRIVMSG", {"m": message
, "h": host
, "n": nick
});
280 this.serverNotice = function(message
) {
281 newServerLine("SERVERNOTICE", {"m": message
});
284 this.userNotice = function(user
, message
) {
285 var nick
= hosttonick(user
);
286 var host
= hosttohost(user
);
288 if(self
.getWindow(nick
)) {
289 newLine(nick
, "PRIVNOTICE", {"m": message
, "h": host
, "n": nick
});
291 newActiveLine("PRIVNOTICE", {"m": message
, "h": host
, "n": nick
});
295 this.userInvite = function(user
, channel
) {
296 var nick
= hosttonick(user
);
297 var host
= hosttohost(user
);
299 newServerLine("INVITE", {"c": channel
, "h": host
, "n": nick
});
302 this.userMode = function(modes
) {
303 newServerLine("UMODE", {"m": modes
, "n": self
.nickname
});
306 this.addPrefix = function(nickchanentry
, prefix
) {
307 var ncp
= nickchanentry
.prefixes
+ prefix
;
308 var prefixes
= new Array();
311 for(var i
=0;i
<self
.prefixes
.length
;i
++) {
312 var pc
= self
.prefixes
.charAt(i
);
313 var index
= ncp
.indexOf(pc
);
318 nickchanentry
.prefixes
= prefixes
.join("");
321 this.removePrefix = function(nickchanentry
, prefix
) {
322 nickchanentry
.prefixes
= nickchanentry
.prefixes
.replaceAll(prefix
, "");
325 this.channelNames = function(channel
, names
) {
326 if(names
.length
== 0) {
327 self
.updateNickList(channel
);
331 forEach(names
, function(nick
) {
333 var splitnick
= nick
.split("");
336 forEach(splitnick
, function(c
) {
337 if(self
.prefixes
.indexOf(c
) == -1) {
338 nick
= nick
.substr(i
);
346 var nc
= self
.tracker
.addNickToChannel(nick
, channel
);
347 forEach(prefixes
, function(p
) {
348 self
.addPrefix(nc
, p
);
353 this.disconnected = function() {
354 for(var x
in this.parent
.channels
) {
358 self
.tracker
= undefined;
360 newServerLine("DISCONNECT");
364 this.supported = function(key
, value
) {
365 if(key
== "PREFIX") {
366 var l
= (value
.length
- 2) / 2;
368 self
.modeprefixes
= value
.substr(1, l
);
369 self
.prefixes
= value
.substr(l
+ 2, l
);
373 this.connected = function() {
374 newServerLine("CONNECT");
377 this.serverError = function(message
) {
378 newServerLine("ERROR", {"m": message
});
381 this.getActiveWindow = function() {
382 return ui
.getActiveWindow();
385 this.getNickname = function() {
386 return self
.nickname
;
389 this.parent
= new BaseIRCClient(nickname
, this);
390 this.__send
= this.parent
.send
;
392 this.commandparser
= new CommandParser(this);
393 this.dispatch
= this.commandparser
.dispatch
395 this.statusWindow
= ui
.newClient(self
);
397 this.connect
= this.parent
.connect
;
398 this.disconnect
= this.parent
.disconnect
;