Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

111 рядки
4.0 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 socket import gaierror, gethostbyname
  24. from time import time
  25. from earwigbot.commands import Command
  26. from earwigbot.exceptions import APIError
  27. class BlockMonitor(Command):
  28. """Monitors for on-wiki blocked users joining a particular channel."""
  29. name = "block_monitor"
  30. hooks = ["join"]
  31. def setup(self):
  32. try:
  33. config = self.config.commands[self.name]
  34. self._monitor_chan = config["monitorChannel"]
  35. self._report_chan = config["reportChannel"]
  36. except KeyError:
  37. self._monitor_chan = self._report_chan = None
  38. self.logger.warn("Cannot use without being properly configured")
  39. self._last = None
  40. def check(self, data):
  41. return (self._monitor_chan and self._report_chan and
  42. data.chan == self._monitor_chan)
  43. def process(self, data):
  44. ip = self._get_ip(data.host)
  45. if not ip:
  46. return
  47. if self._last and self._last[0] == ip:
  48. if time() - self._last[1] < 60 * 5:
  49. self._last = (ip, time())
  50. return
  51. self._last = (ip, time())
  52. block = self._get_block_for_ip(ip)
  53. if not block:
  54. return
  55. msg = ("\x02[{note}]\x0F Joined user \x02{nick}\x0F is {type}blocked "
  56. "on-wiki ([[User:{user}]]) because: {reason}")
  57. self.say(self._report_chan, msg.format(nick=data.nick, **block))
  58. log = ("Reporting block ({note}): {nick} is [[User:{user}]], "
  59. "{type}blocked because: {reason}")
  60. self.logger.info(log.format(nick=data.nick, **block))
  61. def _get_ip(self, host):
  62. """Return the IP corresponding to a particular hostname."""
  63. match = re.search(r"/ip\.(.*?)$", host)
  64. if match:
  65. return match.group(1)
  66. try:
  67. return gethostbyname(host)
  68. except gaierror:
  69. return None
  70. def _get_block_for_ip(self, ip):
  71. """Return a dictionary of blockinfo for an IP."""
  72. site = self.bot.wiki.get_site()
  73. try:
  74. result = site.api_query(
  75. action="query", list="blocks|globalblocks", bkip=ip, bgip=ip,
  76. bklimit=1, bglimit=1, bkprop="user|reason|range",
  77. bgprop="address|reason|range")
  78. except APIError:
  79. return
  80. lblocks = result["query"]["blocks"]
  81. gblocks = result["query"]["globalblocks"]
  82. if not lblocks and not gblocks:
  83. return
  84. block = lblocks[0] if lblocks else gblocks[0]
  85. if block["rangestart"] != block["rangeend"]:
  86. block["type"] = "range"
  87. else:
  88. block["type"] = "IP-"
  89. if not lblocks:
  90. block["type"] = "globally " + block["type"]
  91. block["user"] = block["address"]
  92. if re.search(r"web[ _-]?host", block["reason"], re.IGNORECASE):
  93. block["note"] = "webhost warning"
  94. else:
  95. block["note"] = "alert"
  96. return block