From cb1e01ce514770696b53ceb981d839815efbf612 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 6 Jan 2014 04:00:12 +0100 Subject: ircbot: multi-channel magic --- ircbot/contoller.py | 83 ++++++++++++++++++++++++----------------------------- ircbot/rssbot.py | 20 +++++++++---- 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/ircbot/contoller.py b/ircbot/contoller.py index e965da88..c18361fe 100755 --- a/ircbot/contoller.py +++ b/ircbot/contoller.py @@ -4,64 +4,27 @@ import _thread import rssbot import os -feedfile = 'new_feeds' -url_shortener = 'http://localhost' - -if FEEDFILE in os.environ: - feedfile = os.environ['FEEDFILE'] - -if URLSHORT in os.environ: - url_shortener = os.environ['URLSHORT'] - -bots = {} -knews = NewsBot('knews') - -#config file reading -F = open(feedfile, "r") -lines = F.readlines() -F.close() - -for line in lines: - line = line.strip('\n') - linear = line.split('|') - bot = rssbot.RssBot(linear[1], linear[0], url_shortener) - bot.start() - bots[linear[0]] = bot - -knews.start() - class NewsBot(irc.bot.SingleServerIRCBot): - def __init__(self, name, server='ire', port=6667, chan='#news', timeout=60): + def __init__(self, name, chans=['#news'], server='ire', port=6667, timeout=60): irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], name, name) self.name = name self.server = server self.port = port - self.chan = chan + self.chans = chans self.to = timeout def start(self): self.bot = _thread.start_new_thread(irc.bot.SingleServerIRCBot.start, (self,)) -# def send(self, string): -# if len(string) < 450: -# self.connection.privmsg(self.chan, string) -# else: -# space = 0 -# for x in range(math.ceil(len(string)/400)): -# oldspace = space -# space = string.find(" ", (x+1)*400, (x+1)*400+50) -# self.connection.privmsg(self.chan, string[oldspace:space]) -# sleep(1) - - def on_welcome(self, connection, event): - connection.join(self.chan) + for chan in self.chans: + connection.join(chan) - def send(self, string): + def send(self, target, string): for line in string.split('\n'): - self.connection.privmsg(self.chan, line) + self.connection.privmsg(target, line) sleep(1) def sendq(self, target, string): @@ -78,7 +41,7 @@ class NewsBot(irc.bot.SingleServerIRCBot): args_array = event.arguments[0].split() if args_array[0][:-1]==self.name: answer = self.read_message(args_array[1:]) - self.send(answer) + self.send(event.target, answer) def read_message(self, args): try: @@ -120,7 +83,7 @@ class commands(): def save(args): output_buffer = '' for bot in bots: - output_buffer += bot + '|' + bots[bot].url + '\n' + output_buffer += bot + '|' + bots[bot].url + '|' + ' '.join(bots[bot].channels) + '\n' F = open(feedfile, "w") F.writelines(output_buffer) @@ -147,3 +110,33 @@ class commands(): return output_buffer else: return 'bot not found' + + + +feedfile = 'new_feeds' +url_shortener = 'http://localhost' +init_channels = ['#news'] + +if 'FEEDFILE' in os.environ: + feedfile = os.environ['FEEDFILE'] + +if 'URLSHORT' in os.environ: + url_shortener = os.environ['URLSHORT'] + +bots = {} +knews = NewsBot('knews') + +#config file reading +F = open(feedfile, "r") +lines = F.readlines() +F.close() + +for line in lines: + line = line.strip('\n') + linear = line.split('|') + bot = rssbot.RssBot(linear[1], linear[0], init_channels + linear[2].split(), url_shortener) + bot.start() + bots[linear[0]] = bot + +knews.start() + diff --git a/ircbot/rssbot.py b/ircbot/rssbot.py index cb7a1543..e7349d0d 100755 --- a/ircbot/rssbot.py +++ b/ircbot/rssbot.py @@ -9,18 +9,19 @@ from datetime import datetime from time import sleep class RssBot(irc.bot.SingleServerIRCBot): - def __init__(self, rss, name, url_shortener="http://localhost", server='ire', port=6667, chan='#news', timeout=60): + def __init__(self, rss, name, chans=['#news'], url_shortener="http://localhost", server='ire', port=6667, timeout=60): irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], name, name) self.url = rss self.name = name self.server = server self.port = port - self.chan = chan + self.chans = chans self.to = timeout self.oldnews = [] self.sendqueue = [] self.loop = True self.lastnew = datetime.now() + self.url_shortener = url_shortener def start(self): self.upd_loop = _thread.start_new_thread(self.updateloop, ()) @@ -47,7 +48,7 @@ class RssBot(irc.bot.SingleServerIRCBot): #try: # self.send(entry.title + " " + entry.link + " com: " + entry.comments) #except AttributeError: - shorturl = subprocess.check_output(["curl", "-sS", "-F", "uri=" + entry.link, url_shortener]).decode() + shorturl = subprocess.check_output(["curl", "-sS", "-F", "uri=" + entry.link, self.url_shortener]).decode() self.send(entry.title + " " + shorturl) self.oldnews.append(entry.link) self.lastnew = datetime.now() @@ -58,18 +59,25 @@ class RssBot(irc.bot.SingleServerIRCBot): if self.connection.connected: for line in string.split('\n'): if len(line) < 450: - self.connection.privmsg(self.chan, line) + for chan in self.channels: + self.connection.privmsg(chan, line) else: space = 0 for x in range(math.ceil(len(line)/400)): oldspace = space space = line.find(" ", (x+1)*400, (x+1)*400+50) - self.connection.privmsg(self.chan, line[oldspace:space]) + for chan in self.channels: + self.connection.privmsg(chan, line[oldspace:space]) sleep(1) sleep(1) else: self.connection.reconnect() self.send(string) + def on_invite(self, connection, event): + for chan in event.arguments: + connection.join(chan) + def on_welcome(self, connection, event): - connection.join(self.chan) + for chan in self.chans: + connection.join(chan) -- cgit v1.2.3