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