This repository was archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.js
223 lines (200 loc) · 7.2 KB
/
event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* ----------------------------------------------------------------------------
* "THE VODKA-WARE LICENSE" (Revision 42):
* <[email protected]> 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')
,request = require('request')
,jsdom = require('jsdom')
,nodemailer = require("nodemailer")
,log = require('sys').log
,mustache = require('mustache')
,strftime = require('strftime')
,fs = require('fs')
,glob = require('glob')
,jq = require('jquery')
,wrap = require('wordwrap')(72);
function nextDay(x){
var now = new Date();
now.setDate(now.getDate() + (x+(7-now.getDay())) % 7);
return now;
}
function EventTool() {
events.EventEmitter.call(this);
};
String.prototype.EscapeLatex = function() {
return this.toString()
.replace(/\\/g,'\\textbackslash{}')
.replace(/&/g,'\\&')
.replace(/\|/g,'\\|')
.replace(/\[/g,'\\[')
.replace(/\]/g,'\\]')
.replace(/\%/g,'\\%')
.replace(/\$/g,'\\$');
}
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 = [];
EventTool.prototype.processEventData = function() {
var self = this;
var view = {
events: [],
day: strftime('%d',new Date()),
month: strftime('%m',new Date()),
year: strftime('%Y',new Date()),
week: strftime('%U',new Date())
};
// prepare the data
self.events.forEach(function(el) {
var item = {};
item['startdate'] = strftime('%Y-%m-%d',el.startdate);
item['starttime'] = strftime('%H:%M',el.startdate);
item['enddate'] = strftime('%Y-%m-%d %H:%M',el.enddate);
item['endtime'] = strftime('%H:%M',el.enddate);
item['heading'] = el.heading;
item['text'] = el.text;
view.events.push(item);
});
return view;
}
EventTool.prototype.publishMail = function() {
var self = this;
log('Verschicke eine E-Mail');
var view = self.processEventData();
view.events.forEach(function(el){
el.text = wrap(el.text);
});
// load the template
var template = fs.readFileSync('templates/email/template.mustache','utf-8');
var output = mustache.render(template, view);
var transport = null;
if (self.config.mail.mda == 'smtp') {
log('mda smtp not yet implemented');
} else {
transport = nodemailer.createTransport("sendmail");
}
var nextMonday = nextDay(1);
transport.sendMail({
from: self.config.mail.composing.from
,to: self.config.mail.composing.to
,subject: strftime('Terminankündigungen KW %U/%Y // ab dem %Y-%m-%d',nextMonday)
,text: output
});
}
EventTool.prototype.publishLatex = function() {
var self = this;
log('Erzeuge PDFs mit LaTeX');
var view = self.processEventData();
view.events.forEach(function(el){
el.heading = el.heading.EscapeLatex();
el.text = el.text.EscapeLatex();
});
// generate the pdfs
self.config.latex.templates.forEach(function(template){
var templateData = fs.readFileSync('templates/latex/'+template+'.tex','utf-8');
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');
}
});
});
}
EventTool.prototype.publishIdentica = function() {
log('Verschicke einen Dent');
}
EventTool.prototype.publishConsole = function() {
var self = this;
var view = self.processEventData();
log(util.inspect(view));
}
EventTool.prototype.setupPublishers = function() {
this.on('mail',this.publishMail);
this.on('identica',this.publishIdentica);
this.on('latex',this.publishLatex);
this.on('console',this.publishConsole);
}
EventTool.prototype.processEvents = function() {
var self = this;
this.config.publishers.forEach(function(el){
self.emit(el);
});
}
EventTool.prototype.parseData = function(et) {
var self = et;
jsdom.env(
self.RequestData,
function (errors, window) {
var $ = jq.create(window);
var wiki_text = $("#wiki__text").val();
var re = /\s\* (\d{4})\-(\d{2})\-(\d{2}) (\d{2})\:(\d{2}) - (\d{2})\:(\d{2}) (.*)/g;
var match = null;
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])
,heading : match[8].split("—")[0].trim()
,text : match[8].split("—")[1].trim()
};
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');
}
}
);
}
EventTool.prototype.processResponse = function(error, response, body) {
var self = this.EventTool;
if (response.statusCode == 200) {
self.RequestData = body;
self.parseData(self);
}
}
EventTool.prototype.run = function() {
log('Suche nach Terminen');
var req = request({'url':this.config.url,'strictSSL': false},this.processResponse);
req.EventTool = this;
req.end();
req.on('error', function(e) {
console.error(e);
});
}
exports.EventTool = EventTool;