From 54a62b7f006aaac9539be913d9230b7008f911b1 Mon Sep 17 00:00:00 2001 From: dasman Date: Mon, 21 Oct 2024 17:30:44 +0300 Subject: [PATCH] darkirc: [bots] gracefully close connections --- bin/darkirc/script/bots/irc.py | 9 +++++++++ bin/darkirc/script/bots/meetbot/meetbot.py | 6 ++++++ bin/darkirc/script/bots/mirror-bot.py | 11 +++++++++++ bin/darkirc/script/bots/taubot.py | 9 +++++++++ bin/darkirc/script/bots/test-bot.py | 10 +++++++++- bin/darkirc/script/bots/titlebot.py | 11 ++++++++++- bin/darkirc/script/bots/tweetifier.py | 9 +++++++++ 7 files changed, 63 insertions(+), 2 deletions(-) diff --git a/bin/darkirc/script/bots/irc.py b/bin/darkirc/script/bots/irc.py index 901851c2f1f5..91b4a4261f3b 100644 --- a/bin/darkirc/script/bots/irc.py +++ b/bin/darkirc/script/bots/irc.py @@ -26,6 +26,15 @@ def connect(self, server, port, channels, botnick): # 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 diff --git a/bin/darkirc/script/bots/meetbot/meetbot.py b/bin/darkirc/script/bots/meetbot/meetbot.py index c414e31928c2..ef65ef1cc731 100755 --- a/bin/darkirc/script/bots/meetbot/meetbot.py +++ b/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__": diff --git a/bin/darkirc/script/bots/mirror-bot.py b/bin/darkirc/script/bots/mirror-bot.py index ee6e7861ac6a..3c7679588aa1 100644 --- a/bin/darkirc/script/bots/mirror-bot.py +++ b/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 = 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: diff --git a/bin/darkirc/script/bots/taubot.py b/bin/darkirc/script/bots/taubot.py index 99a2573d0496..424e3d45aa78 100644 --- a/bin/darkirc/script/bots/taubot.py +++ b/bin/darkirc/script/bots/taubot.py @@ -1,4 +1,5 @@ import argparse +import signal import irc import json import sys @@ -21,6 +22,14 @@ 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: diff --git a/bin/darkirc/script/bots/test-bot.py b/bin/darkirc/script/bots/test-bot.py index 19de5729535d..21484fabd935 100644 --- a/bin/darkirc/script/bots/test-bot.py +++ b/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 @@ 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 @@ bot_msg = text.split(':')[-1].strip() if bot_msg.lower() == "test" or bot_msg.lower() == "echo": ircc.send(channel, f"{bot_msg} back") - continue diff --git a/bin/darkirc/script/bots/titlebot.py b/bin/darkirc/script/bots/titlebot.py index 8b8e47c73ae9..896a48f462bf 100644 --- a/bin/darkirc/script/bots/titlebot.py +++ b/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 @@ ## 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: diff --git a/bin/darkirc/script/bots/tweetifier.py b/bin/darkirc/script/bots/tweetifier.py index b12f3a88f045..88b14c8b31f0 100644 --- a/bin/darkirc/script/bots/tweetifier.py +++ b/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 @@ 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: