diff --git a/modules/whois.py b/modules/whois.py new file mode 100644 index 0000000..5d4638e --- /dev/null +++ b/modules/whois.py @@ -0,0 +1,101 @@ +# coding=utf-8 + +import re + +from nemubot import context +from nemubot.exception import IRCException +from nemubot.hooks import hook +from nemubot.tools.xmlparser.node import ModuleState + +nemubotversion = 3.4 + +from more import Response + +PASSWD_FILE = None + +def load(context): + global PASSWD_FILE + if not context.config or not context.config.hasAttribute("passwd"): + print("No passwd file given") + return None + PASSWD_FILE = context.config["passwd"] + + if not context.data.hasNode("aliases"): + context.data.addChild(ModuleState("aliases")) + context.data.getNode("aliases").setIndex("from", "alias") + + if not context.data.hasNode("pics"): + context.data.addChild(ModuleState("pics")) + context.data.getNode("pics").setIndex("login", "pict") + + import nemubot.hooks + context.add_hook("cmd_hook", + nemubot.hooks.Message(cmd_whois, "whois")) + +class Login: + + def __init__(self, line): + s = line.split(":") + self.login = s[0] + self.uid = s[2] + self.gid = s[3] + self.cn = s[4] + self.home = s[5] + + def get_promo(self): + return self.home.split("/")[2].replace("_", " ") + + def get_photo(self): + if self.login in context.data.getNode("pics").index: + return context.data.getNode("pics").index[self.login]["url"] + else: + return "https://static.acu.epita.fr/photos/%s" % self.login + + +def found_login(login): + if login in context.data.getNode("aliases").index: + login = context.data.getNode("aliases").index[login]["to"] + + login_ = login + ":" + lsize = len(login_) + + with open(PASSWD_FILE, encoding="iso-8859-15") as f: + for l in f.readlines(): + if l[:lsize] == login_: + return Login(l.strip()) + return None + +def cmd_whois(msg): + if len(msg.args) < 1: + raise IRCException("Provide a name") + + res = Response(channel=msg.channel, count=" (%d more logins)") + for srch in msg.args: + l = found_login(srch) + if l is not None: + pic = l.get_photo() + res.append_message("%s is %s (%s %s): %s%s" % (srch, l.cn.capitalize(), l.login, l.uid, l.get_promo(), " and looks like %s" % pic if pic is not None else "")) + else: + res.append_message("Unknown %s :(" % srch) + return res + +@hook("ask_default") +def parseask(msg): + res = re.match(r"^(\S+)\s*('s|suis|est|is|was|were)\s+([a-zA-Z0-9_-]{3,8})$", msg.text, re.I) + if res is not None: + nick = res.group(1) + login = res.group(3) + if nick == "my" or nick == "I" or nick == "i" or nick == "je" or nick == "mon" or nick == "ma": + nick = msg.nick + if nick in context.data.getNode("aliases").index: + context.data.getNode("aliases").index[nick]["to"] = login + else: + ms = ModuleState("alias") + ms.setAttribute("from", nick) + ms.setAttribute("to", login) + context.data.getNode("aliases").addChild(ms) + context.save() + return Response("ok, c'est noté, %s est %s" + % (nick, login), + channel=msg.channel, + nick=msg.nick)