Skip to content

Commit

Permalink
Rename some stuff to be intuitive rather than consistent with RFC terms
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Westerlund committed Apr 2, 2012
1 parent 679fa32 commit 4261d9f
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 56 deletions.
10 changes: 5 additions & 5 deletions examples/bot.js
Expand Up @@ -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).
Expand All @@ -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.
Expand All @@ -58,15 +58,15 @@ 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 ) )
})

// Leave a channel if instructed.
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 ) )
Expand All @@ -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 )
Expand Down
10 changes: 4 additions & 6 deletions lib/irc.js
Expand Up @@ -21,16 +21,16 @@ 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" )

// Constructors
const Channel = objects.Channel
, IRCMap = map.IRCMap
, Message = objects.Message
, Observable= observe.Observable
, Observable= notify.Observable
, Person = objects.Person
, Server = objects.Server
// Factory functions
Expand All @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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 )
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/map.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/observable.js → 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" )
Expand Down
28 changes: 15 additions & 13 deletions lib/objects.js
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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( " " )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/observers.js
Expand Up @@ -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
Expand All @@ -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 ) {
Expand All @@ -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
Expand Down Expand Up @@ -90,15 +90,15 @@ 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
}

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 ) ) {
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/irc.spec.js
Expand Up @@ -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()
})

Expand Down
Expand Up @@ -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
Expand All @@ -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 )
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/objects.spec.js
Expand Up @@ -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" ] )
})

Expand Down
22 changes: 11 additions & 11 deletions spec/lib/parser.spec.js
Expand Up @@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit 4261d9f

Please sign in to comment.