1 | var email = require("emailjs");
|
2 | var moment = require('moment');
|
3 | var log = require('./log.js');
|
4 | var util = require('./util.js');
|
5 | var config = util.getConfig().mail;
|
6 | var server;
|
7 |
|
8 | module.exports.init = function(callback) {
|
9 | var setupMail = function(err, result) {
|
10 | if(result) {
|
11 | log.info('Got it.');
|
12 | config.password = result.password;
|
13 | }
|
14 |
|
15 | server = email.server.connect({
|
16 | user: config.email,
|
17 | password: config.password,
|
18 | host: "smtp.gmail.com",
|
19 | ssl: true
|
20 | });
|
21 |
|
22 | log.debug('Setup email adviser.');
|
23 | callback();
|
24 | }
|
25 |
|
26 | if(!config.password) {
|
27 |
|
28 | var prompt = require('prompt-lite');
|
29 | prompt.start();
|
30 | var warning = [
|
31 | '\n\n\tYou configured Gekko to mail you advice, Gekko needs your email',
|
32 | 'password to send emails (to you). Gekko is an opensource project',
|
33 | '[ http://github.com/askmike/gekko ], you can take my word but always',
|
34 | 'check the code yourself.',
|
35 | '\n\n\tWARNING: If you have not downloaded Gekko from the github page above we',
|
36 | 'CANNOT garantuee that your email address & password are safe!\n'
|
37 | ].join('\n\t');
|
38 | log.warn(warning);
|
39 | prompt.get({name: 'password', hidden: true}, setupMail);
|
40 | } else {
|
41 | setupMail(false, false);
|
42 | }
|
43 | }
|
44 |
|
45 | var send = function(err) {
|
46 | if(err)
|
47 | log.warn('ERROR SENDING MAIL', err);
|
48 | else
|
49 | log.info('Send advice via email.');
|
50 | }
|
51 |
|
52 | module.exports.send = function(what, price, meta) {
|
53 | if(what !== 'BUY' && what !== 'SELL')
|
54 | return;
|
55 |
|
56 | var text = [
|
57 | 'Gekko is watching the bitcoin market and has detected a new trend, advice is to ' + what,
|
58 | 'The current BTC price is ' + price,
|
59 | '',
|
60 | 'Additional information:\n',
|
61 | meta
|
62 | ].join('\n');
|
63 |
|
64 | server.send({
|
65 | text: text,
|
66 | from: "Gekko <" + config.email + ">",
|
67 | to: "Bud Fox <" + config.email + ">",
|
68 | subject: "New Gekko advice: " + what
|
69 | }, send);
|
70 | } |
\ | No newline at end of file |