Further IRC linting/formatting

This commit is contained in:
Dpeta 2023-02-12 01:25:06 +01:00
parent 1157e49d9e
commit 9040ad0419
No known key found for this signature in database
GPG key ID: 51227517CEA0030C
3 changed files with 23 additions and 45 deletions

46
irc.py
View file

@ -5,7 +5,7 @@ References for the IRC protocol:
- Updated IRC Client specification: https://www.rfc-editor.org/rfc/rfc2812 - Updated IRC Client specification: https://www.rfc-editor.org/rfc/rfc2812
- Modern IRC client protocol writeup: https://modern.ircdocs.horse - Modern IRC client protocol writeup: https://modern.ircdocs.horse
- IRCv3 protocol additions: https://ircv3.net/irc/ - IRCv3 protocol additions: https://ircv3.net/irc/
- Draft of metadata specification: https://gist.github.com/k4bek4be/92c2937cefd49990fbebd001faf2b237 - Draft of metadata spec: https://gist.github.com/k4bek4be/92c2937cefd49990fbebd001faf2b237
Some code in this file may be based on the oyoyo IRC library, Some code in this file may be based on the oyoyo IRC library,
the license notice included with oyoyo source files is indented here: the license notice included with oyoyo source files is indented here:
@ -26,7 +26,6 @@ the license notice included with oyoyo source files is indented here:
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # THE SOFTWARE.
""" """
import socket import socket
import random import random
@ -77,9 +76,9 @@ class PesterIRC(QtCore.QThread):
self._send_irc = SendIRC() self._send_irc = SendIRC()
self.unresponsive = False self.unresponsive = False
self.registeredIRC = False self.registered_irc = False
self.metadata_supported = False self.metadata_supported = False
self.stopIRC = None self.stop_irc = None
self._conn = None self._conn = None
self._end = False # Set to True when ending connection. self._end = False # Set to True when ending connection.
self.joined = False self.joined = False
@ -130,8 +129,8 @@ class PesterIRC(QtCore.QThread):
is called on the main thread. Returning from this method ends the thread.""" is called on the main thread. Returning from this method ends the thread."""
try: try:
self.irc_connect() self.irc_connect()
except OSError as se: except OSError as socket_exception:
self.stopIRC = se self.stop_irc = socket_exception
return return
while True: while True:
res = True res = True
@ -139,13 +138,13 @@ class PesterIRC(QtCore.QThread):
PchumLog.debug("update_irc()") PchumLog.debug("update_irc()")
self.mainwindow.sincerecv = 0 self.mainwindow.sincerecv = 0
res = self.update_irc() res = self.update_irc()
except socket.timeout as se: except socket.timeout as timeout:
PchumLog.debug("timeout in thread %s", self) PchumLog.debug("timeout in thread %s", self)
self._close() self._close()
self.stopIRC = "{}, {}".format(type(se), se) self.stop_irc = f"{type(timeout)}, {timeout}"
return return
except (OSError, IndexError, ValueError) as se: except (OSError, IndexError, ValueError) as exception:
self.stopIRC = "{}, {}".format(type(se), se) self.stop_irc = f"{type(exception)}, {exception}"
PchumLog.debug("Socket error, exiting thread.") PchumLog.debug("Socket error, exiting thread.")
return return
else: else:
@ -198,13 +197,11 @@ class PesterIRC(QtCore.QThread):
else: else:
if self._end: if self._end:
break break
split_buffer = buffer.split(b"\r\n") split_buffer = buffer.split(b"\r\n")
buffer = b"" buffer = b""
if split_buffer[-1]: if split_buffer[-1]:
# Incomplete line, add it back to the buffer. # Incomplete line, add it back to the buffer.
buffer = split_buffer.pop() buffer = split_buffer.pop()
for line in split_buffer: for line in split_buffer:
line = line.decode(encoding="utf-8", errors="replace") line = line.decode(encoding="utf-8", errors="replace")
tags, prefix, command, args = parse_irc_line(line) tags, prefix, command, args = parse_irc_line(line)
@ -214,7 +211,6 @@ class PesterIRC(QtCore.QThread):
self._run_command(command, prefix, tags, *args) self._run_command(command, prefix, tags, *args)
else: else:
self._run_command(command, prefix, *args) self._run_command(command, prefix, *args)
yield True yield True
except OSError as socket_exception: except OSError as socket_exception:
PchumLog.warning( PchumLog.warning(
@ -271,9 +267,9 @@ class PesterIRC(QtCore.QThread):
raise e raise e
self._conn = self._conn_generator() self._conn = self._conn_generator()
def setConnectionBroken(self): def set_connection_broken(self):
"""Called when the connection is broken.""" """Called when the connection is broken."""
PchumLog.critical("setConnectionBroken() got called, disconnecting.") PchumLog.critical("set_connection_broken() got called, disconnecting.")
self.disconnectIRC() self.disconnectIRC()
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
@ -281,11 +277,10 @@ class PesterIRC(QtCore.QThread):
"""Get a silly scrunkler from the generator!!""" """Get a silly scrunkler from the generator!!"""
try: try:
res = next(self._conn) res = next(self._conn)
except socket.timeout as se: except socket.timeout as socket_exception:
if self.registeredIRC: if self.registered_irc:
return True return True
else: raise socket_exception
raise se
except StopIteration: except StopIteration:
self._conn = self.conn_generator() self._conn = self.conn_generator()
return True return True
@ -304,7 +299,7 @@ class PesterIRC(QtCore.QThread):
self._send_irc.metadata(chum.handle, "get", "mood") self._send_irc.metadata(chum.handle, "get", "mood")
except OSError as e: except OSError as e:
PchumLog.warning(e) PchumLog.warning(e)
self.setConnectionBroken() self.set_connection_broken()
else: else:
# Legacy # Legacy
PchumLog.warning( PchumLog.warning(
@ -317,7 +312,7 @@ class PesterIRC(QtCore.QThread):
self._send_irc.privmsg("#pesterchum", chumglub) self._send_irc.privmsg("#pesterchum", chumglub)
except OSError as e: except OSError as e:
PchumLog.warning(e) PchumLog.warning(e)
self.setConnectionBroken() self.set_connection_broken()
chumglub = "get_mood " chumglub = "get_mood "
# No point in get_mood-ing services # No point in get_mood-ing services
if chum.handle.casefold() not in SERVICES: if chum.handle.casefold() not in SERVICES:
@ -327,7 +322,7 @@ class PesterIRC(QtCore.QThread):
self._send_irc.privmsg("#pesterchum", chumglub) self._send_irc.privmsg("#pesterchum", chumglub)
except OSError as e: except OSError as e:
PchumLog.warning(e) PchumLog.warning(e)
self.setConnectionBroken() self.set_connection_broken()
@QtCore.pyqtSlot(PesterList) @QtCore.pyqtSlot(PesterList)
def get_moods(self, chums): def get_moods(self, chums):
@ -378,7 +373,6 @@ class PesterIRC(QtCore.QThread):
if b: # len > 0 if b: # len > 0
return [a] + splittext([b]) return [a] + splittext([b])
return [a] return [a]
else:
return l return l
textl = splittext(textl) textl = splittext(textl)
@ -593,7 +587,7 @@ class PesterIRC(QtCore.QThread):
def _error(self, *params): def _error(self, *params):
"""'ERROR' message from server, the server is terminating our connection.""" """'ERROR' message from server, the server is terminating our connection."""
self.stopIRC = " ".join(params).strip() self.stop_irc = " ".join(params).strip()
self.disconnectIRC() self.disconnectIRC()
def __ctcp(self, nick: str, chan: str, msg: str): def __ctcp(self, nick: str, chan: str, msg: str):
@ -805,7 +799,7 @@ class PesterIRC(QtCore.QThread):
def _welcome(self, _server, _nick, _msg): def _welcome(self, _server, _nick, _msg):
"""Numeric reply 001 RPL_WELCOME, send when we've connected to the server.""" """Numeric reply 001 RPL_WELCOME, send when we've connected to the server."""
self.registeredIRC = ( self.registered_irc = (
True # Registered as in, the server has accepted our nick & user. True # Registered as in, the server has accepted our nick & user.
) )
self.connected.emit() # Alert main thread that we've connected. self.connected.emit() # Alert main thread that we've connected.
@ -922,7 +916,7 @@ class PesterIRC(QtCore.QThread):
"""Numeric reply 432 ERR_ERRONEUSNICKNAME, we have a forbidden or protocol-breaking nick.""" """Numeric reply 432 ERR_ERRONEUSNICKNAME, we have a forbidden or protocol-breaking nick."""
# Server is not allowing us to connect. # Server is not allowing us to connect.
reason = "Handle is not allowed on this server.\n" + " ".join(args) reason = "Handle is not allowed on this server.\n" + " ".join(args)
self.stopIRC = reason.strip() self.stop_irc = reason.strip()
self.disconnectIRC() self.disconnectIRC()
def _nicknameinuse(self, _server, _cmd, nick, _msg): def _nicknameinuse(self, _server, _cmd, nick, _msg):

View file

@ -4462,7 +4462,7 @@ class MainProgram(QtCore.QObject):
self.widget.loadingscreen.tryAgain.connect(self.tryAgain) self.widget.loadingscreen.tryAgain.connect(self.tryAgain)
if ( if (
hasattr(self, "irc") hasattr(self, "irc")
and self.irc.registeredIRC and self.irc.registered_irc
and not self.irc.unresponsive and not self.irc.unresponsive
): ):
return return

View file

@ -53,19 +53,3 @@ def is_valid_rgb_color(value: str):
if int(component) > 255: if int(component) > 255:
return False return False
return True return True
"""
def is_valid_rgb_color(value: str):
"Returns True if an unparsed value (str) is a valid rgb color."
if re.search(_color_rgb, value):
return True
return False
def is_valid_hex_color(value: str):
"Returns True if an unparsed value (str) is a valid hex color."
if re.search(_color_hex, value):
return True
return False
"""