1
0
Fork 0

Read config from a configuration file

This commit is contained in:
Juhani Krekelä 2018-01-03 18:08:24 +02:00
parent 029083b661
commit 002f1eecf8
5 changed files with 43 additions and 9 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
__pycache__ __pycache__
*.pyc *.pyc
*.swp *.swp
bot.conf

View File

@ -4,6 +4,12 @@ This project seeks to replace o2-base as an easy python base to build IRC bots
on. It is not related to the failed oonbotti3 project, but is rather based on on. It is not related to the failed oonbotti3 project, but is rather based on
the IRC bot framework of HynneFlip. the IRC bot framework of HynneFlip.
Setup
=====
Copy `bot.conf.example` to `bot.conf` and run `python3 ircbot.py` to bring up
the bot skeleton, which can be used as a very quick and dirty IRC client. User
code goes in `botcmd.py`.
License License
------- -------
Everything in this repo is under UNLICENSE / CC0. Everything in this repo is under UNLICENSE / CC0.

7
bot.conf.example Normal file
View File

@ -0,0 +1,7 @@
[server]
host = irc.freenode.net
port = 6667
nick = o3-base
username = o3-base
realname = IRC bot based on o3-base
channels = ##ingsoc

View File

@ -1,12 +1,14 @@
# initialize() # initialize(*, config)
# Called to initialize the IRC bot # Called to initialize the IRC bot
# Runs before even logger is brought up, and blocks further bringup until it's done # Runs before even logger is brought up, and blocks further bringup until it's done
def initialize(): # config is a configpatser.ConfigParser object containig contents of bot.conf
def initialize(*, config):
... ...
# on_connect(*, irc) # on_connect(*, irc)
# Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else # Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else
# Blocks the bot until it's done, including PING/PONG handling # Blocks the bot until it's done, including PING/PONG handling
# irc is the IRC API object
def on_connect(*, irc): def on_connect(*, irc):
... ...
@ -26,6 +28,7 @@ def handle_message(*, prefix, message, nick, channel, irc):
# prefix is the prefix at the start of the message, without the leading ':' # prefix is the prefix at the start of the message, without the leading ':'
# command is the command or number code # command is the command or number code
# arguments is rest of the arguments of the command, represented as a list. ':'-arguments are handled automatically # arguments is rest of the arguments of the command, represented as a list. ':'-arguments are handled automatically
# irc is the IRC API object
# All strings are bytestrings or bytearrays # All strings are bytestrings or bytearrays
def handle_nonmessage(*, prefix, command, arguments, irc): def handle_nonmessage(*, prefix, command, arguments, irc):
... ...

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import configparser
import select import select
import socket import socket
import threading import threading
@ -12,7 +13,7 @@ import botcmd
import cron import cron
import line_handling import line_handling
Server = namedtuple('Server', ['host', 'port', 'nick', 'realname', 'channels']) Server = namedtuple('Server', ['host', 'port', 'nick', 'username', 'realname', 'channels'])
class LoggerThread(threading.Thread): class LoggerThread(threading.Thread):
def __init__(self, logging_channel, dead_notify_channel): def __init__(self, logging_channel, dead_notify_channel):
@ -284,7 +285,7 @@ class ServerThread(threading.Thread):
try: try:
# Run initialization # Run initialization
self.send_line_raw(b'USER HynneFlip a a :' + self.server.realname.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')))
# Set up nick # Set up nick
self.api.nick(self.server.nick.encode('utf-8')) self.api.nick(self.server.nick.encode('utf-8'))
@ -304,7 +305,7 @@ class ServerThread(threading.Thread):
if not reconnecting: if not reconnecting:
# Tell the server we're quiting # Tell the server we're quiting
self.send_line_raw(b'QUIT :HynneFlip exiting normally') self.send_line_raw(b'QUIT :%s exiting normally' % self.server.username.encode('utf-8'))
self.server_socket.close() self.server_socket.close()
break break
@ -341,11 +342,27 @@ def spawn_loggerthread():
LoggerThread(logging_channel, dead_notify_channel).start() LoggerThread(logging_channel, dead_notify_channel).start()
return logging_channel, dead_notify_channel return logging_channel, dead_notify_channel
if __name__ == '__main__': # read_config() → config, server
# TODO: read from a configuration file # Reads the configuration file and returns the configuration object as well as a server object for spawn_serverthread
server = Server(host = 'irc.freenode.net', port = 6667, nick = 'o3-base', realname = 'IRC bot based on o3-base', channels = ['##ingsoc']) def read_config():
config = configparser.ConfigParser()
config.read('bot.conf')
botcmd.initialize() host = config['server']['host']
port = int(config['server']['port'])
nick = config['server']['nick']
username = config['server']['username']
realname = config['server']['realname']
channels = config['server']['channels'].split()
server = Server(host = host, port = port, nick = nick, username = username, realname = realname, channels = channels)
return config, server
if __name__ == '__main__':
config, server = read_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()