A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

124 righe
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  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. from fnmatch import fnmatch
  23. import sqlite3 as sqlite
  24. from threading import Lock
  25. __all__ = ["PermissionsDB"]
  26. class PermissionsDB(object):
  27. ADMIN = 1
  28. OWNER = 2
  29. def __init__(self, dbfile):
  30. self._dbfile = dbfile
  31. self._db_access_lock = Lock()
  32. self._data = {}
  33. def __repr__(self):
  34. """Return the canonical string representation of the PermissionsDB."""
  35. res = "PermissionsDB(dbfile={0!r})"
  36. return res.format(self._dbfile)
  37. def __str__(self):
  38. """Return a nice string representation of the PermissionsDB."""
  39. return "<PermissionsDB at {0}>".format(self._dbfile)
  40. def _create(self, conn):
  41. """Initialize the permissions database with its necessary tables."""
  42. query = """CREATE TABLE users (user_nick, user_ident, user_host,
  43. user_rank)"""
  44. conn.execute(query)
  45. def _is_rank(self, user, rank):
  46. """Return True if the given user has the given rank, else False."""
  47. try:
  48. for rule in self._data[rank]:
  49. if user in rule:
  50. return True
  51. except KeyError:
  52. return False
  53. def _set_rank(self, user, rank):
  54. """Add a User to the database under a given rank."""
  55. try:
  56. self._data[rank].append(user)
  57. except KeyError:
  58. self._data[rank] = [user]
  59. query = "INSERT INTO users VALUES (?, ?, ?, ?)"
  60. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  61. conn.execute(query, (user.nick, user.ident, user.host, rank))
  62. def load(self):
  63. """Load permissions from an existing database, or create a new one."""
  64. query = "SELECT user_nick, user_ident, user_host, user_rank FROM users"
  65. self._data = {}
  66. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  67. try:
  68. for nick, ident, host, rank in conn.execute(query):
  69. try:
  70. self._data[rank].append(_User(nick, ident, host))
  71. except KeyError:
  72. self._data[rank] = [_User(nick, ident, host)]
  73. except sqlite.OperationalError:
  74. self._create(conn)
  75. def is_admin(self, nick="*", ident="*", host="*"):
  76. """Return ``True`` if the given user is a bot admin, else ``False``."""
  77. return self._is_rank(_User(nick, ident, host), rank=self.ADMIN)
  78. def is_owner(self, nick="*", ident="*", host="*"):
  79. """Return ``True`` if the given user is a bot owner, else ``False``."""
  80. return self._is_rank(_User(nick, ident, host), rank=self.OWNER)
  81. def add_admin(self, nick="*", ident="*", host="*"):
  82. """Add an nick/ident/host combo to the bot admins list."""
  83. return self._set_rank(_User(nick, ident, host), rank=self.ADMIN)
  84. def add_owner(self, nick="*", ident="*", host="*"):
  85. """Add a nick/ident/host combo to the bot owners list."""
  86. return self._set_rank(_User(nick, ident, host), rank=self.OWNER)
  87. class _User(object):
  88. """A class that represents an IRC user for the purpose of testing rules."""
  89. def __init__(self, nick, ident, host):
  90. self.nick = nick
  91. self.ident = ident
  92. self.host = host
  93. def __repr__(self):
  94. """Return the canonical string representation of the User."""
  95. res = "_User(nick={0!r}, ident={1!r}, host={2!r})"
  96. return res.format(self.nick, self.ident, self.host)
  97. def __str__(self):
  98. """Return a nice string representation of the User."""
  99. return "{0}!{1}@{2}".format(self.nick, self.ident, self.host)
  100. def __contains__(self, user):
  101. if fnmatch(user.nick, self.nick):
  102. if fnmatch(user.ident, self.ident):
  103. if fnmatch(user.host, self.host):
  104. return True
  105. return False