Extension
This commit is contained in:
parent
b84cb3e291
commit
674c5e26b3
83
bot_main.py
83
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
|
||||
|
||||
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')
|
||||
# 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)
|
||||
|
||||
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()
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
|
||||
|
||||
# Isengard commands
|
||||
|
||||
def cmdping():
|
||||
"""
|
||||
Ping command.
|
||||
"""
|
||||
|
||||
return "pong !"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# Commands
|
||||
|
||||
commandtable = {
|
||||
|
||||
"ping" : cmdping,
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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"]
|
||||
}
|
||||
|
|
|
@ -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")
|
|
@ -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()
|
Loading…
Reference in New Issue