Handle topic via a configuration file

Change-Id: I72d9310c0071ba08f23b804e13514a997458ae56
Partial-Bug: #1190296
This commit is contained in:
Julien Danjou 2013-08-22 11:28:55 +02:00
parent 336c7d9992
commit 003744fc7b
1 changed files with 34 additions and 35 deletions

View File

@ -30,6 +30,9 @@ user=StatusBot
password=password
url=https://wiki.example.com/w/api.php
pageid=1781
[#foo]
topic=Hello world
"""
import argparse
@ -162,9 +165,7 @@ class StatusBot(irc.bot.SingleServerIRCBot):
self.nickname = nickname
self.password = password
self.identify_msg_cap = False
self.ignore_topics = True
self.topic_lock = threading.Lock()
self.topics = {}
self.publishers = publishers
def on_nicknameinuse(self, c, e):
@ -183,7 +184,7 @@ class StatusBot(irc.bot.SingleServerIRCBot):
c.cap('END')
self.log.debug("Identifying to nickserv")
c.privmsg("nickserv", "identify %s " % self.password)
for channel in self.channel_list:
for channel in self.channel_list.iterkeys():
self.log.info("Joining %s" % channel)
c.join(channel)
@ -247,7 +248,7 @@ class StatusBot(irc.bot.SingleServerIRCBot):
command, nick, msg))
def broadcast(self, msg):
for channel in self.channel_list:
for channel in self.channel_list.iterkeys():
self.send(channel, msg)
def restore_all_topics(self):
@ -257,10 +258,8 @@ class StatusBot(irc.bot.SingleServerIRCBot):
def _restore_all_topics(self):
self.topic_lock.acquire()
try:
if self.topics:
for channel in self.channel_list:
self.set_topic(channel, self.topics[channel])
self.topics = {}
for channel, conf in self.channel_list.iteritems():
self.set_topic(channel, conf.get('topic', "Welcome!"))
finally:
self.topic_lock.release()
@ -271,44 +270,35 @@ class StatusBot(irc.bot.SingleServerIRCBot):
def _set_all_topics(self, topic):
self.topic_lock.acquire()
try:
if not self.topics:
self.save_topics()
for channel in self.channel_list:
for channel in self.channel_list.iterkeys():
self.set_topic(channel, topic)
finally:
self.topic_lock.release()
def save_topics(self):
# Save all the current topics
self.ignore_topics = False
for channel in self.channel_list:
self.connection.topic(channel)
time.sleep(0.5)
start = time.time()
done = False
while time.time() < start + 300:
if len(self.topics) == len(self.channel_list):
done = True
break
time.sleep(0.5)
self.ignore_topics = True
if not done:
raise Exception("Unable to save topics")
def on_currenttopic(self, c, e):
if self.ignore_topics:
return
self.topics[e.arguments[0]] = e.arguments[1]
def send(self, channel, msg):
self.connection.privmsg(channel, msg)
time.sleep(0.5)
def set_topic(self, channel, topic):
self.connection.topic(channel, topic)
self.connection.privmsg('ChanServ', 'topic %s %s' % (channel, topic))
self.chanserv_topic(channel, topic)
time.sleep(0.5)
def chanserv(self, message):
"""Send a message to ChanServ."""
self.privmsg('ChanServ', message)
def chanserv_topic(self, channel, topic):
self.chanserv("TOPIC %s %s" % (channel, topic))
def chanserv_flags(self, channel, target=None, flags=None):
"""Set flags on a channel."""
self.chanserv("FLAGS %s %s %s" % (channel, target, flags))
def chanserv_list_access(self, channel):
"""List access on a channel."""
self.chanserv("LIST %s", channel)
def _main(configpath):
config = ConfigParser.ConfigParser()
@ -321,7 +311,16 @@ def _main(configpath):
config.get('ircbot', 'nicks').split(',')]
publishers = [StatusPage(config)]
bot = StatusBot(channels, nicks, publishers,
# Build a dict such as:
# {'#channel1': { 'option1': value1, 'option2': value2, …},
# '#channel2: … }
channels_config = dict((c,
dict([(opt, config.get(c, opt))
for opt in config.options(c)]))
for c in channels)
bot = StatusBot(channels_config,
nicks, publishers,
config.get('ircbot', 'nick'),
config.get('ircbot', 'pass'),
config.get('ircbot', 'server'),