This repository was archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtipbot.js
453 lines (390 loc) · 16.4 KB
/
tipbot.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
var irc = require('irc'),
winston = require('winston'),
fs = require('fs'),
yaml = require('js-yaml'),
coin = require('node-dogecoin'),
webadmin = require('../lib/webadmin/app');
var Cryptsy = require('cryptsy');
// check if the config file exists
if(!fs.existsSync('./config/config.yml')) {
winston.error('Configuration file doesn\'t exist! Please read the README.md file first.');
process.exit(1);
}
// handle sigint
process.on('exit', function() {
winston.info('Exiting...');
if(client != null) {
client.disconnect('My master ordered me to leave.');
}
});
// load settings
var settings = yaml.load(fs.readFileSync('./config/config.yml', 'utf-8'));
// Load Cryptsy
var cryptsy = new Cryptsy(settings.cryptsy.key, settings.cryptsy.secret);
// load winston's cli defaults
winston.cli();
// write logs to file
if(settings.log.file) {
winston.add(winston.transports.File, {
filename: settings.log.file,
level: 'debug'});
}
// connect to coin json-rpc
winston.info('Connecting to coind...');
var coin = coin({
host: settings.rpc.host,
port: settings.rpc.port,
user: settings.rpc.user,
pass: settings.rpc.pass
});
coin.getBalance(function(err, balance) {
if(err) {
winston.error('Could not connect to %s RPC API! ', settings.coin.full_name, err);
process.exit(1);
return;
}
var balance = typeof(balance) == 'object' ? balance.result : balance;
winston.info('Connected to JSON RPC API. Current total balance is %d' + settings.coin.short_name, balance);
})
// run webadmin
if(settings.webadmin.enabled)
{
winston.info('Running webadmin on port %d', settings.webadmin.port);
webadmin.app(settings.webadmin.port, coin, settings, winston);
}
// connect to the server
winston.info('Connecting to the server...');
var client = new irc.Client(settings.connection.host, settings.login.nickname, {
port: settings.connection.port,
secure: settings.connection.secure,
userName: settings.login.username,
realName: settings.login.realname,
debug: settings.connection.debug
});
// gets user's login status
irc.Client.prototype.isIdentified = function(nickname, callback) {
// request login status
this.say('NickServ', 'ACC ' + nickname);
// wait for response
var listener = function(from, to, message) {
// proceed only on NickServ's ACC response
var regexp = new RegExp('^(\\S+) ACC (\\d)');
if(from != undefined && from.toLowerCase() == 'nickserv' && regexp.test(message)) {
var match = message.match(regexp);
var user = match[1];
var level = match[2];
// if the right response, call the callback and remove this listener
if(user.toLowerCase() == nickname.toLowerCase()) {
callback(level == 3);
this.removeListener('notice', listener);
}
}
}
this.addListener('notice', listener);
}
irc.Client.prototype.getNames = function(channel, callback) {
client.send('NAMES', channel);
var listener = function(nicks) {
var names = [];
for(name in nicks) {
names.push(name);
}
callback(names);
this.removeListener('names' + channel, listener);
}
this.addListener('names' + channel, listener);
}
irc.Client.prototype.getAddress = function(nickname, callback) {
winston.debug('Requesting address for %s', nickname);
coin.send('getaccountaddress', settings.rpc.prefix + nickname.toLowerCase(), function(err, address) {
if(err) {
winston.error('Something went wrong while getting address. ' + err);
callback(err);
return false;
}
callback(false, address);
});
}
String.prototype.expand = function(values) {
var global = {
nick: client.nick
}
return this.replace(/%([a-zA-Z_]+)%/g, function(str, variable) {
return typeof(values[variable]) == 'undefined' ?
(typeof(settings.coin[variable]) == 'undefined' ?
(typeof(global[variable]) == 'undefined' ?
str : global[variable]) : settings.coin[variable]) : values[variable];
});
}
// basic handlers
client.addListener('registered', function(message) {
winston.info('Connected to %s.', message.server);
client.say('NickServ', 'IDENTIFY ' + settings.login.nickserv_password);
});
client.addListener('error', function(message) {
winston.error('Received an error from IRC network: ', message);
});
var last_active = {};
client.addListener('message', function(from, channel, message) {
last_active[from] = Date.now();
var match = message.match(/^(!?)(\S+)/);
if(match == null) return;
var prefix = match[1];
var command = match[2];
if(settings.commands[command]) {
if(channel == client.nick && settings.commands[command].pm === false) return;
if(channel != client.nick && (settings.commands[command].channel === false || prefix != '!')) return;
} else {
return;
}
// if pms, make sure to respond to pms instead to itself
if(channel == client.nick) channel = from;
// comands that don't require identifying
if(command == 'help' || command == 'terms') {
var msg = [];
for(var i = 0; i < settings.messages[command].length; i++) {
client.say(from, settings.messages[command][i].expand({}));
}
return;
}
// if not that, message will be undefined for some reason
// todo: find a fix for that
var msg = message;
client.isIdentified(from, function(status) {
var message = msg;
// check if the sending user is logged in (identified) with nickserv
if(!status) {
winston.info('%s tried to use command `%s`, but is not identified.', from, message);
client.say(channel, settings.messages.not_identified.expand({name: from}));
return;
}
switch(command) {
case 'rain':
var match = message.match(/^.?rain (random)?([\d\.]+) ?(\d+)?/);
if(match == null || !match[2]) {
client.say(channel, 'Usage: !rain <amount> [max people]');
return;
}
var random = match[1];
var amount = Number(match[2]);
var max = Number(match[3]);
if(isNaN(amount)) {
client.say(channel, settings.messages.invalid_amount.expand({name: from, amount: match[2]}));
return;
}
if(random) {
var min = settings.coin.min_rain;
var maxAmount = amount;
amount = Math.floor(Math.random() * (maxAmount - min + 1)) + min;
}
if(isNaN(max) || max < 1) {
max = false;
} else {
max = Math.floor(max);
}
coin.getBalance(settings.rpc.prefix + from.toLowerCase(), settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !tip command.', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
var balance = typeof(balance) == 'object' ? balance.result : balance;
if(balance >= amount) {
client.getNames(channel, function(names) {
// rain only on nicknames active within the last x seconds
if(settings.commands.rain.rain_on_last_active) {
for (var i = names.length - 1; i >= 0; i--) {
if(!last_active.hasOwnProperty(names[i]) || last_active[names[i]] + settings.commands.rain.rain_on_last_active * 1000 < Date.now()) {
names.splice(i, 1);
}
};
}
// remove tipper from the list
names.splice(names.indexOf(from), 1);
// shuffle the array
for(var j, x, i = names.length; i; j = Math.floor(Math.random() * i), x = names[--i], names[i] = names[j], names[j] = x);
max = max ? Math.min(max, names.length) : names.length;
if(max == 0) return;
var whole_channel = false;
if(max == names.length) whole_channel = true;
names = names.slice(0, max);
if(amount / max < settings.coin.min_rain) {
client.say(channel, settings.messages.rain_too_small.expand({from: from, amount: amount, min_rain: settings.coin.min_rain * max}));
return;
}
for (var i = 0; i < names.length; i++) {
coin.move(settings.rpc.prefix + from.toLowerCase(), settings.rpc.prefix + names[i].toLowerCase(), amount / max, function(err, reply) {
if(err || !reply) {
winston.error('Error in !tip command', err);
return;
}
});
}
client.say(channel, settings.messages.rain.expand({name: from, amount: amount / max, list: (whole_channel && !settings.commands.rain.rain_on_last_active) ? 'the whole channel' : names.join(', ')}));
});
} else {
winston.info('%s tried to tip %s %d, but has only %d', from, to, amount, balance);
client.say(channel, settings.messages.no_funds.expand({name: from, balance: balance, short: amount - balance, amount: amount}));
}
})
break;
case 'price':
cryptsy.api('singlemarketdata', { marketid: 200 }, function (err, data) {
if(err) {
winston.error('Error in !price command.', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
winston.info('Fetched MYR Price From Cryptsy')
client.say(channel, settings.messages.price.expand({amount: data}));
});
break;
case 'tip':
var match = message.match(/^.?tip (\S+) (random)?([\d\.]+)/);
if(match == null || match.length < 3) {
client.say(channel, 'Usage: !tip <nickname> <amount>')
return;
}
var to = match[1];
var random = match[2];
var amount = Number(match[3]);
if(isNaN(amount)) {
client.say(channel, settings.messages.invalid_amount.expand({name: from, amount: match[3]}));
return;
}
if(random) {
var min = settings.coin.min_tip;
var max = amount;
amount = Math.floor(Math.random() * (max - min + 1)) + min;
}
if(to.toLowerCase() == from.toLowerCase()) {
client.say(channel, settings.messages.tip_self.expand({name: from}));
return;
}
if(amount < settings.coin.min_tip) {
client.say(channel, settings.messages.tip_too_small.expand({from: from, to: to, amount: amount}));
return;
}
// check balance with min. 5 confirmations
coin.getBalance(settings.rpc.prefix + from.toLowerCase(), settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !tip command.', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
var balance = typeof(balance) == 'object' ? balance.result : balance;
if(balance >= amount) {
coin.send('move', settings.rpc.prefix + from.toLowerCase(), settings.rpc.prefix + to.toLowerCase(), amount, function(err, reply) {
if(err || !reply) {
winston.error('Error in !tip command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
winston.info('%s tipped %s %d%s', from, to, amount, settings.coin.short_name)
client.say(channel, settings.messages.tipped.expand({from: from, to: to, amount: amount}));
});
} else {
winston.info('%s tried to tip %s %d, but has only %d', from, to, amount, balance);
client.say(channel, settings.messages.no_funds.expand({name: from, balance: balance, short: amount - balance, amount: amount}));
}
});
break;
case 'address':
var user = from.toLowerCase();
client.getAddress(user, function(err, address) {
if(err) {
winston.error('Error in !address command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
client.say(channel, settings.messages.deposit_address.expand({name: user, address: address}));
});
break;
case 'balance':
var user = from.toLowerCase();
coin.getBalance(settings.rpc.prefix + user, settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !balance command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
var balance = typeof(balance) == 'object' ? balance.result : balance;
coin.getBalance(settings.rpc.prefix + user, 0, function(err, unconfirmed_balance) {
if(err) {
winston.error('Error in !balance command', err);
client.say(channel, settings.messages.balance.expand({balance: balance, name: user}));
return;
}
var unconfirmed_balance = typeof(unconfirmed_balance) == 'object' ? unconfirmed_balance.result : unconfirmed_balance;
client.say(channel, settings.messages.balance_unconfirmed.expand({balance: balance, name: user, unconfirmed: unconfirmed_balance - balance}));
})
});
break;
case 'withdraw':
var match = message.match(/^.?withdraw (\S+)$/);
if(match == null) {
client.say(channel, 'Usage: !withdraw <' + settings.coin.full_name + ' address>');
return;
}
var address = match[1];
coin.validateAddress(address, function(err, reply) {
if(err) {
winston.error('Error in !withdraw command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
if(reply.isvalid) {
coin.getBalance(settings.rpc.prefix + from.toLowerCase(), settings.coin.min_confirmations, function(err, balance) {
if(err) {
winston.error('Error in !withdraw command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
var balance = typeof(balance) == 'object' ? balance.result : balance;
if(balance < settings.coin.min_withdraw) {
winston.warn('%s tried to withdraw %d, but min is set to %d', from, balance, settings.coin.min_withdraw);
client.say(channel, settings.messages.withdraw_too_small.expand({name: from, balance: balance}));
return;
}
coin.sendFrom(settings.rpc.prefix + from.toLowerCase(), address, balance - settings.coin.withdrawal_fee, function(err, reply) {
if(err) {
winston.error('Error in !withdraw command', err);
client.say(channel, settings.messages.error.expand({name: from}));
return;
}
var values = {name: from, address: address, balance: balance, amount: balance - settings.coin.withdrawal_fee, transaction: reply}
for(var i = 0; i < settings.messages.withdraw_success.length; i++) {
var msg = settings.messages.withdraw_success[i];
client.say(channel, msg.expand(values));
};
// transfer the rest (withdrawal fee - txfee) to bots wallet
coin.getBalance(settings.rpc.prefix + from.toLowerCase(), function(err, balance) {
if(err) {
winston.error('Something went wrong while transferring fees', err);
return;
}
var balance = typeof(balance) == 'object' ? balance.result : balance;
// moves the rest to bot's wallet
coin.move(settings.rpc.prefix + from.toLowerCase(), settings.rpc.prefix + settings.login.nickname.toLowerCase(), balance);
});
});
});
} else {
winston.warn('%s tried to withdraw to an invalid address', from);
client.say(channel, settings.messages.invalid_address.expand({address: address, name: from}));
}
});
break;
}
});
});
client.addListener('notice', function(nick, to, text, message) {
if(nick && nick.toLowerCase() == 'nickserv' && !text.match(/ ACC /)) {
winston.info('%s: %s', nick, text);
if(text.match(/^You are now identified/)) {
for (var i = settings.channels.length - 1; i >= 0; i--) {
client.join(settings.channels[i]);
};
}
}
});