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