This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ircclive.py
executable file
·173 lines (157 loc) · 5.13 KB
/
ircclive.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import getpass
import gzip
import io
import json
import sys
import time
import traceback
import urllib.error
import urllib.parse
import urllib.request
__version__ = "1.0.1"
baseurl = "https://www.irccloud.com/chat/"
email, password, stat_user = None, None, None
def rpc(method, path, session=None, token=None, keepalive=False, data=None):
try: # python 3.4 or later
r = urllib.request.Request(urllib.parse.urljoin(baseurl, path), method=method)
r.data = data
except TypeError:
r = urllib.request.Request(urllib.parse.urljoin(baseurl, path))
r.add_data(data)
r.add_header("User-Agent", "IRCCLive")
if method == "POST":
r.add_header("Content-Type","application/x-www-form-urlencoded")
if session:
r.add_header("Cookie", "session=" + session)
if keepalive:
r.add_header("Connection", "keep-alive")
if token:
r.add_header("x-auth-formtoken", token)
return urllib.request.urlopen(r)
def rpc_get(session, path, keepalive=False):
return rpc("GET", path, session, keepalive=keepalive)
def rpc_post(session, path, keepalive=False, data=None):
return rpc("POST", path, session, keepalive=keepalive, data=data)
def getresponse(response):
b = io.BytesIO()
b.write(response.read())
b.seek(0, 0)
if response.info().get("Content-Encoding") == "gzip":
f = gzip.GzipFile(fileobj=b)
return f.read().decode("utf-8")
else:
return b.read().decode("utf-8")
def auth_formtoken():
f = rpc("POST", "auth-formtoken", data=b"")
d = json.loads(getresponse(f))
if d.get("success", False):
return d["token"]
return None
def login(email, password, token):
f = rpc("POST", "login", token=token, data=urllib.parse.urlencode({"email": email, "password": password, "token": token}).encode("ascii"))
d = json.loads(getresponse(f))
if d.get("success", False):
return d["session"]
return None
def stream(session):
f = rpc_get(session, "stream", True)
global stat_user
interval = 0
while True:
d = f.readline()
if not d:
_print("disconnected.")
break
else:
d = json.loads(d.decode("utf-8"))
if d["type"] == "oob_include":
if oob_include(session, d["url"]):
_print("connected successfully.")
else:
_print("connection failed.")
break
elif d["type"] == "stat_user":
stat_user = d
try:
f.close()
except:
pass
def oob_include(session, url):
f = rpc_get(session, url)
d = json.loads(getresponse(f))
for i in d:
if i["type"] == "makeserver":
if i["disconnected"]:
reconnect(session, i["cid"])
return True if d else False
def reconnect(session, cid):
f = rpc_post(session, "reconnect", data=urllib.parse.urlencode({"session": session, "cid": cid}).encode("ascii"))
d = json.loads(getresponse(f))
def _print(*objects, reporter=None, sep=" ", begin="", end="\n", file=sys.stdout, flush=False):
try: # python 3.3 or later
print("%s[%s]" % (begin, (reporter or email or "(unknown)")), *objects, sep=sep, end=end, file=file, flush=flush)
except TypeError:
print("%s[%s]" % (begin, (reporter or email or "(unknown)")), *objects, sep=sep, end=end, file=file)
if flush:
file.flush()
def _identify(clear=False):
global email
global password
oe = email
try:
if clear:
email = None
password = None
while not email:
email = input("email: ")
while not password:
password = getpass.getpass("password: ")
except KeyboardInterrupt:
_print("terminating...", reporter=oe, begin="\n")
sys.exit(0)
def _run():
global stat_user
while True:
err = 0
stat_user = None
try:
_print("connecting...")
stream(login(email, password, auth_formtoken()))
except KeyboardInterrupt:
_print("terminating...")
sys.exit(0)
except urllib.error.HTTPError as e:
err = e.code
except:
_print(begin="\n")
traceback.print_exc()
print()
try:
if stat_user:
zombie = stat_user["limits"]["zombiehours"] * 60 - 10
if zombie < 0:
zombie = 1430
_print("waiting for %d minutes..." % zombie)
time.sleep(zombie * 60)
elif err == 400:
_print("bad request.")
_identify(True)
elif err == 401:
_print("unauthorised.")
_identify(True)
else:
_print("waiting for 90 seconds...")
time.sleep(90)
except KeyboardInterrupt:
_print("terminating...", begin="\n")
sys.exit(0)
def _main():
global email, password
if len(sys.argv) > 2:
password = sys.argv[2]
if len(sys.argv) > 1:
email = sys.argv[1]
_identify()
_run()
if __name__ == "__main__":
_main()