Procházet zdrojové kódy

darkirc: [bots] gracefully close connections

dasman před 1 rokem
rodič
revize
54a62b7f00

+ 9 - 0
bin/darkirc/script/bots/irc.py

@@ -26,6 +26,15 @@ class IRC:
         # join the channel
         for chan in channels:
             self.irc.send(bytes("JOIN " + chan + "\n", "UTF-8"))
+
+    def disconnect(self, server, port):
+        # Disonnect from the server and gracefully shutdown and close socket
+        print("Disonnecting from: " + server + ":" + str(port))
+        # Send QUIT command to IRC server
+        self.irc.send(bytes("QUIT\n", "UTF-8"))
+        self.irc.shutdown(socket.SHUT_RDWR)
+        self.irc.close()
+        
  
     def get_response(self):
         # Get the response

+ 6 - 0
bin/darkirc/script/bots/meetbot/meetbot.py

@@ -266,6 +266,12 @@ async def main(debug=False):
     except ConnectionRefusedError:
         logging.error("%s/%d: Connection refused", host, port)
         return
+    finally:
+        reply = "QUIT\r\n"
+        writer.write(reply.encode("utf-8"))
+        await writer.drain()
+        writer.close()
+        await writer.wait_closed()
 
 
 if __name__ == "__main__":

+ 11 - 0
bin/darkirc/script/bots/mirror-bot.py

@@ -1,5 +1,6 @@
 # -*- coding: utf-8 -*-
 
+import signal
 import irc
 import argparse
 
@@ -34,6 +35,16 @@ ircd_channels = args.channels
 ircd = irc.IRC()
 ircd.connect(ircd_server, ircd_port, ircd_channels, botnick)
 
+def signal_handler(sig, frame):
+    print("Caught termination signal, cleaning up and exiting...")
+    ircd.disconnect(args.server_to, args.port_to)
+    darkirc.disconnect(args.server_from, args.port_from)
+    print("Shut down successfully")
+    exit(0)
+
+signal.signal(signal.SIGINT, signal_handler)
+
+
 while True:
     darkirc_text = darkirc.get_response()
     if not len(darkirc_text.strip()) > 0:

+ 9 - 0
bin/darkirc/script/bots/taubot.py

@@ -1,4 +1,5 @@
 import argparse
+import signal
 import irc
 import json
 import sys
@@ -21,6 +22,14 @@ channels = [args.channel, args.alt_chan] if args.alt_chan is not None else args.
 ircc = irc.IRC()
 ircc.connect(args.server, int(args.port), channels, args.nickname)
 
+def signal_handler(sig, frame):
+    print("Caught termination signal, cleaning up and exiting...")
+    ircc.disconnect(args.server, args.port)
+    print("Shut down successfully")
+    exit(0)
+
+signal.signal(signal.SIGINT, signal_handler)
+
 while True:
     with open(args.pipe) as handle:
         while True:

+ 9 - 1
bin/darkirc/script/bots/test-bot.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import irc
+import signal
 
 ## IRC Config
 server = "127.0.0.1"
@@ -10,6 +11,14 @@ botnick = "testbot"
 ircc = irc.IRC()
 ircc.connect(server, port, channels, botnick)
 
+def signal_handler(sig, frame):
+    print("Caught termination signal, cleaning up and exiting...")
+    ircc.disconnect(server, port)
+    print("Shut down successfully")
+    exit(0)
+
+signal.signal(signal.SIGINT, signal_handler)
+
 while True:
     text = ircc.get_response().strip()
     if not len(text) > 0:
@@ -23,4 +32,3 @@ while True:
         bot_msg = text.split(':')[-1].strip()
         if bot_msg.lower() == "test" or bot_msg.lower() == "echo":
             ircc.send(channel, f"{bot_msg} back")
-            continue

+ 10 - 1
bin/darkirc/script/bots/titlebot.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import re
+import signal
 import irc
 import requests
 from bs4 import BeautifulSoup
@@ -8,12 +9,20 @@ from urllib.parse import urlparse
 
 ## IRC Config
 server = "127.0.0.1"
-port = 11070
+port = 22025
 channels = ["#test", "#test1"]
 botnick = "website-title"
 ircc = irc.IRC()
 ircc.connect(server, port, channels, botnick)
 
+def signal_handler(sig, frame):
+    print("Caught termination signal, cleaning up and exiting...")
+    ircc.disconnect(server, port)
+    print("Shut down successfully")
+    exit(0)
+
+signal.signal(signal.SIGINT, signal_handler)
+
 while True:
     text = ircc.get_response()
     if not len(text) > 0:

+ 9 - 0
bin/darkirc/script/bots/tweetifier.py

@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import re
+import signal
 import irc
 from tweety import Twitter
 from urllib.parse import urlparse
@@ -13,6 +14,14 @@ botnick = "tweetifier"
 ircc = irc.IRC()
 ircc.connect(server, port, channels, botnick)
 
+def signal_handler(sig, frame):
+    print("Caught termination signal, cleaning up and exiting...")
+    ircc.disconnect(server, port)
+    print("Shut down successfully")
+    exit(0)
+
+signal.signal(signal.SIGINT, signal_handler)
+
 while True:
     text = ircc.get_response().strip()
     if not len(text) > 0: