]>
jfr.im git - irc/quakenet/qwebirc.git/blob - js/irc/ircclient.js
1 qwebirc
.irc
.IRCClient
= new Class({
2 Extends: qwebirc
.irc
.BaseIRCClient
,
8 initialize: function(options
, ui
) {
14 this.modeprefixes
= "ov";
17 this.commandparser
= new qwebirc
.irc
.CommandParser(this);
18 this.exec
= this.commandparser
.dispatch
.bind(this.commandparser
);
20 this.statusWindow
= this.ui
.newClient(this);
23 newLine: function(window
, type
, data
) {
27 var w
= this.getWindow(window
);
29 w
.addLine(type
, data
);
31 this.statusWindow
.addLine(type
, data
);
34 newChanLine: function(channel
, type
, user
, extra
) {
38 extra
["n"] = user
.hostToNick();
39 extra
["h"] = user
.hostToHost();
41 extra
["-"] = this.nickname
;
43 this.newLine(channel
, type
, extra
);
45 newServerLine: function(type
, data
) {
46 this.statusWindow
.addLine(type
, data
);
48 newActiveLine: function(type
, data
) {
49 this.getActiveWindow().addLine(type
, data
);
51 newTargetOrActiveLine: function(target
, type
, data
) {
52 if(this.getWindow(target
)) {
53 this.newLine(target
, type
, data
);
55 this.newActiveLine(type
, data
);
58 updateNickList: function(channel
) {
59 var n1
= this.tracker
.getChannel(channel
);
60 var names
= new Array();
61 var tff
= String
.fromCharCode(255);
69 if(nc
.prefixes
.length
> 0) {
70 var c
= nc
.prefixes
.charAt(0);
71 nx
= String
.fromCharCode(this.prefixes
.indexOf(c
)) + n
.toIRCLower();
74 nx
= tff
+ n
.toIRCLower();
82 var sortednames
= new Array();
83 names
.each(function(name
) {
84 sortednames
.push(nh
[name
]);
87 var w
= this.getWindow(channel
);
89 w
.updateNickList(sortednames
);
91 getWindow: function(name
) {
92 return this.windows
[name
];
94 newWindow: function(name
, type
, select
) {
95 var w
= this.getWindow(name
);
97 w
= this.windows
[name
] = this.ui
.newWindow(this, type
, name
);
99 w
.addEvent("close", function(w
) {
100 delete this.windows
[name
];
105 this.ui
.selectWindow(w
);
109 newQueryWindow: function(name
) {
110 if(this.ui
.uiOptions
.DEDICATED_MSG_WINDOW
)
111 return this.ui
.newWindow(this, qwebirc
.ui
.WINDOW_MESSAGES
, "Messages");
112 return this.newWindow(name
, qwebirc
.ui
.WINDOW_QUERY
, false);
114 newQueryLine: function(window
, type
, data
, active
) {
115 if(this.getWindow(window
))
116 return this.newLine(window
, type
, data
);
118 var w
= this.ui
.getWindow(this, qwebirc
.ui
.WINDOW_MESSAGES
);
120 if(this.ui
.uiOptions
.DEDICATED_MSG_WINDOW
&& w
) {
121 return w
.addLine(type
, data
);
124 return this.newActiveLine(window
, type
, data
);
126 return this.newLine(window
, type
, data
);
130 newQueryOrActiveLine: function(window
, type
, data
) {
131 this.newQueryLine(window
, type
, data
, true);
133 getActiveWindow: function() {
134 return this.ui
.getActiveIRCWindow(this);
136 getNickname: function() {
137 return this.nickname
;
139 addPrefix: function(nickchanentry
, prefix
) {
140 var ncp
= nickchanentry
.prefixes
+ prefix
;
144 for(var i
=0;i
<this.prefixes
.length
;i
++) {
145 var pc
= this.prefixes
.charAt(i
);
146 var index
= ncp
.indexOf(pc
);
151 nickchanentry
.prefixes
= prefixes
.join("");
153 stripPrefix: function(nick
) {
154 var l
= nick
.charAt(0);
158 if(this.prefixes
.indexOf(l
) != -1)
159 return nick
.substring(1);
163 removePrefix: function(nickchanentry
, prefix
) {
164 nickchanentry
.prefixes
= nickchanentry
.prefixes
.replaceAll(prefix
, "");
167 /* from here down are events */
168 rawNumeric: function(numeric
, prefix
, params
) {
169 this.newServerLine("RAW", {"n": "numeric", "m": params
.slice(1).join(" ")});
171 signedOn: function(nickname
) {
172 this.tracker
= new qwebirc
.irc
.IRCTracker();
173 this.nickname
= nickname
;
174 this.newServerLine("SIGNON");
176 if(this.options
.autojoin
)
177 this.send("JOIN " + this.options
.autojoin
);
179 userJoined: function(user
, channel
) {
180 var nick
= user
.hostToNick();
181 var host
= user
.hostToHost();
183 if((nick
== this.nickname
) && !this.getWindow(channel
))
184 this.newWindow(channel
, qwebirc
.ui
.WINDOW_CHANNEL
, true);
185 this.tracker
.addNickToChannel(nick
, channel
);
187 if(nick
== this.nickname
) {
188 this.newChanLine(channel
, "OURJOIN", user
);
190 this.newChanLine(channel
, "JOIN", user
);
192 this.updateNickList(channel
);
194 userPart: function(user
, channel
, message
) {
195 var nick
= user
.hostToNick();
196 var host
= user
.hostToHost();
198 if(nick
== this.nickname
) {
199 this.tracker
.removeChannel(channel
);
201 this.tracker
.removeNickFromChannel(nick
, channel
);
202 this.newChanLine(channel
, "PART", user
, {"m": message
});
205 this.updateNickList(channel
);
207 if(nick
== this.nickname
) {
208 var w
= this.getWindow(channel
)
213 userKicked: function(kicker
, channel
, kickee
, message
) {
214 if(kickee
== this.nickname
) {
215 this.tracker
.removeChannel(channel
);
216 this.getWindow(channel
).close();
218 this.tracker
.removeNickFromChannel(kickee
, channel
);
219 this.updateNickList(channel
);
222 this.newChanLine(channel
, "KICK", kicker
, {"v": kickee
, "m": message
});
224 channelMode: function(user
, channel
, modes
, raw
) {
225 modes
.each(function(mo
) {
226 var direction
= mo
[0];
229 var prefixindex
= this.modeprefixes
.indexOf(mode
);
230 if(prefixindex
== -1)
234 var prefixchar
= this.prefixes
.charAt(prefixindex
);
236 var nc
= this.tracker
.getOrCreateNickOnChannel(nick
, channel
);
237 if(direction
== "-") {
238 this.removePrefix(nc
, prefixchar
);
240 this.addPrefix(nc
, prefixchar
);
244 this.newChanLine(channel
, "MODE", user
, {"m": raw
.join(" ")});
246 this.updateNickList(channel
);
248 userQuit: function(user
, message
) {
249 var nick
= user
.hostToNick();
251 var channels
= this.tracker
.getNick(nick
);
254 for(var c
in channels
) {
256 this.newChanLine(c
, "QUIT", user
, {"m": message
});
259 this.tracker
.removeNick(nick
);
261 clist
.each(function(cli
) {
262 this.updateNickList(cli
);
265 nickChanged: function(user
, newnick
) {
266 var oldnick
= user
.hostToNick();
268 if(oldnick
== this.nickname
)
269 this.nickname
= newnick
;
271 this.tracker
.renameNick(oldnick
, newnick
);
273 var channels
= this.tracker
.getNick(newnick
);
276 for(var c
in channels
) {
279 this.newChanLine(c
, "NICK", user
, {"w": newnick
});
280 /* TODO: rename queries */
281 this.updateNickList(c
);
285 this.newChanLine(undefined, "NICK", user
, {"w": newnick
});
287 channelTopic: function(user
, channel
, topic
) {
288 this.newChanLine(channel
, "TOPIC", user
, {"m": topic
});
289 this.getWindow(channel
).updateTopic(topic
);
291 initialTopic: function(channel
, topic
) {
292 this.getWindow(channel
).updateTopic(topic
);
294 channelCTCP: function(user
, channel
, type
, args
) {
295 if(args
== undefined)
298 if(type
== "ACTION") {
299 this.tracker
.updateLastSpoke(user
.hostToNick(), channel
, new Date().getTime());
300 this.newChanLine(channel
, "CHANACTION", user
, {"m": args
, "c": channel
});
304 this.newChanLine(channel
, "CHANCTCP", user
, {"x": type
, "m": args
, "c": channel
});
306 userCTCP: function(user
, type
, args
) {
307 var nick
= user
.hostToNick();
308 var host
= user
.hostToHost();
309 if(args
== undefined)
312 if(type
== "ACTION") {
313 this.newQueryWindow(nick
);
314 this.newQueryLine(nick
, "PRIVACTION", {"m": args
, "x": type
, "h": host
, "n": nick
});
318 this.newTargetOrActiveLine(nick
, "PRIVCTCP", {"m": args
, "x": type
, "h": host
, "n": nick
, "-": this.nickname
});
320 userCTCPReply: function(user
, type
, args
) {
321 var nick
= user
.hostToNick();
322 var host
= user
.hostToHost();
323 if(args
== undefined)
326 this.newTargetOrActiveLine(nick
, "CTCPREPLY", {"m": args
, "x": type
, "h": host
, "n": nick
, "-": this.nickname
});
328 getNickStatus: function(channel
, nick
) {
329 var n
= this.tracker
.getNickOnChannel(nick
, channel
);
333 if(n
.prefixes
.length
== 0)
336 return n
.prefixes
.charAt(0);
338 channelPrivmsg: function(user
, channel
, message
) {
339 var nick
= user
.hostToNick();
341 this.tracker
.updateLastSpoke(nick
, channel
, new Date().getTime());
342 this.newChanLine(channel
, "CHANMSG", user
, {"m": message
, "@": this.getNickStatus(channel
, nick
)});
344 channelNotice: function(user
, channel
, message
) {
345 this.newChanLine(channel
, "CHANNOTICE", user
, {"m": message
});
347 userPrivmsg: function(user
, message
) {
348 var nick
= user
.hostToNick();
349 var host
= user
.hostToHost();
351 this.newQueryWindow(nick
);
352 this.pushLastNick(nick
);
353 this.newQueryLine(nick
, "PRIVMSG", {"m": message
, "h": host
, "n": nick
});
355 serverNotice: function(message
) {
356 this.newServerLine("SERVERNOTICE", {"m": message
});
358 userNotice: function(user
, message
) {
359 var nick
= user
.hostToNick();
360 var host
= user
.hostToHost();
362 this.newTargetOrActiveLine(nick
, "PRIVNOTICE", {"m": message
, "h": host
, "n": nick
});
364 userInvite: function(user
, channel
) {
365 var nick
= user
.hostToNick();
366 var host
= user
.hostToHost();
368 this.newServerLine("INVITE", {"c": channel
, "h": host
, "n": nick
});
370 userMode: function(modes
) {
371 this.newServerLine("UMODE", {"m": modes
, "n": this.nickname
});
373 channelNames: function(channel
, names
) {
374 if(names
.length
== 0) {
375 this.updateNickList(channel
);
379 names
.each(function(nick
) {
381 var splitnick
= nick
.split("");
383 splitnick
.every(function(c
, i
) {
384 if(this.prefixes
.indexOf(c
) == -1) {
385 nick
= nick
.substr(i
);
393 var nc
= this.tracker
.addNickToChannel(nick
, channel
);
394 prefixes
.each(function(p
) {
395 this.addPrefix(nc
, p
);
399 disconnected: function(message
) {
400 for(var x
in this.windows
) {
401 var w
= this.windows
[x
];
402 if(w
.type
== qwebirc
.ui
.WINDOW_CHANNEL
)
405 this.tracker
= undefined;
407 this.newServerLine("DISCONNECT", {"m": message
});
409 supported: function(key
, value
) {
410 if(key
== "PREFIX") {
411 var l
= (value
.length
- 2) / 2;
413 this.modeprefixes
= value
.substr(1, l
);
414 this.prefixes
= value
.substr(l
+ 2, l
);
417 connected: function() {
418 this.newServerLine("CONNECT");
420 serverError: function(message
) {
421 this.newServerLine("ERROR", {"m": message
});
423 quit: function(message
) {
424 this.send("QUIT :" + message
);
427 awayMessage: function(nick
, message
) {
428 this.newQueryLine(nick
, "AWAY", {"n": nick
, "m": message
});
430 whois: function(nick
, type
, data
) {
431 var ndata
= {"n": nick
};
434 var xsend = function() {
435 this.newTargetOrActiveLine(nick
, "WHOIS" + mtype
, ndata
);
440 ndata
.h
= data
.ident
+ "@" + data
.hostname
;
443 ndata
.m
= data
.realname
;
444 } else if(type
== "server") {
446 ndata
.x
= data
.server
;
447 ndata
.m
= data
.serverdesc
;
448 } else if(type
== "oper") {
450 } else if(type
== "idle") {
452 ndata
.x
= qwebirc
.util
.longtoduration(data
.idle
);
453 ndata
.m
= qwebirc
.irc
.IRCDate(new Date(data
.connected
* 1000));
454 } else if(type
== "channels") {
456 ndata
.m
= data
.channels
;
457 } else if(type
== "account") {
459 ndata
.m
= data
.account
;
460 } else if(type
== "away") {
463 } else if(type
== "opername") {
465 ndata
.m
= data
.opername
;
466 } else if(type
== "actually") {
468 ndata
.m
= data
.hostname
;
470 } else if(type
== "end") {
479 genericError: function(target
, message
) {
480 this.newTargetOrActiveLine(target
, "GENERICERROR", {m: message
, t: target
});
482 genericQueryError: function(target
, message
) {
483 this.newQueryOrActiveLine(target
, "GENERICERROR", {m: message
, t: target
});
485 awayStatus: function(state
, message
) {
486 this.newActiveLine("GENERICMESSAGE", {m: message
});
488 pushLastNick: function(nick
) {
489 var i
= this.lastNicks
.indexOf(nick
);
491 this.lastNicks
.splice(i
, 1);
493 if(this.lastNicks
.length
== this.options
.maxnicks
)
494 this.lastNicks
.pop();
496 this.lastNicks
.unshift(nick
);