forked from mlmurray/gekko
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mailer.js
98 lines (87 loc) · 2.66 KB
/
mailer.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
var email = require("emailjs");
var moment = require('moment');
var log = require('./log.js');
var util = require('./util.js');
var _ = require('lodash');
var config = util.getConfig().mail;
var server;
module.exports.init = function(callback) {
var setupMail = function(err, result) {
if(!config.from) { config.from = config.user }
if(!config.smtp) { config.smtp = 'smtp.gmail.com' }
if(!config.port) { config.port = 25 }
if(!config.ssl) { config.ssl = false }
if(config.tls) {
//some mail server may produce error is ssl and tls are set to true
config.ssl = false,
config.tls = true
} else {
config.tls = false
}
if(result) {
log.info('Got it.');
config.password = result.password;
}
server = email.server.connect({
user: config.user,
password: config.password,
host: config.smtp,
port: config.port,
tls: config.tls,
ssl: config.ssl
});
if(config.sendMailOnStart) {
server.send({
from: "Gekko <" + config.from + ">",
to: "Bud Fox <" + config.email + ">",
subject: "Gekko has started",
text: [
"I've just started watching the markets, ",
"I'll let you know when I got some advice"
].join('')
}, send);
}
log.debug('Setup email adviser.');
callback();
}
if(!config.password) {
// ask for the mail password
var prompt = require('prompt-lite');
prompt.start();
var warning = [
'\n\n\tYou configured Gekko to mail you advice, Gekko needs your email',
'password to send emails (to you). Gekko is an opensource project',
'[ http://github.com/askmike/gekko ], you can take my word but always',
'check the code yourself.',
'\n\n\tWARNING: If you have not downloaded Gekko from the github page above we',
'CANNOT garantuee that your email address & password are safe!\n'
].join('\n\t');
log.warn(warning);
prompt.get({name: 'password', hidden: true}, setupMail);
} else {
setupMail(false, false);
}
}
var send = function(err) {
if(err)
log.warn('ERROR SENDING MAIL', err);
else
log.info('Send advice via email.');
}
module.exports.send = function(what, price, meta) {
if (!_.contains(config.what, what))
return;
var text = [
'Gekko is watching the bitcoin market and has detected a new trend, advice is to ' + what,
'The current BTC price is ' + price,
'',
'Additional information:\n',
meta
].join('\n');
server.send({
text: text,
from: "Gekko <" + config.from + ">",
to: "Bud Fox <" + config.email + ">",
subject: "New Gekko advice: " + what
}, send);
}