h/bot.py

55 lines
1.2 KiB
Python
Raw 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
import Queue
import sys
import traceback
import time
2008-06-09 11:59:56 +00:00
2010-03-01 02:32:41 +00:00
sys.path += ['plugins'] # so 'import hook' works without duplication
sys.path += ['lib']
2010-03-01 02:32:41 +00:00
os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
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()
2008-06-09 11:59:56 +00:00
print 'Loading plugins'
2009-03-15 04:14:07 +00:00
# bootstrap the reloader
2010-03-01 02:32:41 +00:00
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
2014-01-14 21:12:37 +00:00
os.path.join('core', 'reload.py'), 'exec'))
reload(init=True)
2008-06-09 11:59:56 +00:00
print 'Connecting to IRC'
2009-11-18 00:27:55 +00:00
try:
config()
if not hasattr(bot, 'config'):
exit()
2009-11-18 00:27:55 +00:00
except Exception, e:
print 'ERROR: malformed config file:', e
traceback.print_exc()
2009-11-18 00:27:55 +00:00
sys.exit()
2008-06-09 11:59:56 +00:00
print 'Running main loop'
while True:
2010-03-01 02:32:41 +00:00
reload() # these functions only do things
config() # if changes have occured
for conn in bot.conns.itervalues():
try:
out = conn.out.get_nowait()
2010-02-22 23:49:30 +00:00
main(conn, out)
except Queue.Empty:
pass
while all(conn.out.empty() for conn in bot.conns.itervalues()):
2010-03-11 23:34:54 +00:00
time.sleep(.1)