Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

101 rader
3.8 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. block = self._get_block_for_ip(ip)
  55. if not block:
  56. return
  57. msg = ("\x02[{note}]\x0F Joined user \x02{nick}\x0F is {type}blocked "
  58. "on-wiki ([[User:{user}]]) because: {reason}")
  59. self.say(self._report_chan, msg.format(nick=data.nick, **block))
  60. log = ("Reporting block ({note}): {nick} is [[User:{user}]], "
  61. "{type}blocked because: {reason}")
  62. self.logger.info(log.format(nick=data.nick, **block))
  63. def _get_block_for_ip(self, ip):
  64. """Return a dictionary of blockinfo for an IP."""
  65. site = self.bot.wiki.get_site()
  66. try:
  67. result = site.api_query(
  68. action="query", list="blocks|globalblocks", bkip=ip, bgip=ip,
  69. bklimit=1, bglimit=1, bkprop="user|reason|range",
  70. bgprop="address|reason|range")
  71. except APIError:
  72. return
  73. lblocks = result["query"]["blocks"]
  74. gblocks = result["query"]["globalblocks"]
  75. if not lblocks and not gblocks:
  76. return
  77. block = lblocks[0] if lblocks else gblocks[0]
  78. if block["rangestart"] != block["rangeend"]:
  79. block["type"] = "range"
  80. else:
  81. block["type"] = "IP-"
  82. if not lblocks:
  83. block["type"] = "globally " + block["type"]
  84. if re.search(r"web[ _-]?host", block["reason"], re.IGNORECASE):
  85. block["note"] = "webhost warning"
  86. else:
  87. block["note"] = "alert"
  88. return block