Server-side support for server-time CAP
[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 };
30 EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection);
31 };
32
33
34 module.exports = IrcChannel;
35
36
37 IrcChannel.prototype.dispose = function (){
38 EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection);
39 this.irc_connection = undefined;
40 };
41
42
43
44 function onJoin(event) {
45 this.irc_connection.clientEvent('join', {
46 channel: this.name,
47 nick: event.nick,
48 ident: event.ident,
49 hostname: event.hostname,
50 time: event.time
51 });
52
53 // If we've just joined this channel then request get a nick list
54 if (event.nick === this.irc_connection.nick) {
55 this.irc_connection.write('NAMES ' + this.name);
56 }
57 }
58
59
60 function onPart(event) {
61 this.irc_connection.clientEvent('part', {
62 nick: event.nick,
63 ident: event.ident,
64 hostname: event.hostname,
65 channel: this.name,
66 message: event.message,
67 time: event.time
68 });
69 }
70
71
72 function onKick(event) {
73 this.irc_connection.clientEvent('kick', {
74 kicked: event.kicked, // Nick of the kicked
75 nick: event.nick, // Nick of the kicker
76 ident: event.ident,
77 hostname: event.hostname,
78 channel: this.name,
79 message: event.message,
80 time: event.time
81 });
82 }
83
84
85 function onQuit(event) {
86 this.irc_connection.clientEvent('quit', {
87 nick: event.nick,
88 ident: event.ident,
89 hostname: event.hostname,
90 message: event.message,
91 time: event.time
92 });
93 }
94
95
96 function onMsg(event) {
97 this.irc_connection.clientEvent('msg', {
98 nick: event.nick,
99 ident: event.ident,
100 hostname: event.hostname,
101 channel: this.name,
102 msg: event.msg,
103 time: event.time
104 });
105 }
106
107
108 function onNotice(event) {
109 this.irc_connection.clientEvent('notice', {
110 from_server: event.from_server,
111 nick: event.nick,
112 ident: event.ident,
113 hostname: event.hostname,
114 target: event.target,
115 msg: event.msg,
116 time: event.time
117 });
118 }
119
120
121 function onCtcpRequest(event) {
122 this.irc_connection.clientEvent('ctcp_request', {
123 nick: event.nick,
124 ident: event.ident,
125 hostname: event.hostname,
126 target: event.target,
127 type: event.type,
128 msg: event.msg,
129 time: event.time
130 });
131 }
132
133
134 function onCtcpResponse(event) {
135 this.irc_connection.clientEvent('ctcp_response', {
136 nick: event.nick,
137 ident: event.ident,
138 hostname: event.hostname,
139 target: event.target,
140 type: event.type,
141 msg: event.msg,
142 time: event.time
143 });
144 }
145
146
147 // TODO: Split event.users into batches of 50
148 function onNicklist(event) {
149 this.irc_connection.clientEvent('userlist', {
150 users: event.users,
151 channel: this.name
152 });
153 // TODO: uncomment when using an IrcUser per nick
154 //updateUsersList.call(this, event.users);
155 }
156
157
158 function onNicklistEnd(event) {
159 this.irc_connection.clientEvent('userlist_end', {
160 users: event.users,
161 channel: this.name
162 });
163 // TODO: uncomment when using an IrcUser per nick
164 //updateUsersList.call(this, event.users);
165 }
166
167 function updateUsersList(users) {
168 var that = this;
169 if (users) {
170 users.forEach(function (user) {
171 if (!that.irc_connection.irc_users[user.nick]) {
172 that.irc_connection.irc_users[user.nick] = new IrcUser(that.irc_connection, user.nick);
173 }
174 });
175 }
176 }
177
178
179 function onTopic(event) {
180 this.irc_connection.clientEvent('topic', {
181 nick: event.nick,
182 channel: this.name,
183 topic: event.topic,
184 time: event.time
185 });
186 }
187
188
189 function onBanList(event) {
190 this.ban_list_buffer.push(event);
191 }
192
193 function onBanListEnd(event) {
194 var that = this;
195 this.ban_list_buffer.forEach(function (ban) {
196 that.irc_connection.clientEvent('banlist', ban);
197 });
198 this.ban_list_buffer = [];
199 }
200
201 function onTopic(event) {
202 this.irc_connection.clientEvent('topic', {
203 channel: event.channel,
204 topic: event.topic
205 });
206 }
207
208 function onTopicSetBy(event) {
209 this.irc_connection.clientEvent('topicsetby', {
210 nick: event.nick,
211 channel: event.channel,
212 when: event.when
213 });
214 }
215
216 function onMode(event) {
217 this.irc_connection.clientEvent('mode', {
218 target: event.target,
219 nick: event.nick,
220 modes: event.modes,
221 time: event.time
222 });
223 }