Removed irc_events from the prototype; Untied IrcEventBinder from other objects;...
[KiwiIRC.git] / server / irc / channel.js
1 var util = require('util'),
2 EventBinder = require('./eventbinder.js');
3
4 function IrcChannel(irc_connection, name) {
5 this.irc_connection = irc_connection;
6 this.name = name;
7
8 this.members = [];
9 this.ban_list_buffer = [];
10
11 // Listen for events on the IRC connection
12 this.irc_events = {
13 join: onJoin,
14 part: onPart,
15 kick: onKick,
16 quit: onQuit,
17 privmsg: onMsg,
18 notice: onNotice,
19 ctcp_request: onCtcpRequest,
20 ctcp_response: onCtcpResponse,
21 topic: onTopic,
22 nicklist: onNicklist,
23 nicklistEnd: onNicklistEnd,
24 banlist: onBanList,
25 banlist_end: onBanListEnd,
26 topicsetby: onTopicSetby,
27 mode: onMode
28 };
29 EventBinder.bindIrcEvents('channel:' + this.name, this.irc_events, this, irc_connection);
30 }
31
32
33 module.exports = IrcChannel;
34
35
36 IrcChannel.prototype.dispose = function (){
37 EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events);
38 this.irc_connection = undefined;
39 };
40
41
42
43 function onJoin(event) {
44 this.irc_connection.clientEvent('join', {
45 channel: this.name,
46 nick: event.nick,
47 ident: event.ident,
48 hostname: event.hostname,
49 });
50
51 // If we've just joined this channel then request get a nick list
52 if (event.nick === this.irc_connection.nick) {
53 this.irc_connection.write('NAMES ' + channel);
54 }
55 };
56
57
58 function onPart(event) {
59 this.irc_connection.clientEvent('part', {
60 nick: event.nick,
61 ident: event.ident,
62 hostname: event.hostname,
63 channel: this.name,
64 message: event.message
65 });
66
67 this.dispose();
68 };
69
70
71 function onKick(event) {
72 this.irc_connection.clientEvent('kick', {
73 kicked: event.kicked, // Nick of the kicked
74 nick: event.nick, // Nick of the kicker
75 ident: event.ident,
76 hostname: event.hostname,
77 channel: this.name,
78 message: event.message
79 });
80
81 this.dispose();
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 });
92
93 this.dispose();
94 };
95
96
97 function onMsg(event) {
98 this.irc_connection.clientEvent('msg', {
99 nick: event.nick,
100 ident: event.ident,
101 hostname: event.hostname,
102 channel: this.name,
103 msg: event.message
104 });
105 };
106
107
108 function onNotice(event) {
109 this.irc_connection.clientEvent('msg', {
110 nick: event.nick,
111 ident: event.ident,
112 hostname: event.hostname,
113 channel: this.name,
114 msg: event.trailing
115 });
116 };
117
118
119 function onCtcpRequest(event) {
120 this.irc_connection.clientEvent('ctcp_request', {
121 nick: event.nick,
122 ident: event.ident,
123 hostname: event.hostname,
124 target: event.target,
125 type: event.type,
126 msg: event.msg
127 });
128 };
129
130
131 function onCtcpResponse(event) {
132 this.irc_connection.clientEvent('ctcp_response', {
133 nick: event.nick,
134 ident: event.ident,
135 hostname: event.hostname,
136 target: event.target,
137 type: event.type,
138 msg: event.msg
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 };
150
151
152 function onNicklistEnd(event) {
153 this.irc_connection.clientEvent('userlist_end', {
154 users: event.users,
155 channel: this.name
156 });
157 };
158
159
160 function onTopic(event) {
161 this.irc_connection.clientEvent('topic', {
162 nick: event.nick,
163 channel: this.name,
164 topic: event.topic
165 });
166 };
167
168
169 function onBanList(event) {
170 this.ban_list_buffer.push(event);
171 };
172
173 function onBanListEnd(event) {
174 var that = this;
175 this.ban_list_buffer.forEach(function (ban) {
176 that.irc_connection.clientEvent('banlist', ban);
177 });
178 this.ban_list_buffer = [];
179 };
180
181 function onTopic(event) {
182 this.irc_connection.clientEvent('topic', {
183 channel: event.channel,
184 topic: event.topic
185 });
186 };
187
188 function onTopicSetBy(event) {
189 this.irc_connection.clientEvent('topicsetby', {
190 nick: event.nick,
191 channel: event.channel,
192 when: event.when
193 });
194 };
195
196 function onMode(event) {
197 this.irc_connection.clientEvent('mode', {
198 target: event.target,
199 nick: event.nick,
200 modes: event.modes
201 });
202 };
203
204
205 /*
206 server:event
207 server:*
208 channel:#channel:event
209 channel:*:event
210 user:event
211 user:*
212
213 Server disconnected:
214 server:disconnect
215 server:*
216
217 Joining channel #kiwiirc:
218 channel:#kiwiirc:join
219 channel:*:join
220
221 Channel message:
222 channel:#kiwiirc:privmsg
223 channel:*:privmsg
224
225 Private message:
226 user:privmsg
227 user:*
228 */