2016-03-12 13:50:43 -05:00
|
|
|
"use strict";
|
|
|
|
|
2016-06-02 19:33:01 -04:00
|
|
|
//! {"category": "libraries", "permissions": ["network"]}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
|
|
|
terminal.print("Hello, world!");
|
|
|
|
|
2016-06-02 19:01:55 -04:00
|
|
|
let kServer = "localhost";
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2016-04-25 16:43:28 -04:00
|
|
|
class Smtp {
|
|
|
|
constructor() {
|
|
|
|
this.inBuffer = "";
|
|
|
|
this.sentFrom = false;
|
|
|
|
this.sentTo = false;
|
|
|
|
this.sentData = false;
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
|
2016-04-25 16:43:28 -04:00
|
|
|
send(message) {
|
|
|
|
let self = this;
|
|
|
|
self.message = message;
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
self.resolve = resolve;
|
|
|
|
self.reject = reject;
|
|
|
|
network.newConnection().then(function(socket) {
|
|
|
|
self.socket = socket;
|
|
|
|
socket.read(function(data) {
|
|
|
|
try {
|
|
|
|
self.dataReceived(data);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error.message);
|
|
|
|
}
|
|
|
|
});
|
2016-06-02 19:01:55 -04:00
|
|
|
socket.connect(kServer, 25).catch(reject);
|
2016-04-25 16:43:28 -04:00
|
|
|
});
|
|
|
|
});
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:43:28 -04:00
|
|
|
dataReceived(data) {
|
|
|
|
let self = this;
|
|
|
|
if (data === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.inBuffer += data;
|
|
|
|
let again = true;
|
|
|
|
while (again) {
|
|
|
|
again = false;
|
|
|
|
let end = self.inBuffer.indexOf("\n");
|
|
|
|
if (end != -1) {
|
|
|
|
again = true;
|
|
|
|
let line = self.inBuffer.substring(0, end);
|
|
|
|
self.inBuffer = self.inBuffer.substring(end + 1);
|
|
|
|
self.lineReceived(line);
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2016-04-25 16:43:28 -04:00
|
|
|
|
|
|
|
lineReceived(line) {
|
|
|
|
let self = this;
|
|
|
|
let parts = line.split(" ", 1);
|
|
|
|
if (parts[0] == "220") {
|
|
|
|
self.socket.write("HELO " + kServer + "\r\n");
|
|
|
|
} else if (parts[0] == "250") {
|
|
|
|
if (!self.sentFrom) {
|
|
|
|
self.socket.write("MAIL FROM: " + self.message.from + "\r\n");
|
|
|
|
self.sentFrom = true;
|
|
|
|
} else if (!self.sentTo) {
|
|
|
|
self.socket.write("RCPT TO: " + self.message.to + "\r\n");
|
|
|
|
self.sentTo = true;
|
|
|
|
} else if (!self.sentData) {
|
|
|
|
self.socket.write("DATA\r\n");
|
|
|
|
self.sentData = true;
|
|
|
|
} else {
|
|
|
|
self.socket.write("QUIT\r\n").then(self.resolve);
|
|
|
|
}
|
|
|
|
} else if (parts[0] == "354") {
|
|
|
|
self.socket.write("Subject: " + self.message.subject + "\r\n\r\n" + self.message.body + "\r\n.\r\n");
|
|
|
|
} else {
|
|
|
|
self.reject("Unexpected response: " + line);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:43:28 -04:00
|
|
|
function sendMail(message) {
|
|
|
|
return new Smtp().send(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
core.register("onInput", function(input) {
|
|
|
|
sendMail({
|
|
|
|
from: core.user.name + "@unprompted.com",
|
|
|
|
to: "test1@unprompted.com",
|
|
|
|
subject: input,
|
|
|
|
body: input,
|
|
|
|
}).then(function() {
|
|
|
|
terminal.print("sent");
|
|
|
|
}).catch(function(error) {
|
|
|
|
terminal.print("error: ", error);
|
2016-03-12 13:50:43 -05:00
|
|
|
});
|
2016-04-25 16:43:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
exports.sendMail = sendMail;
|