From 0a37aeccc2462b69212302a4449514f66f3beb3b Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Thu, 2 Jun 2016 15:02:59 +0200 Subject: [PATCH] sexiibot nettoie les dossiers des canaux ne figurant pas dans ses champs. --- launch.py | 2 +- sexiibot/channel.py | 11 +++++++---- sexiibot/core.py | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/launch.py b/launch.py index ad0f3d6..7fb6aae 100755 --- a/launch.py +++ b/launch.py @@ -3,5 +3,5 @@ import time from sexiibot.core import Sexiibot -sex = Sexiibot("irc.smoothirc.net", "sexiibot", ssl=True, channels=["#hashtagueule"], iipath="/var/bot/ii-1.7/ii") +sex = Sexiibot("irc.smoothirc.net", "sexiibot", ssl=True, channels=["#hashtagueule","#sexiibot"], iipath="/var/bot/ii-1.7/ii") sex.start() diff --git a/sexiibot/channel.py b/sexiibot/channel.py index 7a52294..3e895f7 100644 --- a/sexiibot/channel.py +++ b/sexiibot/channel.py @@ -8,7 +8,7 @@ import os class Channel(object): """Channel class""" - def __init__(self, name, bot, extensions=None): + def __init__(self, name, bot, extensions=None, connected=False): self.__name = name self.__bot = bot self.__in = None @@ -17,18 +17,21 @@ class Channel(object): self.__mode = None self.__alive = True self.__thread = None - self.__connect = False + self.__connect = connected def join(self): if not os.path.exists(self.__bot.getIrcPath() + self.__name): os.makedirs(self.__bot.getIrcPath() + self.__name) open(self.__bot.getIrcPath() + "%s/out" % self.__name, 'a').close() self.__out = open(self.__bot.getIrcPath() + "%s/out" % self.__name, 'r') + print("----> first line of %s logfile is:" % self.__name) + print(self.__out.readline()) self.__out.seek(0,2) - self.__thread = threading.Thread(target=self.watch).start() + self.__thread = threading.Thread(target=self.watch) + self.__thread.start() while not self.__connect: self.__bot.join(self.__name) - time.sleep(0.1) + time.sleep(1) #TODO: add something to wait the FIFO to be ready time.sleep(1) self.__in = open(self.__bot.getIrcPath() + "%s/in" % self.__name, 'w', 1) diff --git a/sexiibot/core.py b/sexiibot/core.py index d36c84a..3988e61 100644 --- a/sexiibot/core.py +++ b/sexiibot/core.py @@ -3,12 +3,14 @@ import os import subprocess import time +import threading +import shutil from sexiibot.channel import Channel class Sexiibot(object): """Core sexiibot class""" - def __init__(self, server, nick, new=True, port=None, ssl=False, channels=None, realname=None, iipath=None): + def __init__(self, server, nick, new=True, port=None, ssl=False, channels=[], realname=None, iipath=None): if new: self.__server = server else: @@ -27,7 +29,13 @@ class Sexiibot(object): self.__iiproc = None for chan in channels: self.__channels += [Channel(chan,self)] - self.__test = None + self.__treated = channels + for dirname in next(os.walk(self.getIrcPath()))[1]: + if not dirname in channels: + shutil.rmtree(self.getIrcPath() + dirname) + self.__quit = False + self.__watch = None + def start(self): if not self.__server is None: @@ -35,6 +43,8 @@ class Sexiibot(object): os.remove(self.getIrcPath()+"in") except OSError: pass + self.__watch = threading.Thread(target=self.watchChan) + self.__watch.start() if self.__port is None: if self.__ssl: self.__port = 6697 @@ -43,11 +53,12 @@ class Sexiibot(object): cmd = [self.__iipath, "-s", self.__server, "-p", "%d" % self.__port, "-n", self.__nick, "-f", self.__realname] if self.__ssl: cmd += ["-e", "ssl"] - print(cmd) + print(" ".join(cmd)) self.__iiproc = subprocess.Popen(cmd) # self.__iiproc.terminate() later to stop time.sleep(2) #temporaire self.openServerIO() self.joinAll() + def joinAll(self): for chan in self.__channels: @@ -72,3 +83,22 @@ class Sexiibot(object): def getNick(self): return self.__nick + + def createChan(self, dirname): + root = self.getIrcPath() + if os.path.isdir(root + dirname): + if os.path.exists(root + dirname + "/in") and os.path.isfile(root + dirname + "/out"): + chan = Channel(dirname, self, connected=True) + chan.join() + self.__channels += [chan] + self.__treated += [dirname] + + def watchChan(self): + root = self.getIrcPath() + while not self.__quit: + chanlist = next(os.walk(self.getIrcPath()))[1] + diff = list(set(chanlist) - set(self.__treated)) + for chan in diff: + print("--> popped chan: " + chan) + self.createChan(chan) + time.sleep(0.1)