This repository has been archived by the owner on Sep 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smib.py
executable file
·141 lines (125 loc) · 5.99 KB
/
smib.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python2
import time
import os
import datetime
import re
import subprocess
from slackclient import SlackClient
token = "xoxb-[PUT THE REAL TOKEN HERE]"
bot_user = "U0XK12X63"
programsdir = '/home/smib/smib-commands/'
FORCE_CHANNEL = "FORCE_CHANNEL:"
all_commands = {}
all_commands_time = 0
error_count = 0
users = {}
chans = {}
sc = SlackClient(token)
def get_commands_by_dir():
#gah
global all_commands_time, all_commands
d = datetime.datetime.fromtimestamp(os.path.getmtime(programsdir))
if all_commands_time == 0 or d > all_commands_time:
for filename in os.listdir(programsdir):
if os.path.isfile(programsdir + filename) and os.access(programsdir + filename, os.X_OK):
match = re.search(r"(\w+).\w+", filename, re.MULTILINE)
if match:
all_commands[match.group(1)] = programsdir + filename
#print programsdir + filename
all_commands_time = d
return
def get_command(command):
global all_commands
if command in all_commands:
return command
return False
# todo: partial matches
if sc.rtm_connect():
# initial poll of smib-commands
get_commands_by_dir()
# initial poll of users
userslist = sc.api_call("users.list")
for member in userslist['members']:
users[member["id"]] = member["name"]
chanslist = sc.api_call("channels.list")
for channel in chanslist['channels']:
chans[channel["id"]] = channel["name"].lower()
while True:
try:
new_evts = sc.rtm_read()
except:
if error_count > 10:
raise
error_count += 1
continue
error_count = 0
for evt in new_evts:
#print evt
if "type" in evt:
if evt["type"] == "message" and "text" in evt:
try:
match = re.search(r"^\?(\w+) (.*)", evt["text"], re.DOTALL)
except:
print evt
continue
if match:
sc.server.send_to_websocket({"type": "typing", "channel": evt["channel"], "id": 1})
get_commands_by_dir()
command = match.group(1).lower()
argline = match.group(2)
is_command = get_command(command)
if is_command == False:
sc.api_call("chat.postMessage", as_user="false:", channel=evt["channel"], text="Sorry, I don't have a "+command+" command.")
continue
script = all_commands[command]
os.chdir(programsdir)
# Channel Messages
if evt["channel"][:1] == "C":
try:
p = subprocess.Popen([script, users[evt["user"]], chans[evt["channel"]], chans[evt["channel"]], argline], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except:
sc.api_call("chat.postMessage", as_user="false:", channel=evt["channel"], text=command+" is on fire!")
lines = p.stdout.readlines()
if len(lines) >= 1:
if FORCE_CHANNEL in x[0]:
#remove the control line
lines = lines[1:]
sc.api_call("chat.postMessage", as_user="false:", channel=evt["channel"], text=''.join(lines))
# Direct Messages
if evt["channel"][:1] == "D":
try:
p = subprocess.Popen([script, users[evt["user"]], 'null', evt["channel"], argline], shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except:
sc.api_call("chat.postMessage", as_user="false:", channel=evt["channel"], text=command+" is on fire!")
lines = p.stdout.readlines()
channel = evt["channel"]
if len(lines) >= 1:
if FORCE_CHANNEL in x[0]:
force = True
postchannel = x[0]
postchannel = postchannel[len(FORCE_CHANNEL):].lower()
if postchannel in chans.values():
for ch_id, chnl in chans.iteritems(): #dictionary.items() for Python 3.x
if chnl == postchannel:
postchannel_id = ch_id
else:
for ch_id, chnl in chans.iteritems(): #dictionary.items() for Python 3.x
if chnl == 'general':
postchannel_id = ch_id
#remove the control line
lines = lines[1:]
sc.api_call("chat.postMessage", as_user="false:", channel=ch_id, text=''.join(lines))
time.sleep(1)
continue
if evt["type"] == "user_change":
userslist = sc.api_call("users.list")
for member in userslist['members']:
users[member["id"]] = member["name"]
continue
if evt["type"] == "channel_created" or evt["type"] == "channel_rename":
chanslist = sc.api_call("channels.list")
for channel in chanslist['channels']:
chans[channel["id"]] = channel["name"]
time.sleep(0.1)
else:
print "Connection Failed, invalid token?"