forked from owenconti/livecodingtv-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.js
285 lines (248 loc) · 7.53 KB
/
Client.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use strict';
const xmpp = require('node-xmpp');
const crypto = require('crypto');
const Log = require('./utils/Log');
const runtime = require('./utils/Runtime');
const User = require('./model/User');
class Client {
/**
* Connect to a room
* @param {object} credentials
* @return {void}
*/
constructor( credentials ) {
this.credentials = credentials;
// Connect to the server
this.client = new xmpp.Client({
jid: this.credentials.jid,
password: this.credentials.password
});
this.client.on('error', function(err) {
console.log('CLIENT ERROR: ', err);
});
// Once online, send presence to the room
this.client.on('online', function( resp ) {
Log.log( 'Connected to server' );
this.sendPresence();
}.bind( this ) );
}
/**
* Sends the bot's presence to the room specified.
* @return {void}
*/
sendPresence() {
this.client.send(
new xmpp.Element('presence', {
to: this.credentials.roomJid + '/' + this.credentials.username
})
);
}
/**
* Sends a message to the specified room.
* @param {string} msg
* @param {string} room
* @return {void}
*/
sendMessage( msg ) {
if ( runtime.debug ) {
Log.log('DEBUGGING: ' + msg);
return false;
}
// Get the previously sent messages
let messages = runtime.brain.get('messages') || {};
// Hash the message and use it as our key.
// Grab the previous message that uses the same hash.
// (ie: the message text is the same).
// Build the new message object.
let hash = crypto.createHash('md5').update( msg ).digest('hex');
let previousMessage = messages[ hash ];
let messageObj = {
message: msg,
time: new Date().getTime()
};
// Compare the previous message time vs the current message time
// Only send the message to the server, if the difference is > 5 seconds
if ( !previousMessage || messageObj.time - previousMessage.time > 5000 ) { // 5 seconds
this.client.send(
new xmpp.Element('message', {
to: this.credentials.roomJid,
type: 'groupchat'
})
.c('body')
.t( msg )
);
} else {
Log.log( 'Skipping sendMessage - previous message sent within 5 seconds' );
}
// Save the message to the messages store
messages[ hash ] = messageObj;
runtime.brain.set( 'messages', messages );
}
/**
* Replies to the specified user.
* @param {string} username
* @param {string} msg
* @return {void}
*/
replyTo( username, msg ) {
this.sendMessage( '@' + username + ': ' + msg );
}
/**
* Listens for messages and calls the passed-in callback.
* @param {function} action
* @return {void}
*/
listen( action ) {
this.client.on('stanza', function( stanza ) {
action( stanza );
});
}
/**
* Returns the user based on the specified username.
* @param {string} username
* @return {object}
*/
getUser( username ) {
const users = runtime.brain.get( 'users' );
let userObj = users[ username ] || {};
return new User( userObj );
}
/**
* Parses a stanza from the server
* @param {Stanza} stanza
* @param {obj} credentials
* @return {obj}
*/
static parseStanza( stanza, credentials ) {
var type = stanza.name;
switch( type ) {
case 'message':
return Client.parseMessage( stanza, credentials );
case 'presence':
return Client.parsePresence( stanza, credentials );
}
}
/**
* Parses the passed-in 'message' stanza.
* @param {Stanza} stanza
* @param {obj} credentials
* @return {obj}
*/
static parseMessage( stanza, credentials ) {
var type = 'message';
var rateLimited = false;
let jid = stanza.attrs.from;
let username = jid.substr( jid.indexOf( '/' ) + 1 );
var body = Client.findChild( 'body', stanza.children );
var message = body.children.join('').replace('\\', '');
// Limit users to only run commands once every 5 seconds
const now = new Date().getTime();
let messages = runtime.brain.get( 'userMessages' ) || {};
let userMessageLog = messages[ username ];
if ( !userMessageLog ) {
userMessageLog = {
messageTimes: [],
lastCommandTime: 0
};
}
// If the user's most recent command was within 5 seconds,
// return false and all commands will be skipped.
if ( username !== credentials.username ) {
// Only rate limit users who are not the bot :)
if ( userMessageLog.lastCommandTime > 0 && now - userMessageLog.lastCommandTime < 5000 ) { // 5 seconds
rateLimited = true;
}
}
// Push 'now' time into the user's message times
userMessageLog.messageTimes.push( now );
// Update the message log for the user
messages[ username ] = userMessageLog;
runtime.brain.set( 'userMessages', messages );
let users = runtime.brain.get( 'users' ) || {};
let userObj = users[ username ] || {
username: username
};
let user = new User( userObj );
// Return the parsed message
return { type, user, message, rateLimited };
}
/**
* Parses the passed-in 'presence' stanza.
* @param {Stanza} stanza
* @param {obj} credentials
* @return {obj}
*/
static parsePresence( stanza, credentials) {
let type = 'presence';
let jid = stanza.attrs.from;
let username = jid.substr( jid.indexOf( '/' ) + 1 );
let message = stanza.attrs.type || 'available';
// Find role
let xObj = Client.findChild( 'x', stanza.children );
let itemObj = Client.findChild( 'item', xObj.children );
let role = itemObj.attrs.role;
// Store new users in the 'users' brain object
let users = runtime.brain.get( 'users' ) || {};
let userObj = users[ username ];
if ( !userObj ) {
// New viewer
userObj = {
username: username,
count: 1,
time: new Date().getTime(),
role: role
};
} else {
// Update the user's view count and presence time
// only if their count hasn't been updated in
// the last 10 minutes.
const now = new Date().getTime();
const minutes = 10;
if ( now - userObj.time > 1000 * 60 * minutes ) {
userObj.count++;
userObj.time = now;
}
}
if ( !userObj.status ) {
userObj.status = 'Viewer';
}
let user = new User( userObj );
// If presence is unavailable,
// return without storing user object
if ( message === 'unavailable' ) {
return { type, user, message, role };
}
users[ user.username ] = userObj;
runtime.brain.set( 'users', users );
return { type, user, message, role };
}
/**
* [updateLatestCommandLog description]
* @param {Stanza} stanza
* @return {void}
*/
static updateLatestCommandLog( stanza ) {
let messages = runtime.brain.get( 'userMessages' ) || {};
let userMessageLog = messages[ stanza.user.username ] || {};
userMessageLog.lastCommandTime = new Date().getTime();
runtime.brain.set( 'userMessages', messages );
}
/**
* Child a child based on the 'name' property
* @param {[type]} name [description]
* @param {[type]} children [description]
* @return {[type]} [description]
*/
static findChild( name, children ) {
var result = null;
for ( var index in children ) {
var child = children[ index ];
if ( child.name === name ) {
result = child;
break;
}
}
return result;
}
}
module.exports = Client;