diff --git a/launch.py b/launch.py new file mode 100755 index 0000000..ad0f3d6 --- /dev/null +++ b/launch.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 + +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.start() diff --git a/sexiibot/channel.py b/sexiibot/channel.py index 103aabe..7a52294 100644 --- a/sexiibot/channel.py +++ b/sexiibot/channel.py @@ -2,25 +2,44 @@ import time import threading +import os #from core import Sexiibot class Channel(object): """Channel class""" def __init__(self, name, bot, extensions=None): - bot.join(name) - self.__name = name - self.__bot = bot - self.__in = open(bot.getIrcPath() + "%s/in" % name, 'w', 1) - self.__out = open(bot.getIrcPath() + "%s/out" % name, 'r') - self.__exts = extentions - self.__mode = None - self.__alive = True - self.__thread = threading.Thread(target=watch).start() + self.__name = name + self.__bot = bot + self.__in = None + self.__out = None + self.__exts = extensions + self.__mode = None + self.__alive = True + self.__thread = None + self.__connect = False + + 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') + self.__out.seek(0,2) + self.__thread = threading.Thread(target=self.watch).start() + while not self.__connect: + self.__bot.join(self.__name) + time.sleep(0.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) + def getName(self): + return self.__name + def message(self, msg): - self.__in.write(msg + "\n") + print("message: %s" % msg) + self.__in.write("%s\n" % msg) def action(self, action): self.message("/j %s" % action) @@ -34,8 +53,20 @@ class Channel(object): def watch(self): while self.__alive: - line = self.__out.readline() + line = self.__out.readline().rstrip() if line: - print(line) #À ce stade du développement on ne traite rien + print(line) + if not self.__connect: + self.__connect = True + else: + self.treat(line) #À ce stade du développement on ne traite rien time.sleep(0.1) + def treat(self, line): # bon là, clairement, c'est pas sa forme définitive, c'est qu'un POC + content = line.split(' ',3) + if content[3] and not content[2] is "<%s>" % self.__bot.getNick() and self.__bot.getNick() in content[3]: + self.message("%s, je suis flattée que l'on s'adresse à moi, malheureusement je suis con comme un balai." % content[2][1:-1]) + + def __del__(self): + self.__alive = False + self.__thread.join() diff --git a/sexiibot/core.py b/sexiibot/core.py index 772cb7d..d36c84a 100644 --- a/sexiibot/core.py +++ b/sexiibot/core.py @@ -2,51 +2,64 @@ import os import subprocess -from channel import Channel +import time +from sexiibot.channel import Channel class Sexiibot(object): """Core sexiibot class""" - def __init__(self, server, nick, new=True, port=None, ssl=False, channel=None, realname=None, extensions=None, iipath=None): + def __init__(self, server, nick, new=True, port=None, ssl=False, channels=None, realname=None, iipath=None): if new: self.__server = server else: self.__server = None self.__port = port self.__ssl = ssl - self.__channels = channels + self.__channels = [] self.__nick = nick if not realname is None: - self.__realname = nick - else: self.__realname = realname + else: + self.__realname = nick self.__in = None self.__out = None self.__iipath = iipath - sekf.__iiproc = None + self.__iiproc = None + for chan in channels: + self.__channels += [Channel(chan,self)] + self.__test = None def start(self): - exists = os.path.isfile(self.getIrcPath() + "in") if not self.__server is None: - # Check if a ii instance isn't already running on our potentially existing FIFO - if exists: - print("FIFO for that server already exist.") - print("Please clean the irc folder and try again.") - sys.exit(1) + try: + os.remove(self.getIrcPath()+"in") + except OSError: + pass if self.__port is None: if self.__ssl: self.__port = 6697 else: self.__port = 6667 - cmd = [iipath, "-s", self.__server, "-p", self.__port, "-n", self.__nick, "-f", self.__realname] + cmd = [self.__iipath, "-s", self.__server, "-p", "%d" % self.__port, "-n", self.__nick, "-f", self.__realname] if self.__ssl: cmd += ["-e", "ssl"] + print(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: + chan.join() + + def openServerIO(self): self.__in = open(self.getIrcPath() + "in", 'w', 1) self.__out = open(self.getIrcPath() + "out", 'r') def messageServer(self, message): self.__in.write(message + "\n") + print("Message: %s" % message) def join(self, channel): self.messageServer("/j %s" % channel) @@ -54,5 +67,8 @@ class Sexiibot(object): def messageChannel(self, channel, message): self.__channels[channel].message(message) - def getIrcPath(): + def getIrcPath(self): return "%s/irc/%s/" % (os.environ["HOME"], self.__server) + + def getNick(self): + return self.__nick