h/bot.py

61 lines
1.6 KiB
Python
Raw Normal View History

2008-06-09 11:59:56 +00:00
#!/usr/bin/python
2009-03-15 03:06:36 +00:00
import os
import Queue
import sys
import time
2008-06-09 11:59:56 +00:00
sys.path += ['plugins'] # so 'import hook' works without duplication
sys.path += ['lib']
os.chdir(sys.path[0] or '.') # do stuff relative to the installation directory
2008-06-09 11:59:56 +00:00
class Bot(object):
pass
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-01-17 20:38:37 +00:00
eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
os.path.join('core', 'reload.py'), 'exec'))
reload(init=True)
2008-06-09 11:59:56 +00:00
print 'Connecting to IRC'
bot.conns = {}
2009-11-18 00:27:55 +00:00
try:
for name, conf in bot.config['connections'].iteritems():
if conf.get('ssl'):
bot.conns[name] = SSLIRC(conf['server'], conf['nick'],
port=conf.get('port', 6667), channels=conf['channels'], conf=conf,
ignore_certificate_errors=conf.get('ignore_cert', True))
else:
bot.conns[name] = IRC(conf['server'], conf['nick'],
port=conf.get('port', 6667), channels=conf['channels'], conf=conf)
2009-11-18 00:27:55 +00:00
except Exception, e:
print 'ERROR: malformed config file', Exception, e
sys.exit()
2009-11-18 00:38:48 +00:00
2009-04-03 03:50:38 +00:00
bot.persist_dir = os.path.abspath('persist')
if not os.path.exists(bot.persist_dir):
os.mkdir(bot.persist_dir)
2008-06-09 11:59:56 +00:00
print 'Running main loop'
while True:
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()):
time.sleep(.3)