-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpygglib.py
492 lines (428 loc) · 17.7 KB
/
pygglib.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
"""
Biblioteka sluzaca do osblugi protokolu Gadu-Gadu (http://www.gadu-gadu.pl).
Biblioteka powstala dzieki opisie protokolu ze strony:
http://ekg.chmurka.net/docs/protocol.html
Autorzy:
Marek Chrusciel
Jakub Kosinski
Marcin Krupowicz
Mateusz Strycharski
"""
from __future__ import with_statement
from IncomingPackets import *
from OutgoingPackets import *
from HeaderPacket import GGHeader
from Helpers import *
from GGConstans import *
from Networking import Connection
from Exceptions import *
from Contacts import *
from HTTPServices import *
import types
import threading
import thread
from threading import Timer
import time
class GGSession(EventsList):
"""
Glowna klasa do obslugi protokolu gg. Jest to pojedyncza sesja obslugujaca protokol.
Pola publiczne:
* contacts_list - zawiera liste kontaktow zalogowanego uzytkownika.
Aby utworzyc obiekt tej klasy nalezy przekazac do konstruktora nastepujace parametry:
* uin - numer gadu-gadu, dla ktorego bedzie tworzona sesja
* password - haslo dla numeru gadu-gadu uin
* initial_status - poczatkowy status dostepnosci (wartosc domyslna - GGStatuses.Avail)
* initial_description - poczatkowy opis (wartosc domyslna - '')
* contacts_list - lista kontaktow (wartosc domyslna - None)
Przyklad uzycia:
1. GGSession(1111111, 'kaczka', , 'moj nowy opis', );
Tworzy nowy obiekt sesji dla uzytkownika o numerze 1111111 z haslem kaczka i z poczatkowym statusem 'moj nowy opis'
"""
def __init__(self, uin, password, initial_status = GGStatuses.Avail, initial_description = '', contacts_list = ContactsList()):
assert type(uin) == types.IntType
assert type(password) == types.StringType
assert initial_status in GGStatuses
assert type(initial_description) == types.StringType and len(initial_description) <= 70
assert type(contacts_list) == ContactsList or contacts_list == None
EventsList.__init__(self, ['on_login_ok', 'on_login_failed', 'on_need_email', 'on_msg_recv', \
'on_unknown_packet', 'on_send_msg_ack', 'on_notify_reply', 'on_pubdir_recv', 'on_userlist_reply', \
'on_status_changed', 'on_disconnecting', 'on_server_not_operating'])
self.__uin = uin
self.__password = password
self.__status = initial_status
self.__description = initial_description
self.__contacts_list = contacts_list
self.__importing = False # informuje, czy aktualnie importujemy liste kontaktow z serwera Gadu-Gadu
self.__contact_buffer = "" # bufor na importowane z serwera kontakty
self.__local_ip = "127.0.0.1"
self.__local_port = 1550
self.__external_ip = "127.0.0.1"
self.__external_port = 0
self.__image_size = 255
self.__connected = False # czy jestesmy polaczeni z serwerem
self.__logged = False # czy uzytkownik jest zalogowany do serwera
self.__connection = None
self.__pinger = Timer(120.0, self.__ping) # co 2 minuty pingujemy serwer
self.__events_thread = threading.Thread(target = self.__events_loop)
self.__lock = threading.RLock() #blokada dla watku
def __get_contacts_list(self):
return self.__contacts_list
def __set_contacts_list(self, contacts_list):
assert type(contacts_list) == ContactsList
self.__contacts_list = contacts_list
def __events_loop(self):
"""
Metoda powoduje uruchomienie listenera
"""
while self.__logged:
header = GGHeader()
try:
header.read(self.__connection)
except: #paskudnie, ale coz... ;) Na koniec sesji to jest potrzebne
break
if header.type == GGIncomingPackets.GGRecvMsg:
in_packet = GGRecvMsg()
with self.__lock:
in_packet.read(self.__connection, header.length)
self.on_msg_recv(self, EventArgs({\
"sender" : in_packet.sender,\
"seq" : in_packet.seq,\
"time" : in_packet.time,\
"msg_class" : in_packet.msg_class,\
"message" : in_packet.message}))
elif header.type == GGIncomingPackets.GGSendMsgAck:
in_packet = GGSendMsgAck()
with self.__lock:
in_packet.read(self.__connection, header.length)
self.on_send_msg_ack(self, EventArgs({\
"status" : in_packet.status,\
"recipient" : in_packet.recipient,\
"seq" : in_packet.seq}))
elif header.type == GGIncomingPackets.GGNotifyReplyOld:
in_packet = GGNotifyReplyOld(self.__contacts_list)
with self.__lock:
in_packet.read(self.__connection, header.length)
self.__contacts_list = in_packet.contacts
self.on_notify_reply(self, EventArgs({"contacts_list" : self.__contacts_list}))
elif header.type == GGIncomingPackets.GGNotifyReply60 or header.type == GGIncomingPackets.GGNotifyReply77:
in_packet = GGNotifyReply(self.__contacts_list, header.type)
with self.__lock:
in_packet.read(self.__connection, header.length)
self.__contacts_list = in_packet.contacts
self.on_notify_reply(self, EventArgs({"contacts_list" : self.__contacts_list}))
elif header.type == GGIncomingPackets.GGPubDir50Reply:
in_packet = GGPubDir50Reply()
with self.__lock:
in_packet.read(self.__connection, header.length)
self.on_pubdir_recv(self, EventArgs({\
"req_type" : in_packet.reqtype,\
"seq" : in_packet.seq,\
"reply" : in_packet.reply}))
elif header.type == GGIncomingPackets.GGDisconnecting:
in_packet = GGDisconnecting()
with self.__lock:
in_packet.read(self.__connection, header.length)
self.on_disconnecting(self, EventArgs({}))
elif header.type == GGIncomingPackets.GGUserListReply:
in_packet = GGUserListReply()
with self.__lock:
in_packet.read(self.__connection, header.length)
if in_packet.reqtype == GGUserListReplyTypes.GetMoreReply:
self.__contact_buffer += in_packet.request
if in_packet.reqtype == GGUserListReplyTypes.GetReply:
self.__importing = False # zaimportowano cala liste
self.__contact_buffer += in_packet.request #... bo lista moze przyjsc w kilku pakietach
self.__make_contacts_list(self.__contact_buffer)
self.__contact_buffer = "" # oprozniamy bufor
self.on_userlist_reply(self, EventArgs({"contacts_list" : self.__contacts_list}))
else:
self.on_userlist_reply(self, EventArgs({"reqtype":in_packet.reqtype, "request":in_packet.request}))
elif header.type == GGIncomingPackets.GGStatus:
in_packet = GGStatus()
with self.__lock:
in_packet.read(self.__connection, header.length)
uin = in_packet.uin
self.__contacts_list[uin].status = in_packet.status
self.__contacts_list[uin].description = in_packet.description
self.__contacts_list[uin].return_time = in_packet.return_time
self.on_status_changed(self, EventArgs({"contact" : self.__contacts_list[uin]}))
elif header.type == GGIncomingPackets.GGStatus60:
in_packet = GGStatus60()
with self.__lock:
in_packet.read(self.__connection, header.length)
uin = in_packet.uin
if self.__contacts_list[uin] == None:
self.__contacts_list.add_contact(Contact({"uin":in_packet.uin}))
self.__contacts_list[uin].status = in_packet.status
self.__contacts_list[uin].description = in_packet.description
self.__contacts_list[uin].return_time = in_packet.return_time
self.__contacts_list[uin].ip = in_packet.ip
self.__contacts_list[uin].port = in_packet.port
self.__contacts_list[uin].version = in_packet.version
self.__contacts_list[uin].image_size = in_packet.image_size
self.on_status_changed(self, EventArgs({"contact" : self.__contacts_list[uin]}))
else:
with self.__lock:
self.__connection.read(header.length) #odbieramy smieci.. ;)
self.on_unknown_packet(self, EventArgs({"type" : header.type, "length" : header.length}))
time.sleep(0.1)
def __ping(self):
"""
Metoda wysyla pakiet GGPing do serwera
"""
if not self.__logged:
raise GGNotLogged
with self.__lock:
out_packet = GGPing()
out_packet.send(self.__connection)
print '[PING]'
self.__pinger.cancel()
self.__pinger = Timer(120.0, self.__ping) # co 2 minuty pingujemy serwer
self.__pinger.start()
def login(self):
"""
Metoda loguje uzytkownika do sieci gadu-gadu. Parametry podawane sa w konstruktorze.
"""
limit = 7 #tyle pobierze nowych serwerow zanim zaprzestanie prob
times = 0 #ile razy juz pobieral nowy serwer
with self.__lock:
while not self.__connected:
server, port = HTTPServices.get_server(self.__uin)
try:
self.__connection = Connection(server, port)
self.__connected = True
except GGServerNotOperating:
times += 1
if times >= limit:
self.on_server_not_operating(self, EventArgs({}))
return
#else niech pobierze inny serwer i probuje dalej :-)
header = GGHeader()
header.read(self.__connection)
if header.type != GGIncomingPackets.GGWelcome:
raise GGUnexceptedPacket((header.type, header.length))
in_packet = GGWelcome()
in_packet.read(self.__connection, header.length)
seed = in_packet.seed
out_packet = GGLogin(self.__uin, self.__password, self.__status, seed, self.__description, self.__local_ip, \
self.__local_port, self.__external_ip, self.__external_port, self.__image_size)
out_packet.send(self.__connection)
header.read(self.__connection)
if header.type == GGIncomingPackets.GGLoginOK:
self.__logged = True
in_packet = GGLoginOK()
in_packet.read(self.__connection, header.length)
self.on_login_ok(self, EventArgs({}))
self.__pinger.start()
self.__events_thread.start() #uruchamiamy watek listenera
time.sleep(0.5) #TODO: potrzebne to?
self.__send_contacts_list()
#self.change_status(self.__status, self.__description) #ustawienie statusu przy pakiecie GGLogin cos nie dziala :/
elif header.type == GGIncomingPackets.GGLoginFailed:
self.on_login_failed(self, EventArgs({}))
elif header.type == GGIncomingPackets.GGNeedEMail:
self.on_need_email(self, EventArgs({}))
elif header.type == GGIncomingPackets.GGDisconnecting:
self.on_disconnecting(self, EventArgs({}))
else:
raise GGUnexceptedPacket((header.type, header.length))
def logout(self, description = ''):
"""
Metoda wylogowuje uzytkownika z sieci gadu-gadu. Ustawiany jest opis zawarty w parametrze description.
Domyslnie opis pusty.
"""
assert type(description) == types.StringType and len(description) <= 70
if not self.__logged:
raise GGNotLogged
self.change_status(description == '' and GGStatuses.NotAvail or GGStatuses.NotAvailDescr, description)
#with self.__lock:
self.__connection.disconnect()
self.__logged = False # przed join(), zeby zakonczyc watek
self.__pinger.join()
self.__events_thread.join()
self.__pinger.cancel()
#self.__connection.disconnect()
self.__connected = False
def change_status(self, status, description = ""):
"""
Metoda powoduje zmiane statusu i opisu. Jako parametry przyjmuje nowy status i nowy opis (domyslnie - opis pusty).
"""
assert type(status) == types.IntType and status in GGStatuses
assert type(description) == types.StringType and len(description) <= 70
if not self.__logged:
raise GGNotLogged
with self.__lock:
out_packet = GGNewStatus(status, description)
out_packet.send(self.__connection)
self.__status = status
self.__description = description
def change_description(self, description):
"""
Metoda powoduje zmiane opisu. Jako parametr przyjmuje nowy opis.
"""
assert type(description) == types.StringType and len(description) <= 70
if self.__status != GGStatuses.AvailDescr and self.__status != GGStatuses.BusyDescr and self.__status != GGStatuses.InvisibleDescr:
raise GGException("Can't change description - current status has'n description")
if not self.__logged:
raise GGNotLogged
self.change_status(self.__status, description)
def send_msg(self, rcpt, msg, seq = 0, msg_class = GGMsgTypes.Msg, richtext = False):
"""
Metoda sluzy do wysylania wiadomosci w siecie gadu-gadu. Parametry:
* rcpt - numer gadu-gadu odbiorcy wiadomosci
* msg - wiadomosc do dostarczenia
* seq - numer sekwencyjny wiadomosci, sluzy do potwierdzen dostarczenia wiadomosci. Domyslnie wartosc 0
* msg_class - klasa wiadomosci (typ GGMsgTypes). Domyslnie wiadomosc pojawia sie w nowym oknie
* richtext - okresla czy wiadomosc bedzie interpretowana jako zwykly tekst czy jako tekst formatowany.
Domyslnie nieformatowany
"""
assert type(rcpt) == types.IntType
assert type(msg) == types.StringType and ((not richtext and len(msg) < 2000) or (richtext)) #TODO: w dalszych iteracjach: obsluga richtextmsg
assert type(seq) == types.IntType
assert msg_class in GGMsgTypes
if not self.__logged:
raise GGNotLogged
if richtext:
message = Helpers.pygglib_rtf_to_gg_rtf(msg)
else:
message = msg
with self.__lock:
out_packet = GGSendMsg(rcpt, message, seq, msg_class)
out_packet.send(self.__connection)
def pubdir_request(self, request, reqtype = GGPubDirTypes.Search):
"""
Metoda obslugujaca katalog publiczny. Wysyla zapytanie do serwera. Parametry:
* request - zapytanie dla serwera
* reqtype - typ zapytania
"""
assert type(request) == types.StringType or type(request) == types.DictType
assert reqtype in GGPubDirTypes
if not self.__logged:
raise GGNotLogged
with self.__lock:
out_packet = GGPubDir50Request(request, reqtype)
out_packet.send(self.__connection)
def export_contacts_list(self, filename = None):
"""
Eksportuje liste kontaktow do serwera lub do pliku, w przypadku podania nazwy jako parametr filename
"""
while self.__importing == True:
time.sleep(0.1)
if self.__contacts_list != None:
if filename == None:
if not self.__logged:
raise GGNotLogged
sub_lists = Helpers.split_list(self.__contacts_list.export_request_string(), 2038)
with self.__lock:
out_packet = GGUserListRequest(GGUserListTypes.Put, sub_lists[0])
out_packet.send(self.__connection)
if len(sub_lists) > 1:
for l in sub_lists[1:len(sub_lists)]:
out_packet = GGUserListRequest(GGUserListTypes.PutMore, l)
out_packet.send(self.__connection)
else:
assert type(filename) == types.StringType
request = self.__contacts_list.export_request_string()
file = open(filename, "w")
file.write(request)
file.close()
def delete_contacts_from_server(self):
"""
Usuwa liste kontaktow z serwera Gadu-Gadu
"""
if not self.__logged:
raise GGNotLogged
with self.__lock:
out_packet = GGUserListRequest(GGUserListTypes.Put, "")
out_packet.send(self.__connection)
def import_contacts_list(self, filename = None):
"""
Wysyla zadanie importu listy z serwera lub pliku, gdy podamy jego nazwe w parametrze filename.
Zaimportowana lista zapisywana jest w self.__contacts_list
"""
if filename == None:
if not self.__logged:
raise GGNotLogged
with self.__lock:
out_packet = GGUserListRequest(GGUserListTypes.Get, "")
out_packet.send(self.__connection)
self.__importing = True
else:
assert type(filename) == types.StringType
file = open(filename, "r")
request = file.read()
file.close()
self.__make_contacts_list(request)
def add_contact(self, contact, user_type = 0x3, notify = True):
"""
Dodajemy kontakt 'contact' do listy kontaktow. Jesli jestesmy polaczeni z serwerem i notify == True to dodatkowo
powiadamiamy o tym fakcie serwer. Od tego momentu serwer bedzie nas informowal o statusie tego kontaktu.
"""
assert type(contact) == Contact
self.__contacts_list.add_contact(contact)
if self.__logged and notify:
with self.__lock:
out_packet = GGAddNotify(contact.uin, user_type)
out_packet.send(self.__connection)
def remove_contact(self, uin, notify = True):
"""
Usuwamy z listy kontaktow kontakt o numerze 'uin'. Jesli jestesmy polaczeni z serwerem i notify == True to dodatkowo
powiadamiamy o tym fakcie serwer. Od tego momentu serwer nie bedzie nas juz informowal o statusie tego kontaktu.
"""
self.__contacts_list.remove_contact(uin)
if self.__logged and notify:
with self.__lock:
out_packet = GGRemoveNotify(uin)
out_packet.send(self.__connection)
def change_user_type(self, uin, user_type):
"""
Zmieniamy typ uzytkownika. user_type jest mapa wartosci z GGUserTypes.
Np. zeby zablokowac uzytkownka piszemy:
change_user_type(12454354, GGUserTypes.Blocked)
"""
if not self.__logged:
raise GGNotLogged
with self.__lock:
out_packet = GGRemoveNotify(uin, user_type)
out_packet.send(self.__connection)
def __send_contacts_list(self):
"""
Wysyla do serwera nasza liste kontaktow w celu otrzymania statusow.
Powinno byc uzyte zaraz po zalogowaniu sie do serwera.
UWAGA: To nie jest eksport listy kontaktow do serwera!
"""
assert self.__contacts_list == None or type(self.__contacts_list) == ContactsList
if not self.__logged:
raise GGNotLogged
if self.__contacts_list == None or len(self.__contacts_list) == 0:
with self.__lock:
out_packet = GGListEmpty()
out_packet.send(self.__connection)
return
uin_type_list = [] # [(uin, type), (uin, type), ...]
for contact in self.__contacts_list.data: #TODO: brrrrrrr, nie .data!!!!
uin_type_list.append((contact.uin, contact.user_type))
sub_lists = Helpers.split_list(uin_type_list, 400)
with self.__lock:
for l in sub_lists[:-1]: #zostawiamy ostatnia podliste
out_packet = GGNotifyFirst(l)
out_packet.send(self.__connection)
# zostala juz ostatnia lista do wyslania
out_packet = GGNotifyLast(sub_lists[-1])
out_packet.send(self.__connection)
def __make_contacts_list(self, request):
contacts = request.split("\n")
if self.__contacts_list == None:
self.__contacts_list = ContactsList()
for contact in contacts:
#TODO: needs to be fixed: groups
if contact != '' and contact != "\n" and contact != "GG70ExportString,;\r":
newcontact = Contact({'request_string':contact})
self.add_contact(newcontact)
def __get_logged(self):
return self.__logged
#
# Properties
#
contacts_list = property(__get_contacts_list, __set_contacts_list)
logged = property(__get_logged)