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;
function lineReceived(socket, line) { this.sentData = false;
terminal.print("> ", line);
let parts = line.split(" ", 1);
terminal.print(JSON.stringify(parts));
if (parts[0] == "220") {
socket.write("HELO rowlf.unprompted.com\r\n");
} else if (parts[0] == "250") {
if (!sentFrom) {
terminal.print("FROM");
socket.write("MAIL FROM: " + kFrom + "\r\n");
sentFrom = true;
} else if (!sentTo) {
terminal.print("TO");
socket.write("RCPT TO: " + kTo + "\r\n");
sentTo = true;
} else if (!sentData) {
terminal.print("DATA");
socket.write("DATA\r\n");
sentData = true;
} else {
terminal.print("QUIT");
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) { 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);
}
});
socket.connect("localhost", 25).catch(reject);
});
});
}
dataReceived(data) {
let self = this;
if (data === null) { if (data === null) {
return; return;
} }
terminal.print(data); self.inBuffer += data;
inBuffer += data;
let again = true; let again = true;
while (again) { while (again) {
again = false; again = false;
let end = inBuffer.indexOf("\n"); let end = self.inBuffer.indexOf("\n");
if (end != -1) { if (end != -1) {
again = true; again = true;
let line = inBuffer.substring(0, end); let line = self.inBuffer.substring(0, end);
inBuffer = inBuffer.substring(end + 1); self.inBuffer = self.inBuffer.substring(end + 1);
lineReceived(socket, line); 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 {
self.reject("Unexpected response: " + 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>