Merge pull request #304 from happytodesign/development
[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 endofwhois: onWhoisEnd,
21 whowas: onWhoWas,
22 endofwhowas: onWhoWasEnd,
23 wasnosuchnick: onWasNoSuchNick,
24 notice: onNotice,
25 ctcp_response: onCtcpResponse,
26 privmsg: onPrivmsg,
27 ctcp_request: onCtcpRequest,
28 mode: onMode
29 };
30 EventBinder.bindIrcEvents('user ' + this.nick, this.irc_events, this, irc_connection);
31 };
32
33
34 module.exports = IrcUser;
35
36
37 IrcUser.prototype.dispose = function () {
38 EventBinder.unbindIrcEvents('user ' + this.nick, this.irc_events, this.irc_connection);
39 this.irc_connection = undefined;
40 };
41
42
43 function onNick(event) {
44 this.irc_connection.clientEvent('nick', {
45 nick: event.nick,
46 ident: event.ident,
47 hostname: event.hostname,
48 newnick: event.newnick
49 });
50
51 // TODO: uncomment when using an IrcUser per nick
52 //EventBinder.unbindIrcEvents('user ' + this.nick, this.irc_events, irc_connection);
53 //this.nick = event.newnick;
54 //EventBinder.bindIrcEvents('user ' + this.nick, this.irc_events, this, irc_connection);
55 }
56
57 function onAway(event) {
58 this.irc_connection.clientEvent('away', {
59 nick: event.nick,
60 msg: event.msg
61 });
62 }
63
64 function onQuit(event) {
65 this.irc_connection.clientEvent('quit', {
66 nick: event.nick,
67 ident: event.ident,
68 hostname: event.hostname,
69 message: event.trailing
70 });
71 }
72
73 function onWhoisUser(event) {
74 this.irc_connection.clientEvent('whois', {
75 nick: event.nick,
76 ident: event.ident,
77 host: event.host,
78 msg: event.msg,
79 end: false
80 });
81 }
82
83 function onWhoisAway(event) {
84 this.irc_connection.clientEvent('whois', {
85 nick: event.nick,
86 away_reason: event.reason,
87 end: false
88 });
89 }
90
91 function onWhoisServer(event) {
92 this.irc_connection.clientEvent('whois', {
93 nick: event.nick,
94 irc_server: event.irc_server,
95 server_info: event.server_info,
96 end: false
97 });
98 }
99
100 function onWhoisOperator(event) {
101 this.irc_connection.clientEvent('whois', {
102 nick: event.nick,
103 msg: event.msg,
104 end: false
105 });
106 }
107
108 function onWhoisChannels(event) {
109 this.irc_connection.clientEvent('whois', {
110 nick: event.nick,
111 chans: event.chans,
112 end: false
113 });
114 }
115
116 function onWhoisModes(event) {
117 this.irc_connection.clientEvent('whois', {
118 nick: event.nick,
119 msg: event.msg,
120 end: false
121 });
122 }
123
124 function onWhoisIdle(event) {
125 this.irc_connection.clientEvent('whois', {
126 nick: event.nick,
127 idle: event.idle,
128 logon: event.logon || undefined,
129 end: false
130 });
131 }
132
133 function onWhoisRegNick(event) {
134 this.irc_connection.clientEvent('whois', {
135 nick: event.nick,
136 msg: event.msg,
137 end: false
138 });
139 }
140
141 function onWhoisEnd(event) {
142 this.irc_connection.clientEvent('whois', {
143 nick: event.nick,
144 msg: event.msg,
145 end: true
146 });
147 }
148
149 function onWhoWas(event) {
150 this.irc_connection.clientEvent('whowas', {
151 nick: event.nick,
152 ident: event.user,
153 host: event.host,
154 real_name: event.real_name,
155 end: false
156 });
157 }
158
159 function onWasNoSuchNick(event) {
160 this.irc_connection.clientEvent('whowas', {
161 nick: event.nick,
162 end: false
163 });
164 }
165
166 function onWhoWasEnd(event) {
167 this.irc_connection.clientEvent('whowas', {
168 nick: event.nick,
169 end: true
170 });
171 }
172
173 function onNotice(event) {
174 this.irc_connection.clientEvent('notice', {
175 from_server: event.from_server,
176 nick: event.nick,
177 ident: event.ident,
178 hostname: event.hostname,
179 target: event.target,
180 msg: event.msg
181 });
182 }
183
184 function onCtcpResponse(event) {
185 this.irc_connection.clientEvent('ctcp_response', {
186 nick: event.nick,
187 ident: event.ident,
188 hostname: event.hostname,
189 channel: event.channel,
190 msg: event.msg
191 });
192 }
193
194 function onPrivmsg(event) {
195 this.irc_connection.clientEvent('msg', {
196 nick: event.nick,
197 ident: event.ident,
198 hostname: event.hostname,
199 channel: event.channel,
200 msg: event.msg
201 });
202 }
203
204 function onCtcpRequest(event) {
205 this.irc_connection.clientEvent('ctcp_request', {
206 nick: event.nick,
207 ident: event.ident,
208 hostname: event.hostname,
209 target: event.target,
210 type: event.type,
211 msg: event.msg
212 });
213 }
214
215 function onMode(event) {
216 this.irc_connection.clientEvent('mode', {
217 target: event.target,
218 nick: event.nick,
219 modes: event.modes
220 });
221 }