Kaynağa Gözat

ffpb.msg_handler: handle incomplete config and shutdown properly

Helge Jung 10 yıl önce
ebeveyn
işleme
535adb7c2b
1 değiştirilmiş dosya ile 26 ekleme ve 13 silme
  1. 26 13
      modules/ffpb.py

+ 26 - 13
modules/ffpb.py

@@ -11,6 +11,8 @@ import socket
 import SocketServer
 import threading
 
+msgserver = None
+
 # TODO: move into config file :)
 msgserver_known_senders = {
 	"127.0.0.1": "localhost",
@@ -42,19 +44,30 @@ class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
 	pass
 
 def setup(bot):
-	host = "localhost"
-	port = 4342
-	if not bot.config.ffpb.msg_host is None: host = bot.config.ffpb.msg_host
-	if not bot.config.ffpb.msg_port is None: port = int(bot.config.ffpb.msg_port)
-
-	msgserver = ThreadingTCPServer((host,port), MsgHandler)
-	msgserver.bot = bot
-	ip, port = msgserver.server_address
-	print("Messaging server listening on {}:{}".format(ip,port))
-
-	msgserver_thread = threading.Thread(target=msgserver.serve_forever)
-	msgserver_thread.daemon = True
-	msgserver_thread.start()
+	global msgserver
+
+	if bot.config.has_section('ffpb') and bot.config.ffpb.msg_enable:
+		host = "localhost"
+		port = 4342
+		if not bot.config.ffpb.msg_host is None: host = bot.config.ffpb.msg_host
+		if not bot.config.ffpb.msg_port is None: port = int(bot.config.ffpb.msg_port)
+
+		msgserver = ThreadingTCPServer((host,port), MsgHandler)
+		msgserver.bot = bot
+		ip, port = msgserver.server_address
+		print("Messaging server listening on {}:{}".format(ip,port))
+
+		msgserver_thread = threading.Thread(target=msgserver.serve_forever)
+		msgserver_thread.daemon = True
+		msgserver_thread.start()
+
+def shutdown(bot):
+	global msgserver
+
+	if not msgserver is None:
+		msgserver.shutdown()
+		print("Closed messaging server.")
+		msgserver = None
 
 @willie.module.commands('status')
 def ffpb_status(bot, trigger):