Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Commit

Permalink
This commit makes the following changes
Browse files Browse the repository at this point in the history
1. Adds a configuration file that replaces previously hardcoded values with configuration values that are picked up by relevant modules (ref: #1 (comment) , #1 (comment), #1 (comment) )
2. Separates scheduler from the main util module and adds one more (mkfilenamer)
3. Interaction between view and controller is made event driven. Previously was callback driven.
4. Elasticsearch is commented out because it is not being used in v0.1.0 anyway
  • Loading branch information
shrikrishnaholla committed Dec 25, 2013
1 parent a47d992 commit 3deec14
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 126 deletions.
53 changes: 53 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Configuration for various components of the project */

module.exports = {
botOpts : {
userName: 'OptimusPrime',
realName: 'Optimus Prime',
nick: 'optimusbot',
password: 'linus',
port: 6667,
debug: false,
showErrors: false,
autoRejoin: true,
autoConnect: true,
ircserver : 'irc.freenode.net',
channels: ['#pes-os', '#optimus-test'],
secure: false,
selfSigned: false,
certExpired: false,
floodProtection: false,
floodProtectionDelay: 1000,
sasl: true,
stripColors: true,
channelPrefixes: "&#",
messageSplit: 512
},
irclogformat : {
channelName : false,
datetime : true,
from : true,
separator : ':\t'
},
irclogarchive : {
interval : 'monthly', // or 'annually' or 'weekly' or 'daily' or 'hourly'
prefix : 'irclog',
channelname : true,
datetime : true,
separator : '-', // Separator to the filename
extension : '.log',
location : './model/irclogs/'
},
debuglogarchive : {
interval : 'monthly', // or 'annually' or 'weekly' or 'daily' or 'hourly'
prefix : 'debuglog',
datetime : true,
separator : '-', // Separator to the filename
extension : '.log',
location : './debuglogger/debuglogs/'
},
ircreplyformat : {
mentionuser : true,
separator : ': '
}
};
22 changes: 16 additions & 6 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
var model = require('../model');
var parser = require('./parser');
var eventManager = require('../util').eventManager;

exports.processmessage = function(from , channel, message, Callback) {
exports.init = function() {

/* Process new message */
eventManager.on('newmessage', function(from, to, message) {
processmessage(from, to, message, function(reply) {
eventManager.emit('reply', from, to, reply);
});
});
};

function processmessage (from, channel, message, Callback) {
var parsedmsg = parser(message);
switch(parsedmsg) {
case 'lastnlogs':
model.readlog(message.split(' ')[1], Callback);
case 'logrequest':
model.readlog(channel, message.split(' ')[1], Callback);
break;
default:
model.writelog(from, channel, message, Callback);
break;
};
message = '[' + (new Date()).toISOString() + ']:\t' + from + ':\t' + message + '\n';
model.writelog(message, Callback);
}
};
2 changes: 1 addition & 1 deletion controller/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var commandpresets = [
{
regex : /!logs?[ ][0-9]+$/i,
op : 'lastnlogs'
op : 'logrequest'
}
];

Expand Down
20 changes: 10 additions & 10 deletions debuglogger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ var logger = require('caterpillar').createLogger({level:7});
var filter = require('caterpillar-filter').createFilter();
var human = require('caterpillar-human').createHuman();
var fs = require('fs');
var schedule = require('../util').scheduleTask;
var schedule = require('../util').schedule;
var makeFilename = require('../util').makeFilename;
var config = require('../config').debuglogarchive;
var writeStream;

// Pipe to filter to human to stdout
logger.pipe(filter).pipe(human).pipe(process.stdout);
if (!fs.existsSync('./debuglogger/debuglogs')) {
fs.mkdirSync('./debuglogger/debuglogs');
if (!fs.existsSync(config.location)) {
fs.mkdirSync(config.location);
}

writeStream = fs.createWriteStream('./debuglogger/debuglogs/debug.log');
writeStream = fs.createWriteStream(makeFilename(config));
logger.pipe(writeStream);

/* For most cases, the default level can be used.
However, if needed, other levels of debug messages can be used as well*/
However, if needed, other levels of debug messages can be used as well */
exports.info = function(message) {
logger.log('info', message);
};
Expand Down Expand Up @@ -55,13 +57,11 @@ exports.debug = function(message) {
logger.log('debug', message);
};

/* Creates a new debug log file every 30 days to avoid pile up */
schedule('monthly', function() {
var date = new Date();
/* Schedule creation of new logfile regularly to avoid pile up */
schedule(config.interval, function() {
logger.unpipe(writeStream);
writeStream.end(function() {
writeStream = fs.createWriteStream('./debuglogger/debuglogs/debug-' +
date.toISOString().split('T')[0].slice(0,7) + '.log'); //debug-yyyy-mm.log
writeStream = fs.createWriteStream(makeFilename(config)); //debug-yyyy-mm.log
logger.pipe(writeStream);
});
});
32 changes: 6 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
var irc = require('irc'),
view = require('./view');
view = require('./view'),
controller = require('./controller'),
botOpts = require('./config').botOpts;

var botOpts = {
userName: 'OptimusPrime',
realName: 'Optimus Prime',
nick: 'optimusbot',
password: 'linus',
port: 6667,
debug: false,
showErrors: false,
autoRejoin: true,
autoConnect: true,
channels: ['#pes-os', '#optimus-test'],
secure: false,
selfSigned: false,
certExpired: false,
floodProtection: false,
floodProtectionDelay: 1000,
sasl: true,
stripColors: true,
channelPrefixes: "&#",
messageSplit: 512
};

var optimusbot = new irc.Client('irc.freenode.net', 'optimusbot', botOpts);

view(optimusbot, function() {});
var bot = new irc.Client(botOpts.ircserver, botOpts.nick, botOpts);
view.init(bot);
controller.init();
80 changes: 43 additions & 37 deletions model/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,66 @@
var debuglogger = require('../debuglogger');
var schedule = require('../util').scheduleTask;
var elasticsearch = require('elasticsearch');
var fs = require('fs');
var exec = require('child_process').exec;
var debuglogger = require('../debuglogger');
var schedule = require('../util').schedule;
var makeFilename = require('../util').makeFilename;
var archiveconfig= require('../config').irclogarchive;
var logformat = require('../config').irclogformat;
var botOpts = require('../config').botOpts;
// var elasticsearch = require('elasticsearch');
var fs = require('fs');
var exec = require('child_process').exec;

var client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
// var client = new elasticsearch.Client({
// host: 'localhost:9200',
// log: 'trace'
// });

if (!fs.existsSync('./model/irclogs')) {
fs.mkdirSync('./model/irclogs');
if (!fs.existsSync(archiveconfig.location)) {
fs.mkdirSync(archiveconfig.location);
}

var date = new Date();
var filename = './model/irclogs/' + 'irclog-' +
date.toISOString().split('T')[0].slice(0,7) + '.log';

if(!fs.existsSync(filename)) {
fs.writeFileSync(filename,'');
}

var fd = fs.open(filename, 'a+');
var filenames = {};
botOpts.channels.map(function(channelname, index, array) {
filenames[channelname] = makeFilename(archiveconfig, channelname);
});

// Schedule creation of a new log file every month
schedule('monthly', function() {
var date = new Date();
fs.close(fd, function() {
debuglogger.notice('Creating new log file for the new month');
filename = './model/irclogs/' + 'irclog-' +
date.toISOString().split('T')[0].slice(0,7) + '.log'
fd = fs.open(filename, 'a+');
// Schedule creation of a new log file
schedule(archiveconfig.interval, function() {
debuglogger.notice('Creating new log files\n');
botOpts.channels.map(function(channelname, index, array) {
filenames[channelname] = makeFilename(archiveconfig, channelname);
});
});

exports.writelog = function(message, Callback) {
fs.writeFile(filename, message, {flag:'a+'}, function(err) {
exports.writelog = function(from, channelname, message, Callback) {
formattedmessage = '';
if (logformat.channelName) {
formattedmessage += channelname + logformat.separator;
};
if (logformat.datetime) {
formattedmessage += '[' + (new Date()).toISOString() + ']' + logformat.separator;
};
if (logformat.from) {
formattedmessage += from + logformat.separator;
};
formattedmessage += message + '\n';

fs.writeFile(filenames[channelname], formattedmessage, {flag:'a+'}, function(err) {
if (err) {
debuglogger.error('Writing into the irc log failed!');
debuglogger.error('Writing into the irc log failed!\n');
};
Callback();
});
};

exports.readlog = function(number, Callback) {
var date = new Date();
exports.readlog = function(channelname, number, Callback) {
if (typeof number === 'string') {
number = parseInt(number);
};
number++;
exec('tail -n ' + number.toString() + ' ' + filename + ' | haste' ,
exec('tail -n ' + number.toString() + ' ' + filenames[channelname] + ' | haste' ,
function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
Callback(stdout);
if (error !== null) {
debuglogger.error('exec error: ' + error);
debuglogger.error('exec error: ' + error + '\n');
}
});
};
24 changes: 8 additions & 16 deletions util/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
/* Consists of utility modules used by the project */

/* Task Scheduler. To schedule running of operations */
var schedule = require('node-schedule');

exports.scheduleTask = function(type, operation) {
var rule = new schedule.RecurrenceRule();
if (type === 'monthly') {
rule.hour = 0;
rule.minute = 0;
rule.date = 1;
} else if (type === 'daily') {
rule.hour = 0;
rule.minute = 0;
} else if (type === 'hourly') {
rule.minute = 0;
}
var job = schedule.scheduleJob(rule, operation);
return job;
};
exports.schedule = require('./scheduler').scheduleTask;

/* Construct filenames for logfiles */
exports.makeFilename = require('./mkfilename');

/* Event emitter. Used to emit and handle events */
var events = require('events');
exports.eventManager = new events.EventEmitter();
49 changes: 49 additions & 0 deletions util/mkfilename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Utility module used to construct filenames for logfiles */

module.exports = function(config, channelname) {
var identifier;

if (config.datetime) {
var datestring = (new Date()).toISOString();
switch(config.interval) {
case 'hourly' :
identifier = datestring.split(':')[0];
break;
case 'daily' :
case 'weekly' :
identifier = datestring.split('T')[0];
break;
case 'monthly' :
identifier = datestring.split('T')[0].slice(0,7);
break;
case 'annually' :
identifier = datestring.split('T')[0].slice(0,4);
break;
default:
identifier = makeid(5);
break;
}
} else {
identifier = makeid(5);
}

if (config.channelname === true) {
identifier = channelname + config.separator + identifier;
};

identifier.replace(/-/g, config.separator);

var filename = config.location + config.prefix + config.separator + identifier + config.extension;
return filename;
};

function makeid(length)
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i < length; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));

return text;
}
28 changes: 28 additions & 0 deletions util/scheduler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Scheduler - Can be used to schedule tasks to be executed at particuar time */

var schedule = require('node-schedule');

exports.scheduleTask = function(type, operation) {
var rule = new schedule.RecurrenceRule();
if (type === 'annually') {
rule.month = 0;
rule.date = 1;
rule.hour = 0;
rule.minute = 0;
} else if (type === 'monthly') {
rule.date = 1;
rule.hour = 0;
rule.minute = 0;
} else if (type === 'weekly') {
rule.dayOfWeek = 0;
rule.hour = 0;
rule.minute = 0;
} else if (type === 'daily') {
rule.hour = 0;
rule.minute = 0;
} else if (type === 'hourly') {
rule.minute = 0;
}
var job = schedule.scheduleJob(rule, operation);
return job;
};
Loading

0 comments on commit 3deec14

Please sign in to comment.