Compare commits
No commits in common. "d755518b3fa92574bff8d1f30bf8f2af288a358c" and "cc71019578a7444a2171e014dc8442e3ebda8632" have entirely different histories.
d755518b3f
...
cc71019578
3 changed files with 10 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
|||
[server]
|
||||
host = irc.libera.chat
|
||||
host = irc.freenode.net
|
||||
port = 6667
|
||||
nick = oonbotti2
|
||||
username = oonbotti2
|
||||
|
|
|
@ -4,7 +4,7 @@ class logmessage_types(enum.Enum):
|
|||
sent, received, internal, status = range(4)
|
||||
|
||||
class internal_submessage_types(enum.Enum):
|
||||
quit, error, server = range(3)
|
||||
quit, error = range(2)
|
||||
|
||||
class controlmessage_types(enum.Enum):
|
||||
quit, reconnect, send_line, ping, ping_timeout = range(5)
|
||||
|
|
26
ircbot.py
26
ircbot.py
|
@ -2,7 +2,6 @@
|
|||
import configparser
|
||||
import select
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from collections import namedtuple
|
||||
|
@ -17,8 +16,7 @@ import line_handling
|
|||
Server = namedtuple('Server', ['host', 'port', 'nick', 'username', 'realname', 'channels'])
|
||||
|
||||
class LoggerThread(threading.Thread):
|
||||
def __init__(self, interactive_console, logging_channel, dead_notify_channel):
|
||||
self.interactive_console = interactive_console
|
||||
def __init__(self, logging_channel, dead_notify_channel):
|
||||
self.logging_channel = logging_channel
|
||||
self.dead_notify_channel = dead_notify_channel
|
||||
|
||||
|
@ -31,11 +29,11 @@ class LoggerThread(threading.Thread):
|
|||
# Lines that were sent between server and client
|
||||
if message_type == logmessage_types.sent:
|
||||
assert len(message_data) == 1
|
||||
if self.interactive_console: print('>' + message_data[0])
|
||||
print('>' + message_data[0])
|
||||
|
||||
elif message_type == logmessage_types.received:
|
||||
assert len(message_data) == 1
|
||||
if self.interactive_console: print('<' + message_data[0])
|
||||
print('<' + message_data[0])
|
||||
|
||||
# Messages that are from internal components
|
||||
elif message_type == logmessage_types.internal:
|
||||
|
@ -50,11 +48,6 @@ class LoggerThread(threading.Thread):
|
|||
assert len(message_data) == 2
|
||||
print('--- Error', message_data[1])
|
||||
|
||||
elif message_data[0] == internal_submessage_types.server:
|
||||
assert len(message_data) == 2
|
||||
assert len(message_data[1]) == 2
|
||||
print(f'--- Connecting to server {message_data[1][0]}:{message_data[1][1]}')
|
||||
|
||||
else:
|
||||
print('--- ???', message_data)
|
||||
|
||||
|
@ -300,7 +293,6 @@ class ServerThread(threading.Thread):
|
|||
while True:
|
||||
# Connect to given server
|
||||
address = (self.server.host, self.server.port)
|
||||
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.server, address))
|
||||
try:
|
||||
self.server_socket = socket.create_connection(address)
|
||||
except (ConnectionRefusedError, socket.gaierror):
|
||||
|
@ -399,12 +391,12 @@ def spawn_serverthread(server, auth, cron_control_channel, logging_channel):
|
|||
ServerThread(server, auth, control_channel, cron_control_channel, logging_channel).start()
|
||||
return control_channel
|
||||
|
||||
# spawn_loggerthread(interactive_console) → logging_channel, dead_notify_channel
|
||||
# spawn_loggerthread() → logging_channel, dead_notify_channel
|
||||
# Spawn logger thread and returns the channel it logs and the channel it uses to notify about quiting
|
||||
def spawn_loggerthread(interactive_console):
|
||||
def spawn_loggerthread():
|
||||
logging_channel = channel.Channel()
|
||||
dead_notify_channel = channel.Channel()
|
||||
LoggerThread(interactive_console, logging_channel, dead_notify_channel).start()
|
||||
LoggerThread(logging_channel, dead_notify_channel).start()
|
||||
return logging_channel, dead_notify_channel
|
||||
|
||||
# read_config() → config, server
|
||||
|
@ -431,17 +423,15 @@ def read_config():
|
|||
return config, server, (user, password)
|
||||
|
||||
if __name__ == '__main__':
|
||||
interactive_console = sys.stdin.isatty()
|
||||
|
||||
config, server, auth = read_config()
|
||||
|
||||
botcmd.initialize(config = config)
|
||||
|
||||
cron_control_channel = cron.start()
|
||||
logging_channel, dead_notify_channel = spawn_loggerthread(interactive_console)
|
||||
logging_channel, dead_notify_channel = spawn_loggerthread()
|
||||
control_channel = spawn_serverthread(server, auth, cron_control_channel, logging_channel)
|
||||
|
||||
while interactive_console:
|
||||
while True:
|
||||
message = dead_notify_channel.recv(blocking = False)
|
||||
if message is not None:
|
||||
if message[0] == controlmessage_types.quit:
|
||||
|
|
Loading…
Reference in a new issue