From f4077cdd3e2ad5b209d391cc842256c6adc78994 Mon Sep 17 00:00:00 2001 From: Juhani Haverinen Date: Tue, 5 Sep 2017 13:45:28 +0300 Subject: [PATCH] Make bot autojoin channel and move nick and username to server config --- ircbot.py | 21 ++++++++++++++++----- line_handling.py | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ircbot.py b/ircbot.py index 8b64a34..62a0ea0 100644 --- a/ircbot.py +++ b/ircbot.py @@ -9,7 +9,7 @@ from constants import logmessage_types, internal_submessage_types, controlmessag import line_handling -Server = namedtuple('Server', ['host', 'port']) +Server = namedtuple('Server', ['host', 'port', 'nick', 'realname', 'channels']) # ServerThread(server, control_socket) # Creates a new server main loop thread @@ -21,6 +21,12 @@ class ServerThread(threading.Thread): self.server_socket_write_lock = threading.Lock() + self.nick = None + self.nick_lock = threading.Lock() + + self.channels = set() + self.channels_lock = threading.Lock() + threading.Thread.__init__(self) def send_line_raw(self, line): @@ -105,9 +111,13 @@ class ServerThread(threading.Thread): self.api = line_handling.API(self) # Run initialization - # TODO: read nick/username/etc. from a config - self.send_line_raw(b'NICK HynneFlip') - self.send_line_raw(b'USER HynneFlip a a :HynneFlip IRC bot') + self.send_line_raw(b'USER HynneFlip a a :' + self.server.realname.encode('utf-8')) + + # Set up nick and channels + self.api.nick(self.server.nick.encode('utf-8')) + + for channel in self.server.channels: + self.api.join(channel.encode('utf-8')) # Run mainloop self.mainloop() @@ -129,7 +139,8 @@ def spawn_serverthread(server): return (control_channel, logging_channel) if __name__ == '__main__': - control_channel, logging_channel = spawn_serverthread(Server('irc.freenode.net', 6667)) + server = Server(host = 'irc.freenode.net', port = 6667, nick = 'HynneFlip', realname = 'HynneFlip IRC bot', channels = ['##ingsoc']) + control_channel, logging_channel = spawn_serverthread(server) while True: cmd = input(': ') diff --git a/line_handling.py b/line_handling.py index 63b98e1..b504c93 100644 --- a/line_handling.py +++ b/line_handling.py @@ -9,14 +9,28 @@ class API: # We need to access the internal functions of the ServerThread object in order to send lines etc. self.serverthread_object = serverthread_object - def send(self, line): + def send_raw(self, line): self.serverthread_object.send_line_raw(line) def msg(self, recipient, message): """Make sending PRIVMSGs much nicer""" - line = 'PRIVMSG ' + recipient + ' :' + message + line = b'PRIVMSG ' + recipient + b' :' + message self.serverthread_object.send_line_raw(line) + def nick(self, nick): + # Send a NICK command and update the internal nick tracking state + with self.serverthread_object.nick_lock: + line = b'NICK ' + nick + self.serverthread_object.send_line_raw(line) + self.serverthread_object.nick = nick + + def join(self, channel): + # Send a JOIN command and update the internal channel tracking state + with self.serverthread_object.channels_lock: + line = b'JOIN ' + channel + self.serverthread_object.send_line_raw(line) + self.serverthread_object.channels.add(channel) + def error(self, message): self.serverthread_object.logging_channel.send((constants.logmessage_types.internal, constants.internal_submessage_types.error, message))