[pybot-commits] CVS: pybot/pybot/modules log.py,1.5,1.6 modulecontrol.py,1.3,1.4
Brought to you by:
niemeyer
From: Gustavo N. <nie...@us...> - 2003-05-08 18:31:03
|
Update of /cvsroot/pybot/pybot/pybot/modules In directory sc8-pr-cvs1:/tmp/cvs-serv15913/pybot/modules Modified Files: log.py modulecontrol.py Log Message: - New xmlrpc module. - New testadora module, supporting compile time command, for now. This is useful for Conectiva only. - Accept "(remove|delete|del) permission" as well. - Implemented sqlite db storage. - Use sqlite storage to maintain logs. - Adopted re system, and added a command to show loaded modules. Index: log.py =================================================================== RCS file: /cvsroot/pybot/pybot/pybot/modules/log.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** log.py 20 Jun 2002 17:37:16 -0000 1.5 --- log.py 8 May 2003 18:30:59 -0000 1.6 *************** *** 17,21 **** # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ! from pybot import config, options, hooks, mm, servers from pybot.user import User import time --- 17,21 ---- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ! from pybot import config, options, hooks, mm, servers, db from pybot.user import User import time *************** *** 38,49 **** class LogMsg: ! def __init__(self, time, servername, type, src, dest, line): ! self.time = time ! self.servername = servername ! self.type = type ! self.src = src ! self.dest = dest ! self.line = line ! def __str__(self): src = User() --- 38,49 ---- class LogMsg: ! def __init__(self, data) ! self.time = int(data.time) ! self.servername = data.servername ! self.type = data.type ! self.src = data.src ! self.dest = data.dest ! self.line = data.line ! def __str__(self): src = User() *************** *** 66,114 **** return s class Log: def __init__(self): ! self.__logname = config.get("log", "logfile") ! def append(self, servername, type, src, dest, line): ! file = open(self.__logname, "a") ! file.write("%d %s %s %s %s %s\n" % ! (int(time.time()), servername, type, src, dest, line)) ! file.close() def seen(self, nick): ! stripnick = re.compile(r"^[\W_]*([^\W_]+)[\W_]*$") ! nick = stripnick.sub(r"\1", nick.lower()) ! file = open(self.__logname) ! logmsg = None ! for line in file.xreadlines(): ! stime, servername, type, src, dest, line = line.split(" ", 5) ! if src == "-" or dest == "-": ! continue ! user = User() ! user.setstring(src) ! if stripnick.sub(r"\1", user.nick.lower()) == nick: ! logmsg = LogMsg(int(stime), servername, type, src, dest, line) ! file.close() ! return logmsg def search(self, regexp, max, searchline): p = re.compile(regexp, re.I) ! file = open(self.__logname) l = [] ! for line in file.xreadlines(): ! line = line[:-1] ! stime, servername, type, src, dest, line = line.split(" ", 5) ! if src == "-" or dest == "-": ! continue ! if p.search(line): ! l.append(LogMsg(int(stime), servername, type, src, dest, line)) if len(l) > max+1: l.pop(0) if l and l[-1].line == searchline: l.pop() elif len(l) > max: l.pop(0) - file.close() return l --- 66,122 ---- return s + STRIPNICK = re.compile(r"^[\W_]*([^\W_]+)[\W_]*$") class Log: def __init__(self): ! self.create_database() ! def create_database(self): ! cursor = db.cursor() ! try: ! cursor.execute("create table log " ! "(timestamp, servername, type," ! " nick, src, dest, line)") ! except db.error: ! pass ! ! def xformnick(self, nick): ! return STRIPNICK.sub(r"\1", nick.lower()) ! ! def append(self, servername, type, nick, src, dest, line): ! nick = self.xformnick(nick) ! cursor = db.cursor() ! values = (int(time.time()), servername, type, nick, src, dest, line) ! places = ','.join(['%s']*len(values)) ! cursor.execute("insert into log values (%s)" % places, values) def seen(self, nick): ! nick = self.xformnick(nick) ! cursor = db.cursor() ! cursor.execute("select * from log where nick=%s and src != '' " ! "and dest != '' order by timestamp desc limit 1", ! (nick,)) ! row = cursor.fetchone() ! if row: ! return LogMsg(row) ! return None def search(self, regexp, max, searchline): p = re.compile(regexp, re.I) ! file = open(self.logname) l = [] ! cursor = db.cursor() ! cursor.execute("select * from log where src != '' and dest != ''") ! row = cursor.fetchone() ! while row: ! if p.search(row.line): ! l.append(LogMsg(row)) if len(l) > max+1: l.pop(0) + row = cursor.fetchone() if l and l[-1].line == searchline: l.pop() elif len(l) > max: l.pop(0) return l *************** *** 184,214 **** def log_message(self, msg): ! target = msg.direct and "-" or msg.target ! self.log.append(msg.server.servername, "MESSAGE", msg.user.string, ! target, msg.rawline) def log_ctcp(self, msg): if msg.ctcp == "ACTION": ! target = msg.direct and "-" or msg.target ! self.log.append(msg.server.servername, "ACTION", msg.user.string, ! target, msg.rawline) def log_outmessage(self, msg): ! self.log.append(msg.server.servername, "MESSAGE", "-", msg.target, msg.rawline) def log_outctcp(self, msg): if msg.ctcp == "ACTION": ! self.log.append(msg.server.servername, "ACTION", "-", msg.target, msg.rawline) def __loadmodule__(bot): ! global module ! module = LogModule() def __unloadmodule__(bot): ! global module ! module.unload() ! del module # vim:ts=4:sw=4:et --- 192,228 ---- def log_message(self, msg): ! if msg.direct: ! target = "" ! else: ! target = msg.target ! self.log.append(msg.server.servername, "MESSAGE", msg.user.nick(), ! msg.user.string, target, msg.rawline) def log_ctcp(self, msg): if msg.ctcp == "ACTION": ! if msg.direct: ! target = "" ! else: ! target = msg.target ! self.log.append(msg.server.servername, "ACTION", msg.user.nick(), ! msg.user.string, target, msg.rawline) def log_outmessage(self, msg): ! self.log.append(msg.server.servername, "MESSAGE", "", "", msg.target, msg.rawline) def log_outctcp(self, msg): if msg.ctcp == "ACTION": ! self.log.append(msg.server.servername, "ACTION", "", "", msg.target, msg.rawline) def __loadmodule__(bot): ! global mod ! mod = LogModule() def __unloadmodule__(bot): ! global mod ! mod.unload() ! del mod # vim:ts=4:sw=4:et Index: modulecontrol.py =================================================================== RCS file: /cvsroot/pybot/pybot/pybot/modules/modulecontrol.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** modulecontrol.py 4 Dec 2001 00:57:38 -0000 1.3 --- modulecontrol.py 8 May 2003 18:30:59 -0000 1.4 *************** *** 1,3 **** ! # Copyright (c) 2000-2001 Gustavo Niemeyer <nie...@co...> # # This file is part of pybot. --- 1,3 ---- ! # Copyright (c) 2000-2002 Gustavo Niemeyer <nie...@co...> # # This file is part of pybot. *************** *** 18,21 **** --- 18,29 ---- from pybot import mm, hooks, options, modls + import re + + HELP = [ + ("""\ + You can load, unload, and reload modules with "[re|un]load module <module>". \ + I can also show you which modules are loaded at the moment with "show \ + [loaded] modules".\ + """,)] class ModuleControl: *************** *** 24,27 **** --- 32,44 ---- hooks.register("Message", self.message) modls.loadlist(self.modules) + + # Match '[re|un]load [the] [module] <module>' + self.re1 = re.compile("(?P<command>(?:re|un)?load)(?:\s+the)?(?:\s+module)?\s+(?P<module>[\w_-]+)\s*[.!]*$", re.I) + + # Match '(show|list) [loaded] modules' + self.re2 = re.compile("(?:show|list)(?:\s+loaded)\s+modules\s*[.!]*$", re.I) + + # Match '[[un|re]load] module[s]' + mm.register_help(0, "(?:(?:un|re)?load\s+)?modules?", HELP) def unload(self): *************** *** 30,77 **** def message(self, msg): var = [] ! if msg.match(var, 1, "%", "reload", [(["the", None], "module"), None], 0, "~", [".", "!", None]): ! if mm.hasperm(1, msg.server.servername, msg.target, msg.user, None): ! if modls.isloaded(var[0]): ! if modls.reload(var[0]): ! msg.answer("%:", ["Reloaded", "Done", "Ready"], ["!", ", sir!"]) ! else: ! # If it was able to unload, remove it from our list. ! if not modls.isloaded(var[0]): ! self.modules.remove(var[0]) ! msg.answer("%:", ["Something wrong happened while", "Something bad happened while", "There was a problem"], "reloading the module", ["!", "."]) ! else: ! msg.answer("%:", ["Sorry sir, but", "Sir,", "Oops, I think that"], "this module is not loaded.") ! else: ! msg.answer("%:", ["You're not that good", "You are not able to reload modules", "No, you can't do this"], [".", "!",". I'm sorry!"]) ! return 0 ! elif msg.match(var, 1, "%", "load", [(["the", None], "module"), None], 0, "~", [".", "!", None]): ! if mm.hasperm(1, msg.server.servername, msg.target, msg.user, None): ! if not modls.isloaded(var[0]): ! if modls.load(var[0]): ! self.modules.append(var[0]) ! msg.answer("%:", ["Loaded", "Done", "Ready"], ["!", "."]) else: ! msg.answer("%:", ["Something wrong happened while", "Something bad happened while", "There was a problem"], "loading the module", ["!", "."]) ! else: ! msg.answer("%:", ["Sorry sir, but", "Sir,", "Oops, I think that"], "this module is already loaded.") ! else: ! msg.answer("%:", ["You're not that good", "You are not able to load modules", "No, you can't do this"], [".", "!",". I'm sorry!"]) ! return 0 ! elif msg.match(var, 1, "%", "unload", [(["the", None], "module"), None], 0, "~", [".", "!", None]): ! if mm.hasperm(1, msg.server.servername, msg.target, msg.user, None): ! if modls.isloaded(var[0]): ! if var[0] in self.modules: ! self.modules.remove(var[0]) ! if modls.unload(var[0]): ! msg.answer("%:", ["Unloaded", "Done", "Ready"], ["!", "."]) else: ! msg.answer("%:", ["Something wrong happened while", "Something bad happened while", "There was a problem"], "unloading the module", ["!", "."]) else: ! msg.answer("%:", ["Sorry, but", "Oops, I think that"], "this module can't be unloaded.") else: ! msg.answer("%:", ["Sorry, but", "Oops, I think that"], "this module is not loaded.") ! else: ! msg.answer("%:", ["You're not that good", "You are not able to unload modules", "No, you can't do this"], [".", "!",". I'm sorry!"]) ! return 0 def __loadmodule__(bot): --- 47,96 ---- def message(self, msg): var = [] ! if msg.forme: ! m = self.re1.match(msg.line) ! if m: ! if mm.hasperm(1, msg.server.servername, msg.target, msg.user, None): ! command, module = m.group("command", "module") ! isloaded = modls.isloaded(module) ! if command == "load" and isloaded: ! msg.answer("%:", ["Sorry sir, but", "Sir,", "Oops, I think that"], "this module is already loaded.") ! elif not isloaded: ! msg.answer("%:", ["Sorry sir, but", "Sir,", "Oops, I think that"], "this module is not loaded.") ! elif command == "load": ! if modls.load(module): ! self.modules.append(module) ! msg.answer("%:", ["Loaded", "Done", "Ready"], ["!", "."]) ! else: ! msg.answer("%:", ["Something wrong happened while", "Something bad happened while", "There was a problem"], "loading the module", ["!", "."]) ! elif command == "reload": ! if modls.reload(module): ! msg.answer("%:", ["Reloaded", "Done", "Ready"], ["!", ", sir!"]) ! else: ! if not modls.isloaded(module): ! self.modules.remove(module) ! msg.answer("%:", ["Something wrong happened while", "Something bad happened while", "There was a problem"], "reloading the module", ["!", "."]) else: ! if module in self.modules: ! self.modules.remove(module) ! if modls.unload(module): ! msg.answer("%:", ["Unloaded", "Done", "Ready"], ["!", "."]) ! else: ! msg.answer("%:", ["Something wrong happened while", "Something bad happened while", "There was a problem"], "unloading the module", ["!", "."]) else: ! msg.answer("%:", ["Sorry, but", "Oops, I think that"], "this module can't be unloaded.") ! else: ! msg.answer("%:", ["You're not that good", "You are not able to work with modules", "No, you can't do this"], [".", "!",". I'm sorry!"]) ! return 0 ! ! m = self.re2.match(msg.line) ! if m: ! if mm.hasperm(1, msg.server.servername, msg.target, msg.user, None): ! if not self.modules: ! msg.answer("%:", ["There are no", "No"], "loaded modules", [".", "!"]) else: ! msg.answer("%:", ["These are the loaded modules:", "The following modules are loaded:", ", ".join(self.modules)) else: ! msg.answer("%:", ["You're not that good", "You are not able to work with modules", "No, you can't do this"], [".", "!",". I'm sorry!"]) ! return 0 def __loadmodule__(bot): |