-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadminlistener.js
153 lines (129 loc) · 3.5 KB
/
adminlistener.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var url = require('url');
var fs = require('fs');
var http = require('http');
var keys = require('./keys');
var util = require('./apputils');
var db = require('./database');
var server;
var status;
var debug = util.debug;
exports.start = function() {
server = http.createServer(function(req, res) {
request(req, res);
});
server.listen(keys.httpPort);
status = true;
util.log("Listening for GET requests on port: " + keys.httpPort);
return db;
};
exports.getStatus = function() {
return status;
};
var request = function(req, res) {
var get = url.parse(req.url, true).query;
if (req.url === '/favicon.ico') {
res.writeHead(200, {'Content-Type': 'image/x-icon'} );
res.end();
return;
}
debug("GET: Request receieved");
res.writeHead(200); // Response header
if (!get["auth"]) {
debug("GET: Authenticaton token not set");
res.end("false");
return;
}
if (get["auth"] != keys.httpSecret) { // Authentication
debug("GET: Authentication token incorrect");
res.end("cannotauth");
return;
}
if (!get["action"]) {
debug("GET: Action not set");
res.end("Sorry, what?");
return;
}
if (!status) {
if (!(get["action"] == "setstatus" || get["auth"] == "getstatus")) {
debug("GET: AdminListener is disabled");
res.end("disabled");
return;
}
}
// ------------ setTwitter ------------ //
if (get["action"] == "settwitter") {
if (!(get["twitter"] && get["player"])) { // Not enough params
res.end("false");
return;
}
db.setTwitter(get["player"], get["twitter"].toLowerCase());
res.end("ok");
util.log("Set twitter: " + get["player"] + " - @" + get["twitter"]);
return;
}
// ------------ getTwitter ------------ //
if (get["action"] == "gettwitter") {
debug("* GET: action=gettwitter");
if (!get["player"]) {
debug("* GET: Player not set");
res.end("false");
return;
}
db.containsPlayer(get["player"], function (contains) {
if (!contains) {
debug("* GET: Player not found");
res.end("notfound");
} else {
db.getTwitter(get["player"], function(twitter) {
debug("* GET: Found twitter: " + twitter["twitter"]);
res.end(twitter["twitter"]);
});
}
});
return;
}
// ------------ delTwitter ------------ //
if (get["action"] == "deltwitter") {
if (!get["player"]) {
res.end("false");
return;
}
db.containsPlayer(get["player"], function (contains) {
if (!contains) {
res.end("notfound");
return;
}
});
db.deleteByPlayer(get["player"]);
res.end("ok");
util.log("Deleted twitter: " + get["player"] + " (" + get["twitter"] + ")");
return;
}
// ------------ setStatus ------------ //
if (get["action"] == "setstatus") {
if (!get["status"]) {
res.end("false");
return;
}
if (get["status"] == "enabled") {
status = true;
util.log("SuperAdmin Adder was re-enabled by remote host");
} else {
util.log("SuperAdmin Adder was disabled by remote host", "warning");
status = false;
}
res.end("ok");
return;
}
// ------------ getStatus ------------ //
if (get["action"] == "getstatus") {
if (status) {
res.end("enabled");
} else {
res.end("disabled");
}
return;
}
util.log("Unknown command issued: " + get["action"]);
res.end("Sorry, what?");
}