diff --git a/modules/ycc.py b/modules/ycc.py index 3edf2f5..7f0cc9c 100644 --- a/modules/ycc.py +++ b/modules/ycc.py @@ -8,11 +8,11 @@ from urllib.request import urlopen nemubotversion = 3.3 def help_tiny (): - """Line inserted in the response to the command !help""" - return "Gets YCC urls" + """Line inserted in the response to the command !help""" + return "Gets YCC urls" def help_full (): - return "!ycc []: with an argument, reduce the given thanks to ycc.fr; without argument, reduce the last URL said on the current channel." + return "!ycc []: with an argument, reduce the given thanks to ycc.fr; without argument, reduce the last URL said on the current channel." def load(context): from hooks import Hook @@ -23,11 +23,11 @@ LAST_URLS = dict() def gen_response(res, msg, srv): if res is None: - return Response(msg.sender, "La situation est embarassante, il semblerait que YCC soit down :(", msg.channel) + raise IRCException("la situation est embarassante, il semblerait que YCC soit down :(") elif isinstance(res, str): return Response(msg.sender, "URL pour %s : %s" % (srv, res), msg.channel) else: - return Response(msg.sender, "Mauvaise URL : %s" % srv, msg.channel) + raise IRCException("mauvaise URL : %s" % srv) def cmd_ycc(msg): if len(msg.cmds) == 1: @@ -35,26 +35,25 @@ def cmd_ycc(msg): if msg.channel in LAST_URLS and len(LAST_URLS[msg.channel]) > 0: msg.cmds.append(LAST_URLS[msg.channel].pop()) else: - return Response(msg.sender, "Je n'ai pas d'autre URL à réduire.", msg.channel) + raise IRCException("je n'ai pas d'autre URL à réduire.") - if len(msg.cmds) < 6: - res = list() - for url in msg.cmds[1:]: - o = urlparse(url, "http") - if o.scheme != "": - snd_url = "http://ycc.fr/redirection/create/" + quote(url, "/:%#@&=?") - print_debug(snd_url) - raw = urlopen(snd_url, timeout=10) - if o.netloc == "": - res.append(gen_response(raw.read().decode(), msg, o.scheme)) - else: - res.append(gen_response(raw.read().decode(), msg, o.netloc)) + if len(msg.cmds) > 5: + raise IRCException("je ne peux pas réduire autant d'URL d'un seul coup.") + + res = list() + for url in msg.cmds[1:]: + o = urlparse(url, "http") + if o.scheme != "": + snd_url = "http://ycc.fr/redirection/create/" + quote(url, "/:%@&=?") + print_debug(snd_url) + raw = urlopen(snd_url, timeout=10) + if o.netloc == "": + res.append(gen_response(raw.read().decode(), msg, o.scheme)) else: - res.append(gen_response(False, msg, url)) - return res - else: - return Response(msg.sender, "je ne peux pas réduire autant d'URL " - "d'un seul coup.", msg.channel, nick=msg.nick) + res.append(gen_response(raw.read().decode(), msg, o.netloc)) + else: + res.append(gen_response(False, msg, url)) + return res def parselisten(msg): global LAST_URLS @@ -67,9 +66,9 @@ def parselisten(msg): continue if msg.channel not in LAST_URLS: LAST_URLS[msg.channel] = list() - LAST_URLS[msg.channel].append(o.geturl()) + LAST_URLS[msg.channel].append(url) except: - pass + pass return False def parseresponse(res):