Initialer support für den Mailversand.

This commit is contained in:
Tim Schumacher 2013-01-17 00:25:54 +01:00
parent 58bea13ce8
commit d328e41fd3
3 changed files with 100 additions and 4 deletions

View file

@ -4,3 +4,20 @@ exports.url = {
path: '/user:0xaffe?do=edit', path: '/user:0xaffe?do=edit',
method: 'GET' method: 'GET'
}; };
exports.publishers = ['mail','identica'];
exports.mail = {
mda: 'sendmail' // smtp is also possible
,host: ''
,port: ''
,secureConnection: true
,auth: {
user: ''
,pass: ''
}
,composing: {
from: ''
,to: ''
}
}

View file

@ -14,8 +14,45 @@ var
,config = require('./config') ,config = require('./config')
,http = require('https') ,http = require('https')
,jsdom = require('jsdom') ,jsdom = require('jsdom')
,nodemailer = require("nodemailer")
,log = require('sys').log; ,log = require('sys').log;
/**
* Returns the week number for this date. dowOffset is the day of week the week
* "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
* the week returned is the ISO 8601 week number.
* @param int dowOffset
* @return int
*/
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.epoch-calendar.com */
dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};
function EventTool() { function EventTool() {
events.EventEmitter.call(this); events.EventEmitter.call(this);
@ -34,6 +71,39 @@ EventTool.prototype.config = require('./config');
EventTool.prototype.RequestData = ''; EventTool.prototype.RequestData = '';
EventTool.prototype.events = []; EventTool.prototype.events = [];
EventTool.prototype.publishMail = function() {
var self = this;
log('Verschicke eine E-Mail');
var transport = null;
if (self.config.mail.mda == 'smtp') {
log('mda smtp not yet implemented');
} else {
transport = nodemailer.createTransport("sendmail");
}
transport.sendMail({
from: self.config.mail.composing.from
,to: self.config.mail.composing.to
,subject: 'Terminankündigungen KW ' + (new Date()).getWeek()
,text: 'moep moep'
});
}
EventTool.prototype.publishIdentica = function() {
log('Verschicke einen Dent');
}
EventTool.prototype.setupPublishers = function() {
this.on('mail',this.publishMail);
this.on('identica',this.publishIdentica);
}
EventTool.prototype.processEvents = function() {
var self = this;
this.config.publishers.forEach(function(el){
self.emit(el);
});
}
EventTool.prototype.parseData = function() { EventTool.prototype.parseData = function() {
var self = this.req.EventTool; var self = this.req.EventTool;
jsdom.env( jsdom.env(
@ -43,16 +113,21 @@ EventTool.prototype.parseData = function() {
var wiki_text = window.$("#wiki__text").val(); var wiki_text = window.$("#wiki__text").val();
var re = /\s\* (\d{4})\-(\d{2})\-(\d{2}) (\d{2})\:(\d{2}) - (\d{2})\:(\d{2}) (.*)/g; var re = /\s\* (\d{4})\-(\d{2})\-(\d{2}) (\d{2})\:(\d{2}) - (\d{2})\:(\d{2}) (.*)/g;
var match = null; var match = null;
var events = []; self.events = [];
while (match = re.exec(wiki_text)) { while (match = re.exec(wiki_text)) {
var event = { var event = {
startdate : new Date(match[1], match[2], match[3], match[4], match[5]) 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]) ,enddate : new Date(match[1], match[2], match[3], match[6], match[7])
,text : match[8] ,text : match[8]
}; };
events.push(event); 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');
} }
console.log(events);
} }
); );
} }
@ -69,6 +144,7 @@ EventTool.prototype.processResponse = function(res) {
} }
EventTool.prototype.run = function() { EventTool.prototype.run = function() {
log('Suche nach Terminen');
var req = http.request(this.config.url,this.processResponse); var req = http.request(this.config.url,this.processResponse);
req.EventTool = this; req.EventTool = this;

View file

@ -7,9 +7,12 @@
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
*/ */
var EventTool = require('./event').EventTool; var
EventTool = require('./event').EventTool;
var et = new EventTool(); var et = new EventTool();
et.setupPublishers();
et.on('events',et.processEvents);
et.run(); et.run();