mirror of
https://github.com/lalbornoz/roar.git
synced 2024-12-23 04:46:37 +00:00
pngbot.py:IrcMiRCARTBot.dispatch{001,353,MODE,None,PING,PRIVMSG}(): split from dispatch().
This commit is contained in:
parent
28e5b9b958
commit
f4218e8ae1
164
pngbot.py
164
pngbot.py
@ -96,83 +96,107 @@ class IrcMiRCARTBot(IrcBot):
|
||||
print("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
|
||||
self.clientLastMessage = 0; self.clientChannelOps = [];
|
||||
# }}}
|
||||
# {{{ dispatchNone(): Dispatch None message from server
|
||||
def dispatchNone(self):
|
||||
print("Disconnected from {}:{}.".format(self.serverHname, self.serverPort))
|
||||
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)
|
||||
# }}}
|
||||
# {{{ 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(), 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 message[3]:
|
||||
if channelModeChar[0] == "-":
|
||||
channelModeType = "-"
|
||||
elif channelModeChar[0] == "+":
|
||||
channelModeType = "+"
|
||||
elif channelModeChar[0].isalpha():
|
||||
if channelModeChar[0] == "o":
|
||||
if channelModeType == "+":
|
||||
channelAuthAdd = message[channelModeArg]; channelAuthDel = "";
|
||||
elif channelModeType == "-":
|
||||
channelAuthAdd = ""; channelAuthDel = message[channelModeArg];
|
||||
channelModeArg += 1
|
||||
if len(channelAuthAdd) \
|
||||
and channelAuthAdd not in self.clientChannelOps:
|
||||
channelAuthAdd = channelAuthAdd.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, message[2].lower()))
|
||||
self.clientChannelOps.remove(channelAuthDel)
|
||||
# }}}
|
||||
# {{{ 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(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(message[2].lower(), message[0], message[3]))
|
||||
self.clientLastMessage = int(time.time())
|
||||
asciiUrl = message[3].split(" ")[1]
|
||||
asciiTmpFilePath = "tmp.txt"; imgTmpFilePath = "tmp.png";
|
||||
if os.path.isfile(asciiTmpFilePath):
|
||||
os.remove(asciiTmpFilePath)
|
||||
if os.path.isfile(imgTmpFilePath):
|
||||
os.remove(imgTmpFilePath)
|
||||
urllib.request.urlretrieve(asciiUrl, asciiTmpFilePath)
|
||||
_MiRCART = mirc2png.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11)
|
||||
imgurResponse = self.uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", "c9a6efb3d7932fd")
|
||||
if imgurResponse[0] == 200:
|
||||
print("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", 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:
|
||||
print("Disconnected from {}:{}.".format(self.serverHname, self.serverPort))
|
||||
self.close(); break;
|
||||
self.dispatchNone(); break;
|
||||
elif serverMessage[1] == "001":
|
||||
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(" "):
|
||||
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():
|
||||
channelModeType = "+"; channelModeArg = 4;
|
||||
channelAuthAdd = ""; channelAuthDel = "";
|
||||
for channelModeChar in serverMessage[3]:
|
||||
if channelModeChar[0] == "-":
|
||||
channelModeType = "-"
|
||||
elif channelModeChar[0] == "+":
|
||||
channelModeType = "+"
|
||||
elif channelModeChar[0].isalpha():
|
||||
if channelModeChar[0] == "o":
|
||||
if channelModeType == "+":
|
||||
channelAuthAdd = serverMessage[channelModeArg]; channelAuthDel = "";
|
||||
elif channelModeType == "-":
|
||||
channelAuthAdd = ""; channelAuthDel = serverMessage[channelModeArg];
|
||||
channelModeArg += 1
|
||||
if len(channelAuthAdd) \
|
||||
and channelAuthAdd not in self.clientChannelOps:
|
||||
channelAuthAdd = channelAuthAdd.lower()
|
||||
print("Authorising {} on {}".format(channelAuthAdd, serverMessage[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()))
|
||||
self.clientChannelOps.remove(channelAuthDel)
|
||||
self.dispatch001(serverMessage)
|
||||
elif serverMessage[1] == "353":
|
||||
self.dispatch353(serverMessage)
|
||||
elif serverMessage[1] == "MODE":
|
||||
self.dispatchMode(serverMessage)
|
||||
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 "):
|
||||
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
|
||||
else:
|
||||
print("Processing request on {} from {}: {}".format(serverMessage[2].lower(), serverMessage[0], serverMessage[3]))
|
||||
self.clientLastMessage = int(time.time())
|
||||
asciiUrl = serverMessage[3].split(" ")[1]
|
||||
asciiTmpFilePath = "tmp.txt"; imgTmpFilePath = "tmp.png";
|
||||
if os.path.isfile(asciiTmpFilePath):
|
||||
os.remove(asciiTmpFilePath)
|
||||
if os.path.isfile(imgTmpFilePath):
|
||||
os.remove(imgTmpFilePath)
|
||||
urllib.request.urlretrieve(asciiUrl, asciiTmpFilePath)
|
||||
_MiRCART = mirc2png.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11)
|
||||
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]))
|
||||
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]))
|
||||
if os.path.isfile(asciiTmpFilePath):
|
||||
os.remove(asciiTmpFilePath)
|
||||
if os.path.isfile(imgTmpFilePath):
|
||||
os.remove(imgTmpFilePath)
|
||||
self.dispatchPing(serverMessage)
|
||||
elif serverMessage[1] == "PRIVMSG":
|
||||
self.dispatchPrivmsg(serverMessage)
|
||||
# }}}
|
||||
# {{{ uploadToImgur(): Upload single file to Imgur
|
||||
def uploadToImgur(self, imgFilePath, imgName, imgTitle, apiKey):
|
||||
|
Loading…
Reference in New Issue
Block a user