<commit_before>"""
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:
	<NAME>
	<NAME>
	<NAME>
	<NAME>
"""

#$Id$

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


## Glowna klasa do obslugi protokolu gg. 
# Umozliwia podstawowe operacje. Tworzy sesje obslugi protokolu.
#
class GGSession(EventsList):	
		## Konstruktor dla sesji gg.
		#
		#\param uin	                          nr gadu-gadu, dla ktorego bedzie tworzona sesja
		#\param password	                 haslo dla nr gadu-gadu
		#\param initial_status           poczatkowy status dostepnosci
		#\param initial_description     poczatkowy opis 
		#
	def __init__(self, uin, password, initial_status = GGStatuses.Avail, initial_description = '', contacts_list = None):
		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'])
		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
	
	## Metoda powoduje uruchomienie listenera
	#
	def __events_loop(self):
		while self.__logged:
			header = GGHeader()
			try: #TODO: yyyyy ugly ;)  (przy wylogowaniu wyjatek leci)
				header.read(self.__connection)
			except:
				return
			if header.type == GGIncomingPackets.GGRecvMsg:
				in_packet = GGRecvMsg()
				in_packet.read(self.__connection, header.length)
				self.on_msg_recv(self, (in_packet.sender, in_packet.seq, in_packet.time, in_packet.msg_class, in_packet.message))

			elif header.type == GGIncomingPackets.GGSendMsgAck:
				in_packet = GGSendMsgAck()
				in_packet.read(self.__connection, header.length)
				self.on_send_msg_ack(self, (in_packet.status, in_packet.recipient, in_packet.seq))

			elif header.type == GGIncomingPackets.GGNotifyReplyOld:
				in_packet = GGNotifyReplyOld(self.__contacts_list)
				in_packet.read(self.__connection, header.length)
				self.on_notify_reply(self, self.__contacts_list)

			elif header.type == GGIncomingPackets.GGNotifyReply60 or header.type == GGIncomingPackets.GGNotifyReply77:
				in_packet = GGNotifyReply(self.__contacts_list, header.type)
				in_packet.read(self.__connection, header.length)
				self.__contacts_list = in_packet.contacts
				self.on_notify_reply(self, self.__contacts_list)

			elif header.type == GGIncomingPackets.GGPubDir50Reply:
				in_packet = GGPubDir50Reply()
				in_packet.read(self.__connection, header.length)
				self.on_pubdir_recv(self, (in_packet.reqtype, in_packet.seq, in_packet.reply))

			elif header.type == GGIncomingPackets.GGDisconnecting:
				in_packet = GGDisconnecting()
				in_packet.read(self.__connection, header.length)
				self.login() # po rozlaczeniu przez serwer laczymy sie ponownie

			elif header.type == GGIncomingPackets.GGUserListReply:
				in_packet = GGUserListReply()
				in_packet.read(self.__connection, header.length)
				self.on_userlist_reply(self, (in_packet.reqtype, in_packet.request))
				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

			elif header.type == GGIncomingPackets.GGStatus:
				in_packet = GGStatus()
				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

			elif header.type == GGIncomngPackets.GGStatus60:
				in_packet = GGStatus60()
				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.__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<commit_msg>on_status_changed<commit_after>"""
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:
	<NAME>
	<NAME>
	<NAME>
	<NAME>
"""

#$Id$

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


## Glowna klasa do obslugi protokolu gg. 
# Umozliwia podstawowe operacje. Tworzy sesje obslugi protokolu.
#
class GGSession(EventsList):	
		## Konstruktor dla sesji gg.
		#
		#\param uin	                          nr gadu-gadu, dla ktorego bedzie tworzona sesja
		#\param password	                 haslo dla nr gadu-gadu
		#\param initial_status           poczatkowy status dostepnosci
		#\param initial_description     poczatkowy opis 
		#
	def __init__(self, uin, password, initial_status = GGStatuses.Avail, initial_description = '', contacts_list = None):
		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'])
		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
	
	## Metoda powoduje uruchomienie listenera
	#
	def __events_loop(self):
		while self.__logged:
			header = GGHeader()
			try: #TODO: yyyyy ugly ;)  (przy wylogowaniu wyjatek leci)
				header.read(self.__connection)
			except:
				return
			if header.type == GGIncomingPackets.GGRecvMsg:
				in_packet = GGRecvMsg()
				in_packet.read(self.__connection, header.length)
				self.on_msg_recv(self, (in_packet.sender, in_packet.seq, in_packet.time, in_packet.msg_class, in_packet.message))

			elif header.type == GGIncomingPackets.GGSendMsgAck:
				in_packet = GGSendMsgAck()
				in_packet.read(self.__connection, header.length)
				self.on_send_msg_ack(self, (in_packet.status, in_packet.recipient, in_packet.seq))

			elif header.type == GGIncomingPackets.GGNotifyReplyOld:
				in_packet = GGNotifyReplyOld(self.__contacts_list)
				in_packet.read(self.__connection, header.length)
				self.on_notify_reply(self, self.__contacts_list)

			elif header.type == GGIncomingPackets.GGNotifyReply60 or header.type == GGIncomingPackets.GGNotifyReply77:
				in_packet = GGNotifyReply(self.__contacts_list, header.type)
				in_packet.read(self.__connection, header.length)
				self.__contacts_list = in_packet.contacts
				self.on_notify_reply(self, self.__contacts_list)

			elif header.type == GGIncomingPackets.GGPubDir50Reply:
				in_packet = GGPubDir50Reply()
				in_packet.read(self.__connection, header.length)
				self.on_pubdir_recv(self, (in_packet.reqtype, in_packet.seq, in_packet.reply))

			elif header.type == GGIncomingPackets.GGDisconnecting:
				in_packet = GGDisconnecting()
				in_packet.read(self.__connection, header.length)
				self.login() # po rozlaczeniu przez serwer laczymy sie ponownie

			elif header.type == GGIncomingPackets.GGUserListReply:
				in_packet = GGUserListReply()
				in_packet.read(self.__connection, header.length)
				self.on_userlist_reply(self, (in_packet.reqtype, in_packet.request))
				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

			elif header.type == GGIncomingPackets.GGStatus:
				in_packet = GGStatus()
				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, (self.__contacts_list[uin],))

			elif header.type == GGIncomngPackets.GGStatus60:
				in_packet = GGStatus60()
				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.__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, (self.__contacts_list[uin],))
{'fraction_non_alphanumeric': 0.09196538936959209, 'fraction_numerical': 0.006427688504326329, 'mean_word_length': 4.466666666666667, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3539585', 'n_tokens_mistral': 4670, 'n_tokens_neox': 4086, 'n_words': 810}