amélioration globale, ajout de contrôle externe
This commit is contained in:
parent
0a37aeccc2
commit
f24dac6714
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
sockpath = "/root/sexiibot/sexiibot.sock"
|
||||||
|
try:
|
||||||
|
sock.connect(sockpath)
|
||||||
|
except socket.error:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# Send data
|
||||||
|
message = 'join #bordel'
|
||||||
|
sock.sendall(bytes(message, 'UTF-8'))
|
||||||
|
|
||||||
|
finally:
|
||||||
|
sock.close()
|
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
sockpath = "/root/sexiibot/sexiibot.sock"
|
||||||
|
try:
|
||||||
|
sock.connect(sockpath)
|
||||||
|
except socket.error:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
# Send data
|
||||||
|
message = 'leave #bordel'
|
||||||
|
sock.sendall(bytes(message, 'UTF-8'))
|
||||||
|
|
||||||
|
finally:
|
||||||
|
sock.close()
|
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
|
||||||
from sexiibot.core import Sexiibot
|
from sexiibot.core import Sexiibot
|
||||||
|
|
||||||
sex = Sexiibot("irc.smoothirc.net", "sexiibot", ssl=True, channels=["#hashtagueule","#sexiibot"], iipath="/var/bot/ii-1.7/ii")
|
sex = Sexiibot("irc.smoothirc.net", "sexiibot", ssl=True, channels=["#sexiibot","#hashtagueule"], iipath="/var/bot/ii-1.7/ii")
|
||||||
sex.start()
|
sex.start()
|
||||||
|
|
|
@ -1,8 +1,20 @@
|
||||||
#!/bin/env python
|
#!/bin/env python
|
||||||
|
|
||||||
|
"""channel.py: Contains the channel class if sexiibot"""
|
||||||
|
|
||||||
|
__author__ = "raspbeguy"
|
||||||
|
__copyright__ = "Copyright 2016, raspbeguy"
|
||||||
|
__credits__ = ["raspbeguy"]
|
||||||
|
__license__ = "WTFPL"
|
||||||
|
__version__ = "0.1.1"
|
||||||
|
__maintainer__ = "raspbeguy"
|
||||||
|
__email__ = "raspbeguy@hashtagueule.fr"
|
||||||
|
__status__ = "Devlopment"
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
#from core import Sexiibot
|
#from core import Sexiibot
|
||||||
|
|
||||||
class Channel(object):
|
class Channel(object):
|
||||||
|
@ -24,13 +36,11 @@ class Channel(object):
|
||||||
os.makedirs(self.__bot.getIrcPath() + self.__name)
|
os.makedirs(self.__bot.getIrcPath() + self.__name)
|
||||||
open(self.__bot.getIrcPath() + "%s/out" % self.__name, 'a').close()
|
open(self.__bot.getIrcPath() + "%s/out" % self.__name, 'a').close()
|
||||||
self.__out = open(self.__bot.getIrcPath() + "%s/out" % self.__name, 'r')
|
self.__out = open(self.__bot.getIrcPath() + "%s/out" % self.__name, 'r')
|
||||||
print("----> first line of %s logfile is:" % self.__name)
|
#self.__out.seek(0,2)
|
||||||
print(self.__out.readline())
|
|
||||||
self.__out.seek(0,2)
|
|
||||||
self.__thread = threading.Thread(target=self.watch)
|
self.__thread = threading.Thread(target=self.watch)
|
||||||
self.__thread.start()
|
self.__thread.start()
|
||||||
while not self.__connect:
|
while not self.__connect:
|
||||||
self.__bot.join(self.__name)
|
self.__bot.sendJoin(self.__name)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
#TODO: add something to wait the FIFO to be ready
|
#TODO: add something to wait the FIFO to be ready
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -41,7 +51,7 @@ class Channel(object):
|
||||||
return self.__name
|
return self.__name
|
||||||
|
|
||||||
def message(self, msg):
|
def message(self, msg):
|
||||||
print("message: %s" % msg)
|
print("%s <-- %s" % (self.__name, msg))
|
||||||
self.__in.write("%s\n" % msg)
|
self.__in.write("%s\n" % msg)
|
||||||
|
|
||||||
def action(self, action):
|
def action(self, action):
|
||||||
|
@ -50,25 +60,28 @@ class Channel(object):
|
||||||
def leave(self, msg="sexiibot, the bot you're dreaming about"):
|
def leave(self, msg="sexiibot, the bot you're dreaming about"):
|
||||||
self.message("/l %s" % msg)
|
self.message("/l %s" % msg)
|
||||||
self.__alive = False
|
self.__alive = False
|
||||||
|
self.__connect = False
|
||||||
self.__thread.join()
|
self.__thread.join()
|
||||||
self.__in.close()
|
self.__in.close()
|
||||||
self.__out.close()
|
self.__out.close()
|
||||||
|
shutil.rmtree(self.__bot.getIrcPath() + self.__name)
|
||||||
|
|
||||||
def watch(self):
|
def watch(self):
|
||||||
while self.__alive:
|
while self.__alive:
|
||||||
line = self.__out.readline().rstrip()
|
line = self.__out.readline().rstrip()
|
||||||
if line:
|
if line:
|
||||||
print(line)
|
|
||||||
if not self.__connect:
|
if not self.__connect:
|
||||||
self.__connect = True
|
self.__connect = True
|
||||||
else:
|
else:
|
||||||
self.treat(line) #À ce stade du développement on ne traite rien
|
message = line.split(' ',3)
|
||||||
|
self.treat(message)
|
||||||
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
|
def treat(self, content): # bon là, clairement, c'est pas sa forme définitive, c'est qu'un POC
|
||||||
content = line.split(' ',3)
|
if not content[2][1:-1] == self.__bot.getNick():
|
||||||
if content[3] and not content[2] is "<%s>" % self.__bot.getNick() and self.__bot.getNick() in content[3]:
|
print("%s --> %s" % (self.__name, content))
|
||||||
self.message("%s, je suis flattée que l'on s'adresse à moi, malheureusement je suis con comme un balai." % content[2][1:-1])
|
if content[3] and self.__bot.getNick() in content[3]:
|
||||||
|
self.message("%s, je suis flattée que l'on s'adresse à moi, malheureusement je suis sexiibot." % content[2][1:-1])
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.__alive = False
|
self.__alive = False
|
||||||
|
|
129
sexiibot/core.py
129
sexiibot/core.py
|
@ -1,23 +1,35 @@
|
||||||
#!/bin/env python
|
#!/bin/env python
|
||||||
|
|
||||||
|
"""core.py: Contains the main class of sexiibot"""
|
||||||
|
|
||||||
|
__author__ = "raspbeguy"
|
||||||
|
__copyright__ = "Copyright 2016, raspbeguy"
|
||||||
|
__credits__ = ["raspbeguy"]
|
||||||
|
__license__ = "WTFPL"
|
||||||
|
__version__ = "0.1.1"
|
||||||
|
__maintainer__ = "raspbeguy"
|
||||||
|
__email__ = "raspbeguy@hashtagueule.fr"
|
||||||
|
__status__ = "Devlopment"
|
||||||
|
|
||||||
|
from sexiibot.channel import Channel
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import shutil
|
import shutil
|
||||||
from sexiibot.channel import Channel
|
import socket
|
||||||
|
|
||||||
class Sexiibot(object):
|
class Sexiibot(object):
|
||||||
"""Core sexiibot class"""
|
"""Core sexiibot class"""
|
||||||
|
|
||||||
def __init__(self, server, nick, new=True, port=None, ssl=False, channels=[], realname=None, iipath=None):
|
def __init__(self, server, nick, new=True, port=None, ssl=False, channels=[], realname=None, iipath=None, sock="./sexiibot.sock"):
|
||||||
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 = []
|
self.__channels = {}
|
||||||
self.__nick = nick
|
self.__nick = nick
|
||||||
if not realname is None:
|
if not realname is None:
|
||||||
self.__realname = realname
|
self.__realname = realname
|
||||||
|
@ -28,16 +40,23 @@ class Sexiibot(object):
|
||||||
self.__iipath = iipath
|
self.__iipath = iipath
|
||||||
self.__iiproc = None
|
self.__iiproc = None
|
||||||
for chan in channels:
|
for chan in channels:
|
||||||
self.__channels += [Channel(chan,self)]
|
self.__channels[chan] = Channel(chan,self)
|
||||||
self.__treated = channels
|
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.__quit = False
|
||||||
self.__watch = None
|
self.__watch = None
|
||||||
|
self.__sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
try:
|
||||||
|
os.unlink(sock)
|
||||||
|
except OSError:
|
||||||
|
if os.path.exists(sock):
|
||||||
|
raise
|
||||||
|
self.__sock.bind(sock)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
if os.path.exists(self.getIrcPath()):
|
||||||
|
for dirname in next(os.walk(self.getIrcPath()))[1]:
|
||||||
|
print ("deleting " + dirname)
|
||||||
|
shutil.rmtree(self.getIrcPath() + dirname)
|
||||||
if not self.__server is None:
|
if not self.__server is None:
|
||||||
try:
|
try:
|
||||||
os.remove(self.getIrcPath()+"in")
|
os.remove(self.getIrcPath()+"in")
|
||||||
|
@ -56,27 +75,27 @@ class Sexiibot(object):
|
||||||
print(" ".join(cmd))
|
print(" ".join(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
|
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')
|
||||||
|
chans_t0 = list(self.__channels.values())
|
||||||
|
for chan in chans_t0:
|
||||||
|
chan.join()
|
||||||
|
self.listenCommands()
|
||||||
|
|
||||||
def messageServer(self, message):
|
def messageServer(self, message):
|
||||||
self.__in.write(message + "\n")
|
self.__in.write(message + "\n")
|
||||||
print("Message: %s" % message)
|
print("Command to server: %s" % message)
|
||||||
|
|
||||||
def join(self, channel):
|
def sendJoin(self, channel):
|
||||||
self.messageServer("/j %s" % channel)
|
self.messageServer("/j %s" % channel)
|
||||||
|
|
||||||
def messageChannel(self, channel, message):
|
def messageChan(self, args):
|
||||||
self.__channels[channel].message(message)
|
if len(args) == 2:
|
||||||
|
self.__channels[args[0]].message(args[1])
|
||||||
|
|
||||||
|
def actionChan(self, args):
|
||||||
|
if len(args) == 3:
|
||||||
|
self.__channels[args[0]].action(args[1])
|
||||||
|
|
||||||
def getIrcPath(self):
|
def getIrcPath(self):
|
||||||
return "%s/irc/%s/" % (os.environ["HOME"], self.__server)
|
return "%s/irc/%s/" % (os.environ["HOME"], self.__server)
|
||||||
|
@ -84,21 +103,75 @@ class Sexiibot(object):
|
||||||
def getNick(self):
|
def getNick(self):
|
||||||
return self.__nick
|
return self.__nick
|
||||||
|
|
||||||
def createChan(self, dirname):
|
def setNick(self, nick):
|
||||||
|
self.messageServer("/n %s" % nick)
|
||||||
|
self.__nick = nick
|
||||||
|
|
||||||
|
def newChan(self, args):
|
||||||
|
if len(args) == 1:
|
||||||
|
chan = Channel(args[0],self)
|
||||||
|
self.__channels[args[0]] = chan
|
||||||
|
chan.join()
|
||||||
|
|
||||||
|
def leaveChan(self, args):
|
||||||
|
if len(args) == 1:
|
||||||
|
self.__channels[args[0]].leave()
|
||||||
|
if len(args) == 2:
|
||||||
|
self.__channels[args[0]].leave(args[1])
|
||||||
|
del self.__channel[args[0]]
|
||||||
|
|
||||||
|
def incomingChan(self, dirname):
|
||||||
root = self.getIrcPath()
|
root = self.getIrcPath()
|
||||||
if os.path.isdir(root + dirname):
|
if os.path.isdir(root + dirname):
|
||||||
if os.path.exists(root + dirname + "/in") and os.path.isfile(root + dirname + "/out"):
|
if os.path.exists(root + dirname + "/in") and os.path.isfile(root + dirname + "/out"):
|
||||||
chan = Channel(dirname, self, connected=True)
|
chan = Channel(dirname, self, connected=True)
|
||||||
chan.join()
|
chan.join()
|
||||||
self.__channels += [chan]
|
self.__channels[dirname] = chan
|
||||||
self.__treated += [dirname]
|
|
||||||
|
|
||||||
def watchChan(self):
|
def watchChan(self):
|
||||||
root = self.getIrcPath()
|
root = self.getIrcPath()
|
||||||
while not self.__quit:
|
while not self.__quit:
|
||||||
chanlist = next(os.walk(self.getIrcPath()))[1]
|
chanlist = next(os.walk(self.getIrcPath()))[1]
|
||||||
diff = list(set(chanlist) - set(self.__treated))
|
usrlist = list(filter(lambda k: not k[0] is "#" and not k in self.__treated, chanlist))
|
||||||
for chan in diff:
|
#diff = list(set(usrlist) - set(self.__treated))
|
||||||
print("--> popped chan: " + chan)
|
for chan in usrlist:
|
||||||
self.createChan(chan)
|
print("*** popped chan: " + chan)
|
||||||
|
self.incomingChan(chan)
|
||||||
|
self.__treated += [chan]
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def listenCommands(self):
|
||||||
|
self.__sock.listen(1)
|
||||||
|
while not self.__quit:
|
||||||
|
connection, client = self.__sock.accept()
|
||||||
|
try:
|
||||||
|
command = ""
|
||||||
|
while True:
|
||||||
|
data = connection.recv(16)
|
||||||
|
command += data.decode('UTF-8')
|
||||||
|
if not data:
|
||||||
|
self.parseCommand(command)
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
def parseCommand(self, command):
|
||||||
|
cmd = command.split(" ")
|
||||||
|
order = {
|
||||||
|
"join" : self.newChan,
|
||||||
|
"leave" : self.leaveChan,
|
||||||
|
"quit" : self.quit,
|
||||||
|
"say" : self.messageChan,
|
||||||
|
"action" : self.actionChan,
|
||||||
|
"nick" : self.setNick
|
||||||
|
}
|
||||||
|
order[cmd[0]](cmd[1:])
|
||||||
|
|
||||||
|
def quit(selfi, args=[]):
|
||||||
|
for chan in self.__channels:
|
||||||
|
self.leaveChan([chan, "sexiibot stopping"])
|
||||||
|
self.__quit = True
|
||||||
|
self.__watch.join()
|
||||||
|
self.__in.close()
|
||||||
|
self.__out.close()
|
||||||
|
|
Loading…
Reference in New Issue