Additional IRC commands and bot tasks for EarwigBot https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

lta_monitor.py 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2015 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, bklimit=1)
  57. except APIError:
  58. return
  59. blocks = result["query"]["blocks"]
  60. if not blocks:
  61. return
  62. msg = ("\x02[Alert]\x0F Joined user \x02{nick}\x0F is IP-blocked "
  63. "on-wiki ([[User:{user}]] by {by}) because: {reason}")
  64. self.say(self._report_chan, msg.format(nick=data.nick, **blocks[0]))