Commit | Line | Data |
---|---|---|
cefa0900 JA |
1 | var util = require('util'), |
2 | EventBinder = require('./eventbinder.js'), | |
3 | IrcUser = require('./user.js'); | |
b1fa8101 | 4 | |
1f6caa9c | 5 | var 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); |
32a09dc1 | 31 | }; |
b1fa8101 | 32 | |
2a8d2d5f JA |
33 | |
34 | module.exports = IrcChannel; | |
35 | ||
b1fa8101 | 36 | |
a9833d9c | 37 | IrcChannel.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 | 44 | function 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 | 55 | } |
32a09dc1 | 56 | } |
b1fa8101 D |
57 | |
58 | ||
a9833d9c | 59 | function 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 | }); | |
32a09dc1 | 67 | } |
b1fa8101 | 68 | |
ffbbc70f | 69 | |
a9833d9c | 70 | function 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 | }); | |
32a09dc1 | 79 | } |
b1fa8101 D |
80 | |
81 | ||
a9833d9c | 82 | function 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 | }); | |
32a09dc1 | 89 | } |
ffbbc70f D |
90 | |
91 | ||
a9833d9c | 92 | function 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 | 99 | }); |
32a09dc1 | 100 | } |
ffbbc70f D |
101 | |
102 | ||
a9833d9c | 103 | function onNotice(event) { |
929a6962 | 104 | this.irc_connection.clientEvent('notice', { |
5c0582ae | 105 | from_server: event.from_server, |
ffbbc70f D |
106 | nick: event.nick, |
107 | ident: event.ident, | |
108 | hostname: event.hostname, | |
929a6962 D |
109 | target: event.target, |
110 | msg: event.msg | |
ffbbc70f | 111 | }); |
32a09dc1 | 112 | } |
ffbbc70f D |
113 | |
114 | ||
a9833d9c | 115 | function onCtcpRequest(event) { |
3c91bff8 | 116 | this.irc_connection.clientEvent('ctcp_request', { |
ffbbc70f D |
117 | nick: event.nick, |
118 | ident: event.ident, | |
119 | hostname: event.hostname, | |
120 | target: event.target, | |
121 | type: event.type, | |
122 | msg: event.msg | |
123 | }); | |
32a09dc1 | 124 | } |
ffbbc70f D |
125 | |
126 | ||
a9833d9c | 127 | function onCtcpResponse(event) { |
3c91bff8 | 128 | this.irc_connection.clientEvent('ctcp_response', { |
e2752476 D |
129 | nick: event.nick, |
130 | ident: event.ident, | |
131 | hostname: event.hostname, | |
132 | target: event.target, | |
133 | type: event.type, | |
134 | msg: event.msg | |
135 | }); | |
32a09dc1 | 136 | } |
e2752476 D |
137 | |
138 | ||
ffbbc70f | 139 | // TODO: Split event.users into batches of 50 |
a9833d9c | 140 | function onNicklist(event) { |
3c91bff8 | 141 | this.irc_connection.clientEvent('userlist', { |
ffbbc70f D |
142 | users: event.users, |
143 | channel: this.name | |
144 | }); | |
df6d68a5 D |
145 | // TODO: uncomment when using an IrcUser per nick |
146 | //updateUsersList.call(this, event.users); | |
32a09dc1 | 147 | } |
ffbbc70f D |
148 | |
149 | ||
a9833d9c | 150 | function onNicklistEnd(event) { |
3c91bff8 | 151 | this.irc_connection.clientEvent('userlist_end', { |
ffbbc70f D |
152 | users: event.users, |
153 | channel: this.name | |
154 | }); | |
df6d68a5 D |
155 | // TODO: uncomment when using an IrcUser per nick |
156 | //updateUsersList.call(this, event.users); | |
32a09dc1 | 157 | } |
ffbbc70f | 158 | |
cefa0900 JA |
159 | function updateUsersList(users) { |
160 | var that = this; | |
161 | if (users) { | |
162 | users.forEach(function (user) { | |
163 | if (!that.irc_connection.irc_users[user.nick]) { | |
164 | that.irc_connection.irc_users[user.nick] = new IrcUser(that.irc_connection, user.nick); | |
165 | } | |
166 | }); | |
167 | } | |
168 | } | |
169 | ||
f4ec5774 | 170 | |
a9833d9c | 171 | function onTopic(event) { |
3c91bff8 | 172 | this.irc_connection.clientEvent('topic', { |
f4ec5774 D |
173 | nick: event.nick, |
174 | channel: this.name, | |
175 | topic: event.topic | |
176 | }); | |
32a09dc1 | 177 | } |
f4ec5774 | 178 | |
059f4918 JA |
179 | |
180 | function onBanList(event) { | |
181 | this.ban_list_buffer.push(event); | |
32a09dc1 | 182 | } |
059f4918 JA |
183 | |
184 | function onBanListEnd(event) { | |
185 | var that = this; | |
186 | this.ban_list_buffer.forEach(function (ban) { | |
187 | that.irc_connection.clientEvent('banlist', ban); | |
188 | }); | |
189 | this.ban_list_buffer = []; | |
32a09dc1 | 190 | } |
059f4918 JA |
191 | |
192 | function onTopic(event) { | |
193 | this.irc_connection.clientEvent('topic', { | |
194 | channel: event.channel, | |
195 | topic: event.topic | |
196 | }); | |
32a09dc1 | 197 | } |
059f4918 JA |
198 | |
199 | function onTopicSetBy(event) { | |
200 | this.irc_connection.clientEvent('topicsetby', { | |
201 | nick: event.nick, | |
202 | channel: event.channel, | |
203 | when: event.when | |
204 | }); | |
32a09dc1 | 205 | } |
059f4918 | 206 | |
a7973dfb JA |
207 | function onMode(event) { |
208 | this.irc_connection.clientEvent('mode', { | |
209 | target: event.target, | |
210 | nick: event.nick, | |
211 | modes: event.modes | |
212 | }); | |
32a09dc1 | 213 | } |