Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

99 řádky
3.9 KiB

  1. # Copyright (C) 2009-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from json import loads
  21. from socket import AF_INET, AF_INET6, gethostbyname, inet_pton
  22. from urllib.request import urlopen
  23. from earwigbot.commands import Command
  24. class Geolocate(Command):
  25. """Geolocate an IP address (via http://ipinfodb.com/)."""
  26. name = "geolocate"
  27. commands = ["geolocate", "locate", "geo", "ip"]
  28. def setup(self):
  29. self.config.decrypt(self.config.commands, self.name, "apiKey")
  30. try:
  31. self.key = self.config.commands[self.name]["apiKey"]
  32. except KeyError:
  33. self.key = None
  34. log = 'Cannot use without an API key for http://ipinfodb.com/ stored as config.commands["{0}"]["apiKey"]'
  35. self.logger.warn(log.format(self.name))
  36. def process(self, data):
  37. if not self.key:
  38. msg = 'I need an API key for http://ipinfodb.com/ stored as \x0303config.commands["{0}"]["apiKey"]\x0f.'
  39. log = 'Need an API key for http://ipinfodb.com/ stored as config.commands["{0}"]["apiKey"]'
  40. self.reply(data, msg.format(self.name))
  41. self.logger.error(log.format(self.name))
  42. return
  43. if data.args:
  44. address = data.args[0]
  45. else:
  46. try:
  47. address = gethostbyname(data.host)
  48. except OSError:
  49. msg = "Your hostname, \x0302{0}\x0f, is not an IP address!"
  50. self.reply(data, msg.format(data.host))
  51. return
  52. if not self.is_ip(address):
  53. msg = "\x0302{0}\x0f is not an IP address!"
  54. self.reply(data, msg.format(address))
  55. return
  56. url = "http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json"
  57. query = urlopen(url.format(self.key, address)).read()
  58. res = loads(query)
  59. country = res["countryName"].title()
  60. region = res["regionName"].title()
  61. city = res["cityName"].title()
  62. latitude = res["latitude"]
  63. longitude = res["longitude"]
  64. utcoffset = res["timeZone"]
  65. if not country and not region and not city:
  66. self.reply(data, f"IP \x0302{address}\x0f not found.")
  67. return
  68. if country == "-" and region == "-" and city == "-":
  69. self.reply(data, f"IP \x0302{address}\x0f is reserved.")
  70. return
  71. msg = "{0}, {1}, {2} ({3}, {4}), UTC {5}"
  72. geo = msg.format(country, region, city, latitude, longitude, utcoffset)
  73. self.reply(data, geo)
  74. def is_ip(self, address):
  75. """Return ``True`` if the input is an IP address, else ``False``.
  76. This tests for IPv4 and IPv6 using :py:func:`socket.inet_pton`.
  77. """
  78. try:
  79. inet_pton(AF_INET, address)
  80. except OSError:
  81. try:
  82. inet_pton(AF_INET6, address)
  83. except OSError:
  84. return False
  85. return True