From e03060a43339d923f95f8a6322619a1e0eefff10 Mon Sep 17 00:00:00 2001 From: Dpeta <69427753+Dpeta@users.noreply.github.com> Date: Sat, 18 Mar 2023 01:31:34 +0100 Subject: [PATCH] Move services functionality to services.py --- irc.py | 10 +--------- memos.py | 6 +++++- nickservmsgs.py | 16 ---------------- pesterchum.py | 28 +++++++-------------------- scripts/services.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 47 deletions(-) delete mode 100644 nickservmsgs.py create mode 100644 scripts/services.py diff --git a/irc.py b/irc.py index 0fe752a..8c4d2b6 100644 --- a/irc.py +++ b/irc.py @@ -47,15 +47,7 @@ from scripts.ssl_context import get_ssl_context from scripts.input_validation import is_valid_mood, is_valid_rgb_color PchumLog = logging.getLogger("pchumLogger") -SERVICES = [ - "nickserv", - "chanserv", - "memoserv", - "operserv", - "helpserv", - "hostserv", - "botserv", -] +from scripts.services import SERVICES class PesterIRC(QtCore.QThread): diff --git a/memos.py b/memos.py index f4a3d0d..af81610 100644 --- a/memos.py +++ b/memos.py @@ -25,6 +25,7 @@ from parsetools import ( smiledict, ) from logviewer import PesterLogViewer +from scripts.services import BOTNAMES PchumLog = logging.getLogger("pchumLogger") _valid_memo_msg_start = re.compile( @@ -389,7 +390,10 @@ class MemoText(PesterText): window = parent.mainwindow me = window.profile() if isinstance(msg, str): - if self.mainwindow.config.force_prefix(): + if ( + self.mainwindow.config.force_prefix() + and chum.handle.casefold() not in BOTNAMES + ): msg = self.make_valid(msg, chum, parent, window, me) lexmsg = lexMessage(msg) else: diff --git a/nickservmsgs.py b/nickservmsgs.py deleted file mode 100644 index 94ed4ab..0000000 --- a/nickservmsgs.py +++ /dev/null @@ -1,16 +0,0 @@ -# Hardcoded messages that NickServ sends and what to display to the user instead - -messages = { - "Your nick isn't registered.": "", # display the same - "Password accepted - you are now recognized.": "", # display the same - "If you do not change within one minute, I will change your nick.": "You have 1 minute to identify.", - "If you do not change within 20 seconds, I will change your nick.": "You have 20 seconds to identify.", -} - - -def translate(msg): - if msg in messages: - if messages[msg] == "": - return msg - return messages[msg] - return None diff --git a/pesterchum.py b/pesterchum.py index 9fb6f57..9e6a66a 100755 --- a/pesterchum.py +++ b/pesterchum.py @@ -16,7 +16,6 @@ if os.path.dirname(sys.argv[0]): os.chdir(os.path.dirname(sys.argv[0])) import ostools -import nickservmsgs import pytwmn from user_profile import ( @@ -61,6 +60,7 @@ from irc import PesterIRC from logviewer import PesterLogUserSelect, PesterLogViewer from randomer import RandomHandler, RANDNICK from toast import PesterToastMachine, PesterToast +from scripts.services import SERVICES, CUSTOMBOTS, BOTNAMES, translate_nickserv_msg try: from PyQt6 import QtCore, QtGui, QtWidgets, QtMultimedia @@ -133,20 +133,6 @@ stream_handler.setFormatter(formatter) PchumLog.addHandler(file_handler) PchumLog.addHandler(stream_handler) -# Global variables -BOTNAMES = [] -CUSTOMBOTS = ["CALSPRITE", RANDNICK.upper()] -SERVICES = [ - "NICKSERV", - "CHANSERV", - "MEMOSERV", - "OPERSERV", - "HELPSERV", - "HOSTSERV", - "BOTSERV", -] -BOTNAMES.extend(CUSTOMBOTS) -BOTNAMES.extend(SERVICES) # Command line arguments _ARGUMENTS = parser.parse_args() @@ -1787,7 +1773,7 @@ class PesterWindow(MovingWindow): t.show() elif not self.config.notifyOptions() & self.config.NEWCONVO: if msg[:11] != "PESTERCHUM:": - if handle.upper() not in BOTNAMES: + if handle.casefold() not in BOTNAMES: t = self.tm.Toast( "From: %s" % handle, re.sub("", "", msg) ) @@ -1912,12 +1898,12 @@ class PesterWindow(MovingWindow): convoWindow.messageSent[str, str].connect(self.sendMessage[str, str]) convoWindow.windowClosed[str].connect(self.closeConvo) self.convos[chum.handle] = convoWindow - if chum.handle.upper() in BOTNAMES: + if chum.handle.casefold() in BOTNAMES: convoWindow.toggleQuirks(True) convoWindow.quirksOff.setChecked(True) if ( not self.config.irc_compatibility_mode() - or chum.handle.upper() in CUSTOMBOTS + or chum.handle.casefold() in CUSTOMBOTS ): self.newConvoStarted.emit(chum.handle, initiated) else: @@ -2450,11 +2436,11 @@ class PesterWindow(MovingWindow): elif h in self.convos: self.newMessage(h, m) elif h.upper() == "NICKSERV" and "PESTERCHUM:" not in m: - m = nickservmsgs.translate(m) + m = translate_nickserv_msg(m) if m: t = self.tm.Toast("NickServ:", m) t.show() - elif ("PESTERCHUM:" not in m) and (h.upper() in SERVICES): + elif "PESTERCHUM:" not in m and h.casefold() in SERVICES: # Show toast for rest services notices # "Your VHOST is actived", "You have one new memo", etc. t = self.tm.Toast("%s:" % h, m) @@ -2742,7 +2728,7 @@ class PesterWindow(MovingWindow): # Presented here so it can be called by other scripts. @staticmethod def isBot(handle): - return handle.upper() in BOTNAMES + return handle.casefold() in BOTNAMES @QtCore.pyqtSlot() def showMemos(self, channel=""): diff --git a/scripts/services.py b/scripts/services.py new file mode 100644 index 0000000..40c10e4 --- /dev/null +++ b/scripts/services.py @@ -0,0 +1,46 @@ +"""Lists, dicts, and functions related to services.""" +from randomer import RANDNICK + +# List of all common services bots. (for .casefold() matching) +# Services packages that provide the bot are in the same-line comments. +SERVICES = [ + "nickserv", # Anope/Atheme/X3/DalekIRC + "chanserv", # Anope/Atheme/X3/DalekIRC + "memoserv", # Anope/Atheme + "operserv", # Anope/Atheme/DalekIRC + "helpserv", # Anope/Atheme/X3 + "hostserv", # Anope/Atheme + "botserv", # Anope/Atheme/DalekIRC + "global", # Anope/Atheme/DalekIRC + "alis", # Atheme + "chanfix", # Atheme + "gameserv", # Atheme + "groupserv", # Atheme + "infoserv", # Atheme + "statserv", # Atheme + "userserv", # Atheme + "authserv", # X3 + "opserv", # X3 + "metaserv", # DalekIRC + "bbserv", # DalekIRC +] +# Pesterchum bots +CUSTOMBOTS = ["calsprite", RANDNICK.casefold()] +# All bots +BOTNAMES = SERVICES + CUSTOMBOTS + +# Hardcoded messages that NickServ sends and what to display to the user instead +nickserv_messages = { + "Your nick isn't registered.": "", # display the same + "Password accepted - you are now recognized.": "", # display the same + "If you do not change within one minute, I will change your nick.": "You have 1 minute to identify.", + "If you do not change within 20 seconds, I will change your nick.": "You have 20 seconds to identify.", +} + + +def translate_nickserv_msg(msg): + if msg in nickserv_messages: + if not nickserv_messages[msg]: # == "": + return msg + return nickserv_messages[msg] + return None