Commands dict
This commit is contained in:
parent
d1e9a5e517
commit
3afa771326
1 changed files with 61 additions and 30 deletions
91
irc.py
91
irc.py
|
@ -35,6 +35,7 @@ SERVICES = [
|
||||||
"botserv",
|
"botserv",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class CommandError(Exception):
|
class CommandError(Exception):
|
||||||
def __init__(self, cmd):
|
def __init__(self, cmd):
|
||||||
self.cmd = cmd
|
self.cmd = cmd
|
||||||
|
@ -49,6 +50,7 @@ class ProtectedCommandError(CommandError):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'Command "%s" is protected' % ".".join(self.cmd)
|
return 'Command "%s" is protected' % ".".join(self.cmd)
|
||||||
|
|
||||||
|
|
||||||
# Python 3
|
# Python 3
|
||||||
QString = str
|
QString = str
|
||||||
|
|
||||||
|
@ -68,9 +70,11 @@ except ImportError:
|
||||||
"certificates if the system-provided root certificates are invalid."
|
"certificates if the system-provided root certificates are invalid."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class IRCClientError(Exception):
|
class IRCClientError(Exception):
|
||||||
pass
|
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)
|
||||||
|
@ -81,17 +85,52 @@ class PesterIRC(QtCore.QThread):
|
||||||
self.verify_hostname = verify_hostname
|
self.verify_hostname = verify_hostname
|
||||||
self.metadata_supported = False
|
self.metadata_supported = False
|
||||||
self.stopIRC = None
|
self.stopIRC = None
|
||||||
self.NickServ = services.NickServ()
|
|
||||||
self.ChanServ = services.ChanServ()
|
|
||||||
|
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.host = self.config.server()
|
self.host = self.config.server()
|
||||||
self.port = self.config.port()
|
self.port = self.config.port()
|
||||||
self.ssl = self.config.ssl()
|
self.ssl = self.config.ssl()
|
||||||
self._end = False
|
self._end = False
|
||||||
|
|
||||||
self.send_irc = scripts.irc.outgoing.SendIRC()
|
self.send_irc = scripts.irc.outgoing.SendIRC()
|
||||||
|
|
||||||
|
self.conn = None
|
||||||
|
self.joined = False
|
||||||
|
|
||||||
|
self.commands = {
|
||||||
|
"001": self.welcome,
|
||||||
|
"005": self.featurelist,
|
||||||
|
"301": self.away,
|
||||||
|
"321": self.liststart,
|
||||||
|
"322": self.list,
|
||||||
|
"323": self.listend,
|
||||||
|
"324": self.channelmodeis,
|
||||||
|
"432": self.erroneusnickname,
|
||||||
|
"433": self.nicknameinuse,
|
||||||
|
"436": self.nickcollision,
|
||||||
|
"448": self.forbiddenchannel, # non-standard
|
||||||
|
"473": self.inviteonlychan,
|
||||||
|
"761": self.keyvalue, # 7XX is ircv3 deprecated metadata spec
|
||||||
|
"762": self.metadataend,
|
||||||
|
"766": self.nomatchingkey,
|
||||||
|
"768": self.keynotset,
|
||||||
|
"769": self.keynopermission,
|
||||||
|
"770": self.metadatasubok,
|
||||||
|
"error": self.error,
|
||||||
|
"join": self.join,
|
||||||
|
"kick": self.kick,
|
||||||
|
"mode": self.mode,
|
||||||
|
"part": self.part,
|
||||||
|
"ping": self.ping,
|
||||||
|
"privmsg": self.privmsg,
|
||||||
|
"quit": self.quit,
|
||||||
|
"invite": self.invite,
|
||||||
|
"nick": self.nick, # We can get svsnicked
|
||||||
|
"metadata": self.metadata, # Metadata specification
|
||||||
|
"tagmsg": self.tagmsg, # IRCv3 message tags extension
|
||||||
|
"cap": self.cap, # IRCv3 Client Capability Negotiation
|
||||||
|
}
|
||||||
|
|
||||||
def get_ssl_context(self):
|
def get_ssl_context(self):
|
||||||
"""Returns an SSL context for connecting over SSL/TLS.
|
"""Returns an SSL context for connecting over SSL/TLS.
|
||||||
Loads the certifi root certificate bundle if the certifi module is less
|
Loads the certifi root certificate bundle if the certifi module is less
|
||||||
|
@ -179,7 +218,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
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")
|
||||||
print(line)
|
print(line)
|
||||||
|
@ -224,7 +263,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
except ValueError as exception:
|
except ValueError as exception:
|
||||||
PchumLog.warning("latin-1 failed too xd")
|
PchumLog.warning("latin-1 failed too xd")
|
||||||
return "", buffer # throw it back in the cooker
|
return "", buffer # throw it back in the cooker
|
||||||
|
|
||||||
data = decoded_buffer.split("\r\n")
|
data = decoded_buffer.split("\r\n")
|
||||||
if data[-1]:
|
if data[-1]:
|
||||||
# Last entry has incomplete data, add back to buffer
|
# Last entry has incomplete data, add back to buffer
|
||||||
|
@ -260,14 +299,14 @@ class PesterIRC(QtCore.QThread):
|
||||||
fused_args = []
|
fused_args = []
|
||||||
for idx, arg in enumerate(args):
|
for idx, arg in enumerate(args):
|
||||||
if arg.startswith(":"):
|
if arg.startswith(":"):
|
||||||
final_param = ' '.join(args[idx:])
|
final_param = " ".join(args[idx:])
|
||||||
fused_args.append(final_param[1:])
|
fused_args.append(final_param[1:])
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
fused_args.append(arg)
|
fused_args.append(arg)
|
||||||
|
|
||||||
return (tags, prefix, command, fused_args)
|
return (tags, prefix, command, fused_args)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
# with extreme prejudice
|
# with extreme prejudice
|
||||||
if self.socket:
|
if self.socket:
|
||||||
|
@ -455,7 +494,9 @@ class PesterIRC(QtCore.QThread):
|
||||||
|
|
||||||
@QtCore.pyqtSlot(QString, bool)
|
@QtCore.pyqtSlot(QString, bool)
|
||||||
def startConvo(self, handle, initiated):
|
def startConvo(self, handle, initiated):
|
||||||
self.send_irc.privmsg(handle, "COLOR >%s" % (self.mainwindow.profile().colorcmd()))
|
self.send_irc.privmsg(
|
||||||
|
handle, "COLOR >%s" % (self.mainwindow.profile().colorcmd())
|
||||||
|
)
|
||||||
if initiated:
|
if initiated:
|
||||||
self.send_irc.privmsg(handle, "PESTERCHUM:BEGIN")
|
self.send_irc.privmsg(handle, "PESTERCHUM:BEGIN")
|
||||||
|
|
||||||
|
@ -490,9 +531,9 @@ class PesterIRC(QtCore.QThread):
|
||||||
# Send color messages
|
# Send color messages
|
||||||
for h in list(self.mainwindow.convos.keys()):
|
for h in list(self.mainwindow.convos.keys()):
|
||||||
self.send_irc.privmsg(
|
self.send_irc.privmsg(
|
||||||
h,
|
h,
|
||||||
"COLOR >%s" % (self.mainwindow.profile().colorcmd()),
|
"COLOR >%s" % (self.mainwindow.profile().colorcmd()),
|
||||||
)
|
)
|
||||||
|
|
||||||
@QtCore.pyqtSlot(QString)
|
@QtCore.pyqtSlot(QString)
|
||||||
def blockedChum(self, handle):
|
def blockedChum(self, handle):
|
||||||
|
@ -518,7 +559,6 @@ class PesterIRC(QtCore.QThread):
|
||||||
@QtCore.pyqtSlot(QString)
|
@QtCore.pyqtSlot(QString)
|
||||||
def leftChannel(self, channel):
|
def leftChannel(self, channel):
|
||||||
self.send_irc.part(channel)
|
self.send_irc.part(channel)
|
||||||
self.joined = False
|
|
||||||
|
|
||||||
@QtCore.pyqtSlot(QString, QString, QString)
|
@QtCore.pyqtSlot(QString, QString, QString)
|
||||||
def kickUser(self, channel, user, reason=""):
|
def kickUser(self, channel, user, reason=""):
|
||||||
|
@ -556,7 +596,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
self.send_irc.quit(f"{_pcVersion} <3")
|
self.send_irc.quit(f"{_pcVersion} <3")
|
||||||
self._end = True
|
self._end = True
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
def notice(self, nick, chan, msg):
|
def notice(self, nick, chan, msg):
|
||||||
handle = nick[0 : nick.find("!")]
|
handle = nick[0 : nick.find("!")]
|
||||||
PchumLog.info('---> recv "NOTICE {} :{}"'.format(handle, msg))
|
PchumLog.info('---> recv "NOTICE {} :{}"'.format(handle, msg))
|
||||||
|
@ -642,8 +682,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
PchumLog.info('---> recv "CTCP {} :{}"'.format(handle, msg[1:-1]))
|
PchumLog.info('---> recv "CTCP {} :{}"'.format(handle, msg[1:-1]))
|
||||||
# VERSION, return version
|
# VERSION, return version
|
||||||
if msg[1:-1].startswith("VERSION"):
|
if msg[1:-1].startswith("VERSION"):
|
||||||
self.send_irc.ctcp(handle, "VERSION", "Pesterchum %s" % (_pcVersion)
|
self.send_irc.ctcp(handle, "VERSION", "Pesterchum %s" % (_pcVersion))
|
||||||
)
|
|
||||||
# CLIENTINFO, return supported CTCP commands.
|
# CLIENTINFO, return supported CTCP commands.
|
||||||
elif msg[1:-1].startswith("CLIENTINFO"):
|
elif msg[1:-1].startswith("CLIENTINFO"):
|
||||||
self.send_irc.ctcp(
|
self.send_irc.ctcp(
|
||||||
|
@ -654,8 +693,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
# PING, return pong
|
# PING, return pong
|
||||||
elif msg[1:-1].startswith("PING"):
|
elif msg[1:-1].startswith("PING"):
|
||||||
if len(msg[1:-1].split("PING ")) > 1:
|
if len(msg[1:-1].split("PING ")) > 1:
|
||||||
self.send_irc.ctcp(handle, "PING", msg[1:-1].split("PING ")[1]
|
self.send_irc.ctcp(handle, "PING", msg[1:-1].split("PING ")[1])
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
self.send_irc.ctcp(handle, "PING")
|
self.send_irc.ctcp(handle, "PING")
|
||||||
# SOURCE, return source
|
# SOURCE, return source
|
||||||
|
@ -745,6 +783,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
self.send_irc.privmsg("#pesterchum", "MOOD >%d" % (mymood))
|
self.send_irc.privmsg("#pesterchum", "MOOD >%d" % (mymood))
|
||||||
|
|
||||||
def erroneusnickname(self, *args):
|
def erroneusnickname(self, *args):
|
||||||
|
"""RFC 432"""
|
||||||
# Server is not allowing us to connect.
|
# Server is not allowing us to connect.
|
||||||
reason = "Handle is not allowed on this server.\n"
|
reason = "Handle is not allowed on this server.\n"
|
||||||
for x in args:
|
for x in args:
|
||||||
|
@ -820,9 +859,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
|
|
||||||
def kick(self, opnick, channel, handle, reason):
|
def kick(self, opnick, channel, handle, reason):
|
||||||
op = opnick[0 : opnick.find("!")]
|
op = opnick[0 : opnick.find("!")]
|
||||||
self.userPresentUpdate.emit(
|
self.userPresentUpdate.emit(handle, channel, "kick:{}:{}".format(op, reason))
|
||||||
handle, channel, "kick:{}:{}".format(op, reason)
|
|
||||||
)
|
|
||||||
# ok i shouldnt be overloading that but am lazy
|
# ok i shouldnt be overloading that but am lazy
|
||||||
|
|
||||||
def part(self, nick, channel, reason="nanchos"):
|
def part(self, nick, channel, reason="nanchos"):
|
||||||
|
@ -949,9 +986,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
("x" in m) | ("z" in m) | ("o" in m) | ("x" in m)
|
("x" in m) | ("z" in m) | ("o" in m) | ("x" in m)
|
||||||
) != True:
|
) != True:
|
||||||
try:
|
try:
|
||||||
self.userPresentUpdate.emit(
|
self.userPresentUpdate.emit(handles[i], channel, m + ":%s" % (op))
|
||||||
handles[i], channel, m + ":%s" % (op)
|
|
||||||
)
|
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
PchumLog.exception("modeSetIndexError: %s" % e)
|
PchumLog.exception("modeSetIndexError: %s" % e)
|
||||||
# print("i = " + i)
|
# print("i = " + i)
|
||||||
|
@ -978,9 +1013,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
self.myHandleChanged.emit(newnick)
|
self.myHandleChanged.emit(newnick)
|
||||||
newchum = PesterProfile(newnick, chumdb=self.mainwindow.chumdb)
|
newchum = PesterProfile(newnick, chumdb=self.mainwindow.chumdb)
|
||||||
self.moodUpdated.emit(oldhandle, Mood("offline"))
|
self.moodUpdated.emit(oldhandle, Mood("offline"))
|
||||||
self.userPresentUpdate.emit(
|
self.userPresentUpdate.emit("{}:{}".format(oldhandle, newnick), "", "nick")
|
||||||
"{}:{}".format(oldhandle, newnick), "", "nick"
|
|
||||||
)
|
|
||||||
if newnick in self.mainwindow.chumList.chums:
|
if newnick in self.mainwindow.chumList.chums:
|
||||||
self.getMood(newchum)
|
self.getMood(newchum)
|
||||||
if oldhandle == self.mainwindow.randhandler.randNick:
|
if oldhandle == self.mainwindow.randhandler.randNick:
|
||||||
|
@ -1020,9 +1053,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
pl = PesterList(namelist)
|
pl = PesterList(namelist)
|
||||||
del self.channelnames[channel]
|
del self.channelnames[channel]
|
||||||
self.namesReceived.emit(channel, pl)
|
self.namesReceived.emit(channel, pl)
|
||||||
if channel == "#pesterchum" and (
|
if channel == "#pesterchum" and not self.joined:
|
||||||
not hasattr(self, "joined") or not self.joined
|
|
||||||
):
|
|
||||||
self.joined = True
|
self.joined = True
|
||||||
self.mainwindow.randhandler.setRunning(
|
self.mainwindow.randhandler.setRunning(
|
||||||
self.mainwindow.randhandler.randNick in namelist
|
self.mainwindow.randhandler.randNick in namelist
|
||||||
|
@ -1116,7 +1147,7 @@ class PesterIRC(QtCore.QThread):
|
||||||
if hasattr(f, "protected"):
|
if hasattr(f, "protected"):
|
||||||
raise ProtectedCommandError(in_command_parts)
|
raise ProtectedCommandError(in_command_parts)
|
||||||
|
|
||||||
#if isinstance(f, self) and command_parts:
|
# if isinstance(f, self) and command_parts:
|
||||||
if command_parts:
|
if command_parts:
|
||||||
return f.get(command_parts)
|
return f.get(command_parts)
|
||||||
p = f
|
p = f
|
||||||
|
|
Loading…
Reference in a new issue