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_runner.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/python
  2. import os, sys, re, subprocess
  3. # If this script is set as your command handler, when any ?command is run in IRC it will
  4. # look in the path defined below for a script matching that command name and run it.
  5. #
  6. # e.g. ?uptime would look in "/usr/share/irccat/" (the default) for any script called
  7. # "uptime", with any extension. It would happily run both uptime.sh and uptime.py, or
  8. # a script in whatever language you like. Command names are limited to [0-9a-z].
  9. path = '/usr/share/irccat/'
  10. bits = sys.argv[1:]
  11. command = bits[3].lower()
  12. found = False
  13. if re.match('^[a-z0-9]+$', command):
  14. for file in os.listdir(path):
  15. if re.match('^%s\.[a-z]+$' % command, file):
  16. found = True
  17. procArgs = [path + file]
  18. procArgs.extend(bits)
  19. proc = subprocess.Popen(procArgs, stdout=subprocess.PIPE)
  20. stdout = proc.stdout
  21. while True:
  22. # We do this to avoid buffering from the subprocess stdout
  23. print os.read(stdout.fileno(), 65536),
  24. sys.stdout.flush()
  25. if proc.poll() != None:
  26. break
  27. if found == False:
  28. print "Unknown command '%s'" % command