-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
109 lines (91 loc) · 3.15 KB
/
node_helper.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
var NodeHelper = require("node_helper");
var util = require('util');
var ImapClient = require('emailjs-imap-client');
//Email-Analyse Function to format the fetched Email-Object
var analyzeEmails = function(path, client, that) { //path is for instance inbox
//console.log("==========ANALYSE-MAILS=========");
//console.log(util.inspect(client));
var query = {unseen: true};
var Result = [];
client.search('inbox',query).then((ids) => {
console.log("Mail-Seach complete");
if(ids.length>0)
{
client.listMessages('inbox', ids, ['uid','envelope']).then((messages) => {
//console.log("Message-List complete");
messages.forEach(function(m){
var newMail = {
id: m.uid,
date: m.envelope.date,
subject: m.envelope.subject,
from: m.envelope.from,
sender: m.envelope.sender,
to: m.envelope.to
}
Result.push(newMail);
})
console.log("%s Mails fetched",Result.length);
that.sendSocketNotification('EMAIL_FETCH',{user: client.options.auth.user, messages: Result});
});
}
else
{
that.sendSocketNotification('EMAIL_FETCH', {user: client.options.auth.user, messages: []});
}
});
};
module.exports = NodeHelper.create({
start: function(){
console.log(this.name + ' helper started ...');
},
socketNotificationReceived : function(notification, payload){
var that = this;
if(notification === 'LISTEN_EMAIL'){
var login = [ payload.host,
payload.port,
{
auth: {
user: payload.user,
pass: payload.pass,
}
}
];
var client = new ImapClient(login[0] , login[1], login[2]);
//Create the Event Functions
//==================================
//--> IMAP-Update event
client.onupdate = function(path, type, value){
analyzeEmails(path,this, that);
if (type === 'exists') {
client.listMessages('inbox', value, ['envelope']).then((messages) => {
messages.forEach((message) => {
var d = message.envelope;
console.log("NEUE EMAIL VON %s (%s) im Postfach %s",d.from[0].name,d.from[0].address,client.options.auth.user);
that.sendSocketNotification('EMAIL_NEWMAIL', {user: client.options.auth.user, sender: d.from[0]});
});
});
};
}
//--> Mailbox close Infomation Event
client.onclosemailbox = function(path){
//console.log("onclosemail Event Called");
console.log("MMM-MailMessage-Helper: Mailbox Closed: " + path);
}
//--> Imap Error Event
client.onerror = function(err) {
//console.log("onerror Event Called");
console.log("MMM-MailMessage-Helper: " + err);
that.sendSocketNotification('EMAIL_ERROR', {user: client.options.auth.user});
};
//====================================
//Last but Not Least: Configure and Start the IMAP-Client
client.logLevel = client.LOG_LEVEL_NONE;
client.connect().then(() => {
console.log("connected");
client.selectMailbox("INBOX").then((inbox) => {
analyzeEmails("INBOX",client, that);
});
});
}
},
});