[pybot-commits] CVS: pybot/pybot/modules log.py,1.1,1.2
Brought to you by:
niemeyer
From: Gustavo N. <nie...@us...> - 2002-06-19 21:41:32
|
Update of /cvsroot/pybot/pybot/pybot/modules In directory usw-pr-cvs1:/tmp/cvs-serv884 Modified Files: log.py Log Message: Implemented "seen" and log search functionalities. Index: log.py =================================================================== RCS file: /cvsroot/pybot/pybot/pybot/modules/log.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** log.py 19 Jun 2002 20:05:37 -0000 1.1 --- log.py 19 Jun 2002 21:41:30 -0000 1.2 *************** *** 18,30 **** from pybot import config, options, hooks, mm, servers import time import re import os ! class Message: ! def __init__(self, time=0, nick="", phrase=""): ! self.phrase = phrase self.time = time ! self.nick = nick class Log: --- 18,69 ---- from pybot import config, options, hooks, mm, servers + from pybot.user import User import time import re import os ! HELP_SEEN = [ ! ("""\ ! You may check when was the last time I've seen somebody with \ ! "[have you] seen <nick>". I'll also let you know what was the \ ! last message the user wrote.\ ! """,)] ! ! HELP_SEARCH = [ ! ("""\ ! You may search the log files with "[show] (log[s]|message[s]) \ ! [with] /<regexp>/'. You must have the "logsearch" permission for \ ! this to work.\ ! """,)] ! ! 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() ! src.setstring(self.src) ! if self.type == "MESSAGE": ! s = "<%s> %s" % (src.nick, self.line) ! elif self.type == "ACTION": ! s = "* %s %s" % (src.nick, self.line) ! else: ! s = "" ! return s ! ! def timestr(self): ! msg = time.localtime(self.time) ! now = time.localtime() ! if msg[:3] == now[:3]: ! s = "today at %d:%d" % msg[3:5] ! else: ! s = "%d-%d-%d at %d:%d" % msg[:5] ! return s ! class Log: *************** *** 38,41 **** --- 77,111 ---- 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): + p = re.compile(regexp, re.I) + file = open(self.__logname) + l = [] + for line in file.xreadlines(): + 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: + l.pop(0) + file.close() + return l + class LogModule: def __init__(self): *************** *** 49,56 **** # Match '[have you] seen <nick> [!?]' ! self.re1 = re.compile(r"(?:have\s+you\s+)?seen\s+(?P<nick>\w+)\s*[!?]*$", re.I) ! # Match '[show] log [with] /<regexp>/[.!]' ! #self.re2 = re.compile("", re.I) def unload(self): --- 119,132 ---- # Match '[have you] seen <nick> [!?]' ! self.re1 = re.compile(r"(?:have\s+you\s+)?seen\s+(?P<nick>[^\s!?]+)\s*[!?]*$", re.I) ! # Match '[show] (log[s]|message[s]) [with] /<regexp>/[.!]' ! self.re2 = re.compile("(?:show\s+)?(?:log|message)s?\s+(?:with\s+)?/(?P<regexp>.*)/\s*[.!?]*", re.I) ! ! # Match 'seen' ! mm.register_help(0, "seen", HELP_SEEN) ! ! # Match '(log|search (log[s]|message[s]))' ! mm.register_help(0, "log|search\s+(?:log|message)s?", HELP_SEARCH) def unload(self): *************** *** 59,63 **** hooks.unregister("CTCP", self.log_ctcp, 90) hooks.unregister("OutMessage", self.log_outmessage, 90) ! hooks.nuregister("OutCTCP", self.log_outctcp, 90) def message(self, msg): --- 135,142 ---- hooks.unregister("CTCP", self.log_ctcp, 90) hooks.unregister("OutMessage", self.log_outmessage, 90) ! hooks.unregister("OutCTCP", self.log_outctcp, 90) ! ! mm.unregister_help(0, HELP_SEEN) ! mm.unregister_help(0, HELP_SEARCH) def message(self, msg): *************** *** 65,69 **** m = self.re1.match(msg.line) if m: ! msg.answer("%:", ["Oops!", "Sorry!"], "Not yet", [".", "!"]) return 0 --- 144,178 ---- m = self.re1.match(msg.line) if m: ! nick = m.group("nick") ! logmsg = self.log.seen(nick) ! if not logmsg: ! msg.answer("%:", "Sorry, I haven't seen %s for a while..." % nick) ! else: ! msg.answer("%:", "I have seen %s %s, with the following message:" % (nick, logmsg.timestr())) ! msg.answer(str(logmsg)) ! return 0 ! m = self.re2.match(msg.line) ! if m: ! if mm.hasperm(1, msg.server.servername, msg.target, msg.user, "logsearch"): ! max = 5 ! logmsgs = self.log.search(m.group("regexp"), max) ! if logmsgs: ! llen = len(logmsgs) ! if llen == 1: ! if max == 1: ! s = "Here is the last entry found:" ! else: ! s = "Here is the only entry found:" ! elif llen == max: ! s = "Here are the last %d entries found:" % llen ! else: ! s = "Here are the only %d entries found:" % llen ! msg.answer("%:", s) ! for logmsg in logmsgs: ! msg.answer(str(logmsg)) ! else: ! msg.answer("%:", ["Sorry!", "Oops!"], ["No messages found", "Can't find any message", "No entries found"], [".", "!"]) ! else: ! msg.answer("%:", [("You're not", ["allowed to search logs.", "that good...", "allowed to do this..."]), "No, sir!", "Nope."]) return 0 |