101 lines
2.8 KiB
Python
101 lines
2.8 KiB
Python
|
#!/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
|
||
|
|
||
|
|
||
|
# Logging
|
||
|
log = logging.getLogger(__name__)
|
||
|
log.addHandler(JournalHandler())
|
||
|
log.setLevel(logging.INFO)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
|
||
|
def cleanExit():
|
||
|
global localservthread
|
||
|
localservthread.pleaseStop = True
|
||
|
# stop tcp server
|
||
|
sockfd = socket.socket()
|
||
|
port = 12346
|
||
|
sockfd.connect(('127.0.0.1', port))
|
||
|
sockfd.send(b'')
|
||
|
sockfd.close()
|
||
|
|
||
|
exit(0)
|
||
|
|
||
|
|
||
|
def signalHandler(sig, frame):
|
||
|
log.info("Exiting...")
|
||
|
cleanExit()
|
||
|
|
||
|
# 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")
|
||
|
parser.add_argument("-r", "--room", dest="room",
|
||
|
help="MUC room to join")
|
||
|
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')
|
||
|
|
||
|
# Setup the MUCBot and register plugins. Note that while plugins may
|
||
|
# have interdependencies, the order in which you register them does
|
||
|
# not matter.
|
||
|
xmpp = MUCBot(args.jid, args.password, args.room, args.nick)
|
||
|
xmpp.register_plugin('xep_0030') # Service Discovery
|
||
|
xmpp.register_plugin('xep_0045') # Multi-User Chat
|
||
|
xmpp.register_plugin('xep_0199') # XMPP Ping
|
||
|
|
||
|
# Create buffer
|
||
|
store = DataStore(xmpp)
|
||
|
xmpp.datastore = store
|
||
|
|
||
|
localservthread = LocalServer(store)
|
||
|
localservthread.start()
|
||
|
|
||
|
while True:
|
||
|
time.sleep(1)
|
||
|
|
||
|
# Connect to the XMPP server and start processing XMPP stanzas.
|
||
|
xmpp.connect()
|
||
|
|
||
|
# Check local server status
|
||
|
if not localservthread.is_alive():
|
||
|
exit(1)
|
||
|
|
||
|
try:
|
||
|
xmpp.process()
|
||
|
except KeyboardInterrupt:
|
||
|
cleanExit()
|