forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
129 lines (110 loc) · 3.23 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
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
var email = require("emailjs");
var _ = require('lodash');
var log = require('../core/log.js');
var util = require('../core/util.js');
var config = util.getConfig();
var mailConfig = config.mailer;
var Mailer = function(done) {
_.bindAll(this);
this.server;
this.price = 'N/A';
this.done = done;
this.setup();
};
Mailer.prototype.setup = function(done) {
var setupMail = function(err, result) {
if(result) {
console.log('Got it.');
mailConfig.password = result.password;
}
if(_.isEmpty(mailConfig.to))
mailConfig.to = mailConfig.email;
if(_.isEmpty(mailConfig.from))
mailConfig.from = mailConfig.email;
if(_.isEmpty(mailConfig.user) && mailConfig.smtpauth)
mailConfig.user = mailConfig.email;
this.server = email.server.connect({
user: mailConfig.user,
password: mailConfig.password,
host: mailConfig.server,
ssl: mailConfig.ssl,
port: mailConfig.port,
tls: mailConfig.tls
});
if(mailConfig.sendMailOnStart) {
this.mail(
"Gekko has started",
[
"I've just started watching ",
config.watch.exchange,
' ',
config.watch.currency,
'/',
config.watch.asset,
". I'll let you know when I got some advice"
].join(''),
_.bind(function(err) {
this.checkResults(err);
this.done();
}, this)
);
} else
this.done();
log.debug('Setup email adviser.');
};
if(!mailConfig.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 guarantuee that your email address & password are safe!\n'
].join('\n\t');
log.warn(warning);
prompt.get({name: 'password', hidden: true}, _.bind(setupMail, this));
} else {
setupMail.call(this);
}
};
Mailer.prototype.mail = function(subject, content, done) {
util.retry(function(cb) {
this.server.send({
text: content,
from: mailConfig.from,
to: mailConfig.to,
subject: mailConfig.tag + subject
}, cb);
}.bind(this), done || this.checkResults);
};
Mailer.prototype.processCandle = function(candle, done) {
this.price = candle.close;
done();
};
Mailer.prototype.processAdvice = function(advice) {
if (advice.recommendation == "soft" && mailConfig.muteSoft) return;
var text = [
'Gekko is watching ',
config.watch.exchange,
' and has detected a new trend, advice is to go ',
advice.recommendation,
'.\n\nThe current ',
config.watch.asset,
' price is ',
config.watch.currency,
' ',
this.price
].join('');
var subject = 'New advice: go ' + advice.recommendation;
this.mail(subject, text);
};
Mailer.prototype.checkResults = function(err) {
if(err)
log.warn('error sending email', err);
else
log.info('Send advice via email.');
};
module.exports = Mailer;