UNPKG

squelch-client

Version:
334 lines (305 loc) 9.91 kB
// Generated by CoffeeScript 1.10.0 (function() { var Channel, getReplyCode; getReplyCode = require('../replies').getReplyCode; Channel = (function() { function Channel(client, name) { this._ = { client: client, name: name, topic: '', topicSetter: '', topicTime: null, users: {}, mode: [] }; } Channel.prototype.name = function() { return this._.name; }; Channel.prototype.toString = function() { return this._.name; }; Channel.prototype.client = function() { return this._.client; }; Channel.prototype.contains = function(nick) { return this._.users[nick] != null; }; Channel.prototype.getStatus = function(nick) { return this._.users[nick]; }; Channel.prototype.topic = function(topic) { if (topic == null) { return this._.topic; } return this._.client.raw("TOPIC " + this._.name + " :" + topic); }; Channel.prototype.topicSetter = function() { return this._.topicSetter; }; Channel.prototype.topicTime = function() { return this._.topicTime; }; Channel.prototype.part = function(reason, cb) { return this._.client.part(this._.name, reason, cb); }; Channel.prototype.kick = function(user, reason) { return this._.client.kick(this._.name, user, reason); }; Channel.prototype.ban = function(hostmask) { return this._.client.ban(this._.name, hostmask); }; Channel.prototype.unban = function(hostmask) { return this._.client.unban(this._.name, hostmask); }; Channel.prototype.mode = function(modeStr) { if (modeStr == null) { return this.mode.join(''); } return this._.client.mode(modeStr); }; Channel.prototype.op = function(user) { return this._.client.op(this._.name, user); }; Channel.prototype.deop = function(user) { return this._.client.deop(this._.name, user); }; Channel.prototype.voice = function(user) { return this._.client.voice(this._.name, user); }; Channel.prototype.devoice = function(user) { return this._.client.devoice(this._.name, user); }; Channel.prototype.msg = function(msg) { return this._.client.msg(this._.name, msg); }; Channel.prototype.users = function() { var nick; return (function() { var results; results = []; for (nick in this._.users) { results.push(nick); } return results; }).call(this); }; Channel.prototype.ops = function() { var nick, status; return (function() { var ref, results; ref = this._.users; results = []; for (nick in ref) { status = ref[nick]; if (status === '@') { results.push(nick); } } return results; }).call(this); }; Channel.prototype.voices = function() { var nick, status; return (function() { var ref, results; ref = this._.users; results = []; for (nick in ref) { status = ref[nick]; if (status === '+') { results.push(nick); } } return results; }).call(this); }; Channel.prototype.normalUsers = function() { var nick, status; return (function() { var ref, results; ref = this._.users; results = []; for (nick in ref) { status = ref[nick]; if (status === '') { results.push(nick); } } return results; }).call(this); }; return Channel; })(); module.exports = function() { return function(client) { var oldMode; client._.channels = {}; client.channels = function() { var chan; return (function() { var results; results = []; for (chan in this._.channels) { results.push(this.getChannel(chan)); } return results; }).call(this); }; client.getChannel = function(name) { return this._.channels[name.toLowerCase()]; }; client.isInChannel = function(name, nick) { var ref; if (nick == null) { nick = this.nick(); } return !!((ref = this.getChannel(name)) != null ? ref.contains(nick) : void 0); }; client.topic = function(chan, topic) { return this.getChannel(chan).topic(topic); }; oldMode = client.mode; client.mode = function(chan, modeStr) { if (modeStr == null) { return this._.channels[chan.toLowerCase()].mode(); } return oldMode.call(client, chan, modeStr); }; client._.internalEmitter.on('raw', function(reply) { var chan, i, len, name, names; if (reply.command === getReplyCode('RPL_NOTOPIC')) { client._.channels[reply.params[1].toLowerCase()]._.topic = ''; client.emit('topic', { chan: reply.params[1], topic: '' }); } if (reply.command === getReplyCode('RPL_TOPIC')) { client._.channels[reply.params[1].toLowerCase()]._.topic = reply.params[2]; client.emit('topic', { chan: reply.params[1], topic: reply.params[2] }); } if (reply.command === getReplyCode('RPL_TOPIC_WHO_TIME')) { chan = client._.channels[reply.params[1].toLowerCase()]; chan._.topicSetter = reply.params[2]; chan._.topicTime = new Date(parseInt(reply.params[3])); client.emit('topicwho', { chan: reply.params[1], hostmask: chan._.topicSetter, time: chan._.topicTime }); } if (reply.command === getReplyCode('RPL_NAMREPLY')) { chan = client._.channels[reply.params[2].toLowerCase()]; if (chan == null) { return; } names = reply.params[3].split(' '); for (i = 0, len = names.length; i < len; i++) { name = names[i]; if (client._.reversePrefix[name[0]] != null) { chan._.users[name.slice(1)] = name[0]; } else { chan._.users[name] = ''; } } } if (reply.command === getReplyCode('RPL_ENDOFNAMES')) { chan = reply.params[1]; return client.emit('names', { chan: chan }); } }); client._.internalEmitter.on('nick', function(arg) { var chan, name, newNick, oldNick, ref, results; oldNick = arg.oldNick, newNick = arg.newNick; ref = client._.channels; results = []; for (name in ref) { chan = ref[name]; if (chan._.users[oldNick] != null) { chan._.users[newNick] = chan._.users[oldNick]; results.push(delete chan._.users[oldNick]); } else { results.push(void 0); } } return results; }); client._.internalEmitter.on('join', function(arg) { var chan, nick; chan = arg.chan, nick = arg.nick; if (nick === client._.nick) { return client._.channels[chan.toLowerCase()] = new Channel(client, chan); } else { return client._.channels[chan.toLowerCase()]._.users[nick] = ''; } }); client._.internalEmitter.on('part', function(arg) { var chan, nick, users; chan = arg.chan, nick = arg.nick; if (nick === client._.nick) { return delete client._.channels[chan.toLowerCase()]; } else { users = client._.channels[chan.toLowerCase()]._.users; return delete users[nick]; } }); client._.internalEmitter.on('kick', function(arg) { var chan, nick; chan = arg.chan, nick = arg.nick; if (nick === client._.nick) { delete client._.channels[chan.toLowerCase()]; if (client.opt.autoRejoin) { return client.raw("JOIN " + chan); } } else { return delete client._.channels[chan.toLowerCase()]._.users[nick]; } }); client._.internalEmitter.on('quit', function(e) { var chan, leftChannels, name, nick, ref; nick = e.nick; leftChannels = []; ref = client._.channels; for (name in ref) { chan = ref[name]; if (!(chan._.users[nick] != null)) { continue; } leftChannels.push(chan.name()); delete chan._.users[nick]; } return e.channels = leftChannels; }); client._.internalEmitter.on('+mode', function(arg) { var chan, channelModes, mode, param, sender; chan = arg.chan, sender = arg.sender, mode = arg.mode, param = arg.param; if (client.modeToPrefix(mode) != null) { return client._.channels[chan.toLowerCase()]._.users[param] = client.modeToPrefix(mode); } else { channelModes = client._.channels[chan.toLowerCase()]._.mode; return channelModes.push(mode); } }); return client._.internalEmitter.on('-mode', function(arg) { var chan, channelModes, index, mode, param, ref, sender; chan = arg.chan, sender = arg.sender, mode = arg.mode, param = arg.param; if (client.modeToPrefix(mode) != null) { return client._.channels[chan.toLowerCase()]._.users[param] = ''; } else { channelModes = client._.channels[chan.toLowerCase()]._.mode; index = channelModes.indexOf(mode); if (index !== -1) { return ([].splice.apply(channelModes, [index, index - index + 1].concat(ref = [])), ref); } } }); }; }; }).call(this);