A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

55 рядки
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. import hashlib
  3. from classes import BaseCommand
  4. import blowfish
  5. class Command(BaseCommand):
  6. """Provides hash functions with !hash (!hash list for supported algorithms)
  7. and blowfish encryption with !encrypt and !decrypt."""
  8. name = "cryptography"
  9. def check(self, data):
  10. if data.is_command and data.command in ["hash", "encrypt", "decrypt"]:
  11. return True
  12. return False
  13. def process(self, data):
  14. if not data.args:
  15. msg = "what do you want me to {0}?".format(data.command)
  16. self.connection.reply(data, msg)
  17. return
  18. if data.command == "hash":
  19. algo = data.args[0]
  20. if algo == "list":
  21. algos = ', '.join(hashlib.algorithms)
  22. msg = algos.join(("supported algorithms: ", "."))
  23. self.connection.reply(data, msg)
  24. elif algo in hashlib.algorithms:
  25. string = ' '.join(data.args[1:])
  26. result = getattr(hashlib, algo)(string).hexdigest()
  27. self.connection.reply(data, result)
  28. else:
  29. msg = "unknown algorithm: '{0}'.".format(algo)
  30. self.connection.reply(data, msg)
  31. else:
  32. key = data.args[0]
  33. text = ' '.join(data.args[1:])
  34. if not text:
  35. msg = "a key was provided, but text to {0} was not."
  36. self.connection.reply(data, msg.format(data.command))
  37. return
  38. try:
  39. if data.command == "encrypt":
  40. self.connection.reply(data, blowfish.encrypt(key, text))
  41. else:
  42. self.connection.reply(data, blowfish.decrypt(key, text))
  43. except blowfish.BlowfishError as error:
  44. msg = "{0}: {1}.".format(error.__class__.__name__, error)
  45. self.connection.reply(data, msg)