locationbot.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. # Location Bot
  3. #
  4. # Douglas Thrift
  5. #
  6. # $Id$
  7. from ConfigParser import NoOptionError, SafeConfigParser
  8. import getpass
  9. import ircbot
  10. import irclib
  11. import sys
  12. import warnings
  13. class LocationBot(ircbot.SingleServerIRCBot):
  14. def __init__(self):
  15. config = SafeConfigParser({'hostname': ''})
  16. config.read('locationbot.ini')
  17. try:
  18. nick = config.sections()[0]
  19. except IndexError:
  20. sys.exit('No nick configured')
  21. servers = []
  22. try:
  23. for server in config.get(nick, 'servers').split():
  24. server = server.split(':', 2)
  25. if len(server) == 1:
  26. servers.append((server[0], 6667))
  27. else:
  28. host = server[0]
  29. port = int(server[1])
  30. ssl = server[1].startswith('+')
  31. if len(server) == 3:
  32. servers.append((host, port, ssl, server[2]))
  33. else:
  34. servers.append((host, port, ssl))
  35. self.__admins = config.get(nick, 'admins').split()
  36. self.__channels = config.get(nick, 'channels').split()
  37. self.__hostname = config.get(nick, 'hostname')
  38. except NoOptionError, error:
  39. sys.exit(error)
  40. ircbot.SingleServerIRCBot.__init__(self, servers, nick, 'Location Bot')
  41. def __admin(self, nick):
  42. if isinstance(nick, irclib.Event):
  43. nick = nick.source()
  44. for admin in self.__admins:
  45. if irclib.mask_matches(nick, admin):
  46. return True
  47. return False
  48. def __command(self, connection, event):
  49. nick = irclib.nm_to_n(event.source())
  50. try:
  51. command, arguments = event.arguments()[0].split(None, 1)
  52. except ValueError:
  53. command = event.arguments()[0].strip()
  54. arguments = ''
  55. if command.startswith('!'):
  56. command = command[1:]
  57. if command == 'help':
  58. commands = [
  59. ('help', 'show this help message'),
  60. ]
  61. if self.__admin(event):
  62. commands += [
  63. ('reload', 'reload my code'),
  64. ]
  65. for command in commands:
  66. connection.privmsg(nick, '!%-7s %s' % command)
  67. elif self.__admin(event):
  68. if command == 'reload':
  69. raise Reload
  70. else:
  71. self.__unknown(connection, nick, command)
  72. else:
  73. self.__unknown(connection, nick, command)
  74. elif event.eventtype() == 'privmsg':
  75. self.__unknown(connection, nick, command)
  76. def __unknown(self, connection, nick, command):
  77. connection.privmsg(nick, 'unknown command (%s); try "!help"' % command)
  78. def _connect(self):
  79. password = None
  80. if len(self.server_list[0]) != 2:
  81. ssl = self.server_list[0][2]
  82. if len(self.server_list[0]) == 4:
  83. password = self.server_list[0][3]
  84. try:
  85. with warnings.catch_warnings():
  86. warnings.filterwarnings('ignore', r'socket\.ssl\(\) is deprecated\. Use ssl\.wrap_socket\(\) instead\.', DeprecationWarning)
  87. self.connect(self.server_list[0][0], self.server_list[0][1], self._nickname, password, getpass.getuser(), self._realname, localaddress = self.__hostname, ssl = ssl)
  88. except irclib.ServerConnectionError:
  89. pass
  90. def get_version(self):
  91. return 'locationbot ' + sys.platform
  92. def on_nicknameinuse(self, connection, event):
  93. connection.nick(connection.get_nickname() + '_')
  94. def on_privmsg(self, connection, event):
  95. self.__command(connection, event)
  96. def on_pubmsg(self, connection, event):
  97. self.__command(connection, event)
  98. def on_welcome(self, connection, event):
  99. for channel in self.__channels:
  100. connection.join(channel)
  101. class Reload(Exception):
  102. pass
  103. if __name__ == '__main__':
  104. import os.path
  105. locationbot = LocationBot()
  106. try:
  107. while True:
  108. try:
  109. locationbot.start()
  110. except Reload:
  111. locationbot.connection.disconnect('Reloading')
  112. locationbot = __import__(os.path.splitext(os.path.basename(__file__))[0]).LocationBot()
  113. except KeyboardInterrupt:
  114. locationbot.connection.disconnect('Oh no!')