From d328e41fd33422a10e3807e5f204988f55daf1d7 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 17 Jan 2013 00:25:54 +0100 Subject: [PATCH] =?UTF-8?q?Initialer=20support=20f=C3=BCr=20den=20Mailvers?= =?UTF-8?q?and.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.js | 17 +++++++++++ event.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- termine.js | 5 +++- 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/config.js b/config.js index 47d913a..f037046 100644 --- a/config.js +++ b/config.js @@ -4,3 +4,20 @@ exports.url = { path: '/user:0xaffe?do=edit', method: 'GET' }; + +exports.publishers = ['mail','identica']; + +exports.mail = { + mda: 'sendmail' // smtp is also possible + ,host: '' + ,port: '' + ,secureConnection: true + ,auth: { + user: '' + ,pass: '' + } + ,composing: { + from: '' + ,to: '' + } +} diff --git a/event.js b/event.js index 19da610..4e7a3f7 100644 --- a/event.js +++ b/event.js @@ -14,8 +14,45 @@ var ,config = require('./config') ,http = require('https') ,jsdom = require('jsdom') + ,nodemailer = require("nodemailer") ,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() { events.EventEmitter.call(this); @@ -34,6 +71,39 @@ EventTool.prototype.config = require('./config'); EventTool.prototype.RequestData = ''; 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() { var self = this.req.EventTool; jsdom.env( @@ -43,16 +113,21 @@ EventTool.prototype.parseData = function() { 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 match = null; - var events = []; + self.events = []; 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]) ,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() { + log('Suche nach Terminen'); var req = http.request(this.config.url,this.processResponse); req.EventTool = this; diff --git a/termine.js b/termine.js index 99d36b7..7d39e70 100644 --- a/termine.js +++ b/termine.js @@ -7,9 +7,12 @@ * ---------------------------------------------------------------------------- */ -var EventTool = require('./event').EventTool; +var + EventTool = require('./event').EventTool; var et = new EventTool(); +et.setupPublishers(); +et.on('events',et.processEvents); et.run();