This repository has been archived by the owner on Jul 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit makes the following changes
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
1 parent
a47d992
commit 3deec14
Showing
11 changed files
with
255 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : ': ' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
var commandpresets = [ | ||
{ | ||
regex : /!logs?[ ][0-9]+$/i, | ||
op : 'lastnlogs' | ||
op : 'logrequest' | ||
} | ||
]; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.