XMPP bot written in Python https://gitlab.com/XRevan86/hptoad
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.2KB

  1. # -*- python -*-
  2. import asyncio
  3. import os
  4. import re
  5. class Plugin:
  6. _trim_regexp = re.compile("(`|\\$|\\.\\.)")
  7. _quote_regexp = re.compile("(\"|')")
  8. @classmethod
  9. def _trim(cls, s):
  10. result = cls._trim_regexp.sub("", s)
  11. result = cls._quote_regexp.sub("“", result).strip()
  12. return result
  13. # letter(ASCII or cyrillic), number, underscore only.
  14. _cmd_validator_regexp = re.compile("^(\\w|\\p{Cyrillic})*$")
  15. @asyncio.coroutine
  16. def _exec_cmd(self, cmd, body, nick, dir_path, is_admin):
  17. is_admin = "true" if is_admin else "false"
  18. path = os.path.join(dir_path, self._trim(cmd))
  19. if not self._cmd_validator_regexp.match(cmd) or \
  20. not os.access(path, os.F_OK | os.X_OK) or not os.path.isfile(path):
  21. return {"handled": False}
  22. if not os.access(path, os.R_OK):
  23. return {"handled": True,
  24. "error": "\"%s\" is not readable" % path}
  25. cmd = [path, self._trim(nick), is_admin, self._trim(body)]
  26. try:
  27. pipe = asyncio.subprocess.PIPE
  28. proc = yield from asyncio.create_subprocess_exec(*cmd,
  29. stdout=pipe,
  30. stderr=pipe)
  31. cmd_reply, cmd_error = yield from proc.communicate()
  32. except OSError as e:
  33. return {"handled": True,
  34. "error": "Execute: %s" % str(e)}
  35. result = {}
  36. if cmd_error and len(cmd_error.strip()) > 0:
  37. result["error"] = "Process: %s" % cmd_error.strip()
  38. if cmd_reply and len(cmd_reply.strip()) > 0:
  39. result["reply"] = cmd_reply.decode().strip()
  40. if result:
  41. result["handled"] = True
  42. return result
  43. @asyncio.coroutine
  44. def command(self, command, body, nick, from_id, is_admin):
  45. result = yield from self._exec_cmd(command, body, nick,
  46. "plugins", is_admin)
  47. return result
  48. @asyncio.coroutine
  49. def question(self, body, nick, from_id, is_admin):
  50. result = yield from self._exec_cmd("answer", body, nick,
  51. "chat", is_admin)
  52. return result