From 692163ca3090861e7405fade669a7467ab062c84 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Thu, 24 Jan 2013 03:53:23 +0000 Subject: [PATCH] IrcUser object + plumbing --- server/irc/commands.js | 59 ++++++++++---- server/irc/user.js | 181 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+), 14 deletions(-) create mode 100755 server/irc/user.js diff --git a/server/irc/commands.js b/server/irc/commands.js index 3903f32..a494302 100644 --- a/server/irc/commands.js +++ b/server/irc/commands.js @@ -117,32 +117,55 @@ var listeners = { this.client.sendIrcCommand('options', {server: this.con_num, options: this.irc_connection.options, cap: this.irc_connection.cap.enabled}); }, 'RPL_ENDOFWHOIS': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: true}); + this.irc_connection.emit('user:' + command.params[1] + ':endofwhois', { + nick: command.params[1], + msg: command.trailing + }); }, 'RPL_WHOISUSER': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], ident: command.params[2], host: command.params[3], msg: command.trailing, end: false}); + this.irc_connection.emit('user:' + command.params[1] + ':whoisuser', { + nick: command.params[1], + ident: command.params[2], + host: command.params[3], + msg: command.trailing + }); }, 'RPL_WHOISSERVER': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], irc_server: command.params[2], end: false}); + this.irc_connection.emit('user:' + command.params[1] + ':whoisserver', { + nick: command.params[1], + irc_server: command.params[2] + }); }, 'RPL_WHOISOPERATOR': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: false}); + this.irc_connection.emit('user:' + command.params[1] + ':whoisoperator', { + nick: command.params[1], + msg: command.trailing + }); }, 'RPL_WHOISCHANNELS': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], chans: command.trailing, end: false}); + this.irc_connection.emit('user:' + command.params[1] + ':whoischannels', { + nick: command.params[1], + chans: commnd.trailing + }); }, 'RPL_WHOISMODES': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: false}); + this.irc_connection.emit('user:' + command.params[1] + ':whoismodes', { + nick: command.params[1], + msg: command.trailing + }); }, 'RPL_WHOISIDLE': function (command) { - if (command.params[3]) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], idle: command.params[2], logon: command.params[3], end: false}); - } else { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], idle: command.params[2], end: false}); - } + this.irc_connection.emit('user:' + command.params[1] + ':whoisidle', { + nick: command.params[1], + idle: command.params[2], + logon: command.params[3] || undefined + }); }, 'RPL_WHOISREGNICK': function (command) { - this.client.sendIrcCommand('whois', {server: this.con_num, nick: command.params[1], msg: command.trailing, end: false}); + this.irc_connection.emit('user:' + command.params[1] + ':whoisregnick', { + nick: command.params[1], + msg: command.trailing + }); }, 'RPL_LISTSTART': function (command) { this.client.sendIrcCommand('list_start', {server: this.con_num}); @@ -309,7 +332,12 @@ var listeners = { } }, 'NICK': function (command) { - this.client.sendIrcCommand('nick', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, newnick: command.trailing || command.params[0]}); + this.irc_connection.emit('user:' + nick + ':nick', { + nick: command.nick, + ident: command.ident, + hostname: command.hostname, + newnick: command.trailing || command.params[0] + }); }, 'TOPIC': function (command) { // If we don't have an associated channel, no need to continue @@ -489,7 +517,10 @@ var listeners = { } }, 'AWAY': function (command) { - this.client.sendIrcCommand('away', {server: this.con_num, nick: command.nick, msg: command.trailing}); + this.irc_connection.emit('user:' + command.nick + ':away', { + nick: command.nick, + msg: command.trailing + }); }, 'RPL_SASLAUTHENTICATED': function (command) { this.irc_connection.write('CAP END'); diff --git a/server/irc/user.js b/server/irc/user.js new file mode 100755 index 0000000..b8d01a9 --- /dev/null +++ b/server/irc/user.js @@ -0,0 +1,181 @@ +var IrcUser = function (irc_connection, nick) { + this.irc_connection = irc_connection; + this.nick = nick; +}; + +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); + }); +}; + + +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('user:' + this.nick + ':' + event_name, irc_events[event_name].bound_fn); + } + }); +}; + +IrcUser.prototype.onNick = function (event) { + this.irc_connection.clientEvent('nick', { + nick: event.nick, + ident: event.ident, + hostname: event.hostname, + newnick: event.newnick + }); +}; + +IrcUser.prototype.onAway = function (event) { + this.irc_connection.clientEvent('away', { + nick: event.nick, + msg: event.msg + }); +}; + +IrcUser.prototype.onQuit = function (event) { + this.irc_connection.clientEvent('quit', { + nick: event.nick, + ident: event.ident, + hostname: event.hostname, + message: event.trailing + }); +}; + +IrcUser.prototype.onWhoisUser = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + ident: event.ident, + host: event.host, + msg: event.msg, + end: false + }); +}; + +IrcUser.prototype.onWhoisServer = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + irc_server: event.irc_server, + end: false + }); +}; + +IrcUser.prototype.onWhoisOperator = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + msg: event.msg, + end: false + }); +}; + +IrcUser.prototype.onWhoisChannels = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + chans: event.chans, + end: false + }); +}; + +IrcUser.prototype.onWhoisModes = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + msg: event.msg, + end: false + }); +}; + +IrcUser.prototype.onWhoisUser = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + idle: event.idle, + logon: event.logon || undefined, + end: false + }); +}; + +IrcUser.prototype.onWhoisRegNick = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + msg: event.msg, + end: false + }); +}; + +IrcUser.prototype.onWhoisEnd = function (event) { + this.irc_connection.clientEvent('whois', { + nick: event.nick, + msg: event.msg, + end: true + }); +}; + +IrcUser.prototype.onNotice = function (event) { + this.irc_connection.clientEvent('notice', { + nick: event.nick, + ident: event.ident, + hostname: event.hostname, + target: event.target, + msg: event.msg + }); +}; + +IrcUser.prototype.onCtcpResponse = function (event) { + this.irc_connection.clientEvent('ctcp_response', { + nick: event.nick, + ident: event.ident, + hostname: event.hostname, + channel: event.channel, + msg: event.msg + }); +}; + +IrcUser.prototype.onPrivmsg = function (event) { + this.irc_connection.clientEvent('privmsg', { + nick: event.nick, + ident: event.ident, + hostname: event.hostname, + channel: event.channel, + msg: event.msg + }); +}; + +IrcUser.prototype.onCtcpRequest = function (event) { + this.irc_connection.clientEvent('ctcp_request', { + nick: event.nick, + ident: event.ident, + hostname: event.hostname, + target: event.target, + type: event.type, + msg: event.msg + }); +}; -- 2.25.1