From 674c5e26b3da54e12ce2079114ea82210451c66b Mon Sep 17 00:00:00 2001 From: admin666 Date: Sat, 5 Feb 2022 00:46:57 +0100 Subject: [PATCH] Extension --- bot_main.py | 83 +++++++++++++++++++++++++++++++++++++++---------- commands.py | 30 ++++++++++++++++++ hintTables.py | 55 +++++++++++++++++++++++++++++--- localServer.py | 29 +++++++++++++++++ sharedBuffer.py | 18 +++++++++++ 5 files changed, 194 insertions(+), 21 deletions(-) create mode 100644 commands.py create mode 100644 localServer.py create mode 100644 sharedBuffer.py diff --git a/bot_main.py b/bot_main.py index 67d8849..3ce2902 100755 --- a/bot_main.py +++ b/bot_main.py @@ -3,11 +3,15 @@ import logging from getpass import getpass from argparse import ArgumentParser - import slixmpp +import random +import threading +import socket -from hintTables import wordtable - +from hintTables import * +from commands import * +from sharedBuffer import SharedBuffer +from localServer import LocalServer class MUCBot(slixmpp.ClientXMPP): @@ -89,20 +93,59 @@ class MUCBot(slixmpp.ClientXMPP): how it may be used. """ - for hint in wordtable: - if hint in msg['body']: - self.send_message(mto=msg['from'].bare, - mbody="%s, ça a l'air d'être une commande... mais je ne l'ai pas saisie." % msg['mucnick'], - mtype='groupchat') + if msg['mucnick'] == self.nick: + return + + # Commands + + cmdhints = list(commandtable.keys()) + random.shuffle(cmdhints) + + for opcod in cmdhints: + if opcod in msg['body']: + self.send_message(mto=msg['from'].bare, + mbody="%s, %s" % (msg['mucnick'], + commandtable[opcod]() ), + mtype='groupchat') + return + + # When people speak to each other without necessarily highlighting Isengard + + anechints = list(annecdotetable.keys()) + random.shuffle(anechints) - if msg['mucnick'] != self.nick and self.nick in msg['body']: - self.send_message(mto=msg['from'].bare, - mbody="%s, je n'ai pas compris (je suis un peu bête) mais j'ai bien lu!" % msg['mucnick'], - mtype='groupchat') - if msg['body'][0] == "!": - self.send_message(mto=msg['from'].bare, - mbody="%s, ça a l'air d'être une commande... mais je ne l'ai pas saisie." % msg['mucnick'], - mtype='groupchat') + for hint in anechints: + if hint in msg['body']: + self.send_message(mto=msg['from'].bare, + mbody="%s" % (annecdotetable[hint][ + random.randrange(0,len(annecdotetable[hint])) + ]), + mtype='groupchat') + return + + if self.nick not in msg['body']: + return + + # When people talk to Isengard + + topichints = list(topictable.keys()) + random.shuffle(topichints) + + for hint in topichints: + if hint in msg['body']: + self.send_message(mto=msg['from'].bare, + mbody="%s, %s" % (msg['mucnick'], + topictable[hint][ + random.randrange(0,len(topictable[hint])) + ]), + mtype='groupchat') + return + + # And fallback + + self.send_message(mto=msg['from'].bare, + mbody="%s, je n'ai pas compris (je suis un peu bête) mais j'ai bien lu!" % msg['mucnick'], + mtype='groupchat') def muc_online(self, presence): """ @@ -121,7 +164,6 @@ class MUCBot(slixmpp.ClientXMPP): mbody="Salut %s !" % (presence['muc']['nick']), mtype='groupchat') - if __name__ == '__main__': # Setup the command line arguments. parser = ArgumentParser() @@ -158,6 +200,13 @@ if __name__ == '__main__': xmpp.register_plugin('xep_0045') # Multi-User Chat xmpp.register_plugin('xep_0199') # XMPP Ping + # Create buffer + sBuffer = SharedBuffer(xmpp) + # Connect to the XMPP server and start processing XMPP stanzas. xmpp.connect() + + localservthread = LocalServer(sBuffer) + localservthread.start() + xmpp.process() diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..f6176ce --- /dev/null +++ b/commands.py @@ -0,0 +1,30 @@ + + +# Isengard commands + +def cmdping(): + """ + Ping command. + """ + + return "pong !" + + + + + + + + + + + +# Commands + +commandtable = { + +"ping" : cmdping, + +} + + diff --git a/hintTables.py b/hintTables.py index 051257a..a7c42a1 100644 --- a/hintTables.py +++ b/hintTables.py @@ -1,14 +1,61 @@ -wordtable = { +# When people speak to each other without necessarily highlighting Isengard +annecdotetable = { -"isengard" : ["eh ?! Moi c'est Isengard, la majuscule compte :<", - "non mais ho, ma majuscule !"], +"isengard" : ["Eh ?! Moi c'est Isengard, la majuscule compte 😟", + "Non mais ho, ma majuscule !"], +"Sauron" : ["Sauron ? On a rompu récemment, trop de DRMs en lui.", + "J'ai jamais aimé ce gars là. Toujours à s'enflammer pour rien...", + "Jamais entendu parler." + ], +"privateur" : ["😨️"], +"bonne nuit" : ["Bonne nuit ! 💤️"], +"Bonne nuit" : ["Bonne nuit ! 💤️"], +"Good night" : ["Bonne nuit ! 💤️"], +"good night" : ["Bonne nuit ! 💤️"], -"Isengard" : ["yop, c'est moi ! ;)", "la Tour, la seule et unique !", +"night'" : ["Bonne nuit ! 💤️"], + +"liberté" : ["🗽️"], + +"libre" : ["🗽️"], + +"Libre" : ["🗽️"], + +"Liberté" : ["🗽️"], + +"d'yeux que pour" : ["Je n'ai d'yeux que pour le libre."], +} + +# When people talk to Isengard +topictable = { + +"hello" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "hello"], + +"hey" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "HAI", "heyo"], + +"Hey" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "HAI", "heyo"], + +"salut" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "hello", "chalut"], + +"bonjour" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "hello"], + +"Hello" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "hello"], + +"Salut" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", + "à vos ordres !", "yop !", "hello", "chalut"], + +"Bonjour" : ["yop, c'est moi ! ;)", "ici la Tour, la seule et unique !", "à vos ordres !", "yop !", "hello"] } diff --git a/localServer.py b/localServer.py new file mode 100644 index 0000000..5bd055d --- /dev/null +++ b/localServer.py @@ -0,0 +1,29 @@ +import random +import threading +import socket + +class LocalServer(threading.Thread): + def __init__(self, sharedBuffer): + threading.Thread.__init__(self) + self.sharedBuffer = sharedBuffer + + def run(self): + sockfd = socket.socket() + print("Socket successfully created") + port = 12345 # Reserve a port on your computer...in our case it is 12345, but it can be anything + sockfd.bind(('127.0.0.1', port)) + print("Socket binded to %s" %(port)) + sockfd.listen(5) # Put the socket into listening mode + print("Socket is listening") + + while True: + client, addr = sockfd.accept() # Establish connection with client + print('Got connection from', addr) + rcvStr = client.recv(1024).decode() + print(rcvStr) + + self.sharedBuffer.push(rcvStr) + + client.send(b'') # Send a message to the client + client.close() + print("Socket is listening") diff --git a/sharedBuffer.py b/sharedBuffer.py new file mode 100644 index 0000000..e8244d2 --- /dev/null +++ b/sharedBuffer.py @@ -0,0 +1,18 @@ +class SharedBuffer: + def __init__(self, linkedBot): + self.buf = [] + self.linkedBot = linkedBot + + def flush(self): + for msg in self.buf: + self.linkedBot.send_message( + mto=msg.split("|")[0], + mbody=msg.split("|")[1], + mtype='groupchat') + self.buf.pop(self.buf.index(msg)) + + def push(self, msg): + self.buf.append(msg) + + if len(self.buf) > 0: + self.flush()