From: Jack Allnutt Date: Fri, 25 Jan 2013 01:20:43 +0000 (+0000) Subject: Move the binding into new prototype object X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=2a8d2d5f689228e3313d885e764bcf630013d51c;p=KiwiIRC.git Move the binding into new prototype object --- diff --git a/server/irc/binder.js b/server/irc/binder.js new file mode 100755 index 0000000..a4fab42 --- /dev/null +++ b/server/irc/binder.js @@ -0,0 +1,24 @@ +var Binder = function () {}; + +module.exports = Binder; + +Binder.prototype.bindEvents = function () { + var that = this; + this.irc_events.forEach(function (fn, event_name, irc_events) { + // Bind the event to `that` context, storing it with the event listing + if (!irc_events[event_name].bound_fn) { + irc_events[event_name].bound_fn = fn.bind(that); + } + + this.irc_connection.on(this.scope + ':' + event_name, irc_events[event_name].bound_fn); + }); +}; + + +Binder.prototype.unbindEvents = function () { + this.irc_events.forEach(function(fn, event_name, irc_events) { + if (irc_events[event_name].bound_fn) { + this.irc_connection.removeListener(this.scope + ':' + event_name, irc_events[event_name].bound_fn); + } + }); +}; \ No newline at end of file diff --git a/server/irc/channel.js b/server/irc/channel.js index 1825969..3a8993f 100644 --- a/server/irc/channel.js +++ b/server/irc/channel.js @@ -1,12 +1,21 @@ +var util = require('util'), + Binder = require('./binder.js'); function IrcChannel(irc_connection, name) { this.irc_connection = irc_connection; this.name = name; + this.scope = 'channel:' + name; + Binder.call(this); + this.members = []; this.ban_list_buffer = []; } +util.inherits(IrcChannel, Binder); + +module.exports = IrcChannel; + IrcChannel.prototype.dispose = function (){ this.unbindEvents(); @@ -14,52 +23,24 @@ IrcChannel.prototype.dispose = function (){ }; -IrcChannel.prototype.bindEvents = function() { - var that = this; - - // If we havent generated an event listing yet, do so now - if (!this.irc_events) { - this.irc_events = { - join: onJoin, - part: onPart, - kick: onKick, - quit: onQuit, - privmsg: onMsg, - notice: onNotice, - ctcp_request: onCtcpRequest, - ctcp_response: onCtcpResponse, - topic: onTopic, - nicklist: onNicklist, - nicklistEnd: onNicklistEnd, - banlist: onBanList, - banlist_end: onBanListEnd, - topicsetby: onTopicSetby - }; - } - - this.irc_events.forEach(function(fn, event_name, irc_events){ - // Bind the event to `that` context, storing it with the event listing - if (!irc_events[event_name].bound_fn) { - irc_events[event_name].bound_fn = fn.bind(that); - } - - this.irc_connection.on(event_name, irc_events[event_name].bound_fn); - }); +IrcChannel.prototype.irc_events = { + join: onJoin, + part: onPart, + kick: onKick, + quit: onQuit, + privmsg: onMsg, + notice: onNotice, + ctcp_request: onCtcpRequest, + ctcp_response: onCtcpResponse, + topic: onTopic, + nicklist: onNicklist, + nicklistEnd: onNicklistEnd, + banlist: onBanList, + banlist_end: onBanListEnd, + topicsetby: onTopicSetby }; -IrcChannel.prototype.unbindEvents = function() { - this.irc_events.forEach(function(fn, event_name, irc_events){ - if (irc_events[event_name].bound_fn) { - this.irc_connection.removeListener(event_name, irc_events[event_name].bound_fn); - } - }); -}; - - - - - function onJoin(event) { this.irc_connection.sendIrcCommand('join', { channel: this.name, diff --git a/server/irc/server.js b/server/irc/server.js index ee469c0..b9c40cc 100755 --- a/server/irc/server.js +++ b/server/irc/server.js @@ -1,61 +1,45 @@ +var util = require('util'), + Binder = require('./binder.js'); + var IrcServer = function (irc_connection, host, port) { this.irc_connection = irc_connection; this.host = host; this.port = port; + this.scope = 'server:' + host; + + Binder.call(this); + this.list_buffer = []; this.motd_buffer = ''; }; +util.inherits(IrcServer, Binder); + module.exports = IrcServer; -IrcServer.prototype.bindEvents = function () { - var that = this; - - // If we havent generated an event listing yet, do so now - if (!this.irc_events) { - this.irc_events = { - connect: onConnect, - options: onOptions, - list_start: onListStart, - list_channel: onListChannel, - list_end: onListEnd, - motd_start: onMotdStart, - motd: onMotd, - motd_end: onMotdEnd, - error: onError, - channel_redirect: onChannelRedirect, - no_such_nick: onNoSuchNick, - cannot_send_to_channel: onChannotSendToChan, - too_many_channels: onTooManyChannels, - user_not_in_channel: onUserNotInChannel, - not_on_channel: onNotOnChannel, - channel_is_full: onChannelisFull, - invite_only_channel: onInviteOnlyChannel, - banned_from_channel: onBannedFromChannel, - bad_channel_key: onBadChannelKey, - chanop_privs_needed: onChanopPrivsNeeded, - nickname_in_use: onNicknameInUse - }; - } - - this.irc_events.forEach(function (fn, event_name, irc_events) { - // Bind the event to `that` context, storing it with the event listing - if (!irc_events[event_name].bound_fn) { - irc_events[event_name].bound_fn = fn.bind(that); - } - - this.irc_connection.on('server:' + this.host + ':' + event_name, irc_events[event_name].bound_fn); - }); -}; - - -IrcServer.prototype.unbindEvents = function () { - this.irc_events.forEach(function(fn, event_name, irc_events) { - if (irc_events[event_name].bound_fn) { - this.irc_connection.removeListener('server:' + this.host + ':' + event_name, irc_events[event_name].bound_fn); - } - }); +IrcServer.prototype.irc_events = { + connect: onConnect, + options: onOptions, + list_start: onListStart, + list_channel: onListChannel, + list_end: onListEnd, + motd_start: onMotdStart, + motd: onMotd, + motd_end: onMotdEnd, + error: onError, + channel_redirect: onChannelRedirect, + no_such_nick: onNoSuchNick, + cannot_send_to_channel: onChannotSendToChan, + too_many_channels: onTooManyChannels, + user_not_in_channel: onUserNotInChannel, + not_on_channel: onNotOnChannel, + channel_is_full: onChannelisFull, + invite_only_channel: onInviteOnlyChannel, + banned_from_channel: onBannedFromChannel, + bad_channel_key: onBadChannelKey, + chanop_privs_needed: onChanopPrivsNeeded, + nickname_in_use: onNicknameInUse }; function onConnect(event) { diff --git a/server/irc/user.js b/server/irc/user.js index c4fa2b7..f3ba146 100755 --- a/server/irc/user.js +++ b/server/irc/user.js @@ -1,50 +1,33 @@ +var util = require('util'), + Binder = require('./binder.js'); + var IrcUser = function (irc_connection, nick) { this.irc_connection = irc_connection; this.nick = nick; + + this.scope = 'user:' + nick; + Binder.call(this); }; -module.exports = IrcUser; - -IrcUser.prototype.bindEvents = function () { - var that = this; - - // If we havent generated an event listing yet, do so now - if (!this.irc_events) { - this.irc_events = { - nick: onNick, - away: onAway, - quit: onKick, - whoisuser: onWhoisUser, - whoisoperator: onWhoisOperator, - whoischannels: onWhoisChannels, - whoismodes: onWhoisModes, - whoisidle: onWhoisIdle, - whoisregnick: onRegNick, - endofwhois: onEhoisEnd, - notice: onNotice, - ctcp_response: onCtcpResponse, - privmsg: onPrivmsg, - ctcp_request: onCtcpRequest - }; - } - - this.irc_events.forEach(function (fn, event_name, irc_events) { - // Bind the event to `that` context, storing it with the event listing - if (!irc_events[event_name].bound_fn) { - irc_events[event_name].bound_fn = fn.bind(that); - } - - this.irc_connection.on('user:' + this.nick + ':' + event_name, irc_events[event_name].bound_fn); - }); -}; +util.inherits(IrcUser, Binder); +module.exports = IrcUser; -IrcUser.prototype.unbindEvents = function () { - this.irc_events.forEach(function(fn, event_name, irc_events) { - if (irc_events[event_name].bound_fn) { - this.irc_connection.removeListener('user:' + this.nick + ':' + event_name, irc_events[event_name].bound_fn); - } - }); +IrcUser.prototype.irc_events = { + nick: onNick, + away: onAway, + quit: onKick, + whoisuser: onWhoisUser, + whoisoperator: onWhoisOperator, + whoischannels: onWhoisChannels, + whoismodes: onWhoisModes, + whoisidle: onWhoisIdle, + whoisregnick: onRegNick, + endofwhois: onEhoisEnd, + notice: onNotice, + ctcp_response: onCtcpResponse, + privmsg: onPrivmsg, + ctcp_request: onCtcpRequest }; function onNick(event) {