diff --git a/examples/bot.js b/examples/bot.js index 7f7b6b3..2f1d7e2 100644 --- a/examples/bot.js +++ b/examples/bot.js @@ -36,7 +36,7 @@ bot.lookFor( fmt( " *@?%s *:? *(you(?:['ā€™]?re)?|u(?: r)|ur?) +([^?]+)", bot.us , function( msg, you, remark ) { // Each group captured by the pattern is passed as an argument. // More capture groups, more arguments. - const wittyReply = fmt( "%s, no %s %s", msg.prefix.nick + const wittyReply = fmt( "%s, no %s %s", msg.from.nick , you.toUpperCase(), remark ) // `Message` objects have some handy methods, like `reply`. // It is useful when you want to respond in the same context (e.g. a channel, private message). @@ -47,7 +47,7 @@ bot.lookFor( fmt( " *@?%s *:? *(you(?:['ā€™]?re)?|u(?: r)|ur?) +([^?]+)", bot.us // For the command names, you can use the provided constans, or type one yourself. bot.observe( "INVITE", function( msg ) { const chan = bot.channels.add( msg.params[1] ) - chan.say( fmt( "Thanks for inviting me, %s", msg.prefix.nick ) ) + chan.say( fmt( "Thanks for inviting me, %s", msg.from.nick ) ) }) // Patterns can be string or RegExp. Strings are case insensitive by default. @@ -58,7 +58,7 @@ bot.lookFor( /\bice +cream\b/i // Look for various commands from bot's human overlords (for now...). bot.lookFor( fmt( "@?%s[: ]+(?:quit|shutdown|die|disconnect) ?(.+)?", bot.user.nick ) , function( msg, partingWords ) { - const overlord = msg.prefix.nick + const overlord = msg.from.nick bot.quit( partingWords || fmt( "%s told me to quit, goodbye!", overlord ) ) }) @@ -66,7 +66,7 @@ bot.lookFor( fmt( "@?%s[: ]+(?:quit|shutdown|die|disconnect) ?(.+)?", bot.user.n bot.lookFor( fmt( "@?%s[: ]+(?:part|leave|gtfo)(?: +([+!#&][^ ]+))?(?: (.+))?", bot.user.nick ) , function( msg, name, txt ) { const chan = bot.channels.get( name || msg.params[0] ) - , from = msg.prefix.nick + , from = msg.from.nick if ( ! chan ) return msg.reply( fmt( "%s, Iā€™m not in %s.", from, name ) ) chan.part( txt || fmt( "%s told me to leave. Bye!", from ) ) @@ -79,7 +79,7 @@ bot.lookFor( fmt( "@?%s[: ]+(?:part|leave|gtfo)(?: +([+!#&][^ ]+))?(?: (.+))?", bot.lookFor( fmt( "@?%s[: ]+(?:join|add) +([+!#&][^ ]+)(?: +([^ ]+))?", bot.user.nick ) , function( msg, name, key ) { const chan = bot.channels.get( name ) - , from = msg.prefix.nick + , from = msg.from.nick if ( chan && chan.name === msg.params[0] ) return msg.reply( fmt( "%s, I am already here!", from ) ) else if ( chan ) diff --git a/lib/irc.js b/lib/irc.js index b21a4a6..6b4f5c3 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -21,8 +21,8 @@ const events = require( "events" ) , log = require( "./logger" ) // THE OLD SWITCHEROO HAHAHAHA , net = require( path.join.apply( path, stream ) ) + , notify = require( "./notifications" ) , objects = require( "./objects" ) - , observe = require( "./observable" ) , observers = require( "./observers" ) , parser = require( "./parser" ) @@ -30,7 +30,7 @@ const events = require( "events" ) const Channel = objects.Channel , IRCMap = map.IRCMap , Message = objects.Message - , Observable= observe.Observable + , Observable= notify.Observable , Person = objects.Person , Server = objects.Server // Factory functions @@ -47,7 +47,7 @@ const Channel = objects.Channel , LEVEL = log.LEVEL , MODE = constants.MODE , REPLY = constants.REPLY - , STATUS = observe.STATUS + , STATUS = notify.STATUS , SOCKET = constants.SOCKET // Level is (re)set later, when config is read @@ -65,8 +65,6 @@ const getConfig = function( conf ) { // Maximum message length, not counting the "\r\n" terminating sequence const MAXLEN = 510 -const queue = [] - /** IRC! * The star of the show. * @@ -180,7 +178,7 @@ const onData = function( internal, data ) { // Give superpowers message.for( this ) - this.observers.notify( message.command, message ) + this.observers.notify( message.type, message ) this.observers.notify( EVENT.ANY, message ) } } diff --git a/lib/map.js b/lib/map.js index 76a1487..15b7a64 100644 --- a/lib/map.js +++ b/lib/map.js @@ -6,7 +6,7 @@ const constants = require( "./constants" ) , parser = require( "./parser" ) , objects = require( "./objects" ) - , observe = require( "./observable" ) + , observe = require( "./notifications" ) , logger = require( "./logger" ) const Channel = objects.Channel diff --git a/lib/observable.js b/lib/notifications.js similarity index 99% rename from lib/observable.js rename to lib/notifications.js index a725389..ab66e06 100644 --- a/lib/observable.js +++ b/lib/notifications.js @@ -1,4 +1,4 @@ -/** @module observable +/** @module notifications * @todo {jonas} A real (well...) interface would be nice, for various objects to implement. */ const o = require( "./objects" ) diff --git a/lib/objects.js b/lib/objects.js index 12f04a3..bfe3a62 100644 --- a/lib/objects.js +++ b/lib/objects.js @@ -8,7 +8,7 @@ const format = require( "util" ).format , constants = require( "./constants" ) , log = require( "./logger" ) , map = require( "./map" ) - , obs = require( "./observable" ) + , obs = require( "./notifications" ) const COMMAND = constants.COMMAND , ERROR = constants.ERROR @@ -67,16 +67,18 @@ const property = function( obj, name, getter, setter ) { * @todo {jonas} When available, use rest params instead of params array * * @constructor - * @param {?Server|?Person} prefix - * @param {string} command Usually something from COMMAND, ERROR or REPLY + * @param {?Server|?Person} from + * @param {string} type Usually something from COMMAND, ERROR or REPLY * @param {Array} params - * @property {?Server|?Person} prefix - * @property {string} command + * @property {Date} date + * @property {?Server|?Person} from + * @property {string} type * @property {Array} params */ -const Message = function( prefix, command, params ) { - this.prefix = prefix - this.command = command +const Message = function( from, type, params ) { + this.date = new Date() + this.from = from + this.type = type this.params = params } @@ -86,9 +88,9 @@ const Message = function( prefix, command, params ) { Message.prototype.toString = function() { const params = this.params , parts = [] - if ( this.prefix !== null ) - parts.push( ":" + this.prefix ) - parts.push( this.command ) + if ( this.from !== null ) + parts.push( ":" + this.from ) + parts.push( this.type ) if ( params.length !== 0 ) parts.push( params.join( " " ) ) return parts.join( " " ) @@ -237,7 +239,7 @@ const send = function( irc ) { const reply = function( irc, text ) { const sender = this.params[0] , recip = sender === irc.user.nick - ? this.prefix.nick : sender + ? this.from.nick : sender irc.send( message( COMMAND.PRIVMSG , [ recip, trailing( text ) ] ) ) return this @@ -453,7 +455,7 @@ const anticipateJoin = function( irc, callback ) { } const handleJoinReply = function( callback, msg ) { - const cmd = msg.command + const cmd = msg.type , chn = ERROR.NOINVITEFORWARD === cmd ? msg.params[1] : REPLY.NAMREPLY === cmd ? msg.params[2] : msg.params[0] var error = null diff --git a/lib/observers.js b/lib/observers.js index 1b5bf4a..91ae1fe 100644 --- a/lib/observers.js +++ b/lib/observers.js @@ -6,7 +6,7 @@ const format = require( "util" ) , log = require( "./logger" ) , map = require( "./map" ) , objects = require( "./objects" ) - , observe = require( "./observable" ) + , observe = require( "./notifications" ) , parser = require( "./parser" ) const Channel = objects.Channel @@ -32,7 +32,7 @@ const onJoinCommand = function( msg ) { /** @todo {jonas} Do some clients use a trailing param for channel name? Saw some of those in the fixtures. */ const name = msg.params[0] - , nick = msg.prefix.nick + , nick = msg.from.nick , self = nick === this.user.nick var chan = null if ( self ) { @@ -42,7 +42,7 @@ const onJoinCommand = function( msg ) { logger.log( LEVEL.INFO, "[INFO] Successfully joined %s", name ) return STATUS.SUCCESS } - const prsn = person( nick, msg.prefix.user, msg.prefix.host ).for( this ) + const prsn = person( nick, msg.from.user, msg.from.host ).for( this ) logger.log( LEVEL.INFO, "[INFO] Adding %s to %s", prsn, name ) this.channels.get( name ).people.add( prsn ) return STATUS.SUCCESS @@ -90,7 +90,7 @@ const onModeCommand = function( msg ) { } const onNickCommand = function( msg ) { - if ( msg.prefix.nick === this.user.nick ) + if ( msg.from.nick === this.user.nick ) this.user.nick = msg.params[0] return STATUS.SUCCESS @@ -98,7 +98,7 @@ const onNickCommand = function( msg ) { const onPartCommand = function( msg ) { const name = msg.params[0] - , nick = msg.prefix.nick + , nick = msg.from.nick , chan = this.channels.get( name ) if ( chan && chan.people.contains( nick ) ) { @@ -153,7 +153,7 @@ const onTopicCommand = function( msg ) { const onQuitCommand = function( msg ) { // Remove from all chans - const user = msg.prefix.nick + const user = msg.from.nick var chan for ( chan in this.channels ) if ( this.channels[chan].people ) // Gross, all sorts of other stuff in this obj... @@ -203,13 +203,13 @@ const onWelcomeReply = function( msg ) { } const onAnyError = function( msg ) { - const num = msg.command + const num = msg.type if ( isNaN( num ) || num < 400 || num >= 600) return STATUS.SUCCESS this.notify( EVENT.ERROR, msg ) logger.log( LEVEL.ERROR , "[ERROR] Received error %s from %s with params %s" - , msg.command, msg.prefix, msg.params.join( ", " ) + , msg.type, msg.from, msg.params.join( ", " ) ) return STATUS.ERROR } diff --git a/spec/lib/irc.spec.js b/spec/lib/irc.spec.js index a850cc3..768891f 100644 --- a/spec/lib/irc.spec.js +++ b/spec/lib/irc.spec.js @@ -271,7 +271,7 @@ describe( "irc", function() { bit( f( "should emit all events as a `%s` event with message as first parameter", EVENT.ANY ), function( done ) { this.observe( EVENT.ANY, function( msg ) { - msg.command.should.equal( COMMAND.PRIVMSG ) + msg.type.should.equal( COMMAND.PRIVMSG ) done() }) diff --git a/spec/lib/observable.spec.js b/spec/lib/notifications.spec.js similarity index 96% rename from spec/lib/observable.spec.js rename to spec/lib/notifications.spec.js index 0c8bb15..088474e 100644 --- a/spec/lib/observable.spec.js +++ b/spec/lib/notifications.spec.js @@ -2,7 +2,7 @@ const fmt = require( "util" ).format , path = require( "path" ) , should = require( "should" ) , lib = path.join( __dirname, "..", "..", "lib" ) - , obs = require( path.join( lib, "observable" ) ) + , obs = require( path.join( lib, "notifications" ) ) , cnst = require( path.join( lib, "constants" ) ) const Observable = obs.Observable @@ -13,7 +13,7 @@ const Observable = obs.Observable , STATUS = obs.STATUS , _obs = obs._observers -describe( "observable", function() { +describe( "notifications", function() { describe( "Observable", function() { it( "should add Observers for one type", function() { const observable = new Observable( true ) diff --git a/spec/lib/objects.spec.js b/spec/lib/objects.spec.js index 798bd5d..74bdb81 100644 --- a/spec/lib/objects.spec.js +++ b/spec/lib/objects.spec.js @@ -47,26 +47,26 @@ describe( "objects", function() { it( "should support convenient signatures", function() { var m = o.message( COMMAND.LIST ) m.should.be.an.instanceof( o.Message ) - m.command.should.equal( COMMAND.LIST ) - should.equal( m.prefix, null ) + m.type.should.equal( COMMAND.LIST ) + should.equal( m.from, null ) m.params.should.eql( [] ) m = o.message( COMMAND.JOIN, [ "#jquery" ] ) m.should.be.an.instanceof( o.Message ) - m.command.should.equal( COMMAND.JOIN ) - should.equal( m.prefix, null ) + m.type.should.equal( COMMAND.JOIN ) + should.equal( m.from, null ) m.params.should.eql( [ "#jquery"] ) m = o.message( COMMAND.PRIVMSG, [ "#jquery", ": Hey" ] ) m.should.be.an.instanceof( o.Message ) - m.command.should.equal( COMMAND.PRIVMSG ) - should.equal( m.prefix, null ) + m.type.should.equal( COMMAND.PRIVMSG ) + should.equal( m.from, null ) m.params.should.eql( [ "#jquery", ": Hey" ] ) m = o.message( o.person( "lol" ), COMMAND.PRIVMSG, [ "#jquery", ": Hey" ] ) m.should.be.an.instanceof( o.Message ) - m.command.should.equal( COMMAND.PRIVMSG ) - m.prefix.should.eql( o.person( "lol" ) ) + m.type.should.equal( COMMAND.PRIVMSG ) + m.from.should.eql( o.person( "lol" ) ) m.params.should.eql( [ "#jquery", ": Hey" ] ) }) diff --git a/spec/lib/parser.spec.js b/spec/lib/parser.spec.js index 559dffb..004c067 100644 --- a/spec/lib/parser.spec.js +++ b/spec/lib/parser.spec.js @@ -19,47 +19,47 @@ describe( "parser", function() { describe( "message", function() { it( "should parse Freenode cloaks", function() { const m = parser.message( ":frigg!~eir@freenode/utility-bot/frigg PRIVMSG protobot :VERSION\r\n" ) - m.prefix.host.should.equal( "freenode/utility-bot/frigg" ) + m.from.host.should.equal( "freenode/utility-bot/frigg" ) }) it( "should parse server messages", function() { const m = parser.message( ":brown.freenode.net 333 js-irc #runlevel6 gf3 1252481170=\r\n" ) - m.prefix.name.should.equal( "brown.freenode.net" ) + m.from.name.should.equal( "brown.freenode.net" ) }) it( "should parse asterisks in server names", function() { const m = parser.message( ":*.quakenet.org MODE #altdeath +v Typone\r\n" ) - m.prefix.name.should.equal( "*.quakenet.org" ) + m.from.name.should.equal( "*.quakenet.org" ) }) it( "should parse server messages with no periods", function() { const m = parser.message( ":localhost 333 js-irc #runlevel6 gf3 1252481170=\r\n" ) - m.prefix.name.should.equal( "localhost" ) + m.from.name.should.equal( "localhost" ) }) it( "should parse nicks with backticks", function() { const m = parser.message( ":nick`!u@h JOIN :#chan\r\n" ) - m.prefix.nick.should.equal( "nick`" ) + m.from.nick.should.equal( "nick`" ) }) it( "should parse nicks with slashes", function() { const m = parser.message( ":ni\\ck!u@h JOIN :#chan\r\n" ) - m.prefix.nick.should.equal( "ni\\ck" ) + m.from.nick.should.equal( "ni\\ck" ) }) it( "should parse nicks with slashes and backticks", function() { const m = parser.message( ":davglass\\test`!~davglass@173-27-206-95.client.mchsi.com JOIN :#yui\r\n" ) - m.prefix.nick.should.equal( "davglass\\test`" ) + m.from.nick.should.equal( "davglass\\test`" ) }) it( "should parse users with slashes and carets", function() { const m = parser.message( ":peol!~andree_^\\@h55eb1e56.selukra.dyn.perspektivbredband.net JOIN :#jquery\r\n" ) - m.prefix.user.should.equal( "~andree_^\\" ) + m.from.user.should.equal( "~andree_^\\" ) }) it( "should parse users with backticks", function() { const m = parser.message( ":luke`!~luke`@117.192.231.56 QUIT :Quit: luke`\r\n" ) - m.prefix.user.should.equal( "~luke`" ) + m.from.user.should.equal( "~luke`" ) }) it( "should parse multiple middle params properly", function() { @@ -82,12 +82,12 @@ describe( "parser", function() { it( "should have a prefix property of the correct type for a server", function() { const m = parser.message( ":brown.freenode.net 333 js-irc #runlevel6 gf3 1252481170=\r\n" ) - m.prefix.should.be.an.instanceof( objects.Server ) + m.from.should.be.an.instanceof( objects.Server ) }) it( "should have a prefix property of the correct type for a person", function() { const m = parser.message( ":gf3!n=gianni@pdpc/supporter/active/gf3 PRIVMSG #runlevel6 :oh hai\r\n" ) - m.prefix.should.be.an.instanceof( objects.Person ) + m.from.should.be.an.instanceof( objects.Person ) }) // Expected to succeed