More channel commands
[KiwiIRC.git] / server / irc / channel.js
1
2 function IrcChannel(irc_connection, name) {
3 this.irc_connection = irc_connection;
4 this.name = name;
5
6 this.members = [];
7 this.ban_list_buffer = [];
8 }
9
10
11 IrcChannel.prototype.dispose = function (){
12 this.unbindEvents();
13 this.irc_connection = undefined;
14 };
15
16
17 IrcChannel.prototype.bindEvents = function() {
18 var that = this;
19
20 // If we havent generated an event listing yet, do so now
21 if (!this.irc_events) {
22 this.irc_events = {
23 join: onJoin,
24 part: onPart,
25 kick: onKick,
26 quit: onQuit,
27 privmsg: onMsg,
28 notice: onNotice,
29 ctcp_request: onCtcpRequest,
30 ctcp_response: onCtcpResponse,
31 topic: onTopic,
32 nicklist: onNicklist,
33 nicklistEnd: onNicklistEnd,
34 banlist: onBanList,
35 banlist_end: onBanListEnd,
36 topicsetby: onTopicSetby
37 };
38 }
39
40 this.irc_events.forEach(function(fn, event_name, irc_events){
41 // Bind the event to `that` context, storing it with the event listing
42 if (!irc_events[event_name].bound_fn) {
43 irc_events[event_name].bound_fn = fn.bind(that);
44 }
45
46 this.irc_connection.on(event_name, irc_events[event_name].bound_fn);
47 });
48 };
49
50
51 IrcChannel.prototype.unbindEvents = function() {
52 this.irc_events.forEach(function(fn, event_name, irc_events){
53 if (irc_events[event_name].bound_fn) {
54 this.irc_connection.removeListener(event_name, irc_events[event_name].bound_fn);
55 }
56 });
57 };
58
59
60
61
62
63 function onJoin(event) {
64 this.irc_connection.sendIrcCommand('join', {
65 channel: this.name,
66 nick: event.nick,
67 ident: event.ident,
68 hostname: event.hostname,
69 });
70
71 // If we've just joined this channel then request get a nick list
72 if (event.nick === this.irc_connection.nick) {
73 this.irc_connection.write('NAMES ' + channel);
74 }
75 };
76
77
78 function onPart(event) {
79 this.irc_connection.sendIrcCommand('part', {
80 nick: event.nick,
81 ident: event.ident,
82 hostname: event.hostname,
83 channel: this.name,
84 message: event.message
85 });
86
87 this.dispose();
88 };
89
90
91 function onKick(event) {
92 this.irc_connection.sendIrcCommand('kick', {
93 kicked: event.kicked, // Nick of the kicked
94 nick: event.nick, // Nick of the kicker
95 ident: event.ident,
96 hostname: event.hostname,
97 channel: this.name,
98 message: event.message
99 });
100
101 this.dispose();
102 };
103
104
105 function onQuit(event) {
106 this.irc_connection.sendIrcCommand('quit', {
107 nick: event.nick,
108 ident: event.ident,
109 hostname: event.hostname,
110 message: event.message
111 });
112
113 this.dispose();
114 };
115
116
117 function onMsg(event) {
118 this.irc_connection.sendIrcCommand('msg', {
119 nick: event.nick,
120 ident: event.ident,
121 hostname: event.hostname,
122 channel: this.name,
123 msg: event.message
124 });
125 };
126
127
128 function onNotice(event) {
129 this.irc_connection.sendIrcCommand('msg', {
130 nick: event.nick,
131 ident: event.ident,
132 hostname: event.hostname,
133 channel: this.name,
134 msg: event.trailing
135 });
136 };
137
138
139 function onCtcpRequest(event) {
140 this.irc_connection.sendIrcCommand('ctcp_request', {
141 nick: event.nick,
142 ident: event.ident,
143 hostname: event.hostname,
144 target: event.target,
145 type: event.type,
146 msg: event.msg
147 });
148 };
149
150
151 function onCtcpResponse(event) {
152 this.irc_connection.sendIrcCommand('ctcp_response', {
153 nick: event.nick,
154 ident: event.ident,
155 hostname: event.hostname,
156 target: event.target,
157 type: event.type,
158 msg: event.msg
159 });
160 };
161
162
163 // TODO: Split event.users into batches of 50
164 function onNicklist(event) {
165 this.irc_connection.sendIrcCommand('userlist', {
166 users: event.users,
167 channel: this.name
168 });
169 };
170
171
172 function onNicklistEnd(event) {
173 this.irc_connection.sendIrcCommand('userlist_end', {
174 users: event.users,
175 channel: this.name
176 });
177 };
178
179
180 function onTopic(event) {
181 this.irc_connection.sendIrcCommand('topic', {
182 nick: event.nick,
183 channel: this.name,
184 topic: event.topic
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
217 /*
218 server:event
219 server:*
220 channel:#channel:event
221 channel:*:event
222 user:event
223 user:*
224
225 Server disconnected:
226 server:disconnect
227 server:*
228
229 Joining channel #kiwiirc:
230 channel:#kiwiirc:join
231 channel:*:join
232
233 Channel message:
234 channel:#kiwiirc:privmsg
235 channel:*:privmsg
236
237 Private message:
238 user:privmsg
239 user:*
240 */