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