forked from nortxort/pinylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinychat.py
949 lines (816 loc) · 35.9 KB
/
tinychat.py
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
import time
import thread
import random
import traceback
from colorama import init, Fore, Style
from api import web_request, tinychat_api
from files import file_handler as fh
from rtmp import rtmp_protocol
# TODO: Error handling.
# TODO: Fix encoding problems.
# Basic settings.
SETTINGS = {
'log': True,
'debug_mode': True,
'log_path': 'files/logs/'
}
# Console colors.
COLOR = {
'white': Fore.WHITE,
'green': Fore.GREEN,
'bright_green': Style.BRIGHT + Fore.GREEN,
'yellow': Fore.YELLOW,
'bright_yellow': Style.BRIGHT + Fore.YELLOW,
'cyan': Fore.CYAN,
'bright_cyan': Style.BRIGHT + Fore.CYAN,
'red': Fore.RED,
'bright_red': Style.BRIGHT + Fore.RED,
'magenta': Fore.MAGENTA,
'bright_magenta': Style.BRIGHT + Fore.MAGENTA
}
init(autoreset=True)
def create_random_string(min_length, max_length, upper=False):
"""
Creates a random string of letters and numbers.
:param min_length: int the minimum length of the string
:param max_length: int the maximum length of the string
:param upper: bool do we need upper letters
:return: random str of letters and numbers
"""
randlength = random.randint(min_length, max_length)
junk = 'abcdefghijklmnopqrstuvwxyz0123456789'
if upper:
junk += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
return ''.join((random.choice(junk) for i in xrange(randlength)))
def console_write(info_list):
"""
Writes to console.
:param info_list: list of info.
: info_list[0] = The console color.
: info_list[1] = The message to write.
: info_list[2] = The room name.
"""
ts = time.strftime('%H:%M:%S')
info = COLOR['white'] + '[' + ts + ']' + Style.RESET_ALL + ' ' + info_list[0] + info_list[1]
print(info)
# Is logging enabled?
if SETTINGS['log']:
to_log = '[' + ts + '] ' + info_list[1]
# Pass the message and room name to write_to_log().
write_to_log(to_log, info_list[2])
def write_to_log(msg, room_name):
"""
Writes to log.
The room name is used to construct a log file name from.
:param msg: str the message to write to the log.
:param room_name: str the room name.
"""
d = time.strftime('%Y-%m-%d')
file_name = d + '_' + room_name + '.log'
fh.file_writer(SETTINGS['log_path'], file_name, msg)
def random_color():
"""
Get a random tinychat color.
:return: str random color
"""
colors = ['#000000', '#7db257', '#a78901', '#9d5bb5', '#5c1a7a', '#c53332', '#821615', '#a08f23',
'#487d21', '#c356a3', '#1d82eb', '#919104', '#b9807f', '#7bb224', '#1965b6', '#32a5d9']
return random.choice(colors)
class RoomUser:
"""
A object to hold info about a user.
Each user will have a object associated with there username.
The object is used to store information about the user.
"""
def __init__(self, nick, uid=None, last_msg=None):
self.nick = nick
self.id = uid
self.last_msg = last_msg
self.is_mod = False
self.has_power = False
self.user_account = None
self.tinychat_id = None
self.last_login = None
class TinychatRTMPClient:
""" Manages a single room connection to a given room. """
def __init__(self, room, tcurl=None, app=None, room_type=None, nick=None, account=None,
password=None, room_pass=None, ip=None, port=None, proxy=None):
self.roomname = room
self.tc_url = tcurl
self.app = app
self.roomtype = room_type
self.client_nick = nick
self.account = account
self.password = password
self.room_pass = room_pass
self.ip = ip
self.port = port
self.proxy = proxy
self.greenroom = False
self.swf_url = u'http://tinychat.com/embed/Tinychat-11.1-1.0.0.0643.swf?version=1.0.0.0643/[[DYNAMIC]]/8'
self.embed_url = u'http://tinychat.com/' + self.roomname
self.autoop = None
self.prokey = None
self.client_id = None
self.is_connected = False
self.is_client_mod = False
self.room_users = {}
self.is_reconnected = False
self.uptime = 0
# Bot instance attributes.
# These are only for the bot_example.py file, and as such can be removed.
self.no_cam = False
self.elapsed_track_time = 0
self.remaining_track_time = 0
self.playlist = []
self.search_list = []
self.inowplay = 0
self.play = True
def prepare_connect(self):
""" Gather necessary connection parameters before attempting to connect. """
if self.is_reconnected and self.account and self.password:
# Not sure about this. But from what ive gathered, the cookies doesn't change, however they might expire.
# The hashes on the other hand do change..
# Try parsing a new set of hashes.
hashes = web_request.find_hashes(self.embed_url, proxy=self.proxy)
self.autoop = hashes['autoop']
self.prokey = hashes['prohash']
else:
if self.account and self.password:
if len(self.account) > 3:
login = web_request.post_login(self.account, self.password)
if 'pass' in login['cookies']:
console_write([COLOR['green'], 'Logged in as: ' + login['cookies']['user'], self.roomname])
hashes = web_request.find_hashes(self.embed_url, proxy=self.proxy)
self.autoop = hashes['autoop']
self.prokey = hashes['prohash']
else:
console_write([COLOR['red'], 'Log in Failed', self.roomname])
self.account = raw_input('Enter account: (optional)')
if self.account:
self.password = raw_input('Enter password: ')
self.prepare_connect()
else:
console_write([COLOR['red'], 'Account name is to short.', self.roomname])
self.account = raw_input('Enter account: ')
self.password = raw_input('Enter password: ')
self.prepare_connect()
console_write([COLOR['white'], 'Parsing room config xml...', self.roomname])
config = tinychat_api.get_roomconfig_xml(self.roomname, self.room_pass, proxy=self.proxy)
while config == 'PW':
self.room_pass = raw_input('The room is password protected. Enter room password: ')
if not self.room_pass:
self.roomname = raw_input('Enter room name: ')
self.room_pass = raw_input('Enter room pass: (optional)')
self.account = raw_input('Enter account: (optional)')
self.password = raw_input('Enter password: (optional)')
self.prepare_connect()
else:
config = tinychat_api.get_roomconfig_xml(self.roomname, self.room_pass, proxy=self.proxy)
if config != 'PW':
break
else:
console_write([COLOR['red'], 'Password Failed.', self.roomname])
self.ip = config['ip']
self.port = config['port']
self.tc_url = config['tcurl']
self.app = config['app']
self.roomtype = config['roomtype']
self.greenroom = config['greenroom']
console_write([COLOR['white'],
'TC Url: ' + self.tc_url + '\n' +
'Application: ' + self.app + '\n' +
'Room type: ' + self.roomtype + '\n' +
'Greenroom: ' + str(self.greenroom) + '\n\n' +
'============ CONNECTING ============\n', self.roomname])
self.connect()
def connect(self):
""" Attempts to make a RTMP connection with the given connection parameters. """
if not self.is_connected:
try:
tinychat_api.recaptcha(proxy=self.proxy)
cauth_cookie = tinychat_api.get_cauth_cookie(self.roomname, proxy=self.proxy)
self.connection = rtmp_protocol.RtmpClient(self.ip, self.port, self.tc_url, self.embed_url,
self.swf_url, self.app, proxy=self.proxy)
self.connection.connect([self.roomname, self.autoop, self.roomtype, u'tinychat',
self.account, '', cauth_cookie, u'Desktop 1.0.0.0643'])
self.is_connected = True
self.callback()
except Exception as e:
self.is_connected = False
if SETTINGS['debug_mode']:
traceback.print_exc()
def disconnect(self):
""" Closes the RTMP connection with the remote server. """
if self.is_connected:
try:
self.is_connected = False
self.is_client_mod = False
self.room_users.clear()
self.uptime = 0
self.connection.shutdown()
except Exception as e:
if SETTINGS['debug_mode']:
traceback.print_exc()
def reconnect(self):
""" Reconnect to a room with the connection parameters already set. """
console_write([COLOR['bright_cyan'], '============ RECONNECTING IN 15 SECONDS ============', self.roomname])
self.is_reconnected = True
self.disconnect()
time.sleep(15)
if self.account and self.password:
self.prepare_connect()
else:
self.connect()
def callback(self):
""" Callback loop that reads from the RTMP stream. """
failures = 0
t = time.time()
while self.is_connected:
# TODO: This uptime stuff should be moved to it's own function.
elapsed = time.time() - t
m, s = divmod(elapsed, 60)
h, m = divmod(m, 60)
d, h = divmod(h, 24)
if d == 0:
self.uptime = '%d:%02d:%02d' % (h, m, s)
if d == 0 and h == 0:
self.uptime = '%02d:%02d' % (m, s)
else:
self.uptime = '%d Day(s) %d:%02d:%02d' % (d, h, m, s)
try:
amf0_data = self.connection.reader.next()
# amf0_data_format = self.amf0_data['msg']
except:
failures += 1
if failures == 3:
self.reconnect()
break
else:
failures = 0
try:
amf0_cmd = amf0_data['command']
cmd = amf0_cmd[0]
iparam0 = 0
# Looking in the SWF file, there seems to be more commands,
# these haven't been included here as i don't know if they are still active...
if cmd == '_result':
print(amf0_data)
elif cmd == '_error':
print(amf0_data)
elif cmd == 'onBWDone':
self.on_bwdone()
elif cmd == 'onStatus':
self.on_status()
elif cmd == 'registered':
self.client_id = amf0_cmd[4]
self.on_registered()
elif cmd == 'join':
usr_join_id = amf0_cmd[3]
user_join_nick = amf0_cmd[4]
self.on_join(usr_join_id, user_join_nick)
elif cmd == 'joins':
id_nick = amf0_cmd[4:]
if len(id_nick) is not 0:
while iparam0 < len(id_nick):
user_id = id_nick[iparam0]
user_nick = id_nick[iparam0 + 1]
self.on_joins(user_id, user_nick)
iparam0 += 2
elif cmd == 'joinsdone':
self.on_joinsdone()
elif cmd == 'oper':
oper_id_name = amf0_cmd[3:]
while iparam0 < len(oper_id_name):
oper_id = str(oper_id_name[iparam0]).split('.0')
oper_name = oper_id_name[iparam0 + 1]
if len(oper_id) == 1:
self.on_oper(oper_id[0], oper_name)
iparam0 += 2
elif cmd == 'deop':
deop_id = amf0_cmd[3]
deop_nick = amf0_cmd[4]
self.on_deop(deop_id, deop_nick)
elif cmd == 'owner':
self.on_owner()
elif cmd == 'avons':
avons_id_name = amf0_cmd[4:]
if len(avons_id_name) is not 0:
while iparam0 < len(avons_id_name):
avons_id = avons_id_name[iparam0]
avons_name = avons_id_name[iparam0 + 1]
self.on_avon(avons_id, avons_name)
iparam0 += 2
elif cmd == 'pros':
pro_ids = amf0_cmd[4:]
if len(pro_ids) is not 0:
for pro_id in pro_ids:
pro_id = str(pro_id).replace('.0', '')
self.on_pro(pro_id)
elif cmd == 'nick':
old_nick = amf0_cmd[3]
new_nick = amf0_cmd[4]
nick_id = amf0_cmd[5]
self.on_nick(old_nick, new_nick, nick_id)
elif cmd == 'nickinuse':
self.on_nickinuse()
elif cmd == 'quit':
quit_name = amf0_cmd[3]
quit_id = amf0_cmd[4]
self.on_quit(quit_id, quit_name)
elif cmd == 'kick':
kick_id = amf0_cmd[3]
kick_name = amf0_cmd[4]
self.on_kick(kick_id, kick_name)
elif cmd == 'banned':
self.on_banned()
elif cmd == 'banlist':
banlist_id_nick = amf0_cmd[3:]
if len(banlist_id_nick) is not 0:
while iparam0 < len(banlist_id_nick):
banned_id = banlist_id_nick[iparam0]
banned_nick = banlist_id_nick[iparam0 + 1]
self.on_banlist(banned_id, banned_nick)
iparam0 += 2
elif cmd == 'startbanlist':
pass
elif cmd == 'topic':
topic = amf0_cmd[3]
self.on_topic(topic)
elif cmd == 'from_owner':
owner_msg = amf0_cmd[3]
self.on_from_owner(owner_msg)
elif cmd == 'privmsg':
msg_text = self._decode_msg(amf0_cmd[4])
# msg_color = amf0_cmd[5]
msg_sender = amf0_cmd[6]
self.on_privmsg(msg_text, msg_sender)
elif cmd == 'notice':
notice_msg = amf0_cmd[3]
notice_msg_id = amf0_cmd[4]
if notice_msg == 'avon':
avon_name = amf0_cmd[5]
self.on_avon(notice_msg_id, avon_name)
elif notice_msg == 'pro':
self.on_pro(notice_msg_id)
elif cmd == 'gift':
# Not exactly sure what this is about..
print(amf0_data)
elif cmd == 'giftpoints':
uid = amf0_cmd[3]
points = amf0_cmd[4]
self.on_giftpoints(uid, points)
else:
console_write([COLOR['bright_red'], 'Unknown command:' + cmd, self.roomname])
except Exception as e:
if SETTINGS['debug_mode']:
traceback.print_exc()
# Callback Event Methods.
def on_result(self, result_info):
pass
def on_error(self, error_info):
pass
def on_status(self):
pass
def on_bwdone(self):
if not self.is_reconnected:
thread.start_new_thread(self.start_auto_job_timer, ())
def on_registered(self):
console_write([COLOR['bright_green'], 'registered with ID: ' + self.client_id + '. Trying to parse captcha key.', self.roomname])
key = tinychat_api.get_captcha_key(self.roomname, self.client_id, proxy=self.proxy)
if key is None:
console_write([COLOR['bright_red'], 'There was a problem parsing the captcha key. Key=' + key, self.roomname])
else:
console_write([COLOR['bright_green'], 'Captcha key found: ' + key, self.roomname])
self.send_cauth_msg(key)
if self.prokey is not None:
# If a prohash was parsed, send pro message.
self.send_pro_msg()
self.set_nick()
def on_join(self, uid, nick):
user = self.add_user_info(nick)
user.id = uid
if uid != self.client_id:
console_write([COLOR['bright_cyan'], nick + ':' + uid + ' joined the room.', self.roomname])
def on_joins(self, uid, nick):
user = self.add_user_info(nick)
user.id = uid
if uid != self.client_id:
console_write([COLOR['bright_green'], nick + ':' + uid, self.roomname])
def on_joinsdone(self):
self.send_userinfo_request_to_all()
def on_oper(self, uid, nick):
user = self.add_user_info(nick)
user.is_mod = True
if uid != self.client_id:
console_write([COLOR['bright_red'], nick + ':' + uid + ' is moderator.', self.roomname])
def on_deop(self, uid, nick):
console_write([COLOR['red'], nick + ':' + uid + ' was deoped.', self.roomname])
def on_owner(self):
self.is_client_mod = True
self.send_banlist_msg()
def on_avon(self, uid, name):
console_write([COLOR['cyan'], name + ':' + uid + ' is broadcasting.', self.roomname])
def on_pro(self, uid):
console_write([COLOR['cyan'], uid + ' is pro.', self.roomname])
def on_nick(self, old, new, uid):
old_info = self.find_user_info(old)
if old in self.room_users.keys():
del self.room_users[old]
self.room_users[new] = old_info
if uid != self.client_id:
console_write([COLOR['cyan'], old + ':' + uid + ' changed nick to: ' + new, self.roomname])
def on_nickinuse(self):
self.nick_inuse()
def on_quit(self, uid, name):
del self.room_users[name]
console_write([COLOR['cyan'], name + ':' + uid + ' left the room.', self.roomname])
def on_kick(self, uid, name):
console_write([COLOR['bright_red'], name + ':' + uid + ' was banned.', self.roomname])
self.send_banlist_msg()
def on_banned(self):
console_write([COLOR['red'], 'You are banned from this room.', self.roomname])
def on_banlist(self, uid, nick):
console_write([COLOR['bright_red'], 'Banned user: ' + nick + ':' + uid, self.roomname])
def on_topic(self, topic):
topic_msg = topic.encode('utf-8', 'replace')
console_write([COLOR['cyan'], 'room topic: ' + topic_msg, self.roomname])
def on_from_owner(self, owner_msg):
msg = str(owner_msg).replace('notice', '').replace('%20', ' ')
console_write([COLOR['bright_red'], msg, self.roomname])
def on_giftpoints(self, uid, points):
msg = 'User ID: ' + str(uid) + ' has ' + str(points) + ' giftpoints.'
console_write([COLOR['bright_yellow'], msg, self.roomname])
def on_privmsg(self, msg, msg_sender):
"""
Message controller.
This method will figure out if the message is a userinfo request, userinfo response, media
playing/stoping or if it's a private message. If it's not, we pass the message along to message_handler.
:param msg: str message.
:param msg_sender: str the sender of the message.
"""
if msg.startswith('/'):
msg_cmd = msg.split(' ')
if msg_cmd[0] == '/userinfo':
if msg_cmd[1] == '$request':
self.info_request_from(msg_sender)
elif msg_cmd[1] == '$noinfo':
self.user_is_guest(msg_sender)
else:
self.user_has_account(msg_sender, msg_cmd[1].lower())
elif msg_cmd[0] == '/msg':
private_msg = ' '.join(msg_cmd[2:])
self.private_message_handler(msg_sender, private_msg)
elif msg_cmd[0] == '/mbs':
self.user_is_playing_media(msg_cmd[1], msg_sender, msg_cmd[2])
elif msg_cmd[0] == '/mbc':
self.user_closed_media(msg_cmd[1], msg_sender)
elif msg_cmd[0] == 'mbpa':
pass
elif msg_cmd[0] == '/mbpl':
pass
elif msg_cmd[0] == '/mbsk':
pass
else:
self.message_handler(msg_sender, msg.encode('ascii', 'ignore'))
# Message Handler.
def message_handler(self, msg_sender, msg):
"""
Message handler.
:param msg_sender: str the user sending a message
:param msg: str the message
"""
console_write([COLOR['green'], msg_sender + ':' + msg, self.roomname])
# Private message Handler.
def private_message_handler(self, msg_sender, private_msg):
"""
A user private message us.
:param msg_sender: str the user sending the private message.
:param private_msg: str the private message.
"""
console_write([COLOR['white'], 'Private message from ' + msg_sender + ':' + private_msg, self.roomname])
# Media Events.
def user_is_playing_media(self, media_type, usr_nick, video_id):
"""
A user started a media broadcast.
:param media_type: str the type of media. youTube or soundCloud.
:param usr_nick: str the user name of the user playing media.
:param video_id: str the youtube ID or souncloud trackID.
"""
console_write([COLOR['bright_magenta'], usr_nick + ' is playing ' + media_type + ' ' + video_id, self.roomname])
def user_closed_media(self, media_type, usr_nick):
"""
A user closed a media broadcast.
:param media_type: str the type of media. youTube or soundCloud.
:param usr_nick: str the user name of the user closing the media.
"""
console_write([COLOR['bright_magenta'], usr_nick + ' closed the ' + media_type, self.roomname])
# User Info Events
def info_request_from(self, usr_nick):
"""
A user requests our user info.
:param usr_nick: str the user name of the user requesting our user info.
"""
self.send_userinfo_response_to(usr_nick, self.roomname)
console_write([COLOR['bright_yellow'], usr_nick + ' requests userinfo.', self.roomname])
def user_is_guest(self, usr_nick):
"""
The user tells us that they are a guest.
:param usr_nick: str the user that is a guest.
"""
user = self.add_user_info(usr_nick)
user.user_account = False
console_write([COLOR['bright_yellow'], usr_nick + ' is not signed in.', self.roomname])
def user_has_account(self, usr_nick, usr_acc):
"""
A user replies to our user info request, that they have a account name.
We look up the account using tinychat's API and add the info to the user object.
:param usr_nick: str the user name of the user having an account.
:param usr_acc: str the account that the user has.
"""
user = self.add_user_info(usr_nick)
user.user_account = usr_acc
console_write([COLOR['bright_yellow'], usr_nick + ' has account: ' + usr_acc, self.roomname])
tc_info = tinychat_api.tinychat_user_info(usr_acc)
if tc_info is not None:
user.tinychat_id = tc_info['tinychat_id']
user.last_login = tc_info['last_active']
# User Related
def add_user_info(self, usr_nick):
"""
Find the user object for a given user name and add to it.
We use this methode to add info to our user info object.
.
:param usr_nick: str the user name of the user we want to find info for.
:return: object a user object containing user info.
"""
if usr_nick not in self.room_users.keys():
self.room_users[usr_nick] = RoomUser(usr_nick)
return self.room_users[usr_nick]
def find_user_info(self, usr_nick):
"""
Find the user object for a given user name.
Instead of adding to the user info object, we return None if the user name is NOT in the room_users dict.
We use this method when we are getting user input to look up.
:param usr_nick: str the user name to find info for.
:return: object or None if no user name is in the room_users dict.
"""
if usr_nick in self.room_users.keys():
return self.room_users[usr_nick]
return None
# Message Functions
def send_bauth_msg(self):
"""
Get and send the bauth key needed before we can start a broadcast.
"""
bauth_key = tinychat_api.get_bauth_token(self.roomname, self.client_nick, self.client_id, proxy=self.proxy)
self._sendCommand('bauth', [u'' + bauth_key])
def send_pro_msg(self):
"""
Send the pro message needed to show as pro user.
"""
self._sendCommand('pro', [u'' + self.prokey])
def send_cauth_msg(self, cauthkey):
"""
Send the cauth key message with a working cauth key, we need to send this before we can chat.
:param cauthkey: str a working cauth key.
"""
self._sendCommand('cauth', [u'' + cauthkey])
def send_owner_run_msg(self, msg):
"""
Send owner run message. The client has to be mod when sending this message.
:param msg: the message str to send.
"""
self._sendCommand('owner_run', [u'notice' + msg.replace(' ', '%20')])
def send_bot_msg(self, msg, is_mod=False):
"""
Send a message in the color black.
:param msg: str the message to send.
:param is_mod: bool if True we send owner run message, else we send a normal message in the color black.
:return:
"""
if is_mod:
self.send_owner_run_msg(msg)
else:
self._sendCommand('privmsg', [u'' + self._encode_msg(msg), u'#000000,en'])
def send_private_bot_msg(self, msg, nick):
"""
Send a private message to a user in the color black.
:param msg: str the message to send.
:param nick: str the user to receive the message.
"""
self._sendCommand('privmsg', [u'' + self._encode_msg('/msg ' + nick + ' ' + msg), u'#000000,en'])
def send_chat_msg(self, msg):
"""
Send a chat room message with a random color.
:param msg: str the message to send.
"""
self._sendCommand('privmsg', [u'' + self._encode_msg(msg), u'' + random_color() + ',en'])
def send_private_msg(self, msg, nick):
"""
Send a private message to a user with a random color.
:param msg: str the private message to send.
:param nick: str the user name to receive the message.
"""
user = self.find_user_info(nick)
if user is not None:
self._sendCommand('privmsg', [u'' + self._encode_msg('/msg ' + nick + ' ' + msg), u'' + random_color() + ',en', u'n' + user.id + '-' + nick])
self._sendCommand('privmsg', [u'' + self._encode_msg('/msg ' + nick + ' ' + msg), u'' + random_color() + ',en', u'b' + user.id + '-' + nick])
def send_userinfo_response_to(self, nick, info):
"""
Send a userinfo response to a user that requests our userinfo.
:param nick: str the user name to send the userinfo to.
:param info: str the user info to send. This should be a account name.
"""
user = self.find_user_info(nick)
if user is not None:
self._sendCommand('privmsg', [u'' + self._encode_msg('/userinfo' + ' ' + u'' + info), '#0,en', u'n' + user.id + '-' + nick])
self._sendCommand('privmsg', [u'' + self._encode_msg('/userinfo' + ' ' + u'' + info), '#0,en', u'b' + user.id + '-' + nick])
def send_userinfo_request_msg(self, nick):
"""
Request userinfo from a user.
:param nick: str the user name of the user we want info from.
"""
user = self.find_user_info(nick)
if user is not None:
self._sendCommand('privmsg', [u'' + self._encode_msg('/userinfo $request'), '#0,en', u'b' + user.id + '-' + nick])
self._sendCommand('privmsg', [u'' + self._encode_msg('/userinfo $request'), '#0,en', u'n' + user.id + '-' + nick])
def send_userinfo_request_to_all(self):
"""
Request userinfo from all users.
Only used when we join a room.
"""
self._sendCommand('privmsg', [u'' + self._encode_msg('/userinfo $request'), '#0,en'])
def send_undercover_msg(self, nick, msg):
"""
Send a 'undercover' message.
This is a special message that appears in the main chat, but is only visible to the user it is sent to.
It can also be used to play 'private' youtubes/soundclouds with.
:param nick: str the user name to send the message to.
:param msg: str the message to send.
"""
user = self.find_user_info(nick)
if user is not None:
self._sendCommand('privmsg', [u'' + self._encode_msg(msg), '#0,en', u'b' + user.id + '-' + nick])
self._sendCommand('privmsg', [u'' + self._encode_msg(msg), '#0,en', u'n' + user.id + '-' + nick])
def set_nick(self):
"""
Send the nick message.
"""
if not self.client_nick:
self.client_nick = create_random_string(5, 25)
console_write([COLOR['white'], 'Setting nick: ' + self.client_nick, self.roomname])
self._sendCommand('nick', [u'' + self.client_nick])
def send_ban_msg(self, nick, uid):
"""
Send ban message. The client has to be mod when sending this message.
:param nick: str the user name of the user we want to ban.
:param uid: str the ID of the user we want to ban.
"""
self._sendCommand('kick', [u'' + nick, uid])
def send_forgive_msg(self, uid):
"""
Send forgive message. The client has to be mod when sending this message.
:param uid: str the ID of the user we want to forgive.
"""
self._sendCommand('forgive', [u'' + uid])
self.send_banlist_msg()
def send_banlist_msg(self):
"""
Send banlist message. The client has to be mod when sending this message.
:return:
"""
self._sendCommand('banlist', [])
def send_topic_msg(self, topic):
"""
Send a room topic message. The client has to be mod when sending this message.
:param topic: str the room topic.
"""
self._sendCommand('topic', [u'' + topic])
def send_close_user_msg(self, nick):
"""
Send close user broadcast message. The client has to be mod when sending this message.
:param nick: str the user name of the user we want to close.
"""
self._sendCommand('owner_run', [u'_close' + nick])
def play_youtube(self, video_id):
"""
Start a youtube video.
:param video_id: str the youtube video ID.
"""
self.send_chat_msg('/mbs youTube ' + str(video_id) + ' 0')
def stop_youtube(self):
"""
Stop/Close a youtube video.
"""
self.send_chat_msg('/mbc youTube')
def play_soundcloud(self, track_id):
"""
Start a soundcloud video.
:param track_id: str soundcloud track ID.
"""
self.send_chat_msg('/mbs soundCloud ' + str(track_id) + ' 0')
def stop_soundcloud(self):
"""
Stop/Close a soundcloud video.
"""
self.send_chat_msg('/mbc soundCloud')
def nick_inuse(self):
"""
Choose a different user name if the one we chose originally was being used.
"""
self.client_nick += str(random.randint(0, 10))
console_write([COLOR['white'], 'Nick already taken. Changing nick to: ' + self.client_nick, self.roomname])
self.set_nick()
# Message Cunstruction.
def _sendCommand(self, cmd, params=[]):
"""
Calls remote procedure calls (RPC) at the receiving end.
:param cmd: str remote command.
:param params: list command parameters.
"""
msg = {'msg': rtmp_protocol.DataTypes.COMMAND, 'command': [u'' + cmd, 0, None] + params}
self.connection.writer.write(msg)
self.connection.writer.flush()
def _sendCreateStream(self):
"""
Send createStream message.
"""
msg = {'msg': rtmp_protocol.DataTypes.COMMAND, 'command': [u'' + 'createStream', 12, None]}
console_write([COLOR['white'], 'Sending createStream message.', self.roomname])
self.connection.writer.write(msg)
self.connection.writer.flush()
def _sendCloseStream(self):
"""
Send closeStream message.
"""
msg = {'msg': rtmp_protocol.DataTypes.COMMAND, 'command': [u'' + 'closeStream', 0, None]}
console_write([COLOR['white'], 'Closing stream.', self.roomname])
self.connection.writer.writepublish(msg)
self.connection.writer.flush()
def _sendPublish(self):
"""
Send publish message.
"""
msg = {'msg': rtmp_protocol.DataTypes.COMMAND, 'command': [u'' + 'publish', 0, None, u'' + self.client_id, u'' + 'live']}
console_write([COLOR['white'], 'Sending publish message.', self.roomname])
self.connection.writer.writepublish(msg)
self.connection.writer.flush()
def _sendPingRequest(self):
# TODO: Make this method work!
msg = {'msg': rtmp_protocol.DataTypes.USER_CONTROL, 'event_type': rtmp_protocol.UserControlTypes.PING_REQUEST}
self.connection.handle_simple_message(msg)
self.connection.writer.flush()
# Helper Methods
def _decode_msg(self, msg):
"""
Decode str from comma separated decimal to normal text str.
:param msg: str the encoded message.
:return: str normal text.
"""
chars = msg.split(',')
msg = ''
for i in chars:
msg += chr(int(i))
return msg
def _encode_msg(self, msg):
"""
Encode normal text str to comma separated decimal.
:param msg: str the normal text to encode
:return: comma separated decimal str.
"""
return ','.join(str(ord(char)) for char in msg)
# Timed Auto Method.
def start_auto_job_timer(self):
"""
Just like using tinychat with a browser, this method will
fetch the room config from tinychat API every 5 minute.
See line 228 at http://tinychat.com/embed/chat.js
"""
ts_now = int(time.time())
while True:
counter = int(time.time())
if counter == ts_now + 300: # 300 seconds = 5 minutes
if self.is_connected:
tinychat_api.get_roomconfig_xml(self.roomname, self.room_pass, proxy=self.proxy)
self.start_auto_job_timer()
break
def main():
room_name = raw_input('Enter room name: ')
nickname = raw_input('Enter nick name: (optional) ')
room_password = raw_input('Enter room password: (optional) ')
login_account = raw_input('Login account: (optional)')
login_password = raw_input('Login password: (optional)')
client = TinychatRTMPClient(room_name, nick=nickname, account=login_account,
password=login_password, room_pass=room_password)
thread.start_new_thread(client.prepare_connect, ())
while not client.is_connected:
time.sleep(1)
while client.is_connected:
chat_msg = raw_input()
if chat_msg.lower() == 'q':
client.disconnect()
else:
client.send_bot_msg(chat_msg, client.is_client_mod)
if __name__ == '__main__':
main()