-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (57 loc) · 1.45 KB
/
index.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
var smtp = require('./smtp_server');
var dropbox = require('./dropbox');
var queue = [];
smtp.onMail(function(mail){
var from = mail.from[0].address;
var to = mail.to[0].address;
var subject = mail.subject;
console.log()
console.log("New message");
console.log("From: " + from);
console.log("To: " + to);
if (subject.indexOf('Fwd:') == 0) {
var origFrom = /From:.*<([^>]+)>/.exec(mail.text)[1];
if (origFrom) {
from = origFrom;
}
}
var sender = from.split('@')[1].split('.').slice(0, -1).reverse().join(' ');
console.log("Sender: "+sender);
if (!mail.attachments) {
mail.attachments = [];
}
mail.attachments.forEach(function(att){
var ext = att.contentType.split('/')[1];
queue.push([sender, ext, att.content]);
});
if (mail.attachments.length == 0) {
if (mail.html) {
queue.push([sender, 'htm', mail.html]);
} else {
queue.push([sender, 'txt', mail.text]);
}
}
startWork();
});
var running = false;
var startWork = function(){
if (running) {
return;
} else {
running = true;
work();
}
}
var work = function() {
if (queue.length == 0) {
running = false;
return
}
var q = queue.pop();
var name = q[0];
var ext = q[1];
var content = q[2];
dropbox.upload(name, ext, content, function(){
work();
});
}