Skip to content

Commit

Permalink
Introduce logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
guipn committed Jun 10, 2012
1 parent 4f0d34b commit 149cb4d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 7 deletions.
6 changes: 6 additions & 0 deletions commands.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var cmd = exports,
fs = require('fs'),
irc = require('./irc.js'),
log = require('./log.js'),
authd = {}; // People authentified

cmd.prefix = '.';
Expand All @@ -18,6 +19,8 @@ function tokenize(msg) {

cmd.load = function (dispatcherName, context) {

log.debug('Loading dispatcher ' + dispatcherName + '.');

var dispatcherFile = context.options.dispatcherDir +
'/' + dispatcherName + '.js',
absolutePath = require.resolve(dispatcherFile);
Expand All @@ -29,6 +32,7 @@ cmd.load = function (dispatcherName, context) {

cmd.unload = function (dispatcherName) {

log.debug('Unloading dispatcher ' + dispatcherName + '.');
return delete cmd[dispatcherName];

};
Expand All @@ -55,6 +59,8 @@ cmd.dispatch = function (context) {
commandFunc = cmd[dispatcher][name];

if (typeof commandFunc === 'undefined') {
log.debug('Command "' + name +
'" not found on dispatcher ' + dispatcherName);
return;
}

Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
"ignore": ["punk", "phunk"],
"gBooksAPIKey": "gbk",
"dispatcherDir": "./dispatchers"
"logDir": "./logs"
}
14 changes: 13 additions & 1 deletion dispatchers/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var pub = exports,
irc = require('../irc.js'),
rfc = require('./modules/ietf.js'),
gbk = require('./modules/gbooks.js'),
log = require('./log.js'),
interp = require('../util.js').interp;


Expand All @@ -10,13 +11,24 @@ function reply(context, message) {
irc.outbound.say(context.channel,
context.sender + ': ' + message)
);

log.publicmsg({
channel: context.channel,
message: message,
sender: context.sender
});
}


function replyQuery(context, message) {
context.network.send(
irc.outbound.say(context.sender, message)
);

log.query({
message: message,
sender: context.sender
});
}


Expand Down Expand Up @@ -72,7 +84,7 @@ pub.quit = function (tokens, context) {

var quitmsg = tokens[1] || "";

console.log('-- Quitting by order of ' + context.sender);
log.debug('Quitting by order of ' + context.sender);
context.network.send(irc.outbound.quit(quitmsg));
process.exit();
};
Expand Down
10 changes: 9 additions & 1 deletion dispatchers/query.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
var qry = exports,
irc = require('../irc.js'),
rfc = require('./modules/ietf.js'),
log = require('./log.js'),
interp = require('../util.js').interp;


function reply(context, message) {

context.network.send(
irc.outbound.say(context.sender, message)
);

log.query({
sender: context.sender,
message: message
});
}



qry.load = function (tokens, context) {

try {
context.cmd.load(tokens[1], context);
reply(context,
Expand Down Expand Up @@ -107,7 +115,7 @@ qry.quit = function (tokens, context) {

var quitmsg = tokens[1] || "";

console.log('-- Quitting by order of ' + context.sender);
log.debug('Quitting by order of ' + context.sender);
context.network.send(irc.outbound.quit(quitmsg));
process.exit();
};
Expand Down
104 changes: 104 additions & 0 deletions log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var fs = require('fs'),
log = exports,
dir = './logs';


log.setDir = function (directory) {
dir = directory;
};


log.publicmsg = function (params) {

var now = getTime();
path = dir + '/' + params.channel + '.log',
line = now + '| <' + params.sender + '> ' +
params.message + '\n';


append(path, line);
};


log.query = function (params) {

var now = getTime();
path = dir + '/' + params.sender + '.log',
line = now + '| <' + params.sender + '> ' + params.message;

append(path, line);
};


log.debug = function (params) {

var now = getTime();
path = './debug.log',
line = now + '| ' + params.message;

console.log('-- ' + params.message);
append(path, line);

}


log.joined = function (params) {

var now = getTime();
path = dir + '/' + params.channel + '.log',
line = now + '| ' + params.who + ' joined channel ' +
params.channel + '.';

append(path, line);
}


log.parted = function (params) {

var now = getTime();
path = dir + '/' + params.channel + '.log',
line = now + '| ' + params.who + ' parted channel ' +
params.channel + '.';

append(path, line);
}


log.renamed = function (params) {

var now = getTime();
path = dir + '/' + params.channel + '.log',
line = now + '| ' + params.old + ' changed nick to ' +
params.new_ + '.';

append(path, line);
}


function getTime() {

return new Date().toString().match(/[^G]+/);

}

function append(path, line) {

console.log(line);

/*
fs.open(path, 'a', 438, function (err, fd) {
var buffer;
if (err) {
console.log('Error trying to log public message.');
return;
}
buffer = new Buffer('' + line, 'utf8');
// writeAll(fd, buffer, 0, buffer.length);
// need ' write ' - check node documentation when on the Internet.
});
*/
}
9 changes: 7 additions & 2 deletions mutalirc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var nwk = require('./network.js'),
cfg = require('./config.js'),
cmd = require('./commands.js'),
irc = require('./irc.js'),
log = require('./log.js'),
opt,
ignored = {};

Expand All @@ -20,6 +21,8 @@ function init(options) {
ignored[nickOrHostmask] = true;
});

log.setDir(opt.logDir);

nwk.connect(opt, react);
}

Expand All @@ -33,10 +36,12 @@ function react(data) {

case 'ping':
nwk.send(resp.pong(parsed.content));
log.debug('PONG: ' + parsed.content);
break;

case 'version':
nwk.send(resp.version(parsed.requester, opt.version));
log.debug('Providing version information to: ' + parsed.requester);
break;

case 'privmsg':
Expand All @@ -52,7 +57,7 @@ function react(data) {
};

if (ignoring(parsed.sender) || ignoring(parsed.hostmask))) {
console.log('-- Ignoring query from ' + parsed.hostmask);
log.debug('Ignoring query from ' + parsed.hostmask);
return;
}

Expand All @@ -73,7 +78,7 @@ function react(data) {
};

if (ignoring(parsed.sender) || ignoring(parsed.hostmask))) {
console.log('-- Ignoring message from ' + parsed.hostmask);
log.debug('Ignoring message from ' + parsed.hostmask);
return;
}

Expand Down
7 changes: 4 additions & 3 deletions network.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var net = require('net'),
irc = require('./irc.js'),
utl = require('./util.js'),
log = require('./log.js'),
nwk = exports,
server;

Expand All @@ -17,22 +18,22 @@ nwk.send = function (text) {

nwk.connect = function (opt, callback) {

console.log(utl.interp('-- Connecting to {srv} on port {prt}',
log.debug(utl.interp('Connecting to {srv} on port {prt}',
{
srv: opt.server,
prt: opt.port
}));

server = net.connect(opt.port, opt.server, function () {

console.log('-- Connected.');
log.debug('Connected.');

nwk.send(irc.outbound.pass(opt.pass));
nwk.send(irc.outbound.nick(opt.nick));
nwk.send(irc.outbound.user(opt.nick, opt.owner));

opt.channels.forEach(function (channel) {
console.log('Joining channel ' + channel);
log.debug('Joining channel ' + channel);
nwk.send(irc.outbound.join(channel));
});

Expand Down

0 comments on commit 149cb4d

Please sign in to comment.