Cleanup and remove python 2 QString == str

This commit is contained in:
Dpeta 2023-02-10 00:14:33 +01:00
parent e8a283ad99
commit 5f50f6420f
No known key found for this signature in database
GPG key ID: 51227517CEA0030C

130
irc.py
View file

@ -36,25 +36,6 @@ SERVICES = [
"botserv", "botserv",
] ]
class CommandError(Exception):
def __init__(self, cmd):
self.cmd = cmd
class NoSuchCommandError(CommandError):
def __str__(self):
return 'No such command "%s"' % ".".join(self.cmd)
class ProtectedCommandError(CommandError):
def __str__(self):
return 'Command "%s" is protected' % ".".join(self.cmd)
# Python 3
QString = str
try: try:
import certifi import certifi
except ImportError: except ImportError:
@ -72,10 +53,6 @@ except ImportError:
) )
class IRCClientError(Exception):
pass
class PesterIRC(QtCore.QThread): class PesterIRC(QtCore.QThread):
def __init__(self, config, window, verify_hostname=True): def __init__(self, config, window, verify_hostname=True):
QtCore.QThread.__init__(self) QtCore.QThread.__init__(self)
@ -96,7 +73,7 @@ class PesterIRC(QtCore.QThread):
self.send_irc = SendIRC() self.send_irc = SendIRC()
self.conn = None self.conn = None
self.joined = False self.joined = False
self.channelnames = {} self.channelnames = {}
self.channel_list = [] self.channel_list = []
@ -293,15 +270,11 @@ class PesterIRC(QtCore.QThread):
try: try:
self.socket.shutdown(socket.SHUT_RDWR) self.socket.shutdown(socket.SHUT_RDWR)
except OSError as e: except OSError as e:
PchumLog.debug( PchumLog.info("Error while shutting down socket, already broken? %s", e)
"Error while shutting down socket, already broken? %s" % str(e)
)
try: try:
self.socket.close() self.socket.close()
except OSError as e: except OSError as e:
PchumLog.debug( PchumLog.info("Error while closing socket, already broken? %s", e)
"Error while closing socket, already broken? %s" % str(e)
)
def IRCConnect(self): def IRCConnect(self):
try: try:
@ -325,7 +298,7 @@ class PesterIRC(QtCore.QThread):
self.mainwindow.sincerecv = 0 self.mainwindow.sincerecv = 0
res = self.updateIRC() res = self.updateIRC()
except socket.timeout as se: except socket.timeout as se:
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.stopIRC = "{}, {}".format(type(se), se)
return return
@ -339,13 +312,17 @@ class PesterIRC(QtCore.QThread):
return return
def setConnected(self): def setConnected(self):
"""Called when connected and registered to server.
Meaning the server has accepted our nick and user and has replied with 001/welcome.
"""
self.registeredIRC = True self.registeredIRC = True
self.connected.emit() self.connected.emit()
def setConnectionBroken(self): def setConnectionBroken(self):
PchumLog.critical("setconnection broken") """Called when the connection is broken."""
PchumLog.critical("setConnectionBroken() got called, disconnecting.")
self.disconnectIRC() self.disconnectIRC()
# self.brokenConnection = True # Unused
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def updateIRC(self): def updateIRC(self):
@ -407,14 +384,14 @@ class PesterIRC(QtCore.QThread):
def getMoods(self, chums): def getMoods(self, chums):
self.getMood(*chums) self.getMood(*chums)
@QtCore.pyqtSlot(QString, QString) @QtCore.pyqtSlot(str, str)
def sendNotice(self, text, handle): def sendNotice(self, text, handle):
self.send_irc.notice(handle, text) self.send_irc.notice(handle, text)
@QtCore.pyqtSlot(QString, QString) @QtCore.pyqtSlot(str, str)
def sendMessage(self, text, handle): def sendMessage(self, text, handle):
h = str(handle) """......sends a message? this is a tad silly;;;"""
textl = [str(text)] textl = [text]
def splittext(l): def splittext(l):
if len(l[0]) > 450: if len(l[0]) > 450:
@ -447,7 +424,7 @@ class PesterIRC(QtCore.QThread):
# start them up again in the second part # start them up again in the second part
for c in hanging: for c in hanging:
b = c + b b = c + b
if len(b) > 0: if b: # len > 0
return [a] + splittext([b]) return [a] + splittext([b])
else: else:
return [a] return [a]
@ -455,28 +432,20 @@ class PesterIRC(QtCore.QThread):
return l return l
textl = splittext(textl) textl = splittext(textl)
try: for t in textl:
for t in textl: self.send_irc.privmsg(handle, t)
self.send_irc.privmsg(h, t)
except OSError as e:
PchumLog.warning(e)
self.setConnectionBroken()
@QtCore.pyqtSlot( @QtCore.pyqtSlot(str, str)
QString,
QString,
)
def sendCTCP(self, handle, text): def sendCTCP(self, handle, text):
self.send_irc.ctcp(handle, text) self.send_irc.ctcp(handle, text)
@QtCore.pyqtSlot(QString, bool) @QtCore.pyqtSlot(str, bool)
def startConvo(self, handle, initiated): def startConvo(self, handle, initiated):
self.send_irc.privmsg(handle, f"COLOR >{self.mainwindow.profile().colorcmd()}") self.send_irc.privmsg(handle, f"COLOR >{self.mainwindow.profile().colorcmd()}")
if initiated: if initiated:
self.send_irc.privmsg(handle, "PESTERCHUM:BEGIN") self.send_irc.privmsg(handle, "PESTERCHUM:BEGIN")
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def endConvo(self, handle): def endConvo(self, handle):
self.send_irc.privmsg(handle, "PESTERCHUM:CEASE") self.send_irc.privmsg(handle, "PESTERCHUM:CEASE")
@ -493,11 +462,11 @@ class PesterIRC(QtCore.QThread):
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def updateMood(self): def updateMood(self):
me = self.mainwindow.profile() mood = str(self.mainwindow.profile().mood.value())
# Moods via metadata # Moods via metadata
self.send_irc.metadata("*", "set", "mood", str(me.mood.value())) self.send_irc.metadata("*", "set", "mood", mood)
# Backwards compatibility # Backwards compatibility
self.send_irc.privmsg("#pesterchum", f"MOOD >{me.mood.value()}") self.send_irc.privmsg("#pesterchum", f"MOOD >{mood}")
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def updateColor(self): def updateColor(self):
@ -505,21 +474,21 @@ class PesterIRC(QtCore.QThread):
color = self.mainwindow.profile().color color = self.mainwindow.profile().color
self.send_irc.metadata("*", "set", "color", str(color.name())) self.send_irc.metadata("*", "set", "color", str(color.name()))
# Send color messages # Send color messages
for h in list(self.mainwindow.convos.keys()): for convo in list(self.mainwindow.convos.keys()):
self.send_irc.privmsg( self.send_irc.privmsg(
h, convo,
"COLOR >%s" % (self.mainwindow.profile().colorcmd()), "COLOR >%s" % (self.mainwindow.profile().colorcmd()),
) )
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def blockedChum(self, handle): def blockedChum(self, handle):
self.send_irc.privmsg(handle, "PESTERCHUM:BLOCK") self.send_irc.privmsg(handle, "PESTERCHUM:BLOCK")
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def unblockedChum(self, handle): def unblockedChum(self, handle):
self.send_irc.privmsg(handle, "PESTERCHUM:UNBLOCK") self.send_irc.privmsg(handle, "PESTERCHUM:UNBLOCK")
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def requestNames(self, channel): def requestNames(self, channel):
self.send_irc.names(channel) self.send_irc.names(channel)
@ -527,28 +496,28 @@ class PesterIRC(QtCore.QThread):
def requestChannelList(self): def requestChannelList(self):
self.send_irc.list() self.send_irc.list()
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def joinChannel(self, channel): def joinChannel(self, channel):
self.send_irc.join(channel) self.send_irc.join(channel)
self.send_irc.mode(channel) self.send_irc.mode(channel)
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def leftChannel(self, channel): def leftChannel(self, channel):
self.send_irc.part(channel) self.send_irc.part(channel)
@QtCore.pyqtSlot(QString, QString, QString) @QtCore.pyqtSlot(str, str, str)
def kickUser(self, channel, user, reason=""): def kickUser(self, channel, user, reason=""):
self.send_irc.kick(channel, user, reason) self.send_irc.kick(channel, user, reason)
@QtCore.pyqtSlot(QString, QString, QString) @QtCore.pyqtSlot(str, str, str)
def setChannelMode(self, channel, mode, command): def setChannelMode(self, channel, mode, command):
self.send_irc.mode(channel, mode, command) self.send_irc.mode(channel, mode, command)
@QtCore.pyqtSlot(QString) @QtCore.pyqtSlot(str)
def channelNames(self, channel): def channelNames(self, channel):
self.send_irc.names(channel) self.send_irc.names(channel)
@QtCore.pyqtSlot(QString, QString) @QtCore.pyqtSlot(str, str)
def inviteChum(self, handle, channel): def inviteChum(self, handle, channel):
self.send_irc.invite(handle, channel) self.send_irc.invite(handle, channel)
@ -563,7 +532,7 @@ class PesterIRC(QtCore.QThread):
else: else:
self.away() self.away()
@QtCore.pyqtSlot(QString, QString) @QtCore.pyqtSlot(str, str)
def killSomeQuirks(self, channel, handle): def killSomeQuirks(self, channel, handle):
self.send_irc.ctcp(channel, "NOQUIRKS", handle) self.send_irc.ctcp(channel, "NOQUIRKS", handle)
@ -619,7 +588,16 @@ class PesterIRC(QtCore.QThread):
return return
PchumLog.info("Pesterchum tag: %s=%s", key, value) PchumLog.info("Pesterchum tag: %s=%s", key, value)
# PESTERCHUM: syntax check # PESTERCHUM: syntax check
if value in ["BEGIN", "BLOCK", "CEASE", "BLOCK", "BLOCKED", "UNBLOCK", "IDLE", "ME"]: if value in [
"BEGIN",
"BLOCK",
"CEASE",
"BLOCK",
"BLOCKED",
"UNBLOCK",
"IDLE",
"ME",
]:
# Process like it's a PESTERCHUM: PRIVMSG # Process like it's a PESTERCHUM: PRIVMSG
msg = "PESTERCHUM:" + value msg = "PESTERCHUM:" + value
self.privmsg(prefix, args[0], msg) self.privmsg(prefix, args[0], msg)
@ -799,9 +777,7 @@ class PesterIRC(QtCore.QThread):
PchumLog.warning( PchumLog.warning(
"Can't remove channel mode that isn't set." "Can't remove channel mode that isn't set."
) )
self.userPresentUpdate.emit( self.userPresentUpdate.emit("", channel, f"{channel_mode}:{op}")
"", channel, f"{channel_mode}:{op}"
)
PchumLog.debug("pre-mode=%s", mode) PchumLog.debug("pre-mode=%s", mode)
mode = mode.replace(md, "") mode = mode.replace(md, "")
PchumLog.debug("post-mode=%s", mode) PchumLog.debug("post-mode=%s", mode)
@ -869,7 +845,7 @@ class PesterIRC(QtCore.QThread):
self.send_irc.cap( self.send_irc.cap(
self, "REQ", "pesterchum-tag" self, "REQ", "pesterchum-tag"
) # <--- Currently not using this ) # <--- Currently not using this
#time.sleep(0.413 + 0.097) # <--- somehow, this actually helps. # time.sleep(0.413 + 0.097) # <--- somehow, this actually helps.
self.send_irc.join("#pesterchum") self.send_irc.join("#pesterchum")
# Moods via metadata # Moods via metadata
self.send_irc.metadata("*", "sub", "mood") self.send_irc.metadata("*", "sub", "mood")
@ -885,7 +861,7 @@ class PesterIRC(QtCore.QThread):
Not in the original specification. Not in the original specification.
Metadata support could be confirmed via CAP ACK/CAP NEK. Metadata support could be confirmed via CAP ACK/CAP NEK.
""" """
features = params[:-1] features = params[:-1]
PchumLog.info("Server featurelist: %s", features) PchumLog.info("Server featurelist: %s", features)
for x in features: for x in features:
@ -1032,7 +1008,7 @@ class PesterIRC(QtCore.QThread):
self.send_irc.privmsg("#pesterchum", f"GETMOOD {failed_handle}") self.send_irc.privmsg("#pesterchum", f"GETMOOD {failed_handle}")
def metadatasubok(self, *params): def metadatasubok(self, *params):
""""METADATA DRAFT numeric reply 770 RPL_METADATASUBOK, we subbed to a key.""" """ "METADATA DRAFT numeric reply 770 RPL_METADATASUBOK, we subbed to a key."""
PchumLog.info("metadatasubok: %s", params) PchumLog.info("metadatasubok: %s", params)
def run_command(self, command, *args): def run_command(self, command, *args):
@ -1043,11 +1019,13 @@ class PesterIRC(QtCore.QThread):
except KeyError: except KeyError:
PchumLog.warning("No matching function for command: %s(%s)", command, args) PchumLog.warning("No matching function for command: %s(%s)", command, args)
return return
try: try:
command_function(*args) command_function(*args)
except TypeError: except TypeError:
PchumLog.exception("Failed to pass command, did the server pass an unsupported paramater?") PchumLog.exception(
"Failed to pass command, did the server pass an unsupported paramater?"
)
except Exception: except Exception:
PchumLog.exception("Exception while parsing command.") PchumLog.exception("Exception while parsing command.")
@ -1072,8 +1050,10 @@ class PesterIRC(QtCore.QThread):
quirkDisable = QtCore.pyqtSignal("QString", "QString", "QString") quirkDisable = QtCore.pyqtSignal("QString", "QString", "QString")
signal_forbiddenchannel = QtCore.pyqtSignal("QString", "QString") signal_forbiddenchannel = QtCore.pyqtSignal("QString", "QString")
class SendIRC: class SendIRC:
"""Provides functions for outgoing IRC commands.""" """Provides functions for outgoing IRC commands."""
def __init__(self): def __init__(self):
self.socket = None # INET socket connected with server. self.socket = None # INET socket connected with server.