From d10c74a64105e214b50ed834c314f29f9d397b52 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Sat, 28 Sep 2013 17:53:40 +0100 Subject: [PATCH] Server-side support for server-time CAP --- server/irc/channel.js | 30 ++++++--- server/irc/commands.js | 146 +++++++++++++++++++++++++++++++++++------ server/irc/user.js | 24 ++++--- 3 files changed, 163 insertions(+), 37 deletions(-) diff --git a/server/irc/channel.js b/server/irc/channel.js index fcdeec2..1a8c6d9 100644 --- a/server/irc/channel.js +++ b/server/irc/channel.js @@ -46,7 +46,8 @@ function onJoin(event) { channel: this.name, nick: event.nick, ident: event.ident, - hostname: event.hostname + hostname: event.hostname, + time: event.time }); // If we've just joined this channel then request get a nick list @@ -62,7 +63,8 @@ function onPart(event) { ident: event.ident, hostname: event.hostname, channel: this.name, - message: event.message + message: event.message, + time: event.time }); } @@ -74,7 +76,8 @@ function onKick(event) { ident: event.ident, hostname: event.hostname, channel: this.name, - message: event.message + message: event.message, + time: event.time }); } @@ -84,7 +87,8 @@ function onQuit(event) { nick: event.nick, ident: event.ident, hostname: event.hostname, - message: event.message + message: event.message, + time: event.time }); } @@ -95,7 +99,8 @@ function onMsg(event) { ident: event.ident, hostname: event.hostname, channel: this.name, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -107,7 +112,8 @@ function onNotice(event) { ident: event.ident, hostname: event.hostname, target: event.target, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -119,7 +125,8 @@ function onCtcpRequest(event) { hostname: event.hostname, target: event.target, type: event.type, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -131,7 +138,8 @@ function onCtcpResponse(event) { hostname: event.hostname, target: event.target, type: event.type, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -172,7 +180,8 @@ function onTopic(event) { this.irc_connection.clientEvent('topic', { nick: event.nick, channel: this.name, - topic: event.topic + topic: event.topic, + time: event.time }); } @@ -208,6 +217,7 @@ function onMode(event) { this.irc_connection.clientEvent('mode', { target: event.target, nick: event.nick, - modes: event.modes + modes: event.modes, + time: event.time }); } diff --git a/server/irc/commands.js b/server/irc/commands.js index e37d454..7ac3c69 100644 --- a/server/irc/commands.js +++ b/server/irc/commands.js @@ -340,53 +340,100 @@ handlers = { }, 'JOIN': function (command) { - var channel; + var channel, time; if (typeof command.trailing === 'string' && command.trailing !== '') { channel = command.trailing; } else if (typeof command.params[0] === 'string' && command.params[0] !== '') { channel = command.params[0]; } + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + this.irc_connection.emit('channel ' + channel + ' join', { nick: command.nick, ident: command.ident, hostname: command.hostname, - channel: channel + channel: channel, + time: time }); }, 'PART': function (command) { + var time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + this.irc_connection.emit('channel ' + command.params[0] + ' part', { nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], - message: command.trailing + message: command.trailing, + time: time }); }, 'KICK': function (command) { + var time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + this.irc_connection.emit('channel ' + command.params[0] + ' kick', { kicked: command.params[1], nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], - message: command.trailing + message: command.trailing, + time: time }); }, 'QUIT': function (command) { + var time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + this.irc_connection.emit('user ' + command.nick + ' quit', { nick: command.nick, ident: command.ident, hostname: command.hostname, - message: command.trailing + message: command.trailing, + time: time }); }, 'NOTICE': function (command) { - var namespace; + var namespace, + time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + if ((command.trailing.charAt(0) === String.fromCharCode(1)) && (command.trailing.charAt(command.trailing.length - 1) === String.fromCharCode(1))) { // It's a CTCP response @@ -396,7 +443,8 @@ handlers = { ident: command.ident, hostname: command.hostname, channel: command.params[0], - msg: command.trailing.substring(1, command.trailing.length - 1) + msg: command.trailing.substring(1, command.trailing.length - 1), + time: time }); } else { namespace = (command.params[0].toLowerCase() == this.irc_connection.nick.toLowerCase() || command.params[0] == '*') ? @@ -409,31 +457,52 @@ handlers = { ident: command.ident, hostname: command.hostname, target: command.params[0], - msg: command.trailing + msg: command.trailing, + time: time }); } }, 'NICK': function (command) { + var time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + this.irc_connection.emit('user ' + command.nick + ' nick', { nick: command.nick, ident: command.ident, hostname: command.hostname, - newnick: command.trailing || command.params[0] + newnick: command.trailing || command.params[0], + time: time }); }, 'TOPIC': function (command) { + var time; + // If we don't have an associated channel, no need to continue if (!command.params[0]) return; + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + var channel = command.params[0], topic = command.trailing || ''; this.irc_connection.emit('channel ' + channel + ' topic', { nick: command.nick, channel: channel, - topic: topic + topic: topic, + time: time }); }, @@ -442,7 +511,14 @@ handlers = { prefixes = this.irc_connection.options.PREFIX || [], always_param = (chanmodes[0] || '').concat((chanmodes[1] || '')), modes = [], - has_param, i, j, add, event; + has_param, i, j, add, event, time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } prefixes = _.reduce(prefixes, function (list, prefix) { list.push(prefix.mode); @@ -492,20 +568,40 @@ handlers = { this.irc_connection.emit(event, { target: command.params[0], nick: command.nick || command.prefix || '', - modes: modes + modes: modes, + time: time }); }, 'PRIVMSG': function (command) { - var tmp, namespace; + var tmp, namespace, time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + if ((command.trailing.charAt(0) === String.fromCharCode(1)) && (command.trailing.charAt(command.trailing.length - 1) === String.fromCharCode(1))) { //CTCP request if (command.trailing.substr(1, 6) === 'ACTION') { - this.irc_connection.clientEvent('action', {nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substring(8, command.trailing.length - 1)}); + this.irc_connection.clientEvent('action', { + nick: command.nick, + ident: command.ident, + hostname: command.hostname, + channel: command.params[0], + msg: command.trailing.substring(8, command.trailing.length - 1), + time: time + }); } else if (command.trailing.substr(1, 4) === 'KIWI') { tmp = command.trailing.substring(6, command.trailing.length - 1); namespace = tmp.split(' ', 1)[0]; - this.irc_connection.clientEvent('kiwi', {namespace: namespace, data: tmp.substr(namespace.length + 1)}); + this.irc_connection.clientEvent('kiwi', { + namespace: namespace, + data: tmp.substr(namespace.length + 1), + time: time + }); } else if (command.trailing.substr(1, 7) === 'VERSION') { this.irc_connection.write('NOTICE ' + command.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1)); } else if (command.trailing.substr(1, 6) === 'SOURCE') { @@ -520,7 +616,8 @@ handlers = { hostname: command.hostname, target: command.params[0], type: (command.trailing.substring(1, command.trailing.length - 1).split(' ') || [null])[0], - msg: command.trailing.substring(1, command.trailing.length - 1) + msg: command.trailing.substring(1, command.trailing.length - 1), + time: time }); } } else { @@ -531,7 +628,8 @@ handlers = { ident: command.ident, hostname: command.hostname, channel: command.params[0], - msg: command.trailing + msg: command.trailing, + time: time }); } }, @@ -543,7 +641,7 @@ handlers = { var request; // Which capabilities we want to enable - var want = ['multi-prefix', 'away-notify']; + var want = ['multi-prefix', 'away-notify', 'server-time']; if (this.irc_connection.password) { want.push('sasl'); @@ -613,9 +711,19 @@ handlers = { }, 'AWAY': function (command) { + var time; + + if (_.contains(this.irc_connection.cap.enabled, 'server-time') && command.tags && command.tags.length > 0) { + time = _.find(command.tags, function (tag) { + return tag.tag === 'time'; + }); + time = time ? time.value : undefined; + } + this.irc_connection.emit('user ' + command.nick + ' away', { nick: command.nick, - msg: command.trailing + msg: command.trailing, + time: time }); }, diff --git a/server/irc/user.js b/server/irc/user.js index 387b7f1..1417d95 100755 --- a/server/irc/user.js +++ b/server/irc/user.js @@ -45,7 +45,8 @@ function onNick(event) { nick: event.nick, ident: event.ident, hostname: event.hostname, - newnick: event.newnick + newnick: event.newnick, + time: event.time }); // TODO: uncomment when using an IrcUser per nick @@ -57,7 +58,8 @@ function onNick(event) { function onAway(event) { this.irc_connection.clientEvent('away', { nick: event.nick, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -66,7 +68,8 @@ function onQuit(event) { nick: event.nick, ident: event.ident, hostname: event.hostname, - message: event.trailing + message: event.trailing, + time: event.time }); } @@ -177,7 +180,8 @@ function onNotice(event) { ident: event.ident, hostname: event.hostname, target: event.target, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -187,7 +191,8 @@ function onCtcpResponse(event) { ident: event.ident, hostname: event.hostname, channel: event.channel, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -197,7 +202,8 @@ function onPrivmsg(event) { ident: event.ident, hostname: event.hostname, channel: event.channel, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -208,7 +214,8 @@ function onCtcpRequest(event) { hostname: event.hostname, target: event.target, type: event.type, - msg: event.msg + msg: event.msg, + time: event.time }); } @@ -216,6 +223,7 @@ function onMode(event) { this.irc_connection.clientEvent('mode', { target: event.target, nick: event.nick, - modes: event.modes + modes: event.modes, + time: event.time }); } -- 2.25.1