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