| 1 | var util = require('util'), |
| 2 | EventBinder = require('./eventbinder.js'), |
| 3 | IrcUser = require('./user.js'); |
| 4 | |
| 5 | var IrcChannel = function(irc_connection, name) { |
| 6 | this.irc_connection = irc_connection; |
| 7 | |
| 8 | // Lowercase the channel name so we don't run into case-sensitive issues |
| 9 | this.name = name.toLowerCase(); |
| 10 | |
| 11 | this.members = []; |
| 12 | this.ban_list_buffer = []; |
| 13 | |
| 14 | // Listen for events on the IRC connection |
| 15 | this.irc_events = { |
| 16 | join: onJoin, |
| 17 | part: onPart, |
| 18 | kick: onKick, |
| 19 | quit: onQuit, |
| 20 | privmsg: onMsg, |
| 21 | notice: onNotice, |
| 22 | ctcp_request: onCtcpRequest, |
| 23 | ctcp_response: onCtcpResponse, |
| 24 | topic: onTopic, |
| 25 | userlist: onNicklist, |
| 26 | userlist_end: onNicklistEnd, |
| 27 | banlist: onBanList, |
| 28 | banlist_end: onBanListEnd, |
| 29 | topicsetby: onTopicSetBy, |
| 30 | mode: onMode, |
| 31 | info: onChannelInfo |
| 32 | }; |
| 33 | EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection); |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | module.exports = IrcChannel; |
| 38 | |
| 39 | |
| 40 | IrcChannel.prototype.dispose = function (){ |
| 41 | EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection); |
| 42 | this.irc_connection = undefined; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | |
| 47 | function onJoin(event) { |
| 48 | var that = this; |
| 49 | |
| 50 | global.modules.emit('irc channel join', { |
| 51 | channel: this, |
| 52 | connection: this.irc_connection, |
| 53 | irc_event: event |
| 54 | }) |
| 55 | .done(function() { |
| 56 | that.irc_connection.clientEvent('join', { |
| 57 | channel: that.name, |
| 58 | nick: event.nick, |
| 59 | ident: event.ident, |
| 60 | hostname: event.hostname, |
| 61 | time: event.time |
| 62 | }); |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | function onPart(event) { |
| 68 | var that = this; |
| 69 | |
| 70 | global.modules.emit('irc channel part', { |
| 71 | channel: this, |
| 72 | connection: this.irc_connection, |
| 73 | irc_event: event |
| 74 | }) |
| 75 | .done(function() { |
| 76 | that.irc_connection.clientEvent('part', { |
| 77 | nick: event.nick, |
| 78 | ident: event.ident, |
| 79 | hostname: event.hostname, |
| 80 | channel: that.name, |
| 81 | message: event.message, |
| 82 | time: event.time |
| 83 | }); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | function onKick(event) { |
| 89 | var that = this; |
| 90 | |
| 91 | global.modules.emit('irc channel kick', { |
| 92 | channel: this, |
| 93 | connection: this.irc_connection, |
| 94 | irc_event: event |
| 95 | }) |
| 96 | .done(function() { |
| 97 | that.irc_connection.clientEvent('kick', { |
| 98 | kicked: event.kicked, // Nick of the kicked |
| 99 | nick: event.nick, // Nick of the kicker |
| 100 | ident: event.ident, |
| 101 | hostname: event.hostname, |
| 102 | channel: that.name, |
| 103 | message: event.message, |
| 104 | time: event.time |
| 105 | }); |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | function onQuit(event) { |
| 111 | var that = this; |
| 112 | |
| 113 | global.modules.emit('irc channel quit', { |
| 114 | channel: this, |
| 115 | connection: this.irc_connection, |
| 116 | irc_event: event |
| 117 | }) |
| 118 | .done(function() { |
| 119 | that.irc_connection.clientEvent('quit', { |
| 120 | nick: event.nick, |
| 121 | ident: event.ident, |
| 122 | hostname: event.hostname, |
| 123 | message: event.message, |
| 124 | time: event.time |
| 125 | }); |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | function onMsg(event) { |
| 131 | var that = this; |
| 132 | |
| 133 | global.modules.emit('irc message', { |
| 134 | channel: this, |
| 135 | connection: this.irc_connection, |
| 136 | irc_event: event |
| 137 | }) |
| 138 | .done(function() { |
| 139 | that.irc_connection.clientEvent('msg', { |
| 140 | nick: event.nick, |
| 141 | ident: event.ident, |
| 142 | hostname: event.hostname, |
| 143 | channel: that.name, |
| 144 | msg: event.msg, |
| 145 | time: event.time |
| 146 | }); |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | function onNotice(event) { |
| 152 | var that = this; |
| 153 | |
| 154 | global.modules.emit('irc channel notice', { |
| 155 | channel: this, |
| 156 | connection: this.irc_connection, |
| 157 | irc_event: event |
| 158 | }) |
| 159 | .done(function() { |
| 160 | that.irc_connection.clientEvent('notice', { |
| 161 | from_server: event.from_server, |
| 162 | nick: event.nick, |
| 163 | ident: event.ident, |
| 164 | hostname: event.hostname, |
| 165 | target: event.target, |
| 166 | msg: event.msg, |
| 167 | time: event.time |
| 168 | }); |
| 169 | }); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | function onCtcpRequest(event) { |
| 174 | this.irc_connection.clientEvent('ctcp_request', { |
| 175 | nick: event.nick, |
| 176 | ident: event.ident, |
| 177 | hostname: event.hostname, |
| 178 | target: event.target, |
| 179 | type: event.type, |
| 180 | msg: event.msg, |
| 181 | time: event.time |
| 182 | }); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | function onCtcpResponse(event) { |
| 187 | this.irc_connection.clientEvent('ctcp_response', { |
| 188 | nick: event.nick, |
| 189 | ident: event.ident, |
| 190 | hostname: event.hostname, |
| 191 | target: event.target, |
| 192 | type: event.type, |
| 193 | msg: event.msg, |
| 194 | time: event.time |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | // TODO: Split event.users into batches of 50 |
| 200 | function onNicklist(event) { |
| 201 | this.irc_connection.clientEvent('userlist', { |
| 202 | users: event.users, |
| 203 | channel: this.name |
| 204 | }); |
| 205 | // TODO: uncomment when using an IrcUser per nick |
| 206 | //updateUsersList.call(this, event.users); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | function onNicklistEnd(event) { |
| 211 | this.irc_connection.clientEvent('userlist_end', { |
| 212 | users: event.users, |
| 213 | channel: this.name |
| 214 | }); |
| 215 | // TODO: uncomment when using an IrcUser per nick |
| 216 | //updateUsersList.call(this, event.users); |
| 217 | } |
| 218 | |
| 219 | function updateUsersList(users) { |
| 220 | var that = this; |
| 221 | if (users) { |
| 222 | users.forEach(function (user) { |
| 223 | if (!that.irc_connection.irc_users[user.nick]) { |
| 224 | that.irc_connection.irc_users[user.nick] = new IrcUser(that.irc_connection, user.nick); |
| 225 | } |
| 226 | }); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
| 231 | function onTopic(event) { |
| 232 | var that = this; |
| 233 | |
| 234 | global.modules.emit('irc channel topic', { |
| 235 | channel: this, |
| 236 | connection: this.irc_connection, |
| 237 | irc_event: event |
| 238 | }) |
| 239 | .done(function() { |
| 240 | that.irc_connection.clientEvent('topic', { |
| 241 | nick: event.nick, |
| 242 | channel: that.name, |
| 243 | topic: event.topic, |
| 244 | time: event.time |
| 245 | }); |
| 246 | }); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | function onChannelInfo(event) { |
| 251 | // Channel info event may contain 1 of several types of info, |
| 252 | // including creation time, modes. So just pipe the event |
| 253 | // right through to the client |
| 254 | this.irc_connection.clientEvent('channel_info', event); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | function onBanList(event) { |
| 259 | this.ban_list_buffer.push(event); |
| 260 | } |
| 261 | |
| 262 | function onBanListEnd(event) { |
| 263 | this.irc_connection.clientEvent('banlist', { |
| 264 | channel: this.name, |
| 265 | bans: this.ban_list_buffer |
| 266 | }); |
| 267 | |
| 268 | this.ban_list_buffer = []; |
| 269 | } |
| 270 | |
| 271 | function onTopicSetBy(event) { |
| 272 | this.irc_connection.clientEvent('topicsetby', { |
| 273 | nick: event.nick, |
| 274 | channel: event.channel, |
| 275 | when: event.when |
| 276 | }); |
| 277 | } |
| 278 | |
| 279 | function onMode(event) { |
| 280 | var that = this; |
| 281 | |
| 282 | global.modules.emit('irc channel mode', { |
| 283 | channel: this, |
| 284 | connection: this.irc_connection, |
| 285 | irc_event: event |
| 286 | }) |
| 287 | .done(function() { |
| 288 | that.irc_connection.clientEvent('mode', { |
| 289 | target: event.target, |
| 290 | nick: event.nick, |
| 291 | modes: event.modes, |
| 292 | time: event.time |
| 293 | }); |
| 294 | }); |
| 295 | } |