]>
Commit | Line | Data |
---|---|---|
e20e5a6b | 1 | qwebirc.irc.NickChanEntry = function() { |
9e769c12 | 2 | this.prefixes = ""; |
4b3dcc22 CP |
3 | this.lastSpoke = -Infinity; |
4 | }; | |
9e769c12 | 5 | |
e20e5a6b | 6 | qwebirc.irc.IRCTracker = new Class({ |
07f9257c | 7 | initialize: function(owner) { |
ea29e3d7 CP |
8 | this.channels = new QHash(); |
9 | this.nicknames = new QHash(); | |
07f9257c CP |
10 | this.owner = owner; |
11 | }, | |
12 | toIRCLower: function(value) { | |
13 | /* proxied because the method can change after we connect */ | |
14 | ||
15 | return this.owner.toIRCLower(value); | |
9e769c12 | 16 | }, |
9e769c12 | 17 | getNick: function(nick) { |
ea29e3d7 | 18 | return this.nicknames.get(nick); |
9e769c12 CP |
19 | }, |
20 | getOrCreateNick: function(nick) { | |
21 | var n = this.getNick(nick); | |
ea29e3d7 CP |
22 | if(!n) { |
23 | n = new QHash(); | |
24 | this.nicknames.put(nick, n); | |
25 | } | |
9e769c12 CP |
26 | return n; |
27 | }, | |
28 | getChannel: function(channel) { | |
ea29e3d7 | 29 | return this.channels.get(this.toIRCLower(channel)); |
9e769c12 CP |
30 | }, |
31 | getOrCreateChannel: function(channel) { | |
32 | var c = this.getChannel(channel); | |
ea29e3d7 CP |
33 | if (!c) { |
34 | c = new QHash(); | |
35 | this.channels.put(this.toIRCLower(channel), c); | |
36 | } | |
9e769c12 CP |
37 | return c; |
38 | }, | |
39 | getOrCreateNickOnChannel: function(nick, channel) { | |
6c0b4653 | 40 | var n = this.getOrCreateNick(nick); |
9e769c12 | 41 | |
ea29e3d7 | 42 | var nc = n.get(this.toIRCLower(channel)); |
9e769c12 CP |
43 | if(!nc) |
44 | return this.addNickToChannel(nick, channel); | |
45 | ||
46 | return nc; | |
47 | }, | |
6c0b4653 CP |
48 | getNickOnChannel: function(nick, channel) { |
49 | var n = this.getNick(nick); | |
50 | if(!n) | |
51 | return; | |
52 | ||
ea29e3d7 | 53 | return n.get(this.toIRCLower(channel)); |
e1615235 | 54 | }, |
9e769c12 | 55 | addNickToChannel: function(nick, channel) { |
e20e5a6b | 56 | var nc = new qwebirc.irc.NickChanEntry(); |
9e769c12 CP |
57 | |
58 | var n = this.getOrCreateNick(nick); | |
ea29e3d7 | 59 | n.put(this.toIRCLower(channel), nc); |
9e769c12 CP |
60 | |
61 | var c = this.getOrCreateChannel(channel); | |
ea29e3d7 | 62 | c.put(nick, nc); |
9e769c12 CP |
63 | |
64 | return nc; | |
65 | }, | |
66 | removeNick: function(nick) { | |
67 | var n = this.getNick(nick); | |
68 | if(!n) | |
69 | return; | |
70 | ||
ea29e3d7 | 71 | n.each(function(channel) { |
07f9257c | 72 | var lchannel = this.toIRCLower(channel); |
ea29e3d7 CP |
73 | var c = this.channels.get(lchannel); |
74 | ||
75 | c.remove(nick); | |
76 | if(c.isEmpty()) | |
77 | this.channels.remove(lchannel); | |
78 | }, this); | |
79 | this.nicknames.remove(nick); | |
9e769c12 CP |
80 | }, |
81 | removeChannel: function(channel) { | |
82 | var c = this.getChannel(channel); | |
83 | if(!c) | |
84 | return; | |
07f9257c CP |
85 | |
86 | var lchannel = this.toIRCLower(channel); | |
87 | ||
ea29e3d7 CP |
88 | c.each(function(nick) { |
89 | var n = this.nicknames.get(nick); | |
9e769c12 | 90 | |
ea29e3d7 CP |
91 | n.remove(lchannel); |
92 | if(n.isEmpty()) | |
93 | this.nicknames.remove(nick); | |
94 | }, this); | |
95 | this.channels.remove(lchannel); | |
9e769c12 CP |
96 | }, |
97 | removeNickFromChannel: function(nick, channel) { | |
07f9257c CP |
98 | var lchannel = this.toIRCLower(channel); |
99 | ||
9e769c12 | 100 | var n = this.getNick(nick); |
07f9257c | 101 | var c = this.getChannel(lchannel); |
9e769c12 CP |
102 | if(!n || !c) |
103 | return; | |
104 | ||
ea29e3d7 CP |
105 | n.remove(lchannel); |
106 | c.remove(nick); | |
9e769c12 | 107 | |
ea29e3d7 CP |
108 | if(n.isEmpty()) |
109 | this.nicknames.remove(nick); | |
110 | if(c.isEmpty()) | |
111 | this.channels.remove(lchannel); | |
9e769c12 CP |
112 | }, |
113 | renameNick: function(oldnick, newnick) { | |
114 | var n = this.getNick(oldnick); | |
115 | if(!n) | |
116 | return; | |
117 | ||
ea29e3d7 CP |
118 | n.each(function(channel) { |
119 | var c = this.channels.get(this.toIRCLower(channel)); | |
120 | c.put(newnick, c.get(oldnick)); | |
7e83091b | 121 | c.remove(oldnick); |
ea29e3d7 | 122 | }, this); |
9e769c12 | 123 | |
ea29e3d7 CP |
124 | this.nicknames.put(newnick, this.nicknames.get(oldnick)); |
125 | this.nicknames.remove(oldnick); | |
6c0b4653 CP |
126 | }, |
127 | updateLastSpoke: function(nick, channel, time) { | |
128 | var nc = this.getNickOnChannel(nick, channel); | |
e1615235 | 129 | if($defined(nc)) |
6c0b4653 CP |
130 | nc.lastSpoke = time; |
131 | }, | |
f9a07b87 | 132 | __mapAndSortEntries: function(channel, fn, pos) { |
e1615235 | 133 | var c = this.getChannel(channel); |
6c0b4653 CP |
134 | if(!c) |
135 | return; | |
f9a07b87 CP |
136 | |
137 | var n = c.map(fn); | |
4b3dcc22 | 138 | n.sort(qwebirc.util.arrayCmp); |
e1615235 CP |
139 | |
140 | var n2 = []; | |
141 | for(var i=0;i<n.length;i++) | |
f9a07b87 CP |
142 | n2.push(n[i][pos]); |
143 | ||
e1615235 | 144 | return n2; |
f9a07b87 CP |
145 | }, |
146 | getSortedByLastSpoke: function(channel) { | |
147 | var l = this.toIRCLower.bind(this); | |
148 | return this.__mapAndSortEntries(channel, function(nick, nc) { | |
149 | return [-nc.lastSpoke, l(nick)]; | |
150 | }, 2); | |
151 | }, | |
152 | getSortedByLastSpokePrefix: function(channel) { | |
153 | var p = this.owner.getPrefixPriority.bind(this.owner); | |
154 | var l = this.toIRCLower.bind(this); | |
155 | return this.__mapAndSortEntries(channel, function(nick, nc) { | |
156 | return [-nc.lastSpoke, p(nc.prefixes), l(nick), nick]; | |
157 | }, 3); | |
9e769c12 CP |
158 | } |
159 | }); |