premier POC fonctionnel, youpi.

This commit is contained in:
raspbeguy 2016-06-02 00:42:47 +02:00
parent b9af403a43
commit f53e65dc89
3 changed files with 80 additions and 26 deletions

7
launch.py Executable file
View File

@ -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()

View File

@ -2,25 +2,44 @@
import time import time
import threading import threading
import os
#from core import Sexiibot #from core import Sexiibot
class Channel(object): class Channel(object):
"""Channel class""" """Channel class"""
def __init__(self, name, bot, extensions=None): def __init__(self, name, bot, extensions=None):
bot.join(name) self.__name = name
self.__name = name self.__bot = bot
self.__bot = bot self.__in = None
self.__in = open(bot.getIrcPath() + "%s/in" % name, 'w', 1) self.__out = None
self.__out = open(bot.getIrcPath() + "%s/out" % name, 'r') self.__exts = extensions
self.__exts = extentions self.__mode = None
self.__mode = None self.__alive = True
self.__alive = True self.__thread = None
self.__thread = threading.Thread(target=watch).start() 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): def message(self, msg):
self.__in.write(msg + "\n") print("message: %s" % msg)
self.__in.write("%s\n" % msg)
def action(self, action): def action(self, action):
self.message("/j %s" % action) self.message("/j %s" % action)
@ -34,8 +53,20 @@ class Channel(object):
def watch(self): def watch(self):
while self.__alive: while self.__alive:
line = self.__out.readline() line = self.__out.readline().rstrip()
if line: 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) 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()

View File

@ -2,51 +2,64 @@
import os import os
import subprocess import subprocess
from channel import Channel import time
from sexiibot.channel import Channel
class Sexiibot(object): class Sexiibot(object):
"""Core sexiibot class""" """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: if new:
self.__server = server self.__server = server
else: else:
self.__server = None self.__server = None
self.__port = port self.__port = port
self.__ssl = ssl self.__ssl = ssl
self.__channels = channels self.__channels = []
self.__nick = nick self.__nick = nick
if not realname is None: if not realname is None:
self.__realname = nick
else:
self.__realname = realname self.__realname = realname
else:
self.__realname = nick
self.__in = None self.__in = None
self.__out = None self.__out = None
self.__iipath = iipath self.__iipath = iipath
sekf.__iiproc = None self.__iiproc = None
for chan in channels:
self.__channels += [Channel(chan,self)]
self.__test = None
def start(self): def start(self):
exists = os.path.isfile(self.getIrcPath() + "in")
if not self.__server is None: if not self.__server is None:
# Check if a ii instance isn't already running on our potentially existing FIFO try:
if exists: os.remove(self.getIrcPath()+"in")
print("FIFO for that server already exist.") except OSError:
print("Please clean the irc folder and try again.") pass
sys.exit(1)
if self.__port is None: if self.__port is None:
if self.__ssl: if self.__ssl:
self.__port = 6697 self.__port = 6697
else: else:
self.__port = 6667 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: if self.__ssl:
cmd += ["-e", "ssl"] cmd += ["-e", "ssl"]
print(cmd)
self.__iiproc = subprocess.Popen(cmd) # self.__iiproc.terminate() later to stop 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.__in = open(self.getIrcPath() + "in", 'w', 1)
self.__out = open(self.getIrcPath() + "out", 'r') self.__out = open(self.getIrcPath() + "out", 'r')
def messageServer(self, message): def messageServer(self, message):
self.__in.write(message + "\n") self.__in.write(message + "\n")
print("Message: %s" % message)
def join(self, channel): def join(self, channel):
self.messageServer("/j %s" % channel) self.messageServer("/j %s" % channel)
@ -54,5 +67,8 @@ class Sexiibot(object):
def messageChannel(self, channel, message): def messageChannel(self, channel, message):
self.__channels[channel].message(message) self.__channels[channel].message(message)
def getIrcPath(): def getIrcPath(self):
return "%s/irc/%s/" % (os.environ["HOME"], self.__server) return "%s/irc/%s/" % (os.environ["HOME"], self.__server)
def getNick(self):
return self.__nick