Merge branch 'development' into channel_info
[KiwiIRC.git] / server / irc / channel.js
1 var util = require('util'),
2 EventBinder = require('./eventbinder.js'),
3 IrcUser = require('./user.js');
4
5 var IrcChannel = function(irc_connection, name) {
6 this.irc_connection = irc_connection;
7 this.name = name;
8
9 this.members = [];
10 this.ban_list_buffer = [];
11
12 // Listen for events on the IRC connection
13 this.irc_events = {
14 join: onJoin,
15 part: onPart,
16 kick: onKick,
17 quit: onQuit,
18 privmsg: onMsg,
19 notice: onNotice,
20 ctcp_request: onCtcpRequest,
21 ctcp_response: onCtcpResponse,
22 topic: onTopic,
23 userlist: onNicklist,
24 userlist_end: onNicklistEnd,
25 banlist: onBanList,
26 banlist_end: onBanListEnd,
27 topicsetby: onTopicSetBy,
28 mode: onMode,
29 info: onChannelInfo
30 };
31 EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection);
32 };
33
34
35 module.exports = IrcChannel;
36
37
38 IrcChannel.prototype.dispose = function (){
39 EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection);
40 this.irc_connection = undefined;
41 };
42
43
44
45 function onJoin(event) {
46 this.irc_connection.clientEvent('join', {
47 channel: this.name,
48 nick: event.nick,
49 ident: event.ident,
50 hostname: event.hostname,
51 time: event.time
52 });
53 }
54
55
56 function onPart(event) {
57 this.irc_connection.clientEvent('part', {
58 nick: event.nick,
59 ident: event.ident,
60 hostname: event.hostname,
61 channel: this.name,
62 message: event.message,
63 time: event.time
64 });
65 }
66
67
68 function onKick(event) {
69 this.irc_connection.clientEvent('kick', {
70 kicked: event.kicked, // Nick of the kicked
71 nick: event.nick, // Nick of the kicker
72 ident: event.ident,
73 hostname: event.hostname,
74 channel: this.name,
75 message: event.message,
76 time: event.time
77 });
78 }
79
80
81 function onQuit(event) {
82 this.irc_connection.clientEvent('quit', {
83 nick: event.nick,
84 ident: event.ident,
85 hostname: event.hostname,
86 message: event.message,
87 time: event.time
88 });
89 }
90
91
92 function onMsg(event) {
93 this.irc_connection.clientEvent('msg', {
94 nick: event.nick,
95 ident: event.ident,
96 hostname: event.hostname,
97 channel: this.name,
98 msg: event.msg,
99 time: event.time
100 });
101 }
102
103
104 function onNotice(event) {
105 this.irc_connection.clientEvent('notice', {
106 from_server: event.from_server,
107 nick: event.nick,
108 ident: event.ident,
109 hostname: event.hostname,
110 target: event.target,
111 msg: event.msg,
112 time: event.time
113 });
114 }
115
116
117 function onCtcpRequest(event) {
118 this.irc_connection.clientEvent('ctcp_request', {
119 nick: event.nick,
120 ident: event.ident,
121 hostname: event.hostname,
122 target: event.target,
123 type: event.type,
124 msg: event.msg,
125 time: event.time
126 });
127 }
128
129
130 function onCtcpResponse(event) {
131 this.irc_connection.clientEvent('ctcp_response', {
132 nick: event.nick,
133 ident: event.ident,
134 hostname: event.hostname,
135 target: event.target,
136 type: event.type,
137 msg: event.msg,
138 time: event.time
139 });
140 }
141
142
143 // TODO: Split event.users into batches of 50
144 function onNicklist(event) {
145 this.irc_connection.clientEvent('userlist', {
146 users: event.users,
147 channel: this.name
148 });
149 // TODO: uncomment when using an IrcUser per nick
150 //updateUsersList.call(this, event.users);
151 }
152
153
154 function onNicklistEnd(event) {
155 this.irc_connection.clientEvent('userlist_end', {
156 users: event.users,
157 channel: this.name
158 });
159 // TODO: uncomment when using an IrcUser per nick
160 //updateUsersList.call(this, event.users);
161 }
162
163 function updateUsersList(users) {
164 var that = this;
165 if (users) {
166 users.forEach(function (user) {
167 if (!that.irc_connection.irc_users[user.nick]) {
168 that.irc_connection.irc_users[user.nick] = new IrcUser(that.irc_connection, user.nick);
169 }
170 });
171 }
172 }
173
174
175 function onTopic(event) {
176 this.irc_connection.clientEvent('topic', {
177 nick: event.nick,
178 channel: this.name,
179 topic: event.topic,
180 time: event.time
181 });
182 }
183
184
185 function onChannelInfo(event) {
186 // Channel info event may contain 1 of several types of info,
187 // including creation time, modes. So just pipe the event
188 // right through to the client
189 this.irc_connection.clientEvent('channel_info', event);
190 }
191
192
193 function onBanList(event) {
194 this.ban_list_buffer.push(event);
195 }
196
197 function onBanListEnd(event) {
198 this.irc_connection.clientEvent('banlist', {
199 channel: this.name,
200 bans: this.ban_list_buffer
201 });
202
203 this.ban_list_buffer = [];
204 }
205
206 function onTopic(event) {
207 this.irc_connection.clientEvent('topic', {
208 channel: event.channel,
209 topic: event.topic
210 });
211 }
212
213 function onTopicSetBy(event) {
214 this.irc_connection.clientEvent('topicsetby', {
215 nick: event.nick,
216 channel: event.channel,
217 when: event.when
218 });
219 }
220
221 function onMode(event) {
222 this.irc_connection.clientEvent('mode', {
223 target: event.target,
224 nick: event.nick,
225 modes: event.modes,
226 time: event.time
227 });
228 }