-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
197 lines (184 loc) · 6.11 KB
/
client.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import request
import json
import time
from threading import Thread
class moodle_chat_pool_thread(Thread):
should_leave_chat = False
def __init__(self, client):
Thread.__init__(self)
self.daemon = True
self.client = client
def run(self):
time.sleep(1)
while not(self.should_leave_chat):
self.client.pull_chat()
time.sleep(1)
class moodle_client():
logged_in = False
ín_chat = False
chat_last_row = 0
chat_last_time = 0
chat_message_history = {}
chat_event_on_message = None
chat_event_parse = None
chat_event_user = None
chat_event_join = None
chat_event_leave = None
chat_event_pull = None
def __init__(self, user, password, url):
self.user = user
self.password = password
self.url = url
def update_cookie_if_needed(self, req):
if (req != False):
cookie = req.get_header("Set-Cookie")
if (cookie != None):
session_cookie_pos = cookie.find("MoodleSessionmoodle04=")
if (session_cookie_pos != -1):
self.session_cookie = cookie[(session_cookie_pos + 22):(session_cookie_pos + 48)]
def moodle_http_request(self, url, method, body=None, redirect=False, force_http=False, anonymous=False):
headers = {}
if (self.logged_in and not(anonymous)):
headers["Cookie"] = "MoodleSessionmoodle04=" + self.session_cookie
req = request.make_http_request(url, method, headers, body, redirect, force_http)
self.update_cookie_if_needed(req)
return req
def moodle_http_form_request(self, url, parameters={}, redirect=False, force_http=False, anonymous=False):
headers = {}
if (self.logged_in and not(anonymous)):
headers["Cookie"] = "MoodleSessionmoodle04=" + self.session_cookie
req = request.make_http_form_request(url, headers, parameters, redirect, force_http)
self.update_cookie_if_needed(req)
return req
def chat_api_interact(self, action, data={}):
if (self.in_chat == False):
return False
ajax_url = self.url + "/mod/chat/chat_ajax.php?sesskey=" + self.sesskey
data["chat_sid"] = self.chat_sid
data["theme"] = "course_theme"
data["action"] = action
req = self.moodle_http_form_request(ajax_url, data)
if (req == False):
return False
if (req.status_code == 200):
return req
return False
def login(self):
search_key = "name=\"logintoken\" value=\""
first_request = self.moodle_http_request(self.url + "/login/index.php", "GET")
if (first_request == False):
return False
if (first_request.status_code != 200):
return False
login_token_start = first_request.body.find(search_key) + 25
login_token = first_request.body[login_token_start:(login_token_start + 32)]
parameters = {
"username": self.user,
"password": self.password,
"logintoken": login_token,
"anchor": ""
}
self.logged_in = True
final_request = self.moodle_http_form_request(self.url + "/login/index.php", parameters)
if (final_request == False):
self.logged_in = False
return False
if (final_request.status_code == 303):
if (final_request.get_header("Location").find("testsession") != -1):
return True
self.logged_in = False
return False
def join_chat(self, url):
if (self.logged_in == False):
return False
chat_sid_search_key = "\"sid\":\""
sesskey_search_key = "\"sesskey\":\""
get_data_request = self.moodle_http_request(url, "GET", redirect=True)
if (get_data_request == False):
return False
chat_sid_start = get_data_request.body.find(chat_sid_search_key) + 7
chat_sid = get_data_request.body[chat_sid_start:(chat_sid_start + 32)]
sesskey_start = get_data_request.body.find(sesskey_search_key) + 11
sesskey = get_data_request.body[sesskey_start:(sesskey_start + 10)]
if (get_data_request.status_code == 200):
self.sesskey = sesskey
self.chat_sid = chat_sid
self.in_chat = True
init_resp = self.chat_api_interact("init", {"chat_init": 1})
if (init_resp == False):
self.in_chat = False
return False
self.users = json.loads(init_resp.body)["users"]
self.pulling_thread = moodle_chat_pool_thread(self)
self.pulling_thread.should_leave_chat = False
if (self.chat_event_join != None):
self.chat_event_join(self)
self.pulling_thread.start()
return True
return False
def leave_chat(self, wait=False):
if (self.in_chat == False):
return False
if (self.chat_event_leave != None):
self.chat_event_leave(self)
self.pulling_thread.should_leave_chat = True
self.in_chat = False
self.chat_last_row = 0
self.chat_last_time = 0
self.chat_message_history = {}
self.users = {}
if (wait):
self.pulling_thread.join()
return True
def send_message(self, message):
if (self.in_chat == False):
return False
resp = self.chat_api_interact("chat", {"chat_message": message})
if (resp != False):
if (resp == "true"):
return True
return False
def pull_chat(self):
if (self.in_chat == False):
return False
last_row = "false"
if (self.chat_last_row > 0):
last_row = str(self.chat_last_row)
resp = self.chat_api_interact("update", {"chat_lastrow": last_row, "chat_lasttime": self.chat_last_time})
if (resp == False):
return False
parsed = json.loads(resp.body)
if (self.chat_event_pull != None):
self.chat_event_pull(self, parsed)
if ("error" in parsed):
return parsed
self.chat_last_row = parsed["lastrow"]
self.chat_last_time = int(parsed["lasttime"])
if ("users" in parsed):
users = parsed["users"]
if (self.chat_event_user != None):
users_joining = []
users_leaving = []
for user in users:
if (not(user in self.users)):
users_joining.append(user)
for user in self.users:
if (not(user in users)):
users_leaving.append(user)
if (self.chat_event_user != None):
self.chat_event_user(self, users_joining, users_leaving)
self.users = users
new_messages = {}
if ("msgs" in parsed):
msgs = parsed["msgs"]
for msg in msgs:
if (not(msg in self.chat_message_history)):
if (self.chat_event_parse != None):
msgs[msg]["parsed"] = self.chat_event_parse(self, msgs[msg])
else:
msgs[msg]["parsed"] = None
self.chat_message_history[msg] = msgs[msg]
new_messages[msg] = msgs[msg]
if (self.chat_event_on_message != None):
self.chat_event_on_message(self, msgs[msg])
return new_messages