/modules/startup.py

https://github.com/rmoore/phenny
Python | 72 lines | 46 code | 13 blank | 13 comment | 8 complexity | 75978c13839e4c631766609c04cab9b2 MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. startup.py - Phenny Startup Module
  4. Copyright 2008, Sean B. Palmer, inamidst.com
  5. Licensed under the Eiffel Forum License 2.
  6. http://inamidst.com/phenny/
  7. """
  8. import threading, time
  9. def setup(phenny):
  10. # by clsn
  11. phenny.data = {}
  12. refresh_delay = 300.0
  13. if hasattr(phenny.config, 'refresh_delay'):
  14. try: refresh_delay = float(phenny.config.refresh_delay)
  15. except: pass
  16. def close():
  17. print("Nobody PONGed our PING, restarting")
  18. phenny.handle_close()
  19. def pingloop():
  20. timer = threading.Timer(refresh_delay, close, ())
  21. phenny.data['startup.setup.timer'] = timer
  22. phenny.data['startup.setup.timer'].start()
  23. # print "PING!"
  24. phenny.write(('PING', phenny.config.host))
  25. phenny.data['startup.setup.pingloop'] = pingloop
  26. def pong(phenny, input):
  27. try:
  28. # print "PONG!"
  29. phenny.data['startup.setup.timer'].cancel()
  30. time.sleep(refresh_delay + 60.0)
  31. pingloop()
  32. except: pass
  33. pong.event = 'PONG'
  34. pong.thread = True
  35. pong.rule = r'.*'
  36. phenny.variables['pong'] = pong
  37. # Need to wrap handle_connect to start the loop.
  38. inner_handle_connect = phenny.handle_connect
  39. def outer_handle_connect():
  40. inner_handle_connect()
  41. if phenny.data.get('startup.setup.pingloop'):
  42. phenny.data['startup.setup.pingloop']()
  43. phenny.handle_connect = outer_handle_connect
  44. def startup(phenny, input):
  45. if hasattr(phenny.config, 'serverpass'):
  46. phenny.write(('PASS', phenny.config.serverpass))
  47. if hasattr(phenny.config, 'password'):
  48. phenny.msg('NickServ', 'IDENTIFY %s' % phenny.config.password)
  49. time.sleep(5)
  50. # Cf. http://swhack.com/logs/2005-12-05#T19-32-36
  51. for channel in phenny.channels:
  52. phenny.write(('JOIN', channel))
  53. time.sleep(0.5)
  54. startup.rule = r'(.*)'
  55. startup.event = '251'
  56. startup.priority = 'low'
  57. if __name__ == '__main__':
  58. print(__doc__.strip())