-
Notifications
You must be signed in to change notification settings - Fork 0
/
workbot.js
585 lines (524 loc) · 15.8 KB
/
workbot.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//Hello, so this would be where you create new commands
var credentials = require('./credentials.json')
function StateMachine(states) {
this.states = states;
this.indexes = {};
for (var i = 0; i < this.states.length; i++) {
this.indexes[this.states[i].name] = i;
if (this.states[i].initial) {
this.currentState = this.states[i];
}
}
};
StateMachine.prototype.consumeEvent = function(e) {
if (this.currentState.events[e]) {
this.currentState = this.states[this.indexes[this.currentState.events[e]]];
}
}
StateMachine.prototype.getStatus = function() {
return this.currentState.name;
}
function workshopBot(chatbot, omeglebot) {
//creating the !roll command
chatbot.createCommand('roll', /^(-?\d+)[ ,](-?\d+)/, function(min, max) {
this.send('Rolling: ' + (Math.round(Math.random() * (+max - min)) + +min));
});
//creating the !hello command
chatbot.createCommand('hello', /(?:)/, function() {
this.send('hello there ' + (this.currentUser ? this.currentUser : ""));
});
(function() {
var streams = {}
chatbot.createCommand('saveChat', /(\S+)/, function(name) {
streams[name] = this.stream;
});
chatbot.createCommand('viewSavedChats', /(?:)/, function(name) {
this.send(JSON.stringify(Object.keys(streams)))
});
chatbot.createCommand('spyChat', /(\S+)/, function(name) {
streams[name].connect().pipe(JSON.stringify).pipe(this.stream.connect())
});
}())
//creating the omegle command
var omegleConnections;
chatbot.createCommand('omegle', /(^(?:[^ ])+) ?(.*)/, function(command, rest) {
var that = this;
if (!that.state) {
that.state = new StateMachine([{
'name': 'disconnected',
'initial': true,
'events': {
'connect': 'connected'
}
}, {
'name': 'connected',
'events': {
'disconnect': 'disconnected'
}
}]);
}
function bindOmegleWithChatbot(inp) {
if (that.omeglebotC) {
that.send('There is already a current omegle connection! Close this one first.'); //Or use multi user o.o
return false
}
that.omeglebotC = omeglebot();
that.state.consumeEvent("connect");
that.omeglebotC.event.on('out', function(message) {
that.send('(omegle)' + message);
});
if (inp) {
var c = that.stream.connect();
// debugger;
c.on('out', function(msg) {
var message, user;
if (typeof msg == 'string') {
message = msg;
user = null;
} else {
message = msg.message;
user = msg.user;
}
if (message.indexOf('(omegle)') !== 0)
that.omeglebotC.event.in((user ? '<' + user + '>' : '') + message);
});
}
that.omeglebotC.event.on('close', function() {
if (inp) {
c.close();
}
that.state.consumeEvent("disconnect");
that.omeglebotC = null;
});
return true
}
if (that.state.getStatus() == "connected") {
switch (command) {
case 'restart':
that.omeglebotC.disconnect()
if (bindOmegleWithChatbot(true)) {
var regexResult = rest.match(/(?:(spy)(?: |$))?(?:lang=(.{2})(?: |$))?(.*)/);
that.omeglebotC.startConversation(regexResult[1] == "spy", regexResult[2], regexResult[3] && regexResult[3].split(','));
}
break;
case 'cap':
that.omeglebotC.cap()
break;
case 'solve':
that.omeglebotC.send(rest)
break;
case 'stop':
that.omeglebotC.disconnect()
break;
default:
this.send("The bot is currently connected, valid commands are: stop, solve, cap, restart")
}
} else if (that.state.getStatus() == "disconnected") {
switch (command) {
case 'restart':
case 'start':
if (bindOmegleWithChatbot(true)) {
var regexResult = rest.match(/(?:(spy)(?: |$))?(?:lang=(.{2})(?: |$))?(.*)/);
that.omeglebotC.startConversation(regexResult[1] == "spy", regexResult[2], regexResult[3] && regexResult[3].split(','));
}
break;
case 'ask':
if (bindOmegleWithChatbot(false)) {
var regexResult = rest.match(/(?:(.*?))?(?:lang=(.{2}))?$/)
that.omeglebotC.ask(regexResult[1], regexResult[2]);
}
break;
default:
this.send("The bot is currently disconnected, valid commands are: start, ask, restart")
}
}
});
}
function multibotConnection() {
}
//I have no idea how it got so big; you don't have to read this if you don't want to...
//Giving input receiving output throgh events.
function eventualIO(input) {
var that = this;
this.connections = [];
this.fire = function(event, data) {
this[event](data)
} //deprecated?
this.input = function(inp) {
this.fire("in", inp);
input(inp);
};
}
eventualIO.fromFunction = function(func) {
var e = new eventualIO(write);
function write(data) {
e.out(func(data))
}
return e.connect();
};
(function() {
var eventTypes = ['out', 'err', 'close', 'connect', 'in'];
eventTypes.forEach(function(event) {
eventualIO.prototype[event] = function(d) {
this.connections.forEach(function(each) {
each.fire(event, d);
});
};
});
}());
eventualIO.prototype.connect = function() {
var con = new eventualIOconnection(this, {})
this.connections.push(con);
return con;
};
function eventualIOconnection(parent, listeners) {
this.connectionConstructor = parent;
this.listeners = listeners;
}
eventualIOconnection.prototype.in = function(input) {
this.connectionConstructor.input(input);
};
eventualIOconnection.prototype.on = function(event, callback) {
if (!this.listeners[event]) this.listeners[event] = [callback];
else this.listeners[event].push(callback);
return function removeEventListener() {
this.listeners[event].splice(this.listeners[event].indexOf(callback), 1);
};
};
//callback will be called like the map function, the input of 1 stream will be the arguments of the callback and whatever the callback returns will be the output of the other stream
eventualIOconnection.prototype.pipe = function(stream) {
if (typeof stream === "function") {
stream = eventualIO.fromFunction(stream);
}
this.on("out", function(output) {
stream.in(output);
})
stream.on("close", function() {
this.close()
});
return stream;
}
eventualIOconnection.prototype.bind = function(stream) {
if (stream.constructor == this.constructor) {
try {
this.pipe(stream);
stream.pipe(this);
return true
} catch (e) {
return false
}
}
}
eventualIOconnection.prototype.fire = function(event, data) {
this.listeners[event] && this.listeners[event].forEach(function(ev) {
ev(data);
})
}
eventualIOconnection.prototype.close = function() {
this.fire("close");
var splice = this.connectionConstructor.connections.indexOf(this);
if (splice > -1)
this.connectionConstructor.connections.splice(splice, 1);
else console.warn("Close executed twice!?");
};
var chatbot = (function() {
//debugger;
var commands = {},
chatbotExport = {
createCommand: createCommand,
commands: commands,
session: function(stream) {
var streamC = stream.connect();
var asdf = {
bot: chatbotExport,
send: function(data) {
streamC.in(data)
},
stream: stream
}
streamC.on("out", function(data) {
read(data, asdf)
})
return asdf;
}
};
function read(query, session) {
var message;
if (typeof query == 'string') {
message = query;
session.currentUser = null;
} else {
message = query.message;
session.currentUser = query.user;
}
var parseResult = message.match(/^!([\w_-]+)/);
if (parseResult && parseResult[1]) {
var e;
try {
callCommand(parseResult[1], message.substring(parseResult[1].length + 2), session);
} catch (e) {
session.send("Error in command (" + message + "):" + e);
}
}
}
function callCommand(command, parameters, session) {
var result,
d = commands[command];
if (d) {
var regexResult = parameters.match(d.regex);
if (regexResult) {
return d.callback.apply(session, Array.prototype.slice.call(regexResult, 1));
}
}
}
function createCommand(command, regexpparameter, callback) {
commands[command] = {
regex: regexpparameter,
callback: callback,
};
}
return chatbotExport;
}());
var omegleBot = (function() {
function write(t) {
omegleConversation.context && omegleConversation.context.sendMessage(t)
}
var userConnection = require('./userConnection.js');
var omegleConversation = new eventualIO(write);
omegleConversation.context = new userConnection(callback);
var captcha = null
function captchad(user, challenge) {
say("I've been captcha'd! Cannot connect to omegle anymore! :(");
captcha = (new(require('./captchaDialog.js'))(challenge, user.id));
}
function say(t) {
omegleConversation.out(t)
}
function callback(event, b) {
if (event == "close") {
omegleConversation.close()
} else if (event == "error") {
say("Error: " + b);
} else if (event == "serverMessage") {
say("Server: " + b);
} else if (event == "captcha") {
captchad(omegleConversation.context, b);
} else if (event == "ban") {
say("I've been banned from omegle, server response: " + b);
omegleConversation.close();
} else if (event == "connected" && b === null) {
say("Stranger has connected" + (omegleConversation.context.question ? ", question: " + omegleConversation.context.question : "."))
} else if (event === "message") {
say("Stranger:" + b)
} else if (event == "spyMessage") {
say(b[0] + ":" + b[1])
} else if (event == "spyDisconnected") {
say(b[0] + " has disconnected.");
omegleConversation.close()
} else if (event == "disconnected") {
say("Stranger has disconnected.");
} else {
console.log(arguments)
}
}
function disconnect() {
say("You have disconnected.");
omegleConversation.context.sendAction("disconnect");
}
function startConversation(spy, lang, interests) {
if (captcha) {
return null
}
omegleConversation.context.questionMode = spy;
lang && (omegleConversation.context.lang = lang)
omegleConversation.context.topics = undefined;
interests && (omegleConversation.context.topics = JSON.stringify(interests))
omegleConversation.context.init();
}
function ask(question, lang) {
if (captcha) {
return null
}
lang && (omegleConversation.context.lang = lang)
omegleConversation.context.ask = question
omegleConversation.context.init();
}
function cap() {
if (captcha) {
if (!omegleConversation) {
captcha = null;
return
}
captcha.update().then(function() {
say("captcha is :" + captcha.getImageUrl());
})
} else if (omegleConversation) {
say("There is no captcha to solve");
}
}
function send(secret) {
if (captcha) captcha.send(secret).then(function(b) {
if (!b) {
say("incorrect captcha!")
}
});
}
return {
startConversation: startConversation,
disconnect: disconnect,
cap: cap,
send: send,
ask: ask,
event: omegleConversation.connect()
};
})
workshopBot(chatbot, omegleBot);
function initializing() {
//"connecting" to the chatbot
var ircChatbotConnection = chatbot.session().connect();
var irc=(function(){var irc = require('irc');
var client = new irc.Client('irc.canternet.org', 'RainBot', {
channels: ['#rgb'],
});
var ircio=new eventualIO(function(msg){client.say("#rgb", msg)})
client.addListener('message', function(from, to, message) {
ircio.out({ message: message, user: from });
});
client.addListener('error', function(message) {
console.log('error: ', message);
});
return ircio.connect();
}())
irc.pipe(ircChatbotConnection);
ircChatbotConnection.pipe(irc);
var login = require("facebook-chat-api");
// Create simple echo bot
login({
email: credentials.fbemail,
password: credentials.fbpassword
}, (err, api) => {
if (err) {
console.error(err, "IT HAS ERRORED");
return;
}
api.setOptions({
selfListen: true
})
var threads = {}
var irc = (function() {
var irc = require('irc');
var client = new irc.Client('irc.canternet.org', 'RainBot', {
channels: ['#rgb'],
});
var ircio = new eventualIO(function(msg) {
client.say("#rgb", msg)
})
client.addListener('message', function(from, to, message) {
ircio.out({
message: message,
user: from
});
});
client.addListener('error', function(message) {
console.log('error: ', message);
});
return ircio;
}())
chatbot.session(irc);
(function() {
var login = require("facebook-chat-api");
// Create simple echo bot
login({
email: credentials.fbemail,
password: credentials.fbpassword
}, (err, api) => {
api2 = api
if (err) {
console.error(err, "IT HAS ERRORED");
return;
}
api.setOptions({
selfListen: true
})
var threads = {}
function getNicks(threadId) {
if (threads[threadId] && threads[threadId].nicks) {
return Promise.resolve(threads[threadId].nicks);
} else {
return new Promise(function(resolve) {
api.getThreadInfo(threadId, function(gfdfg, b) {
resolve(threads[threadId].nicks = b.nicknames || {});
})
})
}
}
function getNick(id, threadId) {
return getNicks(threadId).then(function(nicks) {
if (nicks[id]) return nicks[id];
else return new Promise(function(resolve) {
api.getUserInfo(id, function(err, result) {
resolve(nicks[id] = result[id].vanity || result[id].firstName)
})
})
})
}
api.listen((err, message) => {
if (err) return console.error("ERROR", err)
var chatbot2;
if (!threads[message.threadID]) {
var threadlol = new eventualIO(function(msg) {
api.sendMessage(msg, message.threadID);
})
threads[message.threadID] = {
chatbot: threadlol
}
chatbot.session(threadlol);
}
chatbot2 = threads[message.threadID].chatbot;
if (message && message.body) {
getNick(message.senderID, message.threadID).then(function(user) {
chatbot2.out({
message: message.body,
user: user
})
})
}
})
})
})
/* var Discord = require("discord.io");
var bot = new Discord.Client({
token: credentials.discordtoken,
autorun: true
});
bot.on('ready', function() {
console.log('Logged in as %s - %s\n', bot.username, bot.id);
});
bot.on('ready', function() {
console.log('Logged in as %s - %s\n', bot.username, bot.id);
});
var discordChannels = {}
bot.on('message', function(user, userID, channelID, message, event) {
//console.log(user,userID,channelID,message)
if (!discordChannels[channelID]) {
var channellol = new eventualIO(function(msg) {
bot.sendMessage({
to: channelID,
message: msg
})
});
discordChannels[channelID] = {
chatbot: channellol //chatbot.session().connect()
}
chatbot.session(channellol);
}
//console.log("THIS WAS EXECUTED2")
discordChannels[channelID].chatbot.out({
message: message,
user: user
})
});
//discord2 = bot;*/
}())
}
initializing();