Skip to content
Browse files

V1

  • Loading branch information...
0 parents commit 07774217e75eff02c131e822ae017e605e366a28 @felixge committed Nov 23, 2009
Showing with 217 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +8 −0 config.template.js
  3. +148 −0 lib/irc.js
  4. +59 −0 log.js
  5. 0 log/.empty
2 .gitignore
@@ -0,0 +1,2 @@
+/log/*
+/config.js
8 config.template.js
@@ -0,0 +1,8 @@
+process.mixin(exports, {
+ host: 'localhost',
+ port: 6667,
+ user: 'nodelog',
+ channel: '#nodejs',
+ logUrl: 'http://nodejs.debuggable.com/',
+ logPath: 'log'
+});
148 lib/irc.js
@@ -0,0 +1,148 @@
+var sys = require('sys');
+var tcp = require('tcp');
+var irc = exports;
+
+function bind(fn, scope) {
+ var bindArgs = Array.prototype.slice.call(arguments);
+ bindArgs.shift();
+ bindArgs.shift();
+
+ return function() {
+ var args = Array.prototype.slice.call(arguments);
+ fn.apply(scope, bindArgs.concat(args));
+ };
+}
+
+function each(set, iterator) {
+ for (var i = 0; i < set.length; i++) {
+ var r = iterator(set[i], i);
+ if (r === false) {
+ return;
+ }
+ }
+}
+
+var Client = irc.Client = function(host, port) {
+ this.host = host || 'localhost';
+ this.port = port || 6667;
+
+ this.connection = null;
+ this.buffer = '';
+ this.encoding = 'utf8';
+ this.timeout = 10 * 60 * 60 * 1000;
+
+ this.nick = null;
+ this.user = null;
+ this.real = null;
+}
+sys.inherits(Client, process.EventEmitter);
+
+Client.prototype.connect = function(nick, user, real) {
+ var connection = tcp.createConnection(this.port, this.host);
+ connection.setEncoding(this.encoding);
+ connection.setTimeout(this.timeout);
+ connection.addListener('connect', bind(this.onConnect, this));
+ connection.addListener('receive', bind(this.onReceive, this));
+ connection.addListener('eof', bind(this.onEof, this));
+ connection.addListener('timeout', bind(this.onTimeout, this));
+ connection.addListener('close', bind(this.onClose, this));
+
+ this.nick = nick;
+ this.user = user || 'guest';
+ this.real = real || 'Guest';
+
+ this.connection = connection;
+};
+
+Client.prototype.disconnect = function(why) {
+ if (this.connection.readyState !== 'closed') {
+ this.connection.close();
+ sys.puts('disconnected (reason: '+why+')');
+ }
+};
+
+Client.prototype.send = function(arg1) {
+ if (this.connection.readyState !== 'open') {
+ return this.disconnect('cannot send with readyState: '+this.connection.readyState);
+ }
+
+ var message = [];
+ for (var i = 0; i< arguments.length; i++) {
+ if (arguments[i]) {
+ message.push(arguments[i]);
+ }
+ }
+ message = message.join(' ');
+
+ sys.puts('> '+message);
+ message = message + "\r\n";
+ this.connection.send(message, this.encoding);
+};
+
+Client.prototype.parse = function(message) {
+ var match = message.match(/(?:(:[^\s]+) )?([^\s]+) (.+)/);
+ var parsed = {
+ prefix: match[1],
+ command: match[2]
+ };
+
+ var params = match[3].split(/ ?:/);
+ params = (params[0])
+ ? params[0].split(' ').concat([params[1]])
+ : params[1];
+ parsed.params = params;
+
+ return parsed;
+};
+
+Client.prototype.onConnect = function() {
+ this.send('NICK', this.nick);
+ this.send('USER', this.user, '0', '*', ':'+this.real);
+};
+
+Client.prototype.onReceive = function(chunk) {
+ this.buffer = this.buffer + chunk;
+
+ while (this.buffer) {
+ var offset = this.buffer.indexOf("\r\n");
+ if (offset < 0) {
+ return;
+ }
+
+ var message = this.buffer.substr(0, offset);
+ this.buffer = this.buffer.substr(offset + 2);
+ sys.puts('< '+message);
+
+ message = this.parse(message);
+
+ this.emit.apply(this, [message.command, message.prefix].concat(message.params));
+
+ if (message !== false) {
+ this.onMessage(message);
+ }
+ }
+};
+
+Client.prototype.onMessage = function(message) {
+ switch (message.command) {
+ case 'PING':
+ this.send('PONG', ':'+message.params[0]);
+ break;
+ }
+};
+
+Client.prototype.onEof = function() {
+ this.disconnect('eof');
+};
+
+Client.prototype.onTimeout = function() {
+ this.disconnect('timeout');
+};
+
+Client.prototype.onClose = function() {
+ this.disconnect('close');
+};
+
+exports.user = function(prefix) {
+ return prefix.match(/:([^!]+)!/)[1]
+};
59 log.js
@@ -0,0 +1,59 @@
+process.mixin(require('sys'));
+var config = require('./config');
+
+var irc = require('./lib/irc');
+var file = require('file');
+var path = require('path');
+var repl = require('repl');
+
+var logFile, day;
+function writeLog(text) {
+ var date = new Date;
+ var today = [
+ date.getFullYear(),
+ ('0'+(date.getMonth()+1)).substr(-2),
+ ('0'+date.getDate()).substr(-2),
+ ].join('-');
+
+ if (!logFile || day !== today) {
+ logFile = new file.File(path.join(config.logPath, today+'.txt'), 'a+', {encoding: 'utf8'});
+ day = today;
+ }
+
+ var time = [
+ ('0'+date.getHours()).substr(-2),
+ ('0'+date.getMinutes()).substr(-2)
+ ].join(':');
+ logFile.write('['+time+'] '+text+"\n");
+}
+
+var client = new irc.Client(config.host, config.port);
+client.connect(config.user);
+
+client.addListener('001', function() {
+ this.send('JOIN', config.channel);
+});
+
+client.addListener('JOIN', function(prefix) {
+ var user = irc.user(prefix);
+ writeLog(user+' has joined the channel');
+});
+
+client.addListener('PART', function(prefix) {
+ var user = irc.user(prefix);
+ writeLog(user+' has left the channel');
+});
+
+client.addListener('PRIVMSG', function(prefix, channel, text) {
+ switch (text) {
+ case '!logs':
+ case '!log':
+ this.send('PRIVMSG', channel, ':Logs are here: '+config.logUrl);
+ break;
+ }
+
+ var user = irc.user(prefix);
+ writeLog(user+': '+text);
+});
+
+repl.start("logbot> ");
0 log/.empty
No changes.

0 comments on commit 0777421

Please sign in to comment.
Something went wrong with that request. Please try again.