소스 검색

ffpb.msg_handler: handle incomplete config and shutdown properly

Helge Jung 10 년 전
부모
커밋
535adb7c2b
1개의 변경된 파일26개의 추가작업 그리고 13개의 파일을 삭제
  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):