Permalink
Browse files

add: message source is now Channel, Channel can list clients

  • Loading branch information...
1 parent 5d9c677 commit 054f0838889482e92f724f9f2523eceb87d5eb9e @gf3 committed Jan 9, 2012
Showing with 60 additions and 2 deletions.
  1. +59 −1 lib/jerk.js
  2. +1 −1 package.json
View
@@ -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 )
@@ -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 ) )
@@ -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-- )
@@ -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' )
@@ -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 )
@@ -144,6 +195,13 @@ Jerk = new ( function Jerk() {
}
}
+ function _normalize_nick ( nick ) {
+ if ( nick[0] == '@' )
+ return nick.slice(1)
+ else
+ return nick
+ }
+
})()
/* ------------------------------ Package Info ------------------------------ */
View
@@ -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"

0 comments on commit 054f083

Please sign in to comment.