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.

command_handler.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import re
  5. import subprocess
  6. # If this script is set as your command handler, when any ?command is run in IRC it will
  7. # look in the path defined below for a script matching that command name and run it.
  8. #
  9. # e.g. ?uptime would look in "/usr/share/irccat/" (the default) for any script called
  10. # "uptime", with any extension. It would happily run both uptime.sh and uptime.py, or
  11. # a script in whatever language you like. Command names are limited to [0-9a-z].
  12. path = '/usr/share/irccat/'
  13. # Example of retrieving all the environment variables.
  14. # We only need command here as all the others will be available in the script's environment.
  15. nick = os.environ.get('IRCCAT_NICK')
  16. user = os.environ.get('IRCCAT_USER')
  17. host = os.environ.get('IRCCAT_HOST')
  18. channel = os.environ.get('IRCCAT_CHANNEL')
  19. respond_to = os.environ.get('IRCCAT_RESPOND_TO')
  20. command = os.environ.get('IRCCAT_COMMAND')
  21. args = os.environ.get('IRCCAT_ARGS')
  22. found = False
  23. if re.match('^[a-z0-9]+$', command):
  24. for filename in os.listdir(path):
  25. if re.match('^%s\.[a-z]+$' % command, filename):
  26. found = True
  27. proc = subprocess.Popen(os.path.join(path, filename), stdout=subprocess.PIPE)
  28. stdout = proc.stdout
  29. while True:
  30. # We do this to avoid buffering from the subprocess stdout
  31. print(os.read(stdout.fileno(), 65536))
  32. sys.stdout.flush()
  33. if proc.poll() is not None:
  34. break
  35. if not found:
  36. print("Unknown command '%s'" % command)