-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (146 loc) · 5.53 KB
/
index.js
File metadata and controls
178 lines (146 loc) · 5.53 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var request = require('request');
var deasync = require('deasync');
/**
* Event handler callback.
* @callback eventHandler
* @param {Message[]} messages Array of messages.
*/
/**
* A message received from the API.
* @typedef {Object} Message
* @property {String} id Message ID.
* @property {Number} t Unix timestamp (in milliseconds).
* @property {String} from_user User who sent the message.
* @property {String} msg Message.
* @property {String} to_user User who received the message.
* @property {String} [channel] Channel the message was sent to, if not a tell.
*/
class Client {
/**
* Creates a Client.
* @param {string} auth A token or a password.
* @constructor
*/
constructor(auth) {
this._poll = this._poll.bind(this);
this.subscribe = this.subscribe.bind(this);
this.unsubscribe = this.unsubscribe.bind(this);
this.getToken = this.getToken.bind(this);
this.getUsers = this.getUsers.bind(this);
this.send = this.send.bind(this);
this.tell = this.tell.bind(this);
this.token = auth;
if (auth.length == 5) this.token = this.getToken(auth);
var acctCall = this.getUsers();
this.users = Object.keys(acctCall);
this.channel = this.users.reduce((a,x) => {a[x] = Object.keys(acctCall[x]); return a}, {});
this.last_poll = Date.now();
this.interval_id = setInterval(this._poll, 1500);
this.msgs_after = Date.now()/1000;
this.poll_handlers = [];
}
/**
* Message checker. DO NOT CALL THIS DIRECTLY.
*/
_poll() {
if (Date.now() - this.last_poll < 1000) return;
if (this.poll_handlers.length == 0) return;
var done = false;
var chats;
request.post("https://www.hackmud.com/mobile/chats.json", {json: {chat_token: this.token, after: this.msgs_after+0.1, usernames: this.users}}, function (err, response, body) {
if (err) return; // Handle netowk errors faithfully.
if (!body.ok) throw body.msg;
done = true;
chats = body.chats;
});
deasync.loopWhile(() => !done);
var messages = Object.keys(chats).map(x=>chats[x].map(y=>{y.to_user = y.to_user ? y.to_user : x; y.t*=1000; return y;})).reduce((a,x)=>{a.push(...x);return a;}, []).filter((y,i,s)=>s.findIndex(z => z.id == y.id) == i).sort((a,b) => b.t - a.t);
if (messages.length > 0) {
this.msgs_after = messages[0].t/1000;
this.poll_handlers.forEach(x=>x(messages));
}
this.last_poll = Date.now();
}
/**
* Subscribes an event handler to new messages.
* @param {eventHandler} handler Event handler.
* @param {String[]} [users] User receivers to subscribe to.
*/
subscribe(handler,users) {
return this.poll_handlers.push(x=>handler(x.filter(y=>users?users.includes(y.to_user):true)))-1;
}
/**
* Unsubscribes from new messages.
* @param {Number} index Index to delete.
*/
unsubscribe(index) {
return this.poll_handlers.splice(index, 0);
}
/**
* Retrieves a token from a password.
* @param {string} pass A password from chat_pass.
* @returns {string} The retrieved token from the password.
*/
getToken(pass) {
var done = false;
var token = "";
request.post("https://www.hackmud.com/mobile/get_token.json", {json: {pass}}, function (err, response, body) {
if (err) throw err;
if (!body.ok) throw body.msg;
token = body.chat_token;
done = true;
})
deasync.loopWhile(() => !done);
return token;
}
/**
* Retrieves users.
*/
getUsers() {
var done = false;
var res = null;
request.post("https://www.hackmud.com/mobile/account_data.json", {json: {chat_token: this.token}}, function (err, response, body) {
if (err) throw err;
if (!body.ok) throw body.msg;
res = body.users;
done = true;
})
deasync.loopWhile(() => !done);
return res;
}
/**
* Sends a message to a channel.
* @param {string} username Username to send message from.
* @param {string} channel Channel to send message to.
* @param {string} msg Message to send.
*/
send(username, channel, msg) {
var done = false;
var xerr = null;
request.post("https://www.hackmud.com/mobile/create_chat.json", {json: {chat_token: this.token, username, channel, msg}}, function (err, response, body) {
if (err) xerr = err;
if (!body.ok) xerr = body.msg;
done = true;
})
deasync.loopWhile(() => !done);
return xerr;
}
/**
* Sends a message to a user.
* @param {string} username Username to send message from
* @param {string} tell Username to send message to
* @param {string} msg Message to send
*/
tell(username, tell, msg) {
var done = false;
var xerr = null;
request.post("https://www.hackmud.com/mobile/create_chat.json", {json: {chat_token: this.token, username, tell, msg}}, function (err, response, body) {
if (err) xerr = err;
if (!body.ok) xerr = body.msg;
done = true;
})
deasync.loopWhile(() => !done);
return xerr;
}
}
module.exports = Client;