Add built-in authentication to o3-base
This commit is contained in:
parent
635cab7440
commit
f6fe53385d
1 changed files with 22 additions and 10 deletions
32
ircbot.py
32
ircbot.py
|
@ -142,11 +142,12 @@ class API:
|
||||||
self.serverthread_object.logging_channel.send((logmessage_types.internal, internal_submessage_types.error, message))
|
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
|
# Creates a new server main loop thread
|
||||||
class ServerThread(threading.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.server = server
|
||||||
|
self.auth = auth
|
||||||
self.control_channel = control_channel
|
self.control_channel = control_channel
|
||||||
self.cron_control_channel = cron_control_channel
|
self.cron_control_channel = cron_control_channel
|
||||||
self.logging_channel = logging_channel
|
self.logging_channel = logging_channel
|
||||||
|
@ -330,10 +331,15 @@ class ServerThread(threading.Thread):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Run initialization
|
# 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.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
|
# Run the on_connect hook, to allow further setup
|
||||||
botcmd.on_connect(irc = self.api)
|
botcmd.on_connect(irc = self.api)
|
||||||
|
@ -377,12 +383,12 @@ class ServerThread(threading.Thread):
|
||||||
# Tell cron we're quiting
|
# Tell cron we're quiting
|
||||||
cron.quit(cron_control_channel)
|
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
|
# 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()
|
thread_control_socket, spawner_control_socket = socket.socketpair()
|
||||||
control_channel = channel.Channel()
|
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
|
return control_channel
|
||||||
|
|
||||||
# spawn_loggerthread() → logging_channel, dead_notify_channel
|
# spawn_loggerthread() → logging_channel, dead_notify_channel
|
||||||
|
@ -406,18 +412,24 @@ def read_config():
|
||||||
realname = config['server']['realname']
|
realname = config['server']['realname']
|
||||||
channels = config['server']['channels'].split()
|
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)
|
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__':
|
if __name__ == '__main__':
|
||||||
config, server = read_config()
|
config, server, auth = read_config()
|
||||||
|
|
||||||
botcmd.initialize(config = config)
|
botcmd.initialize(config = config)
|
||||||
|
|
||||||
cron_control_channel = cron.start()
|
cron_control_channel = cron.start()
|
||||||
logging_channel, dead_notify_channel = spawn_loggerthread()
|
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:
|
while True:
|
||||||
message = dead_notify_channel.recv(blocking = False)
|
message = dead_notify_channel.recv(blocking = False)
|
||||||
|
|
Loading…
Reference in a new issue