Skip to content

Commit

Permalink
darkirc: [bots] gracefully close connections
Browse files Browse the repository at this point in the history
  • Loading branch information
dasman committed Oct 21, 2024
1 parent 2914aff commit 54a62b7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
9 changes: 9 additions & 0 deletions bin/darkirc/script/bots/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions bin/darkirc/script/bots/meetbot/meetbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
11 changes: 11 additions & 0 deletions bin/darkirc/script/bots/mirror-bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import signal
import irc
import argparse

Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions bin/darkirc/script/bots/taubot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import signal
import irc
import json
import sys
Expand All @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion bin/darkirc/script/bots/test-bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import irc
import signal

## IRC Config
server = "127.0.0.1"
Expand All @@ -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:
Expand All @@ -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
11 changes: 10 additions & 1 deletion bin/darkirc/script/bots/titlebot.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
# -*- coding: utf-8 -*-

import re
import signal
import irc
import requests
from bs4 import BeautifulSoup
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:
Expand Down
9 changes: 9 additions & 0 deletions bin/darkirc/script/bots/tweetifier.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import re
import signal
import irc
from tweety import Twitter
from urllib.parse import urlparse
Expand All @@ -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:
Expand Down

0 comments on commit 54a62b7

Please sign in to comment.