2013-01-16 23:34:35 +01:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE VODKA-WARE LICENSE" (Revision 42):
|
|
|
|
* <tim@bandenkrieg.hacked.jp> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a vodka in return. Tim Schumacher
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
var
|
|
|
|
events = require('events')
|
|
|
|
,util = require('util')
|
|
|
|
,dgram = require('dgram')
|
|
|
|
,config = require('./config')
|
2013-08-11 17:14:43 +02:00
|
|
|
,request = require('request')
|
2013-01-16 23:34:35 +01:00
|
|
|
,jsdom = require('jsdom')
|
2013-01-17 00:25:54 +01:00
|
|
|
,nodemailer = require("nodemailer")
|
2013-01-17 22:29:30 +01:00
|
|
|
,log = require('sys').log
|
|
|
|
,mustache = require('mustache')
|
|
|
|
,strftime = require('strftime')
|
2013-01-17 23:31:44 +01:00
|
|
|
,fs = require('fs')
|
2013-01-22 20:42:38 +01:00
|
|
|
,glob = require('glob')
|
|
|
|
,jq = require('jquery');
|
2013-01-17 22:29:30 +01:00
|
|
|
|
|
|
|
function nextDay(x){
|
|
|
|
var now = new Date();
|
|
|
|
now.setDate(now.getDate() + (x+(7-now.getDay())) % 7);
|
|
|
|
return now;
|
|
|
|
}
|
2013-01-16 23:34:35 +01:00
|
|
|
|
|
|
|
function EventTool() {
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
EventTool.super_ = events.EventEmitter;
|
|
|
|
EventTool.prototype = Object.create(events.EventEmitter.prototype, {
|
|
|
|
constructor: {
|
|
|
|
value: EventTool,
|
|
|
|
enumerable: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
EventTool.prototype.config = require('./config');
|
|
|
|
EventTool.prototype.RequestData = '';
|
|
|
|
EventTool.prototype.events = [];
|
|
|
|
|
2013-01-17 23:31:44 +01:00
|
|
|
EventTool.prototype.processEventData = function() {
|
2013-01-17 00:25:54 +01:00
|
|
|
var self = this;
|
2013-01-17 22:29:30 +01:00
|
|
|
var view = {
|
|
|
|
events: []
|
|
|
|
};
|
|
|
|
// prepare the data
|
|
|
|
self.events.forEach(function(el) {
|
|
|
|
var item = {};
|
|
|
|
item['startdate'] = strftime('%Y-%m-%d %H:%M',el.startdate);
|
|
|
|
item['enddate'] = strftime('%Y-%m-%d %H:%M',el.enddate);
|
|
|
|
item['heading'] = el.heading;
|
|
|
|
item['text'] = el.text;
|
|
|
|
view.events.push(item);
|
|
|
|
});
|
2013-01-17 23:31:44 +01:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventTool.prototype.publishMail = function() {
|
|
|
|
var self = this;
|
|
|
|
log('Verschicke eine E-Mail');
|
|
|
|
var view = self.processEventData();
|
2013-01-17 22:29:30 +01:00
|
|
|
// load the template
|
|
|
|
var template = fs.readFileSync('templates/email/template.mustache','utf-8');
|
|
|
|
var output = mustache.render(template, view);
|
2013-01-17 00:25:54 +01:00
|
|
|
var transport = null;
|
|
|
|
if (self.config.mail.mda == 'smtp') {
|
|
|
|
log('mda smtp not yet implemented');
|
|
|
|
} else {
|
|
|
|
transport = nodemailer.createTransport("sendmail");
|
|
|
|
}
|
2013-01-17 22:29:30 +01:00
|
|
|
var nextMonday = nextDay(1);
|
2013-01-17 00:25:54 +01:00
|
|
|
transport.sendMail({
|
|
|
|
from: self.config.mail.composing.from
|
|
|
|
,to: self.config.mail.composing.to
|
2013-01-17 22:29:30 +01:00
|
|
|
,subject: strftime('Terminankündigungen KW %U/%Y // ab dem %Y-%m-%d',nextMonday)
|
|
|
|
,text: output
|
2013-01-17 00:25:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-17 23:31:44 +01:00
|
|
|
EventTool.prototype.publishLatex = function() {
|
|
|
|
var self = this;
|
|
|
|
log('Erzeuge PDFs mit LaTeX');
|
|
|
|
var view = self.processEventData();
|
|
|
|
// generate the pdfs
|
|
|
|
self.config.latex.templates.forEach(function(template){
|
2013-08-11 17:14:43 +02:00
|
|
|
var templateData = fs.readFileSync('templates/latex/'+template+'.tex','utf-8');
|
2013-01-17 23:31:44 +01:00
|
|
|
var output = mustache.render(templateData, view);
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(self.config.latex.tempDir);
|
|
|
|
} catch(e) {
|
|
|
|
// POKEMON! GOTTA CATCH THEM ALL!
|
|
|
|
}
|
|
|
|
var tempfilePrefix = self.config.latex.tempDir + '/' + template;
|
|
|
|
fs.writeFileSync(tempfilePrefix+'.tex',output);
|
|
|
|
var spawn = require('child_process').spawn,
|
|
|
|
pdflatex = spawn('pdflatex', ['-interaction','nonstopmode','-output-directory', self.config.latex.tempDir,tempfilePrefix+'.tex']);
|
|
|
|
|
|
|
|
pdflatex.on('exit', function (code) {
|
|
|
|
log('child process exited with code ' + code);
|
|
|
|
if (code === 0) {
|
|
|
|
log('Copy the PDF to the output dir');
|
|
|
|
fs.createReadStream(tempfilePrefix+'.pdf').pipe(fs.createWriteStream(self.config.latex.output+'/'+template+'.pdf'));
|
|
|
|
|
|
|
|
// now clean up the tempdir
|
|
|
|
glob('**/*',{cwd:self.config.latex.tempDir},function(err,files){
|
|
|
|
files.forEach(function(file){
|
|
|
|
fs.unlinkSync(self.config.latex.tempDir+'/'+file);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log('Failed to generate the pdf. Please chek the logs under '+ tempfilePrefix+'.log');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-17 00:25:54 +01:00
|
|
|
EventTool.prototype.publishIdentica = function() {
|
|
|
|
log('Verschicke einen Dent');
|
|
|
|
}
|
|
|
|
|
2013-01-22 20:41:42 +01:00
|
|
|
EventTool.prototype.publishConsole = function() {
|
|
|
|
var self = this;
|
|
|
|
var view = self.processEventData();
|
|
|
|
log(util.inspect(view));
|
|
|
|
}
|
|
|
|
|
2013-01-17 00:25:54 +01:00
|
|
|
EventTool.prototype.setupPublishers = function() {
|
|
|
|
this.on('mail',this.publishMail);
|
|
|
|
this.on('identica',this.publishIdentica);
|
2013-01-17 23:31:44 +01:00
|
|
|
this.on('latex',this.publishLatex);
|
2013-01-22 20:41:42 +01:00
|
|
|
this.on('console',this.publishConsole);
|
2013-01-17 00:25:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EventTool.prototype.processEvents = function() {
|
|
|
|
var self = this;
|
|
|
|
this.config.publishers.forEach(function(el){
|
|
|
|
self.emit(el);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-08-11 17:14:43 +02:00
|
|
|
EventTool.prototype.parseData = function(et) {
|
|
|
|
var self = et;
|
2013-01-16 23:34:35 +01:00
|
|
|
jsdom.env(
|
|
|
|
self.RequestData,
|
|
|
|
function (errors, window) {
|
2013-01-22 20:42:38 +01:00
|
|
|
var $ = jq.create(window);
|
|
|
|
var wiki_text = $("#wiki__text").val();
|
2013-01-16 23:34:35 +01:00
|
|
|
var re = /\s\* (\d{4})\-(\d{2})\-(\d{2}) (\d{2})\:(\d{2}) - (\d{2})\:(\d{2}) (.*)/g;
|
|
|
|
var match = null;
|
2013-01-17 00:25:54 +01:00
|
|
|
self.events = [];
|
2013-01-16 23:34:35 +01:00
|
|
|
while (match = re.exec(wiki_text)) {
|
|
|
|
var event = {
|
|
|
|
startdate : new Date(match[1], match[2], match[3], match[4], match[5])
|
|
|
|
,enddate : new Date(match[1], match[2], match[3], match[6], match[7])
|
2013-01-17 22:29:30 +01:00
|
|
|
,heading : match[8].split("—")[0].trim()
|
|
|
|
,text : match[8].split("—")[1].trim()
|
2013-01-16 23:34:35 +01:00
|
|
|
};
|
2013-01-17 00:25:54 +01:00
|
|
|
self.events.push(event);
|
|
|
|
}
|
|
|
|
if (self.events.length > 0) {
|
|
|
|
log(self.events.length + ' Termine gefunden, starte Verarbeitung');
|
|
|
|
self.emit('events');
|
|
|
|
} else {
|
|
|
|
log('Keine Termine gefunden');
|
2013-01-16 23:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-08-11 17:14:43 +02:00
|
|
|
EventTool.prototype.processResponse = function(error, response, body) {
|
2013-01-16 23:34:35 +01:00
|
|
|
var self = this.EventTool;
|
2013-08-11 17:14:43 +02:00
|
|
|
if (response.statusCode == 200) {
|
|
|
|
self.RequestData = body;
|
|
|
|
self.parseData(self);
|
2013-01-16 23:34:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EventTool.prototype.run = function() {
|
2013-01-17 00:25:54 +01:00
|
|
|
log('Suche nach Terminen');
|
2013-08-11 17:14:43 +02:00
|
|
|
var req = request({'url':this.config.url,'strictSSL': false},this.processResponse);
|
2013-01-16 23:34:35 +01:00
|
|
|
req.EventTool = this;
|
|
|
|
|
|
|
|
req.end();
|
|
|
|
|
|
|
|
req.on('error', function(e) {
|
|
|
|
console.error(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.EventTool = EventTool;
|