This repository was archived by the owner on Sep 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtinybot.py
More file actions
1959 lines (1687 loc) · 84.7 KB
/
tinybot.py
File metadata and controls
1959 lines (1687 loc) · 84.7 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
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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
""" tinybot by nortxort (https://github.com/nortxort/tinybot) """
import logging
import re
import threading
import pinylib
import util.media_manager
from page import privacy
from apis import youtube, soundcloud, lastfm, other, locals_
__version__ = '6.1.0'
log = logging.getLogger(__name__)
class TinychatBot(pinylib.TinychatRTMPClient):
privacy_settings = None
media = util.media_manager.MediaManager()
media_timer_thread = None
search_list = []
is_search_list_youtube_playlist = False
is_broadcasting = False
def on_join(self, join_info):
""" Application message received when a user joins the room.
:param join_info: Information about the user joining.
:type join_info: dict
"""
log.info('user join info: %s' % join_info)
_user = self.users.add(join_info)
if _user is not None:
if _user.account:
tc_info = pinylib.apis.tinychat.user_info(_user.account)
if tc_info is not None:
_user.tinychat_id = tc_info['tinychat_id']
_user.last_login = tc_info['last_active']
if _user.is_owner:
_user.user_level = 1
self.console_write(pinylib.COLOR['red'], 'Room Owner %s:%d:%s' %
(_user.nick, _user.id, _user.account))
elif _user.is_mod:
_user.user_level = 3
self.console_write(pinylib.COLOR['bright_red'], 'Moderator %s:%d:%s' %
(_user.nick, _user.id, _user.account))
else:
self.console_write(pinylib.COLOR['bright_yellow'], '%s:%d has account: %s' %
(_user.nick, _user.id, _user.account))
if _user.account in pinylib.CONFIG.B_ACCOUNT_BANS:
if self.is_client_mod:
self.send_ban_msg(_user.nick, _user.id)
if pinylib.CONFIG.B_FORGIVE_AUTO_BANS:
self.send_forgive_msg(_user.id)
self.send_bot_msg('*Auto-Banned:* (bad account)')
else:
if _user.id is not self._client_id:
if _user.lf and not pinylib.CONFIG.B_ALLOW_LURKERS and self.is_client_mod:
self.send_ban_msg(_user.nick, _user.id)
if pinylib.CONFIG.B_FORGIVE_AUTO_BANS:
self.send_forgive_msg(_user.id)
self.send_bot_msg('*Auto-Banned:* (lurkers not allowed)')
elif not pinylib.CONFIG.B_ALLOW_GUESTS and self.is_client_mod:
self.send_ban_msg(_user.nick, _user.id)
if pinylib.CONFIG.B_FORGIVE_AUTO_BANS:
self.send_forgive_msg(_user.id)
self.send_bot_msg('*Auto-Banned:* (guests not allowed)')
else:
self.console_write(pinylib.COLOR['cyan'], '%s:%d joined the room.' % (_user.nick, _user.id))
def on_joinsdone(self):
""" Application message received when all room users information have been received. """
if self.is_client_mod:
self.send_banlist_msg()
self.load_list(nicks=True, accounts=True, strings=True)
if self.is_client_owner and self.param.roomtype != 'default':
threading.Thread(target=self.get_privacy_settings).start()
def on_avon(self, uid, name, greenroom=False):
""" Application message received when a user starts broadcasting.
:param uid: The Id of the user.
:type uid: str
:param name: The nick name of the user.
:type name: str
:param greenroom: True the user is waiting in the greenroom.
:type greenroom: bool
"""
if greenroom:
_user = self.users.search_by_id(name)
if _user is not None:
_user.is_waiting = True
self.send_bot_msg('%s:%s *is waiting in the greenroom.*' % (_user.nick, _user.id))
else:
_user = self.users.search(name)
if _user is not None and _user.is_waiting:
_user.is_waiting = False
if not pinylib.CONFIG.B_ALLOW_BROADCASTS and self.is_client_mod:
self.send_close_user_msg(name)
self.console_write(pinylib.COLOR['cyan'], 'Auto closed broadcast %s:%s' % (name, uid))
else:
self.console_write(pinylib.COLOR['cyan'], '%s:%s is broadcasting.' % (name, uid))
def on_nick(self, old, new, uid):
""" Application message received when a user changes nick name.
:param old: The old nick name of the user.
:type old: str
:param new: The new nick name of the user
:type new: str
:param uid: The Id of the user.
:type uid: int
"""
if uid == self._client_id:
self.nickname = new
old_info = self.users.search(old)
old_info.nick = new
if not self.users.change(old, new, old_info):
log.error('failed to change nick for user: %s' % new)
if self.check_nick(old, old_info):
if pinylib.CONFIG.B_FORGIVE_AUTO_BANS:
self.send_forgive_msg(uid)
elif uid != self._client_id:
if self.is_client_mod and pinylib.CONFIG.B_GREET:
if old_info.account:
self.send_bot_msg('*Welcome* %s:%s:%s' % (new, uid, old_info.account))
else:
self.send_bot_msg('*Welcome* %s:%s' % (new, uid))
if self.media.has_active_track():
if not self.media.is_mod_playing:
self.send_media_broadcast_start(self.media.track().type,
self.media.track().id,
time_point=self.media.elapsed_track_time(),
private_nick=new)
self.console_write(pinylib.COLOR['bright_cyan'], '%s:%s changed nick to: %s' % (old, uid, new))
# Media Events.
def on_media_broadcast_start(self, media_type, video_id, usr_nick):
""" A user started a media broadcast.
:param media_type: The type of media. youTube or soundCloud.
:type media_type: str
:param video_id: The youtube ID or souncloud trackID.
:type video_id: str
:param usr_nick: The user name of the user playing media.
:type usr_nick: str
"""
self.cancel_media_event_timer()
if media_type == 'youTube':
_youtube = youtube.video_details(video_id, check=False)
if _youtube is not None:
self.media.mb_start(self.active_user.nick, _youtube)
elif media_type == 'soundCloud':
_soundcloud = soundcloud.track_info(video_id)
if _soundcloud is not None:
self.media.mb_start(self.active_user.nick, _soundcloud)
self.media_event_timer(self.media.track().time)
self.console_write(pinylib.COLOR['bright_magenta'], '%s is playing %s %s' %
(usr_nick, media_type, video_id))
def on_media_broadcast_close(self, media_type, usr_nick):
""" A user closed a media broadcast.
:param media_type: The type of media. youTube or soundCloud.
:type media_type: str
:param usr_nick: The user name of the user closing the media.
:type usr_nick: str
"""
self.cancel_media_event_timer()
self.media.mb_close()
self.console_write(pinylib.COLOR['bright_magenta'], '%s closed the %s' % (usr_nick, media_type))
def on_media_broadcast_paused(self, media_type, usr_nick):
""" A user paused the media broadcast.
:param media_type: The type of media being paused. youTube or soundCloud.
:type media_type: str
:param usr_nick: The user name of the user pausing the media.
:type usr_nick: str
"""
self.cancel_media_event_timer()
self.media.mb_pause()
self.console_write(pinylib.COLOR['bright_magenta'], '%s paused the %s' % (usr_nick, media_type))
def on_media_broadcast_play(self, media_type, time_point, usr_nick):
""" A user resumed playing a media broadcast.
:param media_type: The media type. youTube or soundCloud.
:type media_type: str
:param time_point: The time point in the tune in milliseconds.
:type time_point: int
:param usr_nick: The user resuming the tune.
:type usr_nick: str
"""
self.cancel_media_event_timer()
new_media_time = self.media.mb_play(time_point)
self.media_event_timer(new_media_time)
self.console_write(pinylib.COLOR['bright_magenta'], '%s resumed the %s at: %s' %
(usr_nick, media_type, self.format_time(time_point)))
def on_media_broadcast_skip(self, media_type, time_point, usr_nick):
""" A user time searched a tune.
:param media_type: The media type. youTube or soundCloud.
:type media_type: str
:param time_point: The time point in the tune in milliseconds.
:type time_point: int
:param usr_nick: The user time searching the tune.
:type usr_nick: str
"""
self.cancel_media_event_timer()
new_media_time = self.media.mb_skip(time_point)
if not self.media.is_paused:
self.media_event_timer(new_media_time)
self.console_write(pinylib.COLOR['bright_magenta'], '%s time searched the %s at: %s' %
(usr_nick, media_type, self.format_time(time_point)))
# Message Method.
def send_bot_msg(self, msg, use_chat_msg=False):
""" Send a chat message to the room.
NOTE: If the client is moderator, send_owner_run_msg will be used.
If the client is not a moderator, send_chat_msg will be used.
Setting use_chat_msg to True, forces send_chat_msg to be used.
:param msg: The message to send.
:type msg: str
:param use_chat_msg: True, use normal chat messages,
False, send messages depending on weather or not the client is mod.
:type use_chat_msg: bool
"""
if use_chat_msg:
self.send_chat_msg(msg)
else:
if self.is_client_mod:
self.send_owner_run_msg(msg)
else:
self.send_chat_msg(msg)
# Command Handler.
def message_handler(self, decoded_msg):
""" Message handler.
:param decoded_msg: The decoded msg(text).
:type decoded_msg: str
"""
prefix = pinylib.CONFIG.B_PREFIX
# Is this a custom command?
if decoded_msg.startswith(prefix):
# Split the message in to parts.
parts = decoded_msg.split(' ')
# parts[0] is the command..
cmd = parts[0].lower().strip()
# The rest is a command argument.
cmd_arg = ' '.join(parts[1:]).strip()
# Owner and super mod commands.
if self.has_level(1):
if self.is_client_owner:
# Only possible if bot is using the room owner account.
if cmd == prefix + 'mod':
threading.Thread(target=self.do_make_mod, args=(cmd_arg,)).start()
elif cmd == prefix + 'rmod':
threading.Thread(target=self.do_remove_mod, args=(cmd_arg,)).start()
elif cmd == prefix + 'dir':
threading.Thread(target=self.do_directory).start()
elif cmd == prefix + 'p2t':
threading.Thread(target=self.do_push2talk).start()
elif cmd == prefix + 'gr':
threading.Thread(target=self.do_green_room).start()
elif cmd == prefix + 'crb':
threading.Thread(target=self.do_clear_room_bans).start()
if cmd == prefix + 'kill':
self.do_kill()
elif cmd == prefix + 'reboot':
self.do_reboot()
# Bot controller commands.
if self.has_level(2):
if cmd == prefix + 'mi':
self.do_media_info()
# Mod commands.
if self.has_level(3):
if cmd == prefix + 'op':
self.do_op_user(cmd_arg)
elif cmd == prefix + 'deop':
self.do_deop_user(cmd_arg)
elif cmd == prefix + 'up':
self.do_cam_up()
elif cmd == prefix + 'down':
self.do_cam_down()
elif cmd == prefix + 'nocam':
self.do_nocam()
elif cmd == prefix + 'noguest':
self.do_guests()
elif cmd == prefix + 'lurkers':
self.do_lurkers()
elif cmd == prefix + 'guestnick':
self.do_guest_nicks()
elif cmd == prefix + 'newusers':
self.do_newusers()
elif cmd == prefix + 'greet':
self.do_greet()
elif cmd == prefix + 'pub':
self.do_public_cmds()
if cmd == prefix + 'rs':
self.do_room_settings()
elif cmd == prefix + 'top':
threading.Thread(target=self.do_lastfm_chart, args=(cmd_arg,)).start()
elif cmd == prefix + 'ran':
threading.Thread(target=self.do_lastfm_random_tunes, args=(cmd_arg,)).start()
elif cmd == prefix + 'tag':
threading.Thread(target=self.do_search_lastfm_by_tag, args=(cmd_arg,)).start()
elif cmd == prefix + 'pls':
threading.Thread(target=self.do_youtube_playlist_search, args=(cmd_arg,)).start()
elif cmd == prefix + 'plp':
threading.Thread(target=self.do_play_youtube_playlist, args=(cmd_arg,)).start()
elif cmd == prefix + 'ssl':
self.do_show_search_list()
if self.has_level(4):
if cmd == prefix + 'close':
self.do_close_broadcast(cmd_arg)
elif cmd == prefix + 'clr':
self.do_clear()
elif cmd == prefix + 'skip':
self.do_skip()
elif cmd == prefix + 'del':
self.do_delete_playlist_item(cmd_arg)
elif cmd == prefix + 'rpl':
self.do_media_replay()
elif cmd == prefix + 'mbpl':
self.do_play_media()
elif cmd == prefix + 'mbpa':
self.do_media_pause()
elif cmd == prefix + 'seek':
self.do_seek_media(cmd_arg)
elif cmd == prefix + 'cm':
self.do_close_media()
elif cmd == prefix + 'cpl':
self.do_clear_playlist()
elif cmd == prefix + 'spl':
self.do_playlist_info()
elif cmd == prefix + 'nick':
self.do_nick(cmd_arg)
elif cmd == prefix + 'topic':
self.do_topic(cmd_arg)
elif cmd == prefix + 'kick':
threading.Thread(target=self.do_kick, args=(cmd_arg,)).start()
elif cmd == prefix + 'ban':
threading.Thread(target=self.do_ban, args=(cmd_arg,)).start()
elif cmd == prefix + 'bn':
self.do_bad_nick(cmd_arg)
elif cmd == prefix + 'rmbn':
self.do_remove_bad_nick(cmd_arg)
elif cmd == prefix + 'bs':
self.do_bad_string(cmd_arg)
elif cmd == prefix + 'rmbs':
self.do_remove_bad_string(cmd_arg)
elif cmd == prefix + 'ba':
self.do_bad_account(cmd_arg)
elif cmd == prefix + 'rmba':
self.do_remove_bad_account(cmd_arg)
elif cmd == prefix + 'list':
self.do_list_info(cmd_arg)
elif cmd == prefix + 'uinfo':
self.do_user_info(cmd_arg)
elif cmd == prefix + 'yts':
threading.Thread(target=self.do_youtube_search, args=(cmd_arg,)).start()
elif cmd == prefix + 'pyts':
self.do_play_youtube_search(cmd_arg)
elif cmd == prefix + 'cam':
threading.Thread(target=self.do_cam_approve, args=(cmd_arg,)).start()
# Public commands. (if enabled)
if (pinylib.CONFIG.B_PUBLIC_CMD and self.has_level(5)) or self.active_user.user_level < 5:
if cmd == prefix + 'fs':
self.do_full_screen(cmd_arg)
elif cmd == prefix + 'wp':
self.do_who_plays()
elif cmd == prefix + 'v':
self.do_version()
elif cmd == prefix + 'help':
self.do_help()
elif cmd == prefix + 't':
self.do_uptime()
elif cmd == prefix + 'pmme':
self.do_pmme()
elif cmd == prefix + 'q':
self.do_playlist_status()
elif cmd == prefix + 'n':
self.do_next_tune_in_playlist()
elif cmd == prefix + 'np':
self.do_now_playing()
elif cmd == prefix + 'yt':
threading.Thread(target=self.do_play_youtube, args=(cmd_arg,)).start()
elif cmd == prefix + 'pyt':
threading.Thread(target=self.do_play_private_youtube, args=(cmd_arg,)).start()
elif cmd == prefix + 'sc':
threading.Thread(target=self.do_play_soundcloud, args=(cmd_arg,)).start()
elif cmd == prefix + 'psc':
threading.Thread(target=self.do_play_private_soundcloud, args=(cmd_arg,)).start()
# Tinychat API commands.
elif cmd == prefix + 'spy':
threading.Thread(target=self.do_spy, args=(cmd_arg,)).start()
elif cmd == prefix + 'acspy':
threading.Thread(target=self.do_account_spy, args=(cmd_arg,)).start()
# Other API commands.
elif cmd == prefix + 'urb':
threading.Thread(target=self.do_search_urban_dictionary, args=(cmd_arg,)).start()
elif cmd == prefix + 'wea':
threading.Thread(target=self.do_weather_search, args=(cmd_arg,)).start()
elif cmd == prefix + 'ip':
threading.Thread(target=self.do_whois_ip, args=(cmd_arg,)).start()
# Just for fun.
elif cmd == prefix + 'cn':
threading.Thread(target=self.do_chuck_noris).start()
elif cmd == prefix + '8ball':
self.do_8ball(cmd_arg)
elif cmd == prefix + 'roll':
self.do_dice()
elif cmd == prefix + 'flip':
self.do_flip_coin()
# Print command to console.
self.console_write(pinylib.COLOR['yellow'], self.active_user.nick + ': ' + cmd + ' ' + cmd_arg)
else:
# Print chat message to console.
self.console_write(pinylib.COLOR['green'], self.active_user.nick + ': ' + decoded_msg)
# Only check chat msg for ban string if we are mod.
if self.is_client_mod and self.active_user.user_level > 4:
threading.Thread(target=self.check_msg, args=(decoded_msg,)).start()
self.active_user.last_msg = decoded_msg
def do_make_mod(self, account):
""" Make a tinychat account a room moderator.
:param account: The account to make a moderator.
:type account: str
"""
if self.is_client_owner:
if len(account) is 0:
self.send_bot_msg('Missing account name.')
else:
tc_user = self.privacy_settings.make_moderator(account)
if tc_user is None:
self.send_bot_msg('*The account is invalid.*')
elif not tc_user:
self.send_bot_msg('*%s* is already a moderator.' % account)
elif tc_user:
self.send_bot_msg('*%s* was made a room moderator.' % account)
def do_remove_mod(self, account):
""" Removes a tinychat account from the moderator list.
:param account: The account to remove from the moderator list.
:type account: str
"""
if self.is_client_owner:
if len(account) is 0:
self.send_bot_msg('Missing account name.')
else:
tc_user = self.privacy_settings.remove_moderator(account)
if tc_user:
self.send_bot_msg('*%s* is no longer a room moderator.' % account)
elif not tc_user:
self.send_bot_msg('*%s* is not a room moderator.' % account)
def do_directory(self):
""" Toggles if the room should be shown on the directory. """
if self.is_client_owner:
if self.privacy_settings.show_on_directory():
self.send_bot_msg('*Room IS shown on the directory.*')
else:
self.send_bot_msg('*Room is NOT shown on the directory.*')
def do_push2talk(self):
""" Toggles if the room should be in push2talk mode. """
if self.is_client_owner:
if self.privacy_settings.set_push2talk():
self.send_bot_msg('*Push2Talk is enabled.*')
else:
self.send_bot_msg('*Push2Talk is disabled.*')
def do_green_room(self):
""" Toggles if the room should be in greenroom mode. """
if self.is_client_owner:
if self.privacy_settings.set_greenroom():
self.send_bot_msg('*Green room is enabled.*')
else:
self.send_bot_msg('*Green room is disabled.*')
def do_clear_room_bans(self):
""" Clear all room bans. """
if self.is_client_owner:
if self.privacy_settings.clear_bans():
self.send_bot_msg('*All room bans was cleared.*')
def do_kill(self):
""" Kills the bot. """
self.disconnect()
if self.is_green_connected:
self.disconnect(greenroom=True)
def do_reboot(self):
""" Reboots the bot. """
if self.is_green_connected:
self.disconnect(greenroom=True)
self.reconnect()
def do_media_info(self):
""" Shows basic media info. """
if self.is_client_mod:
self.send_owner_run_msg('*Playlist Length:* ' + str(len(self.media.track_list)))
self.send_owner_run_msg('*Track List Index:* ' + str(self.media.track_list_index))
self.send_owner_run_msg('*Elapsed Track Time:* ' +
self.format_time(self.media.elapsed_track_time()))
self.send_owner_run_msg('*Active Track:* ' + str(self.media.has_active_track()))
self.send_owner_run_msg('*Active Threads:* ' + str(threading.active_count()))
def do_op_user(self, user_name):
""" Lets the room owner, a mod or a bot controller make another user a bot controller.
:param user_name: The user to op.
:type user_name: str
"""
if self.is_client_mod:
if len(user_name) is 0:
self.send_bot_msg('Missing username.')
else:
_user = self.users.search(user_name)
if _user is not None:
_user.user_level = 4
self.send_bot_msg('*%s* is now a bot controller (L4)' % user_name)
else:
self.send_bot_msg('No user named: %s' % user_name)
def do_deop_user(self, user_name):
""" Lets the room owner, a mod or a bot controller remove a user from being a bot controller.
:param user_name: The user to deop.
:type user_name: str
"""
if self.is_client_mod:
if len(user_name) is 0:
self.send_bot_msg('Missing username.')
else:
_user = self.users.search(user_name)
if _user is not None:
_user.user_level = 5
self.send_bot_msg('*%s* is not a bot controller anymore (L5)' % user_name)
else:
self.send_bot_msg('No user named: %s' % user_name)
def do_cam_up(self):
""" Makes the bot cam up. """
if not self.is_broadcasting:
self.send_bauth_msg()
self.connection.createstream()
self.is_broadcasting = True
def do_cam_down(self):
""" Makes the bot cam down. """
if self.is_broadcasting:
self.connection.closestream()
self.is_broadcasting = False
def do_nocam(self):
""" Toggles if broadcasting is allowed or not. """
pinylib.CONFIG.B_ALLOW_BROADCASTS = not pinylib.CONFIG.B_ALLOW_BROADCASTS
self.send_bot_msg('*Allow Broadcasts:* %s' % pinylib.CONFIG.B_ALLOW_BROADCASTS)
def do_guests(self):
""" Toggles if guests are allowed to join the room or not. """
pinylib.CONFIG.B_ALLOW_GUESTS = not pinylib.CONFIG.B_ALLOW_GUESTS
self.send_bot_msg('*Allow Guests:* %s' % pinylib.CONFIG.B_ALLOW_GUESTS)
def do_lurkers(self):
""" Toggles if lurkers are allowed or not. """
pinylib.CONFIG.B_ALLOW_LURKERS = not pinylib.CONFIG.B_ALLOW_LURKERS
self.send_bot_msg('*Allowe Lurkers:* %s' % pinylib.CONFIG.B_ALLOW_LURKERS)
def do_guest_nicks(self):
""" Toggles if guest nicks are allowed or not. """
pinylib.CONFIG.B_ALLOW_GUESTS_NICKS = not pinylib.CONFIG.B_ALLOW_GUESTS_NICKS
self.send_bot_msg('*Allow Guest Nicks:* %s' % pinylib.CONFIG.B_ALLOW_GUESTS_NICKS)
def do_newusers(self):
""" Toggles if newusers are allowed to join the room or not. """
pinylib.CONFIG.B_ALLOW_NEWUSERS = not pinylib.CONFIG.B_ALLOW_NEWUSERS
self.send_bot_msg('*Allow Newusers:* %s' % pinylib.CONFIG.B_ALLOW_NEWUSERS)
def do_greet(self):
""" Toggles if users should be greeted on entry. """
pinylib.CONFIG.B_GREET = not pinylib.CONFIG.B_GREET
self.send_bot_msg('*Greet Users:* %s' % pinylib.CONFIG.B_GREET)
def do_public_cmds(self):
""" Toggles if public commands are public or not. """
pinylib.CONFIG.B_PUBLIC_CMD = not pinylib.CONFIG.B_PUBLIC_CMD
self.send_bot_msg('*Public Commands Enabled:* %s' % pinylib.CONFIG.B_PUBLIC_CMD)
def do_room_settings(self):
""" Shows current room settings. """
if self.is_client_owner:
settings = self.privacy_settings.current_settings()
self.send_owner_run_msg('*Broadcast Password:* %s' % settings['broadcast_pass'])
self.send_owner_run_msg('*Room Password:* %s' % settings['room_pass'])
self.send_owner_run_msg('*Login Type:* %s' % settings['allow_guest'])
self.send_owner_run_msg('*Directory:* %s' % settings['show_on_directory'])
self.send_owner_run_msg('*Push2Talk:* %s' % settings['push2talk'])
self.send_owner_run_msg('*Greenroom:* %s' % settings['greenroom'])
def do_lastfm_chart(self, chart_items):
""" Makes a playlist from the currently most played tunes on last.fm
:param chart_items: The amount of tunes we want. The string must be able to be converted to int.
:type chart_items: str
"""
if self.is_client_mod:
if chart_items is 0 or chart_items is None:
self.send_bot_msg('Please specify the amount of tunes you want.')
else:
try:
_items = int(chart_items)
except ValueError:
self.send_bot_msg('Only numbers allowed.')
else:
if 0 < _items < 30:
self.send_bot_msg('Please wait while creating a playlist...')
last = lastfm.chart(_items)
if last is not None: #
self.media.add_track_list(self.active_user.nick, last)
self.send_bot_msg('*Added:* ' + str(len(last)) + ' *tunes from last.fm chart.*')
if not self.media.has_active_track():
track = self.media.get_next_track()
self.send_media_broadcast_start(track.type, track.id)
self.media_event_timer(track.time)
else:
self.send_bot_msg('Failed to retrieve a result from last.fm.')
else:
self.send_bot_msg('No more than 30 tunes.')
def do_lastfm_random_tunes(self, max_tunes):
""" Creates a playlist from what other people are listening to on last.fm.
:param max_tunes: The max amount of tunes. The string must be able to be converted to int.
:type max_tunes: str
"""
if self.is_client_mod:
if max_tunes is 0 or max_tunes is None:
self.send_bot_msg('Please specify the max amount of tunes you want.')
else:
try:
_items = int(max_tunes)
except ValueError:
self.send_bot_msg('Only numbers allowed.')
else:
if 0 < _items < 50:
self.send_bot_msg('Please wait while creating a playlist...')
last = lastfm.listening_now(max_tunes)
if last is not None:
self.media.add_track_list(self.active_user.nick, last)
self.send_bot_msg('*Added:* ' + str(len(last)) + ' *tunes from last.fm*')
if not self.media.has_active_track():
track = self.media.get_next_track()
self.send_media_broadcast_start(track.type, track.id)
self.media_event_timer(track.time)
else:
self.send_bot_msg('Failed to retrieve a result from last.fm.')
else:
self.send_bot_msg('No more than 50 tunes.')
def do_search_lastfm_by_tag(self, search_str):
""" Searches last.fm for tunes matching the search term and creates a playlist from them.
:param search_str: The search term to search for.
:type search_str: str
"""
if self.is_client_mod:
if len(search_str) is 0:
self.send_bot_msg('Missing search tag.')
else:
self.send_bot_msg('Please wait while creating playlist..')
last = lastfm.tag_search(search_str)
if last is not None:
self.media.add_track_list(self.active_user.nick, last)
self.send_bot_msg('*Added:* ' + str(len(last)) + ' *tunes from last.fm*')
if not self.media.has_active_track():
track = self.media.get_next_track()
self.send_media_broadcast_start(track.type, track.id)
self.media_event_timer(track.time)
else:
self.send_bot_msg('Failed to retrieve a result from last.fm.')
def do_youtube_playlist_search(self, search_term):
""" Searches youtube for a play list matching the search term.
:param search_term: The search to search for.
:type search_term: str
"""
if self.is_client_mod:
if len(search_term) is 0:
self.send_bot_msg('Missing search term.')
else:
self.search_list = youtube.playlist_search(search_term)
if len(self.search_list) is not 0:
self.is_search_list_youtube_playlist = True
for i in range(0, len(self.search_list)):
self.send_owner_run_msg('(%s) *%s*' % (i, self.search_list[i]['playlist_title']))
else:
self.send_bot_msg('Failed to find playlist matching search term: %s' % search_term)
def do_play_youtube_playlist(self, int_choice):
""" Finds the videos from the youtube playlist search.
:param int_choice: The index of the play list on the search_list.
The string must be able to be converted to int.
:type int_choice: str
"""
if self.is_client_mod:
if self.is_search_list_youtube_playlist:
try:
index_choice = int(int_choice)
except ValueError:
self.send_bot_msg('Only numbers allowed.')
else:
if 0 <= index_choice <= len(self.search_list) - 1:
self.send_bot_msg('Please wait while creating playlist..')
tracks = youtube.playlist_videos(self.search_list[index_choice]['playlist_id'])
if len(tracks) is not 0:
self.media.add_track_list(self.active_user.nick, tracks)
self.send_bot_msg('*Added:* %s *tracks from youtube playlist.*' % len(tracks))
if not self.media.has_active_track():
track = self.media.get_next_track()
self.send_media_broadcast_start(track.type, track.id)
self.media_event_timer(track.time)
else:
self.send_bot_msg('Failed to retrieve videos from youtube playlist.')
else:
self.send_bot_msg('Please make a choice between 0-%s' % str(len(self.search_list) - 1))
else:
self.send_bot_msg('The search list does not contain any youtube playlist id\'s.')
def do_close_broadcast(self, user_name):
""" Close a user broadcasting.
:param user_name: The username to close.
:type user_name: str
"""
if self.is_client_mod:
if len(user_name) is 0:
self.send_bot_msg('Missing username.')
else:
if self.users.search(user_name) is not None:
self.send_close_user_msg(user_name)
else:
self.send_bot_msg('No user named: ' + user_name)
def do_clear(self):
""" Clears the chat box. """
if self.is_client_mod:
for x in range(0, 10):
self.send_owner_run_msg(' ')
else:
clear = '133,133,133,133,133,133,133,133,133,133,133,133,133,133,133'
self.connection.call('privmsg', [clear, u'#262626,en'])
def do_skip(self):
""" Play the next item in the playlist. """
if self.is_client_mod:
if self.media.is_last_track():
self.send_bot_msg('*This is the last tune in the playlist.*')
elif self.media.is_last_track() is None:
self.send_bot_msg('*No tunes to skip. The playlist is empty.*')
else:
self.cancel_media_event_timer()
current_type = self.media.track().type
next_track = self.media.get_next_track()
if current_type != next_track.type:
self.send_media_broadcast_close(media_type=current_type)
self.send_media_broadcast_start(next_track.type, next_track.id)
self.media_event_timer(next_track.time)
def do_delete_playlist_item(self, to_delete):
""" Delete item(s) from the playlist by index.
:param to_delete: The index(es) to delete from the playlist.
:type to_delete: str
"""
if self.is_client_mod:
if len(self.media.track_list) is 0:
self.send_bot_msg('The track list is empty.')
elif len(to_delete) is 0:
self.send_bot_msg('No indexes to delete provided.')
else:
indexes = None
by_range = False
try:
if ':' in to_delete:
range_indexes = map(int, to_delete.split(':'))
temp_indexes = range(range_indexes[0], range_indexes[1] + 1)
if len(temp_indexes) > 1:
by_range = True
else:
temp_indexes = map(int, to_delete.split(','))
except ValueError as ve:
log.error('wrong format: %s' % ve)
else:
indexes = []
for i in temp_indexes:
if i < len(self.media.track_list) and i not in indexes:
indexes.append(i)
if indexes is not None and len(indexes) > 0:
result = self.media.delete_by_index(indexes, by_range)
if result is not None:
if by_range:
self.send_bot_msg('*Deleted from index:* %s *to index:* %s' %
(result['from'], result['to']))
elif result['deleted_indexes_len'] is 1:
self.send_bot_msg('*Deleted* %s' % result['track_title'])
else:
self.send_bot_msg('*Deleted tracks at index:* %s' %
', '.join(result['deleted_indexes']))
else:
self.send_bot_msg('Nothing was deleted.')
def do_media_replay(self):
""" Replays the last played media."""
if self.is_client_mod:
if self.media.track() is not None:
self.cancel_media_event_timer()
self.media.we_play(self.media.track())
self.send_media_broadcast_start(self.media.track().type,
self.media.track().id)
self.media_event_timer(self.media.track().time)
def do_play_media(self):
""" Resumes a track in pause mode. """
if self.is_client_mod:
track = self.media.track()
if track is not None:
if self.media.has_active_track():
self.cancel_media_event_timer()
if self.media.is_paused:
ntp = self.media.mb_play(self.media.elapsed_track_time())
self.send_media_broadcast_play(track.type, self.media.elapsed_track_time())
self.media_event_timer(ntp)
def do_media_pause(self):
""" Pause the media playing. """
if self.is_client_mod:
track = self.media.track()
if track is not None:
if self.media.has_active_track():
self.cancel_media_event_timer()
self.media.mb_pause()
self.send_media_broadcast_pause(track.type)
def do_close_media(self):
""" Closes the active media broadcast."""
if self.is_client_mod:
if self.media.has_active_track():
self.cancel_media_event_timer()
self.media.mb_close()
self.send_media_broadcast_close(self.media.track().type)
def do_seek_media(self, time_point):
""" Seek on a media playing.
:param time_point The time point to skip to.
:type time_point: str
"""
if self.is_client_mod:
if ('h' in time_point) or ('m' in time_point) or ('s' in time_point):
mls = pinylib.string_util.convert_to_millisecond(time_point)
if mls is 0:
self.console_write(pinylib.COLOR['bright_red'], 'invalid seek time.')
else:
track = self.media.track()
if track is not None:
if 0 < mls < track.time:
if self.media.has_active_track():
self.cancel_media_event_timer()
new_media_time = self.media.mb_skip(mls)
if not self.media.is_paused:
self.media_event_timer(new_media_time)
self.send_media_broadcast_skip(track.type, mls)
def do_clear_playlist(self):
""" Clear the playlist. """
if self.is_client_mod:
if len(self.media.track_list) > 0:
pl_length = str(len(self.media.track_list))
self.media.clear_track_list()
self.send_bot_msg('*Deleted* ' + pl_length + ' *items in the playlist.*')
else:
self.send_bot_msg('*The playlist is empty, nothing to delete.*')
def do_playlist_info(self):
""" Shows the next 5 tracks in the track list. """
if self.is_client_mod:
if len(self.media.track_list) > 0:
tracks = self.media.get_track_list()
if tracks is not None:
i = 0
for pos, track in tracks:
if i == 0:
self.send_owner_run_msg('(%s) *Next track: %s* %s' %
(pos, track.title, self.format_time(track.time)))
else:
self.send_owner_run_msg('(%s) *%s* %s' %
(pos, track.title, self.format_time(track.time)))
i += 1
else:
self.send_owner_run_msg('*No tracks in the playlist.*')
def do_show_search_list(self):