Merge remote-tracking branch 'upstream/development' into development
[KiwiIRC.git] / server / irc / user.js
1 var EventBinder = require('./eventbinder.js');
2
3 var IrcUser = function (irc_connection, nick) {
4 this.irc_connection = irc_connection;
5 this.nick = nick;
6
7 this.irc_events = {
8 nick: onNick,
9 away: onAway,
10 quit: onQuit,
11 whoisuser: onWhoisUser,
12 whoisaway: onWhoisAway,
13 whoisoperator: onWhoisOperator,
14 whoischannels: onWhoisChannels,
15 whoismodes: onWhoisModes,
16 whoisidle: onWhoisIdle,
17 whoisregnick: onWhoisRegNick,
18 whoisserver: onWhoisServer,
19 whoishost: onWhoisHost,
20 whoissecure: onWhoisSecure,
21 whoisaccount: onWhoisAccount,
22 whoishelpop: onWhoisHelpOp,
23 whoisbot: onWhoisBot,
24 whoisswhois: onWhoisSwhois,
25 endofwhois: onWhoisEnd,
26 whowas: onWhoWas,
27 endofwhowas: onWhoWasEnd,
28 wasnosuchnick: onWasNoSuchNick,
29 notice: onNotice,
30 ctcp_response: onCtcpResponse,
31 privmsg: onPrivmsg,
32 action: onAction,
33 ctcp_request: onCtcpRequest,
34 mode: onMode,
35 wallops: onWallops
36 };
37 EventBinder.bindIrcEvents('user ' + this.nick, this.irc_events, this, irc_connection);
38 };
39
40
41 module.exports = IrcUser;
42
43
44 IrcUser.prototype.dispose = function () {
45 EventBinder.unbindIrcEvents('user ' + this.nick, this.irc_events, this.irc_connection);
46 this.irc_connection = undefined;
47 };
48
49
50 function onNick(event) {
51 this.irc_connection.clientEvent('nick', {
52 nick: event.nick,
53 ident: event.ident,
54 hostname: event.hostname,
55 newnick: event.newnick,
56 time: event.time
57 });
58
59 // TODO: uncomment when using an IrcUser per nick
60 //EventBinder.unbindIrcEvents('user ' + this.nick, this.irc_events, irc_connection);
61 //this.nick = event.newnick;
62 //EventBinder.bindIrcEvents('user ' + this.nick, this.irc_events, this, irc_connection);
63 }
64
65 function onAway(event) {
66 this.irc_connection.clientEvent('away', {
67 nick: event.nick,
68 msg: event.msg,
69 time: event.time
70 });
71 }
72
73 function onQuit(event) {
74 this.irc_connection.clientEvent('quit', {
75 nick: event.nick,
76 ident: event.ident,
77 hostname: event.hostname,
78 message: event.message,
79 time: event.time
80 });
81 }
82
83 function onWhoisUser(event) {
84 this.irc_connection.clientEvent('whois', {
85 nick: event.nick,
86 ident: event.ident,
87 hostname: event.host,
88 msg: event.msg,
89 end: false
90 });
91 }
92
93 function onWhoisAway(event) {
94 this.irc_connection.clientEvent('whois', {
95 nick: event.nick,
96 away_reason: event.reason,
97 end: false
98 });
99 }
100
101 function onWhoisServer(event) {
102 this.irc_connection.clientEvent('whois', {
103 nick: event.nick,
104 irc_server: event.irc_server,
105 server_info: event.server_info,
106 end: false
107 });
108 }
109
110 function onWhoisOperator(event) {
111 this.irc_connection.clientEvent('whois', {
112 nick: event.nick,
113 msg: event.msg,
114 end: false
115 });
116 }
117
118 function onWhoisChannels(event) {
119 this.irc_connection.clientEvent('whois', {
120 nick: event.nick,
121 chans: event.chans,
122 end: false
123 });
124 }
125
126 function onWhoisModes(event) {
127 this.irc_connection.clientEvent('whois', {
128 nick: event.nick,
129 msg: event.msg,
130 end: false
131 });
132 }
133
134 function onWhoisIdle(event) {
135 this.irc_connection.clientEvent('whois', {
136 nick: event.nick,
137 idle: event.idle,
138 logon: event.logon || undefined,
139 end: false
140 });
141 }
142
143 function onWhoisRegNick(event) {
144 this.irc_connection.clientEvent('whois', {
145 nick: event.nick,
146 msg: event.msg,
147 end: false
148 });
149 }
150
151 function onWhoisHost(event) {
152 this.irc_connection.clientEvent('whois', {
153 nick: event.nick,
154 msg: event.msg,
155 end: false
156 });
157 }
158
159 function onWhoisSecure(event) {
160 this.irc_connection.clientEvent('whois', {
161 nick: event.nick,
162 msg: 'Using a secure connection',
163 end: false
164 });
165 }
166
167 function onWhoisAccount(event) {
168 this.irc_connection.clientEvent('whois', {
169 nick: event.nick,
170 msg: 'Logged in as ' + event.account,
171 end: false
172 });
173 }
174
175 function onWhoisHelpOp(event) {
176 this.irc_connection.clientEvent('whois', {
177 nick: event.nick,
178 msg: event.msg,
179 end: false
180 });
181 }
182
183 function onWhoisBot(event) {
184 this.irc_connection.clientEvent('whois', {
185 nick: event.nick,
186 msg: event.msg,
187 end: false
188 });
189 }
190
191 function onWhoisSwhois(event) {
192 this.irc_connection.clientEvent('whois', {
193 nick: event.nick,
194 msg: event.msg,
195 end: false
196 });
197 }
198
199 function onWhoisEnd(event) {
200 this.irc_connection.clientEvent('whois', {
201 nick: event.nick,
202 msg: event.msg,
203 end: true
204 });
205 }
206
207 function onWhoWas(event) {
208 this.irc_connection.clientEvent('whowas', {
209 nick: event.nick,
210 ident: event.user,
211 hostname: event.host,
212 real_name: event.real_name,
213 end: false
214 });
215 }
216
217 function onWasNoSuchNick(event) {
218 this.irc_connection.clientEvent('whowas', {
219 nick: event.nick,
220 end: false
221 });
222 }
223
224 function onWhoWasEnd(event) {
225 this.irc_connection.clientEvent('whowas', {
226 nick: event.nick,
227 end: true
228 });
229 }
230
231 function onNotice(event) {
232 var that = this;
233 global.modules.emit('irc user notice', {
234 connection: this.irc_connection,
235 irc_event: event
236 })
237 .done(function() {
238 that.irc_connection.clientEvent('message', {
239 type: 'notice',
240 from_server: event.from_server,
241 nick: event.nick,
242 ident: event.ident,
243 hostname: event.hostname,
244 target: event.target,
245 msg: event.msg,
246 time: event.time
247 });
248 });
249 }
250
251 function onCtcpResponse(event) {
252 this.irc_connection.clientEvent('ctcp_response', {
253 nick: event.nick,
254 ident: event.ident,
255 hostname: event.hostname,
256 target: event.target,
257 msg: event.msg,
258 time: event.time
259 });
260 }
261
262 function onPrivmsg(event) {
263 var that = this;
264
265 global.modules.emit('irc message', {
266 connection: this.irc_connection,
267 irc_event: event
268 })
269 .done(function() {
270 that.irc_connection.clientEvent('message', {
271 type: 'message',
272 nick: event.nick,
273 ident: event.ident,
274 hostname: event.hostname,
275 target: event.target,
276 msg: event.msg,
277 time: event.time
278 });
279 });
280 }
281
282 function onAction(event) {
283 var that = this;
284
285 global.modules.emit('irc action', {
286 connection: this.irc_connection,
287 irc_event: event
288 })
289 .done(function() {
290 that.irc_connection.clientEvent('message', {
291 type: 'action',
292 nick: event.nick,
293 ident: event.ident,
294 hostname: event.hostname,
295 target: event.target,
296 msg: event.msg,
297 time: event.time
298 });
299 });
300 }
301
302 function onCtcpRequest(event) {
303 this.irc_connection.clientEvent('ctcp_request', {
304 nick: event.nick,
305 ident: event.ident,
306 hostname: event.hostname,
307 target: event.target,
308 type: event.type,
309 msg: event.msg,
310 time: event.time
311 });
312 }
313
314 function onMode(event) {
315 this.irc_connection.clientEvent('mode', {
316 target: event.target,
317 nick: event.nick,
318 modes: event.modes,
319 time: event.time
320 });
321 }
322
323 function onWallops(event) {
324 this.irc_connection.clientEvent('wallops', {
325 nick: event.nick,
326 ident: event.ident,
327 hostname: event.hostname,
328 msg: event.msg,
329 time: event.time
330 });
331 }