IRC rewrite continued

- Make functions for handling incoming IRC commands private where possible.
 - Add a few checks for input validation
 - Rewrite CTCP handling.
This commit is contained in:
Dpeta 2023-02-12 00:36:36 +01:00
parent fbe8f48d63
commit 1d4d1dbab6
No known key found for this signature in database
GPG key ID: 51227517CEA0030C
5 changed files with 389 additions and 295 deletions

593
irc.py

File diff suppressed because it is too large Load diff

View file

@ -61,11 +61,15 @@ class Mood:
} }
def __init__(self, mood): def __init__(self, mood):
if type(mood) is int: if isinstance(mood, int):
self.mood = mood self.mood = mood
else: else:
self.mood = self.moods.index(mood) self.mood = self.moods.index(mood)
def value_str(self):
"""Return mood index as str."""
return str(self.mood)
def value(self): def value(self):
return self.mood return self.mood

View file

@ -4320,7 +4320,12 @@ class MainProgram(QtCore.QObject):
self.attempts = 0 self.attempts = 0
# but it's at least better than the way it was before. # but it's at least better than the way it was before.
self.irc = PesterIRC(self.widget.config, self.widget) self.irc = PesterIRC(
self.widget,
self.widget.config.server(),
self.widget.config.port(),
self.widget.config.ssl(),
)
self.connectWidgets(self.irc, self.widget) self.connectWidgets(self.irc, self.widget)
self.widget.passIRC( self.widget.passIRC(

View file

@ -0,0 +1,71 @@
"""Provides functions for validating input from the server and other clients."""
# import re
# _color_rgb = re.compile(r"^\d{1,3},\d{1,3},\d{1,3}$")
# _color_hex = re.compile(r"^#(?:[0-9a-fA-F]{3}){1,2}$")
def is_valid_mood(value: str):
"""Returns True if an unparsed value (str) is a valid mood index."""
if value in [
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
]:
return True
return False
def is_valid_rgb_color(value: str):
"""Returns True if an unparsed value (str) is a valid rgb color as "r,g,b".
Yeah you could do this via re but this is faster."""
if not isinstance(value, str):
return False
if 4 > len(value) > 11:
return False
components = value.split(",")
if len(components) != 3:
return False
for component in components:
if not component.isnumeric():
return False
if int(component) > 255:
return False
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
"""

View file

@ -90,6 +90,13 @@ class SendIRC:
).strip() # Extra spaces break protocol, so strip. ).strip() # Extra spaces break protocol, so strip.
self.privmsg(target, f"\x01{outgoing_ctcp}\x01") self.privmsg(target, f"\x01{outgoing_ctcp}\x01")
def ctcp_reply(self, target, command, msg=""):
"""Send Client-to-Client Protocol reply message, responding to a CTCP message."""
outgoing_ctcp = " ".join(
[command, msg]
).strip() # Extra spaces break protocol, so strip.
self.notice(target, f"\x01{outgoing_ctcp}\x01")
def metadata(self, target, subcommand, *params): def metadata(self, target, subcommand, *params):
"""Send Metadata command to get or set metadata. """Send Metadata command to get or set metadata.