@@ -456,6 +456,15 @@ def _yield_tasks(now: float) -> Generator[Task, None, None]:
456
456
yield from Task ._tasks_once
457
457
Task ._tasks_once .clear ()
458
458
459
+ @staticmethod
460
+ def get_next_tick_target () -> float | None :
461
+ while Task ._tasks_queue :
462
+ target , _tid , task = Task ._tasks_queue [0 ]
463
+ if task .cancelled :
464
+ heapq .heappop (Task ._tasks_queue )
465
+ continue
466
+ return target
467
+
459
468
@staticmethod
460
469
def tick () -> float | None :
461
470
"""
@@ -482,11 +491,7 @@ def tick() -> float | None:
482
491
483
492
Task .running_task = None
484
493
485
- while Task ._tasks_queue :
486
- target , _tid , task = Task ._tasks_queue [0 ]
487
- if task .cancelled :
488
- heapq .heappop (Task ._tasks_queue )
489
- continue
494
+ if target := Task .get_next_tick_target ():
490
495
return target - now
491
496
492
497
@@ -597,7 +602,7 @@ def _getAuth(self, name: str, password: str) -> str | None:
597
602
def _auth (self ):
598
603
auid = self ._getAuth (self ._mgr .name , self ._mgr .password )
599
604
if auid is None :
600
- self ._callEvent ("onLoginFail" )
605
+ self ._mgr . _callEvent (self , "onLoginFail" )
601
606
return False
602
607
self ._sendCommand ("tlogin" , auid , "2" )
603
608
self ._setWriteLock (True )
@@ -606,7 +611,7 @@ def _auth(self):
606
611
def disconnect (self ):
607
612
"""Disconnect the bot from PM"""
608
613
self ._disconnect ()
609
- self ._callEvent ("onPMDisconnect" )
614
+ self ._mgr . _callEvent (self , "onPMDisconnect" )
610
615
611
616
def _disconnect (self ):
612
617
self .connected = False
@@ -674,7 +679,7 @@ def _process(self, data: str):
674
679
if not self .connected :
675
680
return
676
681
677
- self ._callEvent ("onRaw" , data )
682
+ self ._mgr . _callEvent (self , "onRaw" , data )
678
683
cmd , * args = data .split (":" )
679
684
func = "_rcmd_" + cmd
680
685
try :
@@ -690,7 +695,7 @@ def _rcmd_OK(self, _args: list[str]):
690
695
self ._setWriteLock (False )
691
696
self ._sendCommand ("wl" )
692
697
self ._sendCommand ("getblock" )
693
- self ._callEvent ("onPMConnect" )
698
+ self ._mgr . _callEvent (self , "onPMConnect" )
694
699
695
700
def _rcmd_wl (self , args : list [str ]):
696
701
self .contacts = set ()
@@ -707,14 +712,14 @@ def _rcmd_wl(self, args: list[str]):
707
712
print (" -> " , name , last_on , is_on , idle )
708
713
continue
709
714
self .contacts .add (user )
710
- self ._callEvent ("onPMContactlistReceive" )
715
+ self ._mgr . _callEvent (self , "onPMContactlistReceive" )
711
716
712
717
def _rcmd_block_list (self , args : list [str ]):
713
718
new_blocklist = {User (name ) for name in args if name != "" }
714
719
if self .blocklist :
715
720
for user in new_blocklist - self .blocklist :
716
721
self .blocklist .add (user )
717
- self ._callEvent ("onPMBlock" , user )
722
+ self ._mgr . _callEvent (self , "onPMBlock" , user )
718
723
self .blocklist = new_blocklist
719
724
720
725
def _rcmd_idleupdate (self , args : list [str ]):
@@ -736,7 +741,7 @@ def _rcmd_status(self, args: list[str]):
736
741
737
742
def _rcmd_DENIED (self , _args : list [str ]):
738
743
self ._disconnect ()
739
- self ._callEvent ("onLoginFail" )
744
+ self ._mgr . _callEvent (self , "onLoginFail" )
740
745
741
746
def _rcmd_msg (self , args : list [str ]):
742
747
user = User (args [0 ])
@@ -763,40 +768,40 @@ def _rcmd_msg(self, args: list[str]):
763
768
)
764
769
765
770
self .msgs [msgtime ] = msg
766
- self ._callEvent ("onPMMessage" , user , msg )
771
+ self ._mgr . _callEvent (self , "onPMMessage" , user , msg )
767
772
768
773
def _rcmd_msgoff (self , args : list [str ]):
769
774
user = User (args [0 ])
770
775
body = _strip_html (":" .join (args [5 :]))
771
- self ._callEvent ("onPMOfflineMessage" , user , body )
776
+ self ._mgr . _callEvent (self , "onPMOfflineMessage" , user , body )
772
777
773
778
def _rcmd_wladd (self , args : list [str ]):
774
779
user = User (args [0 ])
775
780
self ._updateStatus (user , args [1 ], int (args [2 ]), args [2 ])
776
781
self .contacts .add (user )
777
- self ._callEvent ("onPMContactAdd" , user )
782
+ self ._mgr . _callEvent (self , "onPMContactAdd" , user )
778
783
779
784
def _rcmd_wldelete (self , args : list [str ]):
780
785
user = User (args [0 ])
781
786
del self .status [user ]
782
787
self .contacts .remove (user )
783
- self ._callEvent ("onPMContactRemove" , user )
788
+ self ._mgr . _callEvent (self , "onPMContactRemove" , user )
784
789
785
790
def _rcmd_wlapp (self , args : list [str ]):
786
791
user = User (args [0 ])
787
792
self .status [user ] = (0 , True )
788
- self ._callEvent ("onPMContactOnline" , user )
793
+ self ._mgr . _callEvent (self , "onPMContactOnline" , user )
789
794
790
795
def _rcmd_wlonline (self , args : list [str ]):
791
796
user = User (args [0 ])
792
797
self .status [user ] = (0 , True )
793
- self ._callEvent ("onPMContactOnline" , user )
798
+ self ._mgr . _callEvent (self , "onPMContactOnline" , user )
794
799
795
800
def _rcmd_wloffline (self , args : list [str ]):
796
801
user = User (args [0 ])
797
802
last_on = int (float (args [1 ]))
798
803
self .status [user ] = (last_on , False )
799
- self ._callEvent ("onPMContactOffline" , user )
804
+ self ._mgr . _callEvent (self , "onPMContactOffline" , user )
800
805
801
806
def _rcmd_kickingoff (self , _args : list [str ]):
802
807
self .disconnect ()
@@ -809,15 +814,15 @@ def _rcmd_unblocked(self, args: list[str]):
809
814
user = User (args [0 ])
810
815
if user in self .blocklist :
811
816
self .blocklist .remove (user )
812
- self ._callEvent ("onPMUnblock" , user )
817
+ self ._mgr . _callEvent (self , "onPMUnblock" , user )
813
818
814
819
####
815
820
# Commands
816
821
####
817
822
def ping (self ):
818
823
"""send a ping"""
819
824
self ._sendCommand ("" )
820
- self ._callEvent ("onPMPing" )
825
+ self ._mgr . _callEvent (self , "onPMPing" )
821
826
822
827
def message (self , user : User , msg : str ):
823
828
"""send a pm to a user"""
@@ -875,10 +880,6 @@ def getIdle(self, user: User):
875
880
####
876
881
# Util
877
882
####
878
- def _callEvent (self , evt : str , * args : Any , ** kw : Any ):
879
- getattr (self ._mgr , evt )(self , * args , ** kw )
880
- self ._mgr .onEventCalled (self , evt , * args , ** kw )
881
-
882
883
def _write (self , data : bytes ):
883
884
if self ._wlock :
884
885
self ._wlockbuf += data
@@ -994,7 +995,7 @@ def reconnect(self):
994
995
def disconnect (self ):
995
996
"""Disconnect."""
996
997
self ._disconnect ()
997
- self ._callEvent ("onDisconnect" )
998
+ self ._mgr . _callEvent (self , "onDisconnect" )
998
999
999
1000
def _disconnect (self ):
1000
1001
"""Disconnect from the server."""
@@ -1111,7 +1112,7 @@ def _process(self, line: str):
1111
1112
if not self .connected :
1112
1113
return
1113
1114
1114
- self ._callEvent ("onRaw" , line )
1115
+ self ._mgr . _callEvent (self , "onRaw" , line )
1115
1116
cmd , * args = line .split (":" )
1116
1117
func = "_rcmd_" + cmd
1117
1118
if hasattr (self , func ):
@@ -1142,7 +1143,7 @@ def _rcmd_ok(self, args: list[str]):
1142
1143
self ._sendCommand ("blogin" , self ._mgr .name )
1143
1144
# if name and password is provided but fail to login
1144
1145
elif args [2 ] != "M" : # unsuccessful login
1145
- self ._callEvent ("onLoginFail" )
1146
+ self ._mgr . _callEvent (self , "onLoginFail" )
1146
1147
self .disconnect ()
1147
1148
# Successful login
1148
1149
elif args [2 ] == "M" :
@@ -1161,23 +1162,23 @@ def _rcmd_pwdok(self, _args: list[str]):
1161
1162
1162
1163
def _rcmd_denied (self , _args : list [str ]):
1163
1164
self ._disconnect ()
1164
- self ._callEvent ("onConnectFail" )
1165
+ self ._mgr . _callEvent (self , "onConnectFail" )
1165
1166
1166
1167
def _rcmd_inited (self , _args : list [str ]):
1167
1168
self ._sendCommand ("g_participants" , "start" )
1168
1169
self ._sendCommand ("getpremium" , "1" )
1169
1170
self .requestBanlist ()
1170
1171
self .requestUnBanlist ()
1171
1172
if self ._connectAmount == 0 :
1172
- self ._callEvent ("onConnect" )
1173
+ self ._mgr . _callEvent (self , "onConnect" )
1173
1174
for msg in reversed (self ._i_log ):
1174
1175
user = msg .user
1175
- self ._callEvent ("onHistoryMessage" , user , msg )
1176
+ self ._mgr . _callEvent (self , "onHistoryMessage" , user , msg )
1176
1177
self ._addHistory (msg )
1177
1178
self ._i_log .clear ()
1178
- self ._callEvent ("onHistoryMessageUpdate" )
1179
+ self ._mgr . _callEvent (self , "onHistoryMessageUpdate" )
1179
1180
else :
1180
- self ._callEvent ("onReconnect" )
1181
+ self ._mgr . _callEvent (self , "onReconnect" )
1181
1182
# we do not repeat onHistoryMessage calls but we still need to clear the log
1182
1183
# in case the users uses getMoreHistory
1183
1184
self ._i_log .clear ()
@@ -1200,11 +1201,11 @@ def _rcmd_mods(self, args: list[str]):
1200
1201
premods = self ._mods
1201
1202
for user in mods - premods : # modded
1202
1203
self ._mods .add (user )
1203
- self ._callEvent ("onModAdd" , user )
1204
+ self ._mgr . _callEvent (self , "onModAdd" , user )
1204
1205
for user in premods - mods : # demodded
1205
1206
self ._mods .remove (user )
1206
- self ._callEvent ("onModRemove" , user )
1207
- self ._callEvent ("onModChange" )
1207
+ self ._mgr . _callEvent (self , "onModRemove" , user )
1208
+ self ._mgr . _callEvent (self , "onModChange" )
1208
1209
1209
1210
def _rcmd_b (self , args : list [str ]):
1210
1211
mtime = float (args [0 ])
@@ -1254,7 +1255,7 @@ def _rcmd_u(self, args: list[str]):
1254
1255
if msg := self ._mqueue .pop (args [0 ], None ):
1255
1256
msg .attach (self , args [1 ])
1256
1257
self ._addHistory (msg )
1257
- self ._callEvent ("onMessage" , msg .user , msg )
1258
+ self ._mgr . _callEvent (self , "onMessage" , msg .user , msg )
1258
1259
1259
1260
def _rcmd_i (self , args : list [str ]):
1260
1261
mtime = float (args [0 ])
@@ -1304,10 +1305,10 @@ def _rcmd_gotmore(self, _args: list[str]):
1304
1305
self ._gettingmorehistory = False
1305
1306
for msg in reversed (self ._i_log ):
1306
1307
user = msg .user
1307
- self ._callEvent ("onHistoryMessage" , user , msg )
1308
+ self ._mgr . _callEvent (self , "onHistoryMessage" , user , msg )
1308
1309
self ._addHistory (msg )
1309
1310
self ._i_log .clear ()
1310
- self ._callEvent ("onHistoryMessageUpdate" )
1311
+ self ._mgr . _callEvent (self , "onHistoryMessageUpdate" )
1311
1312
1312
1313
def _rcmd_nomore (self , _args : list [str ]):
1313
1314
self ._ihistoryIndex = None
@@ -1353,7 +1354,7 @@ def _rcmd_participant(self, args: list[str]):
1353
1354
user .removeSessionId (self , args [1 ])
1354
1355
self ._userlist .remove (user )
1355
1356
if user not in self ._userlist or not self ._mgr .userlistEventUnique :
1356
- self ._callEvent ("onLeave" , user , puid )
1357
+ self ._mgr . _callEvent (self , "onLeave" , user , puid )
1357
1358
else : # join
1358
1359
user .addSessionId (self , args [1 ])
1359
1360
if user not in self ._userlist :
@@ -1362,23 +1363,23 @@ def _rcmd_participant(self, args: list[str]):
1362
1363
doEvent = False
1363
1364
self ._userlist .append (user )
1364
1365
if doEvent or not self ._mgr .userlistEventUnique :
1365
- self ._callEvent ("onJoin" , user , puid )
1366
+ self ._mgr . _callEvent (self , "onJoin" , user , puid )
1366
1367
1367
1368
def _rcmd_show_fw (self , _args : list [str ]):
1368
- self ._callEvent ("onFloodWarning" )
1369
+ self ._mgr . _callEvent (self , "onFloodWarning" )
1369
1370
1370
1371
def _rcmd_show_tb (self , _args : list [str ]):
1371
- self ._callEvent ("onFloodBan" )
1372
+ self ._mgr . _callEvent (self , "onFloodBan" )
1372
1373
1373
1374
def _rcmd_tb (self , _args : list [str ]):
1374
- self ._callEvent ("onFloodBanRepeat" )
1375
+ self ._mgr . _callEvent (self , "onFloodBanRepeat" )
1375
1376
1376
1377
def _rcmd_delete (self , args : list [str ]):
1377
1378
msg = self .msgs .get (args [0 ])
1378
1379
if msg :
1379
1380
if msg in self .history :
1380
1381
self .history .remove (msg )
1381
- self ._callEvent ("onMessageDelete" , msg .user , msg )
1382
+ self ._mgr . _callEvent (self , "onMessageDelete" , msg .user , msg )
1382
1383
msg .detach ()
1383
1384
1384
1385
def _rcmd_deleteall (self , args : list [str ]):
@@ -1387,7 +1388,7 @@ def _rcmd_deleteall(self, args: list[str]):
1387
1388
1388
1389
def _rcmd_n (self , args : list [str ]):
1389
1390
self .usercount = int (args [0 ], 16 )
1390
- self ._callEvent ("onUserCountChange" )
1391
+ self ._mgr . _callEvent (self , "onUserCountChange" )
1391
1392
1392
1393
def _rcmd_blocklist (self , args : list [str ]):
1393
1394
self ._banlist = dict ()
@@ -1400,7 +1401,7 @@ def _rcmd_blocklist(self, args: list[str]):
1400
1401
continue
1401
1402
user = User (p [2 ])
1402
1403
self ._banlist [user ] = BanRecord (p [0 ], p [1 ], user , float (p [3 ]), User (p [4 ]))
1403
- self ._callEvent ("onBanlistUpdate" )
1404
+ self ._mgr . _callEvent (self , "onBanlistUpdate" )
1404
1405
1405
1406
def _rcmd_unblocklist (self , args : list [str ]):
1406
1407
self ._unbanlist = dict ()
@@ -1413,7 +1414,7 @@ def _rcmd_unblocklist(self, args: list[str]):
1413
1414
continue
1414
1415
user = User (p [2 ])
1415
1416
self ._unbanlist [user ] = BanRecord (p [0 ], p [1 ], user , float (p [3 ]), User (p [4 ]))
1416
- self ._callEvent ("onUnBanlistUpdate" )
1417
+ self ._mgr . _callEvent (self , "onUnBanlistUpdate" )
1417
1418
1418
1419
def _rcmd_blocked (self , args : list [str ]):
1419
1420
if args [2 ] == "" :
@@ -1422,7 +1423,7 @@ def _rcmd_blocked(self, args: list[str]):
1422
1423
user = User (args [3 ])
1423
1424
self ._banlist [target ] = BanRecord (args [0 ], args [1 ], target , float (args [4 ]), user )
1424
1425
1425
- self ._callEvent ("onBan" , user , target )
1426
+ self ._mgr . _callEvent (self , "onBan" , user , target )
1426
1427
1427
1428
def _rcmd_unblocked (self , args : list [str ]):
1428
1429
if args [2 ] == "" :
@@ -1431,7 +1432,7 @@ def _rcmd_unblocked(self, args: list[str]):
1431
1432
user = User (args [3 ])
1432
1433
del self ._banlist [target ]
1433
1434
self ._unbanlist [user ] = BanRecord (args [0 ], args [1 ], target , float (args [4 ]), user )
1434
- self ._callEvent ("onUnban" , user , target )
1435
+ self ._mgr . _callEvent (self , "onUnban" , user , target )
1435
1436
1436
1437
####
1437
1438
# Commands
@@ -1452,7 +1453,7 @@ def logout(self):
1452
1453
def ping (self ):
1453
1454
"""Send a ping."""
1454
1455
self ._sendCommand ("" )
1455
- self ._callEvent ("onPing" )
1456
+ self ._mgr . _callEvent (self , "onPing" )
1456
1457
1457
1458
def rawMessage (self , msg : str ):
1458
1459
"""
@@ -1680,10 +1681,6 @@ def _getBanRecord(self, user: User):
1680
1681
return self ._banlist [user ]
1681
1682
return None
1682
1683
1683
- def _callEvent (self , evt : str , * args : ..., ** kw : ...):
1684
- getattr (self ._mgr , evt )(self , * args , ** kw )
1685
- self ._mgr .onEventCalled (self , evt , * args , ** kw )
1686
-
1687
1684
def _write (self , data : bytes ):
1688
1685
if self ._wlock :
1689
1686
self ._wlockbuf += data
@@ -1880,6 +1877,10 @@ def safePrint(self, text: str):
1880
1877
except UnicodeEncodeError as ex :
1881
1878
text = (text [0 :ex .start ]+ '(unicode)' + text [ex .end :])
1882
1879
1880
+ def _callEvent (self , conn : Conn , evt : str , * args : ..., ** kw : ...):
1881
+ getattr (self , evt )(conn , * args , ** kw )
1882
+ self .onEventCalled (conn , evt , * args , ** kw )
1883
+
1883
1884
def onConnect (self , room : Room ):
1884
1885
"""
1885
1886
Called when connected to the room.
@@ -2163,7 +2164,7 @@ def onPMContactOffline(self, pm: PM, user: User):
2163
2164
@param user: the user that went offline
2164
2165
"""
2165
2166
2166
- def onEventCalled (self , room : Room | PM , evt : str , * args : ..., ** kw : ...):
2167
+ def onEventCalled (self , room : Conn , evt : str , * args : ..., ** kw : ...):
2167
2168
"""
2168
2169
Called on every room-based event.
2169
2170
0 commit comments