/bot.py

https://github.com/Alligator/homero
Python | 71 lines | 50 code | 19 blank | 2 comment | 14 complexity | e569117f42d61eb4a5f0814256efa9b6 MD5 | raw file
  1. #!/usr/bin/env python
  2. a = 34123
  3. import os
  4. import Queue
  5. import sys
  6. import time
  7. import remotedebug
  8. sys.path += ['plugins'] # so 'import hook' works without duplication
  9. sys.path += ['lib']
  10. os.chdir(sys.path[0] or '.') # do stuff relative to the install directory
  11. class Bot(object):
  12. pass
  13. bot = Bot()
  14. remotedebug.listen()
  15. print 'Loading plugins'
  16. # bootstrap the reloader
  17. eval(compile(open(os.path.join('core', 'reload.py'), 'U').read(),
  18. os.path.join('core', 'reload.py'), 'exec'))
  19. reload_core(init=True)
  20. config()
  21. reload_plugs(init=True)
  22. if not hasattr(bot, 'config'):
  23. exit()
  24. print 'Connecting to IRC'
  25. bot.conns = {}
  26. try:
  27. for name, conf in bot.config['connections'].iteritems():
  28. if conf.get('ssl'):
  29. bot.conns[name] = SSLIRC(conf['server'], conf['nick'], conf=conf,
  30. port=conf.get('port', 6667), channels=conf['channels'],
  31. ignore_certificate_errors=conf.get('ignore_cert', True))
  32. else:
  33. bot.conns[name] = IRC(conf['server'], conf['nick'], conf=conf,
  34. port=conf.get('port', 6667), channels=conf['channels'])
  35. except Exception, e:
  36. print 'ERROR: malformed config file', e
  37. sys.exit()
  38. bot.persist_dir = os.path.abspath('persist')
  39. if not os.path.exists(bot.persist_dir):
  40. os.mkdir(bot.persist_dir)
  41. print 'Running main loop'
  42. while True:
  43. reload() # these functions only do things
  44. config() # if changes have occured
  45. for conn in bot.conns.itervalues():
  46. try:
  47. out = conn.out.get_nowait()
  48. main(conn, out)
  49. except Queue.Empty:
  50. pass
  51. while all(conn.out.empty() for conn in bot.conns.itervalues()):
  52. time.sleep(.1)