isengard-bot/main.py

130 lines
3.6 KiB
Python
Raw Normal View History

2022-02-08 15:46:34 +01:00
#!/usr/bin/env python3
from systemd.journal import JournalHandler
from getpass import getpass
from argparse import ArgumentParser
import random
import threading
import socket
import time
import signal
import logging
from bot import MUCBot
from data import DataStore
from localServer import LocalServer
def cleanExit():
global localservthread
localservthread.pleaseStop = True
time.sleep(1)
# stop tcp server
sockfd = socket.socket()
port = 12346
sockfd.connect(('127.0.0.1', port))
sockfd.send(b'')
sockfd.close()
time.sleep(1)
2022-02-08 15:46:34 +01:00
exit(0)
2022-02-08 15:46:34 +01:00
def signalHandler(sig, frame):
log.info("Exiting...")
cleanExit()
2022-02-08 15:46:34 +01:00
if __name__ == '__main__':
2022-02-08 15:46:34 +01:00
# Register SIGINT signal handler
signal.signal(signal.SIGINT, signalHandler)
# Setup the command line arguments.
parser = ArgumentParser()
# Output verbosity options.
parser.add_argument("-q", "--quiet", help="set logging to ERROR",
action="store_const", dest="loglevel",
const=logging.ERROR, default=logging.INFO)
parser.add_argument("-d", "--debug", help="set logging to DEBUG",
action="store_const", dest="loglevel",
const=logging.DEBUG, default=logging.INFO)
# JID and password options.
parser.add_argument("-j", "--jid", dest="jid",
help="JID to use")
parser.add_argument("-p", "--password", dest="password",
help="password to use")
2022-02-08 20:25:51 +01:00
parser.add_argument("-r", "--rooms", dest="rooms",
help="MUC rooms to join separated by comma")
2022-02-08 15:46:34 +01:00
parser.add_argument("-n", "--nick", dest="nick",
help="MUC nickname")
args = parser.parse_args()
# Setup logging.
logging.basicConfig(level=args.loglevel,
format='%(levelname)-8s %(message)s')
2022-02-08 20:25:51 +01:00
log = logging.getLogger(__name__)
log.addHandler(JournalHandler())
2022-02-08 15:46:34 +01:00
# Setup the MUCBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
2022-02-08 20:25:51 +01:00
jid = args.jid
password = args.password
rooms = args.rooms
nick = args.nick
if not jid:
jid = "isengard@a-lec.org"
if not password:
2022-02-09 10:47:27 +01:00
password = getpass()
2022-02-08 20:25:51 +01:00
if not rooms:
rooms = "tests-isengard@salons.a-lec.org"
if not nick:
nick = "Isengard"
2022-02-16 12:43:22 +01:00
ERRORS = 0
2023-03-15 19:41:29 +01:00
datastore = None
2022-02-16 12:43:22 +01:00
localservthread = None
2022-02-08 15:46:34 +01:00
2022-02-16 12:43:22 +01:00
while True:
2022-02-08 15:46:34 +01:00
2022-02-16 12:43:22 +01:00
xmpp = MUCBot(jid, password, rooms.split(","), nick)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0045') # Multi-User Chat
xmpp.register_plugin('xep_0199') # XMPP Ping
2022-02-08 15:46:34 +01:00
2022-02-16 12:43:22 +01:00
# Create buffer
2023-03-15 19:41:29 +01:00
if not datastore:
datastore = DataStore(xmpp)
xmpp.datastore = datastore
2022-02-16 12:43:22 +01:00
# Launch local server
if not localservthread:
2023-03-15 19:41:29 +01:00
localservthread = LocalServer(datastore)
2022-02-16 12:43:22 +01:00
localservthread.start()
time.sleep(1)
2022-02-08 15:46:34 +01:00
# Connect to the XMPP server and start processing XMPP stanzas.
xmpp.connect()
# Check local server status
if not localservthread.is_alive():
exit(1)
try:
log.info("Launching XMPP connection until disconnection (ERRORS: %s)" % ERRORS)
xmpp.loop.run_until_complete(xmpp.disconnected)
2022-02-08 15:46:34 +01:00
except KeyboardInterrupt:
cleanExit()
except Exception as e:
2022-02-16 12:43:22 +01:00
log.error("ERROR IN MAIN : " + str(e))
if ERRORS >= 3:
log.critical("3 consecutive errors. Aborting...")
cleanExit()
ERRORS += 1
2022-02-16 12:43:22 +01:00
del xmpp