Compare commits

...

3 Commits

Author SHA1 Message Date
Juhani Krekelä 4ee71a6d4a Remove obsoleted startcmd.txt feature 2021-05-02 00:43:15 +03:00
Juhani Krekelä 187b08d3b5 Update o3-base to add built-in authentication support 2021-05-02 00:41:31 +03:00
Juhani Krekelä f6fe53385d Add built-in authentication to o3-base 2021-05-02 00:40:22 +03:00
4 changed files with 27 additions and 16 deletions

2
README
View File

@ -13,8 +13,6 @@ trusted.txt: People that have access to #op and friends. Format is
sensitive.
gods.txt: Same as above, except can't be #untrust'ed
msgs.txt: Messages that haven't been delivered. Leave it empty
startcmd.txt: Raw IRC messages to send to the server after USER and NICK. Used
to authenticate with NickServ
bot.conf: See bot.conf.example
Run `python3 ircbot.py`

View File

@ -5,3 +5,7 @@ nick = oonbotti2
username = oonbotti2
realname = oonbotti2
channels = ##ingsoc
[auth]
user = oonbotti2
password = nicetryofficer

View File

@ -13,10 +13,7 @@ def initialize(*, config):
# Blocks the bot until it's done, including PING/PONG handling
# irc is the IRC API object
def on_connect(*, irc):
with open('startcmd.txt', 'r') as f:
for i in f:
while i[-1:] == '\n': i = i[:-1]
irc.send_raw(i.encode())
pass
# on_quit(*, irc)
# Called just before IRC bot sends QUIT

View File

@ -142,11 +142,12 @@ class API:
self.serverthread_object.logging_channel.send((logmessage_types.internal, internal_submessage_types.error, message))
# ServerThread(server, control_channel, cron_control_channel, logging_channel)
# ServerThread(server, auth, control_channel, cron_control_channel, logging_channel)
# Creates a new server main loop thread
class ServerThread(threading.Thread):
def __init__(self, server, control_channel, cron_control_channel, logging_channel):
def __init__(self, server, auth, control_channel, cron_control_channel, logging_channel):
self.server = server
self.auth = auth
self.control_channel = control_channel
self.cron_control_channel = cron_control_channel
self.logging_channel = logging_channel
@ -330,10 +331,15 @@ class ServerThread(threading.Thread):
try:
# Run initialization
self.send_line_raw(b'USER %s a a :%s' % (self.server.username.encode('utf-8'), self.server.realname.encode('utf-8')))
# Set up nick
# Use server-password based authentication if it's set up
user, password = self.auth
if user is not None:
self.send_line_raw(b'PASS %s:%s' % (user.encode(), password.encode()))
# Set up nick and username
self.api.nick(self.server.nick.encode('utf-8'))
self.send_line_raw(b'USER %s a a :%s' % (self.server.username.encode('utf-8'), self.server.realname.encode('utf-8')))
# Run the on_connect hook, to allow further setup
botcmd.on_connect(irc = self.api)
@ -377,12 +383,12 @@ class ServerThread(threading.Thread):
# Tell cron we're quiting
cron.quit(cron_control_channel)
# spawn_serverthread(server, cron_control_channel, logging_channel) → control_channel
# spawn_serverthread(server, auth, cron_control_channel, logging_channel) → control_channel
# Creates a ServerThread for given server and returns the channel for controlling it
def spawn_serverthread(server, cron_control_channel, logging_channel):
def spawn_serverthread(server, auth, cron_control_channel, logging_channel):
thread_control_socket, spawner_control_socket = socket.socketpair()
control_channel = channel.Channel()
ServerThread(server, control_channel, cron_control_channel, logging_channel).start()
ServerThread(server, auth, control_channel, cron_control_channel, logging_channel).start()
return control_channel
# spawn_loggerthread() → logging_channel, dead_notify_channel
@ -406,18 +412,24 @@ def read_config():
realname = config['server']['realname']
channels = config['server']['channels'].split()
user = None
password = None
if 'auth' in config:
user = config['auth']['user']
password = config['auth']['password']
server = Server(host = host, port = port, nick = nick, username = username, realname = realname, channels = channels)
return config, server
return config, server, (user, password)
if __name__ == '__main__':
config, server = read_config()
config, server, auth = read_config()
botcmd.initialize(config = config)
cron_control_channel = cron.start()
logging_channel, dead_notify_channel = spawn_loggerthread()
control_channel = spawn_serverthread(server, cron_control_channel, logging_channel)
control_channel = spawn_serverthread(server, auth, cron_control_channel, logging_channel)
while True:
message = dead_notify_channel.recv(blocking = False)