Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import re
  23. from earwigbot.commands import Command
  24. from earwigbot.exceptions import APIError
  25. class LTAMonitor(Command):
  26. """Monitors for LTAs. No further information is available."""
  27. name = "lta_monitor"
  28. hooks = ["join", "part"]
  29. def setup(self):
  30. try:
  31. config = self.config.commands[self.name]
  32. self._monitor_chan = config["monitorChannel"]
  33. self._report_chan = config["reportChannel"]
  34. except KeyError:
  35. self._monitor_chan = self._report_chan = None
  36. self.logger.warn("Cannot use without being properly configured")
  37. self._recent = []
  38. self._recent_max = 10
  39. def check(self, data):
  40. return (self._monitor_chan and self._report_chan and
  41. data.chan == self._monitor_chan)
  42. def process(self, data):
  43. if not data.host.startswith("gateway/web/"):
  44. return
  45. match = re.search(r"/ip\.(.*?)$", data.host)
  46. if not match:
  47. return
  48. ip = match.group(1)
  49. if ip in self._recent:
  50. return
  51. self._recent.append(ip)
  52. if len(self._recent) > self._recent_max:
  53. self._recent.pop(0)
  54. site = self.bot.wiki.get_site()
  55. try:
  56. result = site.api_query(action="query", list="blocks", bkip=ip,
  57. bklimit=1, bkprop="user|reason|range")
  58. except APIError:
  59. return
  60. blocks = result["query"]["blocks"]
  61. if not blocks:
  62. return
  63. block = blocks[0]
  64. if re.search(r"web[ _-]?host", block["reason"], re.IGNORECASE):
  65. block["note"] = "webhost warning"
  66. else:
  67. block["note"] = "alert"
  68. if block["rangestart"] != block["rangeend"]:
  69. block["type"] = "range"
  70. else:
  71. block["type"] = "IP-"
  72. msg = ("\x02[{note}]\x0F Joined user \x02{nick}\x0F is {type}blocked "
  73. "on-wiki ([[User:{user}]]) because: {reason}")
  74. self.say(self._report_chan, msg.format(nick=data.nick, **block))
  75. log = ("Reporting block ({note}): {nick} is [[User:{user}]], "
  76. "{type}blocked because: {reason}")
  77. self.logger.info(log.format(nick=data.nick, **block))