Repository navigation
Expand file tree
/
Copy pathpoller.js
More file actions
200 lines (150 loc) · 5.57 KB
/
Copy pathpoller.js
File metadata and controls
200 lines (150 loc) · 5.57 KB
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
/**
* Mail Poller
*
*/
function collect(val, memo) {
memo.push(val);
return memo;
}
function parseBoolean(val) {
if (typeof val === "boolean") return val
return null
}
function fixColors(str) {
return unescape(
escape(
str
)
.replace(/\%1B/i, '\\u%1B')
.replace('\\u', '')
)
}
var help = function (msg) {
console.log("\n")
console.log(fixColors(colors.bgRed(msg)))
console.log("\n")
program.outputHelp();
}
var isValidMailProperties = function (config) {
if (!config.user) {
console.log("\n" + fixColors(colors.grey("Not a valid [mail] section, user not specified")))
return false
}
if (!config.password) {
console.log("\n" + fixColors(colors.grey("Not a valid [mail] section, password not specified")))
return false
}
if (!config.host) {
console.log("\n" + fixColors(colors.grey("Not a valid [mail] section, host not specified")))
return false
}
return true
}
var isValidConfigFile = function (val) {
if (!fs.existsSync(val)) {
console.log("\n" + fixColors(colors.grey("File not found: " + val)))
return false
}
var properties = PropertiesReader(val);
if (!properties.path().mail) {
console.log("\n" + fixColors(colors.grey("Not a valid property file, specify [mail] section")))
return false
}
if (!properties.path().mail.type) {
console.log("\n" + fixColors(colors.grey("Not a valid [mail] section, havent specified 'type'")))
return false
}
if (['imap'].indexOf(properties.path().mail.type) == -1) {
console.log("\n" + fixColors(colors.grey("Not a valid [mail.type] property, supports (imap)")))
return false
}
if (!isValidMailProperties(properties.path().mail[properties.path().mail.type])) {
return false
}
return properties
}
var program = require('commander');
var colors = require('colors');
var PropertiesReader = require('properties-reader');
const fs = require('fs')
program
.version('1.0.0')
.option('-c, --clientname <string>', 'clientname')
.option('--config <file>', 'config file (property)', isValidConfigFile)
.option('-l, --lookback [number]', 'lookback period in seconds', parseInt, 60)
.option('-f, --fetch <items>', 'fields to fetch HEADER(FROM,TO..) TEXT', collect, "HEADER,TEXT".split(","))
.option('--limit <n>', 'Limit of mails to process after fetch', parseInt, -1)
.option('--unseen', 'Only unseen mails')
.option('--mark-seen', 'Marked fetched mails as seen', parseBoolean, false)
.option('-b, --box', 'Box name (INBOX, SENT ..)', 'INBOX')
.option('-s, --search', 'Search criteria', '[]')
.option('--watch', 'Run in watch mode', false)
.option('-t, --test', 'Test mail connectivity')
.parse(process.argv);
if (!program.clientname) return help("Do specify a valid clientname")
if (!program.config) return help("Do specify a valid mail config")
var properties = program.config
if (program.box) properties.set('mail.box', program.box)
if (program.search) {
var modules = properties.path().modules
for (var module in modules) {
if (modules.hasOwnProperty(module) && modules[module] == 'true') {
properties.set([module, 'mail', 'poller', 'search_criteria'].join('.'), program.search)
}
}
}
if (program.markSeen) properties.set(['mail', 'mark_seen'].join('.'), program.markSeen)
console.log("")
/**
* initialize logger
*/
require('./logger/init').initialize(program.config.logging)(function () {
console.log(fixColors(colors.gray("Logger started\n")))
});
var mailAdapter = null;
var MailBackend = require('./mail-backends/MailBackend')
var Poller = require('./mail-pollers/Poller')
var Publisher = require('./pubsub/Publisher')
const mailBackend = new MailBackend(program.clientname, properties.path())
const publisher = new Publisher(program.clientname, properties.path().pubsub)
switch (properties.path().mail.type) {
case 'imap':
var IMAP = require('./mail-adapters/imap')
mailAdapter = new IMAP(program.clientname, properties.path().mail, mailBackend)
break
}
mailBackend.init((err) => {
if (err) return
var modules = properties.path().modules
console.log('registering modules\n')
const registered_modules = []
const pollers = {}
for (var module in modules) {
if (modules.hasOwnProperty(module) && modules[module] == 'true') {
try {
var Module = require('./modules/Module')
/**
* create mail poller
*/
var pollerName = properties.path()[module].poller || "default-poller"
var poller = null
if (pollers[pollerName]) {
poller = pollers[pollerName]
} else {
poller = new Poller(program.clientname, pollerName, mailAdapter, properties.path()["poller-" + pollerName], mailBackend)
}
poller.start((err) => {
/**
* create module and set mail adapter
*/
registered_modules.push(new Module(module, program.clientname, properties.path()[module]).setPublisher(publisher).setPoller(poller))
console.log(fixColors(colors.green(" + [" + module + "]")))
})
} catch (e) {
console.log(e)
console.log(fixColors(colors.bgRed("Unknown module specified '" + module + "'")))
}
}
}
console.log("")
})