pngbot.py:IrcMiRCARTBot.dispatch{001,353,MODE,None,PING,PRIVMSG}(): split from dispatch().

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-01-02 23:35:01 +01:00
parent 28e5b9b958
commit f4218e8ae1

View File

@ -96,29 +96,32 @@ class IrcMiRCARTBot(IrcBot):
print("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos)) print("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
self.clientLastMessage = 0; self.clientChannelOps = []; self.clientLastMessage = 0; self.clientChannelOps = [];
# }}} # }}}
# {{{ dispatch(): Read, parse, and dispatch single line from server # {{{ dispatchNone(): Dispatch None message from server
def dispatch(self): def dispatchNone(self):
while True:
serverMessage = self.readline()
if serverMessage == None:
print("Disconnected from {}:{}.".format(self.serverHname, self.serverPort)) print("Disconnected from {}:{}.".format(self.serverHname, self.serverPort))
self.close(); break; self.close()
elif serverMessage[1] == "001": # }}}
# {{{ dispatch001(): Dispatch single 001 (RPL_WELCOME)
def dispatch001(self, message):
print("Registered on {}:{} as {}, {}, {}.".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos)) print("Registered on {}:{} as {}, {}, {}.".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
print("Joining {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort)) print("Joining {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort))
self.sendline("JOIN", self.clientChannel) self.sendline("JOIN", self.clientChannel)
elif serverMessage[1] == "353" \ # }}}
and serverMessage[4].lower() == self.clientChannel.lower(): # {{{ dispatch353(): Dispatch single 353 (RPL_NAMREPLY)
for channelNickSpec in serverMessage[5].split(" "): def dispatch353(self, message):
if message[4].lower() == self.clientChannel.lower():
for channelNickSpec in message[5].split(" "):
if channelNickSpec[0] == "@" \ if channelNickSpec[0] == "@" \
and len(channelNickSpec[1:]): and len(channelNickSpec[1:]):
self.clientChannelOps.append(channelNickSpec[1:].lower()) self.clientChannelOps.append(channelNickSpec[1:].lower())
print("Authorising {} on {}".format(channelNickSpec[1:].lower(), serverMessage[2].lower())) print("Authorising {} on {}".format(channelNickSpec[1:].lower(), message[2].lower()))
elif serverMessage[1] == "MODE" \ # }}}
and serverMessage[2].lower() == self.clientChannel.lower(): # {{{ dispatchMode(): Dispatch single MODE message from server
def dispatchMode(self, message):
if message[2].lower() == self.clientChannel.lower():
channelModeType = "+"; channelModeArg = 4; channelModeType = "+"; channelModeArg = 4;
channelAuthAdd = ""; channelAuthDel = ""; channelAuthAdd = ""; channelAuthDel = "";
for channelModeChar in serverMessage[3]: for channelModeChar in message[3]:
if channelModeChar[0] == "-": if channelModeChar[0] == "-":
channelModeType = "-" channelModeType = "-"
elif channelModeChar[0] == "+": elif channelModeChar[0] == "+":
@ -126,35 +129,39 @@ class IrcMiRCARTBot(IrcBot):
elif channelModeChar[0].isalpha(): elif channelModeChar[0].isalpha():
if channelModeChar[0] == "o": if channelModeChar[0] == "o":
if channelModeType == "+": if channelModeType == "+":
channelAuthAdd = serverMessage[channelModeArg]; channelAuthDel = ""; channelAuthAdd = message[channelModeArg]; channelAuthDel = "";
elif channelModeType == "-": elif channelModeType == "-":
channelAuthAdd = ""; channelAuthDel = serverMessage[channelModeArg]; channelAuthAdd = ""; channelAuthDel = message[channelModeArg];
channelModeArg += 1 channelModeArg += 1
if len(channelAuthAdd) \ if len(channelAuthAdd) \
and channelAuthAdd not in self.clientChannelOps: and channelAuthAdd not in self.clientChannelOps:
channelAuthAdd = channelAuthAdd.lower() channelAuthAdd = channelAuthAdd.lower()
print("Authorising {} on {}".format(channelAuthAdd, serverMessage[2].lower())) print("Authorising {} on {}".format(channelAuthAdd, message[2].lower()))
self.clientChannelOps.append(channelAuthAdd) self.clientChannelOps.append(channelAuthAdd)
elif len(channelAuthDel) \ elif len(channelAuthDel) \
and channelAuthDel in self.clientChannelOps: and channelAuthDel in self.clientChannelOps:
channelAuthDel = channelAuthDel.lower() channelAuthDel = channelAuthDel.lower()
print("Deauthorising {} on {}".format(channelAuthDel, serverMessage[2].lower())) print("Deauthorising {} on {}".format(channelAuthDel, message[2].lower()))
self.clientChannelOps.remove(channelAuthDel) self.clientChannelOps.remove(channelAuthDel)
elif serverMessage[1] == "PING": # }}}
self.sendline("PONG", serverMessage[2]) # {{{ dispatchPing(): Dispatch single PING message from server
elif serverMessage[1] == "PRIVMSG" \ def dispatchPing(self, message):
and serverMessage[2].lower() == self.clientChannel.lower() \ self.sendline("PONG", message[2])
and serverMessage[3].startswith("!pngbot "): # }}}
# {{{ dispatchPrivmsg(): Dispatch single PRIVMSG message from server
def dispatchPrivmsg(self, message):
if message[2].lower() == self.clientChannel.lower() \
and message[3].startswith("!pngbot "):
if (int(time.time()) - self.clientLastMessage) < 45: if (int(time.time()) - self.clientLastMessage) < 45:
print("Ignoring request on {} from {} due to rate limit: {}".format(serverMessage[2].lower(), serverMessage[0], serverMessage[3])) print("Ignoring request on {} from {} due to rate limit: {}".format(message[2].lower(), message[0], message[3]))
continue return
elif serverMessage[0].split("!")[0].lower() not in self.clientChannelOps: elif message[0].split("!")[0].lower() not in self.clientChannelOps:
print("Ignoring request on {} from {} due to lack of authorisation: {}".format(serverMessage[2].lower(), serverMessage[0], serverMessage[3])) print("Ignoring request on {} from {} due to lack of authorisation: {}".format(message[2].lower(), message[0], message[3]))
continue return
else: else:
print("Processing request on {} from {}: {}".format(serverMessage[2].lower(), serverMessage[0], serverMessage[3])) print("Processing request on {} from {}: {}".format(message[2].lower(), message[0], message[3]))
self.clientLastMessage = int(time.time()) self.clientLastMessage = int(time.time())
asciiUrl = serverMessage[3].split(" ")[1] asciiUrl = message[3].split(" ")[1]
asciiTmpFilePath = "tmp.txt"; imgTmpFilePath = "tmp.png"; asciiTmpFilePath = "tmp.txt"; imgTmpFilePath = "tmp.png";
if os.path.isfile(asciiTmpFilePath): if os.path.isfile(asciiTmpFilePath):
os.remove(asciiTmpFilePath) os.remove(asciiTmpFilePath)
@ -165,15 +172,32 @@ class IrcMiRCARTBot(IrcBot):
imgurResponse = self.uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd") imgurResponse = self.uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd")
if imgurResponse[0] == 200: if imgurResponse[0] == 200:
print("Uploaded as: {}".format(imgurResponse[1])) print("Uploaded as: {}".format(imgurResponse[1]))
self.sendline("PRIVMSG", serverMessage[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1])) self.sendline("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1]))
else: else:
print("Upload failed with HTTP status code {}".format(imgurResponse[0])) print("Upload failed with HTTP status code {}".format(imgurResponse[0]))
self.sendline("PRIVMSG", serverMessage[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponse[0])) self.sendline("PRIVMSG", message[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponse[0]))
if os.path.isfile(asciiTmpFilePath): if os.path.isfile(asciiTmpFilePath):
os.remove(asciiTmpFilePath) os.remove(asciiTmpFilePath)
if os.path.isfile(imgTmpFilePath): if os.path.isfile(imgTmpFilePath):
os.remove(imgTmpFilePath) os.remove(imgTmpFilePath)
# }}} # }}}
# {{{ dispatch(): Read, parse, and dispatch single line from server
def dispatch(self):
while True:
serverMessage = self.readline()
if serverMessage == None:
self.dispatchNone(); break;
elif serverMessage[1] == "001":
self.dispatch001(serverMessage)
elif serverMessage[1] == "353":
self.dispatch353(serverMessage)
elif serverMessage[1] == "MODE":
self.dispatchMode(serverMessage)
elif serverMessage[1] == "PING":
self.dispatchPing(serverMessage)
elif serverMessage[1] == "PRIVMSG":
self.dispatchPrivmsg(serverMessage)
# }}}
# {{{ uploadToImgur(): Upload single file to Imgur # {{{ uploadToImgur(): Upload single file to Imgur
def uploadToImgur(self, imgFilePath, imgName, imgTitle, apiKey): def uploadToImgur(self, imgFilePath, imgName, imgTitle, apiKey):
requestImageData = open(imgFilePath, "rb").read() requestImageData = open(imgFilePath, "rb").read()