h/bot.py

60 lines
1.4 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
2008-06-09 11:59:56 +00:00
2009-03-15 03:06:36 +00:00
import os
2015-11-15 16:36:35 +00:00
import queue
import sys
import traceback
import time
2008-06-09 11:59:56 +00:00
2008-06-09 11:59:56 +00:00
class Bot(object):
def __init__(self):
self.conns = {}
self.persist_dir = os.path.abspath('persist')
if not os.path.exists(self.persist_dir):
2014-05-01 03:12:28 +00:00
os.mkdir(self.persist_dir)
bot = Bot()
def main():
sys.path += ['plugins'] # so 'import hook' works without duplication
sys.path += ['lib']
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
2015-11-15 16:36:35 +00:00
print('Loading plugins')
2008-06-09 11:59:56 +00:00
# bootstrap the reloader
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
os.path.join('core', 'reload.py'), 'exec'),
globals())
reload(init=True)
2009-03-15 04:14:07 +00:00
2015-11-15 16:36:35 +00:00
print('Connecting to IRC')
try:
config()
if not hasattr(bot, 'config'):
exit()
2015-11-15 16:36:35 +00:00
except Exception as e:
print('ERROR: malformed config file:', e)
traceback.print_exc()
sys.exit()
2015-11-15 16:36:35 +00:00
print('Running main loop')
while True:
reload() # these functions only do things
config() # if changes have occured
2008-06-09 11:59:56 +00:00
2015-11-15 16:36:35 +00:00
for conn in bot.conns.values():
try:
out = conn.out.get_nowait()
main(conn, out)
2015-11-15 16:36:35 +00:00
except queue.Empty:
pass
2015-11-15 16:36:35 +00:00
while all(conn.out.empty() for conn in bot.conns.values()):
time.sleep(.1)
if __name__ == '__main__':
main()