-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoof.js
90 lines (81 loc) · 2.69 KB
/
Poof.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
var fs = require("fs");
var dash_button = require('node-dash-button');
var nodemailer = require('nodemailer');
var request = require('request');
var intercept = require("intercept-stdout");
var config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
var transporter;
var dash = dash_button(config.mac_address, null, null, 'udp');
var timeLastPressed = Math.floor(new Date().getTime() / 1000);
init();
/**
* Initializes the program.
*/
function init() {
var unhook_intercept = intercept(function(txt) {
var time = new Date().toLocaleString().split(", ")[1];
return "[" + time + "] (Poof) " + txt;
});
dash.on("detected", function (){
var currentTime = Math.floor(new Date().getTime() / 1000);
console.log("Button pressed! Time since last press: " + (timeLastPressed - currentTime));
if (timeLastPressed === 0 || currentTime - timeLastPressed > config.cooldown) {
timeLastPressed = currentTime;
notify();
} else {
console.log("Cooldown period still active! Time remaining: " + (config.cooldown - (currentTime - timeLastPressed)));
}
});
transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: config.gmail,
pass: config.gmail_password
}
});
console.log("Poof activated!");
}
/**
* Sends an email to the "email_to_notify" set in the config with the specified message and a random gif.
*/
function notify() {
request("http://api.giphy.com/v1/gifs/random?api_key=" + config.giphy_key + "&tag=" + config.giphy_tag, function (error, response, body) {
var gif = "";
if (!error && response.statusCode == 200) {
gif = "\n" + JSON.parse(body).data.url;
}
var subject = config.message_subject.replace("[TIME]", getTime()).replace("[time]", getTime());
sendEmail(config.email_to_notify, subject, config.message + gif);
});
}
/**
* Sends an email with the given parameters.
* @param {String} to The email address of the person to send the email to
* @param {String} subject The subject of the email
* @param {String} message The body of the email in plain-text
*/
function sendEmail(to, subject, message) {
var options = {
from: '"Poof" <' + config.gmail + '>',
to: to,
subject: subject,
text: message
};
transporter.sendMail(options, function(error, info) {
if(error){
console.log("Error sending email:", error);
return;
} else {
console.log("Email sent to " + to + ": [" + subject + "] " + options.text);
}
});
}
/**
* Returns the formatted time.
* @return {String} The time, formatted to look all nice and pretty-like
*/
function getTime() {
return "[" + new Date().toLocaleString().split(", ")[1] + "]";
}