Skip to content

Commit

Permalink
add: message source is now Channel, Channel can list clients
Browse files Browse the repository at this point in the history
  • Loading branch information
gf3 committed Jan 9, 2012
1 parent 5d9c677 commit 054f083
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
60 changes: 59 additions & 1 deletion lib/jerk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@ var path = require( 'path' )
, util = require('util')
, IRC
, Jerk
, globalCache = {}

/* ------------------------------ Init ------------------------------ */
require( './strftime' )
IRC = require( 'irc-js' )

/* ------------------------------ Channel ------------------------------ */
function Channel ( chan, clients ) {
this.toString = function toString () {
return msg.source
}

Object.defineProperty( this,
"clients",
{ enumerable: true
, configurable: false
, get: function(){
return Object.keys( clients )
}
})
}

/* ------------------------------ Jerk ------------------------------ */
Jerk = new ( function Jerk() {
var bot
, watchers = []
, join_watchers = []
, leave_watchers = []
, clients = {}
, connect = _connect.bind( this )
, watch_for = _watch_for.bind( this )
, user_join = _user_join.bind( this )
Expand All @@ -33,6 +51,7 @@ Jerk = new ( function Jerk() {
.on( 'join', _user_joined.bind( this ) )
.on( 'part', _user_leaving.bind( this ) )
.on( 'quit', _user_leaving.bind( this ) )
.on( '353', _channel_clients.bind( this ) )
.on( 'error', function( message ) {
console.log( 'There was an error! "' + message.params[0] + '"' )
this.disconnect().connect( _on_connect.bind( this ) )
Expand Down Expand Up @@ -93,6 +112,9 @@ Jerk = new ( function Jerk() {
if ( message.person.nick == bot.options.nick )
return

// Add user to client cache
clients[ message.params[0] ][ _normalize_nick( message.person.nick ) ] = true

var i = join_watchers.length

while ( i-- )
Expand All @@ -103,11 +125,35 @@ Jerk = new ( function Jerk() {
if ( message.person.nick == bot.options.nick )
return

// Remove user from clients cache
var nick = _normalize_nick( message.person.nick )
if ( message.command == 'part' )
delete clients[ message.params[0] ][ nick ]
else
Object.keys( clients ).forEach( function( chan ) {
var client
for ( client in clients[ chan ] )
if ( client == nick )
delete clients[ chan ][ nick ]
})

var i = leave_watchers.length

while ( i-- )
leave_watchers[i]( _make_message( message ) )
}

function _channel_clients ( message ) {
if ( message.params[1] == '=' ) {
if ( ! clients[ message.params[2] ] )
clients[ message.params[2] ] = {}

message.params[3].split(" ").forEach( function( client ) {
client = _normalize_nick( client )
clients[ message.params[2] ][ client ] = true
})
}
}

function _bot_do( what ) {
if ( typeof what === 'string' )
Expand All @@ -132,7 +178,12 @@ Jerk = new ( function Jerk() {
}

function _make_message ( message, md ) {
var source = message.params[0] == bot.options.nick ? message.person.nick : message.params[0]
var source
if ( message.params[0] == bot.options.nick )
source = message.person.nick
else
source = new Channel( message.params[0], clients[ message.params[0] ] )

return true,
{ say: _privmsg_protected.bind( this, source )
, msg: _privmsg_protected.bind( this, message.person.nick )
Expand All @@ -144,6 +195,13 @@ Jerk = new ( function Jerk() {
}
}

function _normalize_nick ( nick ) {
if ( nick[0] == '@' )
return nick.slice(1)
else
return nick
}

})()

/* ------------------------------ Package Info ------------------------------ */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
, "Tomás Senart <tsenart@me.com> http://about.me/tsenart"
]
, "homepage" : "http://github.com/gf3/Jerk"
, "version" : "1.1.17"
, "version" : "1.1.18"
, "main" : "./lib/jerk"
, "dependencies" :
{ "irc-js": "=0.2.27"
Expand Down

0 comments on commit 054f083

Please sign in to comment.