88
99class Client (object ):
1010 listeners = []
11- def connect (self , config_class = None ):
11+
12+ def connect (self , config_class = None ):
1213
1314 self .fp = floodProtect ()
1415 if not hasattr (self , "connection" ):
1516 raise NoSocket ("{0} has no attribute 'connection'" .format (self ))
1617 if config_class is None :
1718 raise NoConfig ("config_class not a argument when calling connect" )
18-
19+
1920 self ._config = config_class
2021 self .socket = self .connection ((self ._config ["host" ], self ._config ["port" ]))
2122
2223 self ._config ["caps" ](self )
2324
2425 if self ._config .get ("password" ):
2526 self .send ("PASS {0}" .format (self ._config ["password" ]))
26-
27+
2728 self .send ("NICK {0}" .format (self ._config ["nickname" ]))
2829 self .send ("USER {0} * * :{1}" .format (self ._config ["ident" ], self ._config ["realname" ]))
29-
30+
3031 self ._channels = self ._config ["channels" ]
3132 self .loop = EventLoop (self .recv )
33+
3234 def recv (self ):
33- self .buffer = ""
35+ self .buffer = ""
3436 while not self .buffer .endswith ("\r \n " ):
3537 self .buffer += self .socket .recv (2048 ).decode ("utf-8" , errors = "replace" )
3638 self .buffer = self .buffer .strip ().split ("\r \n " )
3739 return self .buffer
40+
3841 def send (self , data ):
3942 if hasattr (self , "on_send" ):
4043 self .on_send (data )
4144 self .fp .queue_add (self .socket , "{0}\r \n " .format (data ).encode ("UTF-8" ))
45+
4246 def start (self ):
4347 self .loop .create_job ("main" , self .main_job )
4448 self .loop .run ()
49+
4550 def main_job (self , event ):
4651 """loop job to provide a event based system for clients."""
4752 args = {"event" : event , "bot" : self , "irc" : connection_wrapper (self ), "args" : " " .join (event .arguments ).split (" " )[1 :]}
48-
49- #add arguments from event, for easier access
53+
54+ # add arguments from event, for easier access
5055 args .update ({k : getattr (event , k ) for k in dir (event ) if not k .startswith ("__" ) and not k .endswith ("__" )})
51-
56+
5257 if event .type == "001" :
5358 for channel in self ._channels :
5459 self .send ("JOIN {0}" .format (channel ))
5560
5661 to_call = []
57-
62+
5863 if hasattr (self , "on_all" ):
5964 to_call .append (self .on_all )
60-
61- if hasattr (self , "on_" + event .type .lower ()):
62- to_call .append (getattr (self , "on_" + event .type .lower ()))
63-
65+
66+ if hasattr (self , "on_" + event .type .lower ()):
67+ to_call .append (getattr (self , "on_" + event .type .lower ()))
68+
6469 if event .type != event .text_type :
65- if hasattr (self , "on_" + event .text_type .lower ()):
66- to_call .append (getattr (self , "on_" + event .text_type .lower ()))
70+ if hasattr (self , "on_" + event .text_type .lower ()):
71+ to_call .append (getattr (self , "on_" + event .text_type .lower ()))
6772
6873 for event_name , func in self .listeners :
6974 if event_name == event .text_type .lower () or event_name == event .type .lower ():
7075 to_call .append (func )
71- #Call the functions here
76+ # Call the functions here
7277 for call_func in to_call :
7378 util .function_argument_call (call_func , args )()
74-
79+
7580 if event .type == "PING" :
7681 self .send ("PONG :{0}" .format (" " .join (event .arguments )))
77-
78- #CTCP Replies
82+
83+ # CTCP Replies
7984 if event .type == "PRIVMSG" and " " .join (event .arguments ).startswith ("\x01 " ) and hasattr (self , "ctcp" ):
8085 ctcp_message = " " .join (event .arguments ).replace ("\x01 " , "" ).upper ()
8186 if ctcp_message in self .ctcp .keys ():
@@ -84,16 +89,19 @@ def main_job(self, event):
8489 else :
8590 result = self .ctcp [ctcp_message ]
8691 self .send ("NOTICE {0} :{1} {2}" .format (event .source .nick , ctcp_message , result ))
87- #Basic client use
92+
93+ # Basic client use
8894 def privmsg (self , channel , message ):
8995 MSGLEN = 400 - len ("PRIVMSG {} :\r \n " .format (channel ).encode ())
90- strings = [message [i :i + MSGLEN ] for i in range (0 , len (message ), MSGLEN )]
96+ strings = [message [i :i + MSGLEN ] for i in range (0 , len (message ), MSGLEN )]
9197 for message in strings :
9298 self .send ("PRIVMSG {0} :{1}" .format (channel , message ))
99+
93100 def reply (self , event , message ):
94101 if event .target == self ._config ['nickname' ]:
95102 self .privmsg (event .source .nick , message )
96103 else :
97104 self .privmsg (event .target , message )
105+
98106 def listen (self , func , event_name ):
99107 self .listeners .append ((event_name .lower (), func ))
0 commit comments