MODE handling
[KiwiIRC.git] / server / irc / channel.js
1 var util = require('util'),
2 Binder = require('./binder.js');
3
4 function IrcChannel(irc_connection, name) {
5 this.irc_connection = irc_connection;
6 this.name = name;
7
8 this.scope = 'channel:' + name;
9 Binder.call(this);
10
11 this.members = [];
12 this.ban_list_buffer = [];
13 }
14
15 util.inherits(IrcChannel, Binder);
16
17 module.exports = IrcChannel;
18
19
20 IrcChannel.prototype.dispose = function (){
21 this.unbindEvents();
22 this.irc_connection = undefined;
23 };
24
25
26 IrcChannel.prototype.irc_events = {
27 join: onJoin,
28 part: onPart,
29 kick: onKick,
30 quit: onQuit,
31 privmsg: onMsg,
32 notice: onNotice,
33 ctcp_request: onCtcpRequest,
34 ctcp_response: onCtcpResponse,
35 topic: onTopic,
36 nicklist: onNicklist,
37 nicklistEnd: onNicklistEnd,
38 banlist: onBanList,
39 banlist_end: onBanListEnd,
40 topicsetby: onTopicSetby,
41 mode: onMode
42 };
43
44
45 function onJoin(event) {
46 this.irc_connection.sendIrcCommand('join', {
47 channel: this.name,
48 nick: event.nick,
49 ident: event.ident,
50 hostname: event.hostname,
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 ' + channel);
56 }
57 };
58
59
60 function onPart(event) {
61 this.irc_connection.sendIrcCommand('part', {
62 nick: event.nick,
63 ident: event.ident,
64 hostname: event.hostname,
65 channel: this.name,
66 message: event.message
67 });
68
69 this.dispose();
70 };
71
72
73 function onKick(event) {
74 this.irc_connection.sendIrcCommand('kick', {
75 kicked: event.kicked, // Nick of the kicked
76 nick: event.nick, // Nick of the kicker
77 ident: event.ident,
78 hostname: event.hostname,
79 channel: this.name,
80 message: event.message
81 });
82
83 this.dispose();
84 };
85
86
87 function onQuit(event) {
88 this.irc_connection.sendIrcCommand('quit', {
89 nick: event.nick,
90 ident: event.ident,
91 hostname: event.hostname,
92 message: event.message
93 });
94
95 this.dispose();
96 };
97
98
99 function onMsg(event) {
100 this.irc_connection.sendIrcCommand('msg', {
101 nick: event.nick,
102 ident: event.ident,
103 hostname: event.hostname,
104 channel: this.name,
105 msg: event.message
106 });
107 };
108
109
110 function onNotice(event) {
111 this.irc_connection.sendIrcCommand('msg', {
112 nick: event.nick,
113 ident: event.ident,
114 hostname: event.hostname,
115 channel: this.name,
116 msg: event.trailing
117 });
118 };
119
120
121 function onCtcpRequest(event) {
122 this.irc_connection.sendIrcCommand('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 });
130 };
131
132
133 function onCtcpResponse(event) {
134 this.irc_connection.sendIrcCommand('ctcp_response', {
135 nick: event.nick,
136 ident: event.ident,
137 hostname: event.hostname,
138 target: event.target,
139 type: event.type,
140 msg: event.msg
141 });
142 };
143
144
145 // TODO: Split event.users into batches of 50
146 function onNicklist(event) {
147 this.irc_connection.sendIrcCommand('userlist', {
148 users: event.users,
149 channel: this.name
150 });
151 };
152
153
154 function onNicklistEnd(event) {
155 this.irc_connection.sendIrcCommand('userlist_end', {
156 users: event.users,
157 channel: this.name
158 });
159 };
160
161
162 function onTopic(event) {
163 this.irc_connection.sendIrcCommand('topic', {
164 nick: event.nick,
165 channel: this.name,
166 topic: event.topic
167 });
168 };
169
170
171 function onBanList(event) {
172 this.ban_list_buffer.push(event);
173 };
174
175 function onBanListEnd(event) {
176 var that = this;
177 this.ban_list_buffer.forEach(function (ban) {
178 that.irc_connection.clientEvent('banlist', ban);
179 });
180 this.ban_list_buffer = [];
181 };
182
183 function onTopic(event) {
184 this.irc_connection.clientEvent('topic', {
185 channel: event.channel,
186 topic: event.topic
187 });
188 };
189
190 function onTopicSetBy(event) {
191 this.irc_connection.clientEvent('topicsetby', {
192 nick: event.nick,
193 channel: event.channel,
194 when: event.when
195 });
196 };
197
198 function onMode(event) {
199 this.irc_connection.clientEvent('mode', {
200 target: event.target,
201 nick: event.nick,
202 modes: event.modes
203 });
204 };
205
206
207 /*
208 server:event
209 server:*
210 channel:#channel:event
211 channel:*:event
212 user:event
213 user:*
214
215 Server disconnected:
216 server:disconnect
217 server:*
218
219 Joining channel #kiwiirc:
220 channel:#kiwiirc:join
221 channel:*:join
222
223 Channel message:
224 channel:#kiwiirc:privmsg
225 channel:*:privmsg
226
227 Private message:
228 user:privmsg
229 user:*
230 */