Make smtp require-able, and add some better credentials checks for users who aren't logged in at all.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3218 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2016-04-25 20:43:28 +00:00
parent a3524b761b
commit 8ae1d23763
3 changed files with 90 additions and 63 deletions

View File

@ -4,71 +4,96 @@
terminal.print("Hello, world!"); terminal.print("Hello, world!");
let kFrom = core.user.name + "@unprompted.com"; let kServer = "rowlf.unprompted.com";
let kTo = "test@unprompted.com";
let kSubject = "Hello, world!";
let kBody = "This is the body of the email."
let inBuffer = ""; class Smtp {
let sentFrom = false; constructor() {
let sentTo = false; this.inBuffer = "";
let sentData = false; this.sentFrom = false;
this.sentTo = false;
this.sentData = false;
}
function lineReceived(socket, line) { send(message) {
terminal.print("> ", line); let self = this;
let parts = line.split(" ", 1); self.message = message;
terminal.print(JSON.stringify(parts)); return new Promise(function(resolve, reject) {
if (parts[0] == "220") { self.resolve = resolve;
socket.write("HELO rowlf.unprompted.com\r\n"); self.reject = reject;
} else if (parts[0] == "250") { network.newConnection().then(function(socket) {
if (!sentFrom) { self.socket = socket;
terminal.print("FROM"); socket.read(function(data) {
socket.write("MAIL FROM: " + kFrom + "\r\n"); try {
sentFrom = true; self.dataReceived(data);
} else if (!sentTo) { } catch (error) {
terminal.print("TO"); reject(error.message);
socket.write("RCPT TO: " + kTo + "\r\n"); }
sentTo = true; });
} else if (!sentData) { socket.connect("localhost", 25).catch(reject);
terminal.print("DATA"); });
socket.write("DATA\r\n"); });
sentData = true; }
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);
}
}
}
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 { } else {
terminal.print("QUIT"); self.reject("Unexpected response: " + line);
socket.write("QUIT\r\n");
}
} else if (parts[0] == "354") {
terminal.print("MESSAGE");
socket.write("Subject: " + kSubject + "\r\n\r\n" + kBody + "\r\n.\r\n");
}
}
function dataReceived(socket, data) {
if (data === null) {
return;
}
terminal.print(data);
inBuffer += data;
let again = true;
while (again) {
again = false;
let end = inBuffer.indexOf("\n");
if (end != -1) {
again = true;
let line = inBuffer.substring(0, end);
inBuffer = inBuffer.substring(end + 1);
lineReceived(socket, line);
} }
} }
} }
network.newConnection().then(function(socket) { function sendMail(message) {
socket.read(function(data) { return new Smtp().send(message);
try { }
dataReceived(socket, data);
} catch (error) { core.register("onInput", function(input) {
terminal.print("ERROR: ", error.message); 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);
}); });
socket.connect("localhost", 25);
}); });
exports.sendMail = sendMail;

View File

@ -5,7 +5,8 @@ function fileList(settings) {
terminal.setEcho(false); terminal.setEcho(false);
terminal.clear(); terminal.clear();
terminal.print(settings.title); terminal.print(settings.title);
if (core.user.credentials.permissions if (core.user.credentials
&& core.user.credentials.permissions
&& core.user.credentials.permissions.authenticated) { && core.user.credentials.permissions.authenticated) {
terminal.print({command: "new"}); terminal.print({command: "new"});
} }
@ -80,7 +81,8 @@ function fileList(settings) {
}); });
for (var i = 0; i < entries.length; i++) { for (var i = 0; i < entries.length; i++) {
if (entries[i].substring(0, prefix.length) == prefix) { if (entries[i].substring(0, prefix.length) == prefix) {
if (core.user.credentials.permissions if (core.user.credentials
&& core.user.credentials.permissions
&& core.user.credentials.permissions.authenticated) { && core.user.credentials.permissions.authenticated) {
terminal.print( terminal.print(
"* ", "* ",

View File

@ -109,7 +109,7 @@ function editPage(event) {
<body> <body>
<div id="menu"> <div id="menu">
<input type="button" value="Back" onclick="index()"> <input type="button" value="Back" onclick="index()">
` + (core.user.credentials.permissions.authenticated ? ` ` + (core.user.credentials.permissions && core.user.credentials.permissions.authenticated ? `
<input type="button" value="Save" onclick="submit()"> <input type="button" value="Save" onclick="submit()">
` : "") + ` : "") +
` <a target="_top" href="https://casual-effects.com/markdeep/">Markdeep</a> ` <a target="_top" href="https://casual-effects.com/markdeep/">Markdeep</a>