xmppmirror/test/testbot

155 lines
4.5 KiB
Plaintext
Raw Normal View History

2022-06-13 17:33:08 +00:00
#!/usr/bin/env python3
# XMPP bot that tests mirror bot
import argparse
import slixmpp
import configparser
2022-06-13 21:34:16 +00:00
import time
2022-06-13 17:33:08 +00:00
CONFIG_PATH = "./testconfig.ini"
VERSION='0.1.0'
# Read test messages from testmsgs.txt
ftestmsgs=open("testmsgs.txt")
testmsgs=ftestmsgs.readlines()
ftestmsgs.close()
# Generate lists with times that test messages are sent and recieved in rooms
results1=[]
for i in testmsgs:
results1.append([i,0,0])
results2=[]
for i in testmsgs:
results2.append([i,0,0])
# Check if all sent messages are recieved
def allmsgsrecv():
ret = True
for i in results1:
if i[2] == 0:
return False
for i in results2:
if i[2] == 0:
return False
2022-06-13 21:34:16 +00:00
return ret
2022-06-13 17:33:08 +00:00
# Print results and exit
def printandexit():
print("Results of test:")
2022-06-13 23:11:35 +00:00
print("Room1 -> Room2:")
2022-06-13 17:33:08 +00:00
for i in results1:
2022-06-13 21:34:16 +00:00
if i[2] == 0:
2022-06-13 21:36:08 +00:00
print(i[0] + " : Not recieved")
2022-06-13 21:34:16 +00:00
else:
print(i[0].rstrip() + " : " + str(round(i[2]-i[1],2)) + " secounds")
2022-06-13 23:11:35 +00:00
print("Room2 -> Room1:")
2022-06-13 17:33:08 +00:00
for i in results2:
2022-06-13 21:34:16 +00:00
if i[2] == 0:
2022-06-13 21:36:08 +00:00
print(i[0] + " : Not recieved")
2022-06-13 21:34:16 +00:00
else:
print(i[0].rstrip() + " : " + str(round(i[2]-i[1],2)) + " secounds")
2022-06-13 17:33:08 +00:00
exit()
def show_version():
print("XMPP test bot " + VERSION)
print("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>")
print("This is free software: you are free to change and redistribute it.")
print("There is NO WARRANTY, to the extent permitted by law.")
class MUCBot(slixmpp.ClientXMPP):
def __init__(self, jid, password, nick, room1, room2):
slixmpp.ClientXMPP.__init__(self, jid, password)
self.nick = nick
self.room1 = room1
self.room2 = room2
self.add_event_handler("session_start", self.start)
self.add_event_handler("groupchat_message", self.muc_message)
async def start(self, event):
await self.get_roster()
self.send_presence()
self.plugin['xep_0045'].join_muc(self.room1,self.nick)
self.plugin['xep_0045'].join_muc(self.room2,self.nick)
# Send test messages to both rooms
for i in results1:
time.sleep(2)
self.send_message(mto=self.room1,mbody="%s" % i[0],mtype='groupchat')
i[1]=time.time()
time.sleep(5)
for i in results2:
time.sleep(2)
self.send_message(mto=self.room2,mbody="%s" % i[0],mtype='groupchat')
i[1]=time.time()
# When message is recieved find the word in results and record time
def muc_message(self, msg):
rtime=time.time()
2022-06-14 13:00:51 +00:00
if msg['mucnick'] == self.nick:
return
2022-06-13 17:33:08 +00:00
body="%(body)s" % msg
2022-06-14 13:00:51 +00:00
unknownmsg = True
2022-06-13 17:33:08 +00:00
if self.room1 in str(msg['from']):
for i in results2:
2022-06-14 14:39:19 +00:00
if "admin: " + i[0] == body:
2022-06-13 17:33:08 +00:00
i[2] = rtime
2022-06-14 13:00:51 +00:00
unknownmsg = False
2022-06-13 17:33:08 +00:00
if self.room2 in str(msg['from']):
for i in results1:
2022-06-14 14:39:19 +00:00
if "admin: " + i[0] == body:
2022-06-13 17:33:08 +00:00
i[2] = rtime
2022-06-14 13:00:51 +00:00
unknownmsg = False
if unknownmsg:
print("Unknown message recieved: " + body)
2022-06-13 17:33:08 +00:00
2022-06-13 21:34:16 +00:00
if allmsgsrecv() is True:
2022-06-13 17:33:08 +00:00
printandexit()
if __name__ == '__main__':
argparser = argparse.ArgumentParser(description="XMPP bot that mirrors chat between two MUC rooms")
argparser.add_argument("--version", dest="version", help="print version information and exit", action="store_true")
args=argparser.parse_args()
## Version argument
if args.version:
show_version()
exit()
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
JID = config.get('credentials', 'JID')
PASSWORD = config.get('credentials', 'PASSWORD')
NICK = config.get('credentials', 'NICK')
ROOM1 = config.get('credentials', 'ROOM1')
ROOM2 = config.get('credentials', 'ROOM2')
2022-06-13 23:11:35 +00:00
HOST = None
PORT = 5222
if "host" in config.options('credentials'):
HOST = config.get('credentials', 'HOST')
if "port" in config.options('credentials'):
PORT = config.get('credentials', 'PORT')
2022-06-13 17:33:08 +00:00
xmpp = MUCBot(JID, PASSWORD, NICK, ROOM1, ROOM2)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping
# Connect to the XMPP server and start processing XMPP stanzas.
2022-06-13 23:11:35 +00:00
if HOST != None:
xmpp.connect(address=(HOST,int(PORT)))
else:
xmpp.connect()
2022-06-13 17:33:08 +00:00
xmpp.process(forever=False)