-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·97 lines (78 loc) · 2.73 KB
/
app.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
#!/usr/bin/python3
from urllib.parse import urlparse
from gmapi import api
import gmapi, shlex, sys, http.client, http.server, json, cmd
def fetchPastebin(fileId):
conn = http.client.HTTPSConnection('pastebin.com')
conn.request('GET', '/raw/%s' % fileID, None, {'User-agent': 'gmapi',
'Accept': '*/*'})
resp = conn.getresponse()
if resp.code != 200:
raise ValueError('Invalid pastebin url')
data = resp.read()
conn.close()
return data
def fetchGist(user, fileId, rev = ''):
if rev:
rev = '/' + rev
conn = http.client.HTTPSConnection('gist.github.com')
conn.request('GET', '/%s/%s/raw%s' % (user, fileId, rev), None, {'User-agent': 'gmapi',
'Accept': '*/*'})
resp = conn.getresponse()
if resp.code != 200:
raise ValueError('Invalid gist url')
data = resp.read()
conn.close()
return data
def fetchCode(url):
o = urlparse(url)
if not o.scheme:
url = 'http://' + url
o = urlparse(url)
valid = False
if o.netloc == 'pastebin.com':
parts = o.path.split('/')
if len(parts) == 1:
return fetchPastebin(parts[0])
elif len(parts) == 2:
validCmds = {'raw', 'edit', 'print'}
if parts[0] in validCmds:
return fetchPastebin(parts[1])
raise ValueError('Invalid pastebin url')
elif o.netloc == 'gist.github.com':
parts = o.path.split('/')
if len(parts) == 2 or (len(parts) == 3 and parts[2] == 'raw'):
return fetchGist(parts[0], parts[1])
elif len(parts) == 4 and parts[2] == 'raw':
return fetchGist(parts[0], parts[1], parts[3])
else:
raise ValueError('Invalid gist url')
else:
raise ValueError('Please specify a valid github gist or pastebin url')
class CallbackHandler(http.server.BaseHTTPRequestHandler):
def do_POST(self):
print('callback: new message')
length = int(self.headers['content-length'])
msg = gmapi.Message(json.loads(self.rfile.read(length).decode('utf-8')))
msgParser.parseMessage(msg.text)
def run(host, port):
httpd = http.server.HTTPServer((host, port), CallbackHandler)
httpd.serve_forever()
host = '0.0.0.0'
port = 1234
sys.stdout.write('Input access token: ')
token = input()
print('Loading...\n')
api.auth(token)
api.init()
bots = [b for b in api.bots()]
print('You have the following bots:')
for i in range(len(bots)):
b = bots[i]
print(' %d. %s' % (i + 1, b))
print(' Listens to: %s' % (b.group, 'dm notifications') \
[b.dm_notifications])
sys.stdout.write('Select the bot as output (input the index): ')
bot = bots[int(input()) - 1]
msgParser = cmd.CmdParser(bot.postMessage)
run(host, port)