-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapi_server.js
More file actions
115 lines (98 loc) · 3.27 KB
/
api_server.js
File metadata and controls
115 lines (98 loc) · 3.27 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var http = require('http'),
sys = require('sys'),
querystring = require('querystring');
// Set listening port
var port = "8079";
// Set API Key
var api_key = "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX";
// Set Default From Email Address
var from = "noreply@yenn.co";
// Create the listening server
http.createServer(function(request, response)
{
sys.puts('Request for ' + request.url);
switch (request.url)
{
case '/':
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(
'<h1>Haraka API Server</h1>' +
'<p>You you can use this server by calling <strong>http://localhost:' + port + '/api</strong> and passing <strong>key</strong>, ' +
'<strong>to</strong>, <strong>from</strong> <i>(optional)</i>, <strong>subject</strong>, and <strong>body</strong> as the POST variables.</p>' +
'<form action="/api" method="post">' +
'Auth Key: <input type="password" name="key" /><br />' +
'To: <input type="text" name="to"><br />' +
'From: <input type="text" name="from"> <i>(optional)</i><br />' +
'Subject: <input type="text" name="subject"><br />' +
'Body: <textarea name="body" style="width: 500px;height: 250px;"></textarea><br />' +
'<input type="submit" value="Submit">' +
'</form>'
);
response.end();
break;
case '/api':
response.writeHead(200, {
'Content-Type': 'application/json'
});
post_handler(request, api_key, from,
function(post, api_key, from)
{
// Begin send mail
var outbound = require('./outbound');
// Grab Parent object
var plugin = this;
// Allow overriding of from address
if (post.from) var from = post.from;
var contents = [
"From: " + from,
"To: " + post.to,
"MIME-Version: 1.0",
"Content-Type: text/html; charset=ISO-8859-1",
"Subject: " + post.subject,
"",
post.body,
""].join("\n");
var outnext = function(code, msg) {
switch (code) {
case DENY:
var output = "Sending mail failed: " + msg;
break;
case OK:
var output = "mail sent";
break;
default:
var output = "Unrecognised return code from sending email: " + msg;
break;
}
response.write(output);
response.end();
};
if (post.key === api_key)
outbound.send_email(from, post.to, contents, outnext);
else response.write(sys.inspect("API Key was invalid."));
});
break;
};
}).listen(port);
// Grab post data
function post_handler(request, api_key, from, callback)
{
var _REQUEST = {};
var _CONTENT = '';
if (request.method == 'POST')
{
request.addListener('data',
function(chunk)
{
_CONTENT += chunk;
});
request.addListener('end',
function()
{
_REQUEST = querystring.parse(_CONTENT);
callback(_REQUEST, api_key, from);
});
};
};