Move services functionality to services.py

This commit is contained in:
Dpeta 2023-03-18 01:31:34 +01:00
parent db69aecb56
commit e03060a433
No known key found for this signature in database
GPG key ID: 51227517CEA0030C
5 changed files with 59 additions and 47 deletions

10
irc.py
View file

@ -47,15 +47,7 @@ from scripts.ssl_context import get_ssl_context
from scripts.input_validation import is_valid_mood, is_valid_rgb_color from scripts.input_validation import is_valid_mood, is_valid_rgb_color
PchumLog = logging.getLogger("pchumLogger") PchumLog = logging.getLogger("pchumLogger")
SERVICES = [ from scripts.services import SERVICES
"nickserv",
"chanserv",
"memoserv",
"operserv",
"helpserv",
"hostserv",
"botserv",
]
class PesterIRC(QtCore.QThread): class PesterIRC(QtCore.QThread):

View file

@ -25,6 +25,7 @@ from parsetools import (
smiledict, smiledict,
) )
from logviewer import PesterLogViewer from logviewer import PesterLogViewer
from scripts.services import BOTNAMES
PchumLog = logging.getLogger("pchumLogger") PchumLog = logging.getLogger("pchumLogger")
_valid_memo_msg_start = re.compile( _valid_memo_msg_start = re.compile(
@ -389,7 +390,10 @@ class MemoText(PesterText):
window = parent.mainwindow window = parent.mainwindow
me = window.profile() me = window.profile()
if isinstance(msg, str): 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) msg = self.make_valid(msg, chum, parent, window, me)
lexmsg = lexMessage(msg) lexmsg = lexMessage(msg)
else: else:

View file

@ -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

View file

@ -16,7 +16,6 @@ if os.path.dirname(sys.argv[0]):
os.chdir(os.path.dirname(sys.argv[0])) os.chdir(os.path.dirname(sys.argv[0]))
import ostools import ostools
import nickservmsgs
import pytwmn import pytwmn
from user_profile import ( from user_profile import (
@ -61,6 +60,7 @@ from irc import PesterIRC
from logviewer import PesterLogUserSelect, PesterLogViewer from logviewer import PesterLogUserSelect, PesterLogViewer
from randomer import RandomHandler, RANDNICK from randomer import RandomHandler, RANDNICK
from toast import PesterToastMachine, PesterToast from toast import PesterToastMachine, PesterToast
from scripts.services import SERVICES, CUSTOMBOTS, BOTNAMES, translate_nickserv_msg
try: try:
from PyQt6 import QtCore, QtGui, QtWidgets, QtMultimedia from PyQt6 import QtCore, QtGui, QtWidgets, QtMultimedia
@ -133,20 +133,6 @@ stream_handler.setFormatter(formatter)
PchumLog.addHandler(file_handler) PchumLog.addHandler(file_handler)
PchumLog.addHandler(stream_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 # Command line arguments
_ARGUMENTS = parser.parse_args() _ARGUMENTS = parser.parse_args()
@ -1787,7 +1773,7 @@ class PesterWindow(MovingWindow):
t.show() t.show()
elif not self.config.notifyOptions() & self.config.NEWCONVO: elif not self.config.notifyOptions() & self.config.NEWCONVO:
if msg[:11] != "PESTERCHUM:": if msg[:11] != "PESTERCHUM:":
if handle.upper() not in BOTNAMES: if handle.casefold() not in BOTNAMES:
t = self.tm.Toast( t = self.tm.Toast(
"From: %s" % handle, re.sub("</?c(=.*?)?>", "", msg) "From: %s" % handle, re.sub("</?c(=.*?)?>", "", msg)
) )
@ -1912,12 +1898,12 @@ class PesterWindow(MovingWindow):
convoWindow.messageSent[str, str].connect(self.sendMessage[str, str]) convoWindow.messageSent[str, str].connect(self.sendMessage[str, str])
convoWindow.windowClosed[str].connect(self.closeConvo) convoWindow.windowClosed[str].connect(self.closeConvo)
self.convos[chum.handle] = convoWindow self.convos[chum.handle] = convoWindow
if chum.handle.upper() in BOTNAMES: if chum.handle.casefold() in BOTNAMES:
convoWindow.toggleQuirks(True) convoWindow.toggleQuirks(True)
convoWindow.quirksOff.setChecked(True) convoWindow.quirksOff.setChecked(True)
if ( if (
not self.config.irc_compatibility_mode() not self.config.irc_compatibility_mode()
or chum.handle.upper() in CUSTOMBOTS or chum.handle.casefold() in CUSTOMBOTS
): ):
self.newConvoStarted.emit(chum.handle, initiated) self.newConvoStarted.emit(chum.handle, initiated)
else: else:
@ -2450,11 +2436,11 @@ class PesterWindow(MovingWindow):
elif h in self.convos: elif h in self.convos:
self.newMessage(h, m) self.newMessage(h, m)
elif h.upper() == "NICKSERV" and "PESTERCHUM:" not in m: elif h.upper() == "NICKSERV" and "PESTERCHUM:" not in m:
m = nickservmsgs.translate(m) m = translate_nickserv_msg(m)
if m: if m:
t = self.tm.Toast("NickServ:", m) t = self.tm.Toast("NickServ:", m)
t.show() 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 # Show toast for rest services notices
# "Your VHOST is actived", "You have one new memo", etc. # "Your VHOST is actived", "You have one new memo", etc.
t = self.tm.Toast("%s:" % h, m) t = self.tm.Toast("%s:" % h, m)
@ -2742,7 +2728,7 @@ class PesterWindow(MovingWindow):
# Presented here so it can be called by other scripts. # Presented here so it can be called by other scripts.
@staticmethod @staticmethod
def isBot(handle): def isBot(handle):
return handle.upper() in BOTNAMES return handle.casefold() in BOTNAMES
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def showMemos(self, channel=""): def showMemos(self, channel=""):

46
scripts/services.py Normal file
View file

@ -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