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