Commit 9ef04f27 authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

botconf et ménage

parent 1b027f46
server_name = irc.crans.org
server_port = 6667
nickname = legifrance
remain_time = 300
......@@ -39,6 +39,8 @@ import legifrance
import random
import re
import configobj
class MessageLogger:
def log(self, message):
......@@ -46,8 +48,9 @@ class MessageLogger:
class MmBot(irc.IRCClient):
def __init__(self):
self.nickname = 'legifrance'
def __init__(self,botconf):
self.botconf = botconf
self.nickname = self.botconf['nickname']
self.codes = legifrance.codes('legifrance.conf')
self.view = {}
......@@ -64,7 +67,7 @@ class MmBot(irc.IRCClient):
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
pass
def kickedFrom(self, channel, kicker, message):
self.logger.log("[kicked from %s by %s at %s]" %
......@@ -79,21 +82,26 @@ class MmBot(irc.IRCClient):
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
self.logger.log("%s: <%s> %s" % (channel, user, msg))
if channel == self.nickname:
# pv
if re.match('help',msg):
rep = "Commandes disponnibles en query:\n"
rep+= " - list\n"
rep+= " - add <codename> <codeid> (e.g. \"add CP LEGITEXT000006070719\")\n"
rep+= " - expire <codename> (e.g. \"expire CP\")\n"
rep+= " - reloadconf\n"
rep+= " - join <channel>\n"
rep+= "Commandes disponnibles sur les chans:\n"
rep+= " - !legifrance join <channel>\n"
rep+= " - !legifrance part\n"
self.msg(user, rep)
rep = u"Quand du texte est rencontré sur un channel, de la forme :\n"
rep+= u" [CodeName Article] ou [Article du CodeName]\n"
rep+= u" alors le lien vers legifrance est affiché.\n"
rep+= u" Exemple: [CP 323-1] ou [323-1 du CP].\n"
rep+= u"Commandes disponnibles en query:\n"
rep+= u" - list\n"
rep+= u" - add <codename> <codeid> (e.g. \"add CP LEGITEXT000006070719\")\n"
rep+= u" - expire <codename> (e.g. \"expire CP\")\n"
rep+= u" - reload_legifranceconf\n"
rep+= u" - reload_botconf\n"
rep+= u" - join <channel>\n"
rep+= u"Commandes disponnibles sur les chans:\n"
rep+= u" - !legifrance join <channel>\n"
rep+= u" - !legifrance part\n"
self.msg(user, rep.encode('utf8'))
if re.match('list',msg):
rep = "Codes disponnibles :\n"
......@@ -122,9 +130,13 @@ class MmBot(irc.IRCClient):
else:
self.msg(user, "nothing to do")
if re.match('reloadconf', msg):
if re.match('reload_legifranceconf', msg):
self.codes.conf.reload()
self.msg(user, "done")
if re.match('reload_botconf', msg):
self.botconf.reload()
self.msg(user, "done")
a=re.match('join (#[A-Za-z0-9-]+)', msg)
if a:
......@@ -151,7 +163,7 @@ class MmBot(irc.IRCClient):
trop_jeune = False
if self.view.has_key( (channel,codename,codeid) ):
if time.time()-self.view[(channel,codename,codeid)] < 300:
if time.time()-self.view[(channel,codename,codeid)] < self.botconf['remain_time']:
trop_jeune = True
if not trop_jeune:
......@@ -162,25 +174,14 @@ class MmBot(irc.IRCClient):
m= re.sub('\[([A-Za-z0-9]+) ([^ \]]+)\]','',m,1)
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
self.logger.log("* %s %s" % (user, msg))
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
self.logger.log("%s is now known as %s" % (old_nick, new_nick))
class MmBotFactory(protocol.ClientFactory):
def __init__(self):
self.channel = "#legitest"
def __init__(self,botconf):
self.botconf = botconf
pass
def buildProtocol(self, addr):
p = MmBot()
p = MmBot(self.botconf)
p.factory = self
return p
......@@ -193,10 +194,10 @@ class MmBotFactory(protocol.ClientFactory):
if __name__ == '__main__':
log.startLogging(sys.stdout)
f = MmBotFactory()
reactor.connectTCP("irc.crans.org", 6667, f)
botconf = configobj.ConfigObj('bot.conf')
log.startLogging(sys.stdout)
f = MmBotFactory(botconf)
reactor.connectTCP(botconf['server_name'], int(botconf['server_port']), f)
reactor.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment