2018-01-02 19:51:32 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
#
|
|
|
|
|
# mirc2png -- convert ASCII w/ mIRC control codes to monospaced PNG (for EFnet #MiRCART)
|
|
|
|
|
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from itertools import chain
|
|
|
|
|
import base64
|
|
|
|
|
import json
|
|
|
|
|
import mirc2png
|
2018-01-02 21:02:36 +00:00
|
|
|
|
import os, socket, sys, time
|
2018-01-02 19:51:32 +00:00
|
|
|
|
import requests
|
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
|
|
class IrcBot:
|
|
|
|
|
"""Blocking abstraction over the IRC protocol"""
|
|
|
|
|
serverHname = serverPort = None;
|
|
|
|
|
clientNick = clientIdent = clientGecos = None;
|
|
|
|
|
clientSocket = clientSocketFile = None;
|
|
|
|
|
|
|
|
|
|
# {{{ connect(): Connect to server and register
|
|
|
|
|
def connect(self):
|
|
|
|
|
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
self.clientSocket.connect((self.serverHname, int(self.serverPort)))
|
|
|
|
|
self.clientSocketFile = self.clientSocket.makefile()
|
|
|
|
|
self.sendline("NICK", self.clientNick)
|
|
|
|
|
self.sendline("USER", self.clientIdent, "0", "0", self.clientGecos)
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ close(): Close connection to server
|
|
|
|
|
def close(self):
|
2018-01-02 21:20:20 +00:00
|
|
|
|
if self.clientSocket != None:
|
|
|
|
|
self.clientSocket.close()
|
2018-01-02 19:51:32 +00:00
|
|
|
|
self.clientSocket = self.clientSocketFile = None;
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ readline(): Read and parse single line from server into canonicalised list
|
|
|
|
|
def readline(self):
|
|
|
|
|
msg = self.clientSocketFile.readline()
|
|
|
|
|
if len(msg):
|
|
|
|
|
msg = msg.rstrip("\r\n")
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
msg = msg.split(" :", 1)
|
|
|
|
|
if len(msg) == 1:
|
|
|
|
|
msg = list(chain.from_iterable(m.split(" ") for m in msg))
|
|
|
|
|
elif len(msg) == 2:
|
|
|
|
|
msg = msg[0].split(" ") + [msg[1]]
|
|
|
|
|
if msg[0][0] == ':':
|
|
|
|
|
msg = [msg[0][1:]] + msg[1:]
|
|
|
|
|
else:
|
|
|
|
|
msg = [""] + msg[1:]
|
|
|
|
|
return msg
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ sendline(): Parse and send single line to server from list
|
|
|
|
|
def sendline(self, *args):
|
|
|
|
|
msg = ""; argNumMax = len(args);
|
|
|
|
|
for argNum in range(0, argNumMax):
|
|
|
|
|
if argNum == (argNumMax - 1):
|
|
|
|
|
msg += ":" + args[argNum]
|
|
|
|
|
else:
|
|
|
|
|
msg += args[argNum] + " "
|
|
|
|
|
return self.clientSocket.send((msg + "\r\n").encode())
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ Initialisation method
|
|
|
|
|
def __init__(self, serverHname, serverPort, clientNick, clientIdent, clientGecos):
|
|
|
|
|
self.serverHname = serverHname; self.serverPort = serverPort;
|
|
|
|
|
self.clientNick = clientNick; self.clientIdent = clientIdent; self.clientGecos = clientGecos;
|
|
|
|
|
# }}}
|
|
|
|
|
|
2018-01-02 21:20:20 +00:00
|
|
|
|
class IrcMiRCARTBot(IrcBot):
|
|
|
|
|
"""IRC<->MiRCART bot"""
|
|
|
|
|
clientLastMessage = clientChannel = None
|
|
|
|
|
|
|
|
|
|
# {{{ connect(): Connect to server and (re)initialise
|
|
|
|
|
def connect(self):
|
|
|
|
|
print("Connecting to {}:{}...".format(self.serverHname, self.serverPort))
|
|
|
|
|
super().connect()
|
|
|
|
|
print("Connected to {}:{}.".format(self.serverHname, self.serverPort))
|
|
|
|
|
print("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
|
|
|
|
|
self.clientLastMessage = 0
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ 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;
|
|
|
|
|
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] == "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:
|
|
|
|
|
continue
|
|
|
|
|
else:
|
|
|
|
|
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)
|
|
|
|
|
imgurResponseHttp = requests.post("https://api.imgur.com/3/upload.json", data={"key":"c9a6efb3d7932fd", "image":base64.b64encode(open(imgTmpFilePath, "rb").read()), "type":"base64", "name":"tmp.png", "title":"tmp.png"}, headers={"Authorization": "Client-ID c9a6efb3d7932fd"})
|
|
|
|
|
imgurResponse = json.loads(imgurResponseHttp.text)
|
|
|
|
|
if imgurResponseHttp.status_code == 200:
|
|
|
|
|
imgurResponseUrl = imgurResponse.get("data").get("link")
|
|
|
|
|
self.sendline("PRIVMSG", serverMessage[2], "8/!\\ Uploaded as: {}".format(imgurResponseUrl))
|
|
|
|
|
else:
|
|
|
|
|
self.sendline("PRIVMSG", serverMessage[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponseHttp.status_code))
|
|
|
|
|
os.remove(asciiTmpFilePath); os.remove(imgTmpFilePath);
|
|
|
|
|
# }}}
|
|
|
|
|
# {{{ Initialisation method
|
|
|
|
|
def __init__(self, serverHname, serverPort="6667", clientNick="pngbot", clientIdent="pngbot", clientGecos="pngbot", clientChannel="#MiRCART"):
|
|
|
|
|
super().__init__(serverHname, serverPort, clientNick, clientIdent, clientGecos)
|
|
|
|
|
self.clientChannel = clientChannel
|
|
|
|
|
# }}}
|
|
|
|
|
|
2018-01-02 19:51:32 +00:00
|
|
|
|
#
|
|
|
|
|
# Entry point
|
2018-01-02 21:20:20 +00:00
|
|
|
|
def main(*argv):
|
|
|
|
|
_IrcMiRCARTBot = IrcMiRCARTBot(*argv[1:])
|
2018-01-02 19:51:32 +00:00
|
|
|
|
while True:
|
2018-01-02 21:20:20 +00:00
|
|
|
|
_IrcMiRCARTBot.connect()
|
|
|
|
|
_IrcMiRCARTBot.dispatch()
|
|
|
|
|
_IrcMiRCARTBot.close()
|
2018-01-02 19:51:32 +00:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
if ((len(sys.argv) - 1) < 1)\
|
|
|
|
|
or ((len(sys.argv) - 1) > 4):
|
|
|
|
|
print("usage: {} " \
|
|
|
|
|
"<IRC server hostname> " \
|
|
|
|
|
"[<IRC server port; defaults to 6667>] " \
|
|
|
|
|
"[<IRC bot nick name; defaults to pngbot>] " \
|
|
|
|
|
"[<IRC bot user name; defaults to pngbot>] " \
|
|
|
|
|
"[<IRC bot real name; defaults to pngbot>] " \
|
|
|
|
|
"[<IRC bot channel name; defaults to #MiRCART>] ".format(sys.argv[0]), file=sys.stderr)
|
|
|
|
|
else:
|
|
|
|
|
main(*sys.argv)
|
|
|
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=8 ts=8 tw=120
|