22# SPDX-License-Identifier: MIT
33import time
44
5-
65import adafruit_connection_manager
76
87ANSI_ESCAPE_CODES = [
@@ -22,7 +21,8 @@ class IRCClient:
2221 Handles interaction with IRC Server and makes incoming messages available.
2322
2423 :param radio: The network radio to connect with.
25- :param dict irc_config: Dictionary containing IRC configration for server, port, username and channel.
24+ :param dict irc_config: Dictionary containing IRC configration for
25+ server, port, username and channel.
2626 :param audio_interface: Optional interface to play audio from for beep messages
2727 :param beep_wave: Optional wave file to use for beep messages
2828 :param int max_line_length: Maximum characters per line to format messages into.
@@ -142,7 +142,7 @@ def update(self):
142142 # no data before timeout
143143 # print(e)
144144 if "ETIMEDOUT" not in str (e ):
145- raise RuntimeError (e )
145+ raise RuntimeError (e ) from e
146146 # raise RuntimeError("Connection timed out")
147147 return updated_display_lines
148148
@@ -163,7 +163,6 @@ def send_dm(self, to_user, message):
163163 """
164164 irc_command = f"PRIVMSG { to_user } :{ message } \r \n "
165165 self .socket .send (irc_command .encode ("utf-8" ))
166- # self.process_message(f":{self.config['username']}!~{self.config['username']}@localhost " + irc_command[:-2])
167166 color = self .get_color_for_user (to_user )
168167 self .message_buffer .append (f"DM out: <{ color } { to_user } { ANSI_RESET } > { message } " )
169168
@@ -205,7 +204,7 @@ def get_technical_name(self, nickname):
205204 if "ETIMEDOUT" in str (e ):
206205 whois_resp_lines = None
207206 else :
208- raise RuntimeError (e )
207+ raise RuntimeError (e ) from e
209208
210209 if whois_resp_lines is None :
211210 return None
@@ -221,10 +220,11 @@ def get_technical_name(self, nickname):
221220
222221 whois_response = parts [2 ].split (" " , 1 )[1 ]
223222 response_parts = whois_response .split (" " )
224- showname = response_parts [0 ]
225223 technical_name = f"*!{ response_parts [1 ]} @{ response_parts [2 ]} "
226224 return technical_name
227225
226+ return None
227+
228228 def ban (self , user ):
229229 """
230230 Ban the specified user from the channel
@@ -301,13 +301,14 @@ def split_string_chunks(s, chunk_size):
301301 return chunks
302302
303303 def process_message (self , message ):
304+ # pylint: disable=too-many-branches, too-many-statements
304305 """
305306 Process an incoming IRC message
306307 :param message: The message that came from the IRC server.
307308
308309 :return lines_added: The number of lines added to the display
309310 """
310-
311+ # pylint: disable=too-many-locals
311312 lines_added = 0
312313
313314 message = message .lstrip ("\x00 " )
@@ -317,12 +318,12 @@ def process_message(self, message):
317318 if message .startswith ("PING" ):
318319 pong_response = message .replace ("PING" , "PONG" )
319320 self .socket .send (f"{ pong_response } \r \n " .encode ("utf-8" ))
320- print (f "Responded to PING" )
321+ print ("Responded to PING" )
321322 return 0
322323
323324 # Parse IRC message format: :prefix COMMAND params
324325 parts = message .split (" " , 2 )
325-
326+ # pylint: disable=too-many-nested-blocks
326327 if len (parts ) >= 2 :
327328 command = parts [1 ]
328329 try :
@@ -331,7 +332,7 @@ def process_message(self, message):
331332 command_num = None
332333
333334 # End of MOTD - now we can join the channel
334- if command == "376" or command == "422" : # 422 is "no MOTD"
335+ if command in { "376" , "422" } : # 422 is "no MOTD"
335336 # join channel
336337 self .join ()
337338
@@ -357,7 +358,7 @@ def process_message(self, message):
357358 welcome_text = welcome_text [1 :]
358359
359360 print (
360- f"'{ welcome_text [0 :11 ]} ' startswith '{ self .config ['username' ]} ' ? { welcome_text .startswith (self .config ['username' ])} "
361+ f"'{ welcome_text [0 :11 ]} ' startswith '{ self .config ['username' ]} ' ? { welcome_text .startswith (self .config ['username' ])} " # pylint: disable=line-too-long
361362 )
362363 if welcome_text .startswith (self .config ["username" ]):
363364 welcome_text = welcome_text .replace (
@@ -451,11 +452,13 @@ def process_message(self, message):
451452 action_user = parts [0 ].split ("!" , 1 )[0 ][1 :]
452453 mode_msg_parts = parts [2 ].split (" " , 2 )
453454 if len (mode_msg_parts ) >= 3 :
454- channel , mode , target_user = mode_msg_parts
455+ channel , mode , target_user = (
456+ mode_msg_parts # pylint: disable=unused-variable
457+ )
455458 action_user_color = self .get_color_for_user (action_user )
456459 target_user_color = self .get_color_for_user (target_user )
457460 self .message_buffer .append (
458- f"{ action_user_color } { action_user } { ANSI_RESET } sets mode { mode } on { target_user_color } { target_user } { ANSI_RESET } "
461+ f"{ action_user_color } { action_user } { ANSI_RESET } sets mode { mode } on { target_user_color } { target_user } { ANSI_RESET } " # pylint: disable=line-too-long
459462 )
460463 lines_added += 1
461464
0 commit comments