A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

110 rindas
4.3 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. import sqlite3 as sqlite
  23. from threading import Lock
  24. __all__ = ["PermissionsDB"]
  25. class PermissionsDB(object):
  26. ADMIN = 1
  27. OWNER = 2
  28. def __init__(self, dbfile):
  29. self._dbfile = dbfile
  30. self._db_access_lock = Lock()
  31. self._data = {}
  32. def __repr__(self):
  33. """Return the canonical string representation of the PermissionsDB."""
  34. res = "PermissionsDB(dbfile={0!r})"
  35. return res.format(self._dbfile)
  36. def __str__(self):
  37. """Return a nice string representation of the PermissionsDB."""
  38. return "<PermissionsDB at {0}>".format(self._dbfile)
  39. def _create(self, conn):
  40. """Initialize the permissions database with its necessary tables."""
  41. query = """CREATE TABLE users (user_nick, user_ident, user_host,
  42. user_rank)"""
  43. conn.execute(query)
  44. def _is_rank(self, user, rank):
  45. """Return True if the given user has the given rank, else False."""
  46. try:
  47. for rule in self._data[rank]:
  48. if user == rule:
  49. return True
  50. except KeyError:
  51. return False
  52. def load(self):
  53. """Load permissions from an existing database, or create a new one."""
  54. query = "SELECT user_nick, user_ident, user_host, user_rank FROM users"
  55. self._data = {}
  56. with sqlite.connect(self._dbfile) as conn, self._db_access_lock:
  57. try:
  58. for nick, ident, host, rank in conn.execute(query):
  59. try:
  60. self._data[rank].append(_User(nick, ident, host))
  61. except KeyError:
  62. self._data[rank] = [_User(nick, ident, host)]
  63. except sqlite.OperationalError:
  64. self._create(conn)
  65. def is_admin(self, nick="*", ident="*", host="*"):
  66. """Return ``True`` if the given user is a bot admin, else ``False``."""
  67. return self._is_rank(_User(nick, ident, host), rank=self.ADMIN)
  68. def is_owner(self, nick="*", ident="*", host="*"):
  69. """Return ``True`` if the given user is a bot owner, else ``False``."""
  70. return self._is_rank(_User(nick, ident, host), rank=self.OWNER)
  71. class _User(object):
  72. """A class that represents an IRC user for the purpose of testing rules."""
  73. def __init__(self, nick, ident, host):
  74. self.nick = nick
  75. self.ident = ident
  76. self.host = host
  77. def __repr__(self):
  78. """Return the canonical string representation of the User."""
  79. res = "_User(nick={0!r}, ident={1!r}, host={2!r})"
  80. return res.format(self.nick, self.ident, self.host)
  81. def __str__(self):
  82. """Return a nice string representation of the User."""
  83. return "{0}!{1}@{2}".format(self.nick, self.ident, self.host)
  84. def __eq__(self, user):
  85. if self.nick == user.nick or (self.nick == "*" or user.nick == "*"):
  86. if self.ident == user.ident or (self.ident == "*" or
  87. user.ident == "*"):
  88. if self.host == user.host or (self.host == "*" or
  89. user.host == "*"):
  90. return True
  91. return False
  92. def __ne__(self, user):
  93. return not self == user