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 __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 dataReceived(self, data): header = GGHeader() header.read(data) #logowanie if header.type == GGIncomingPackets.GGWelcome: in_packet = GGWelcome() in_packet.read(data, header.length) self.seed = in_packet.seed d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_auth_got_seed, self.seed) d = None elif header.type == GGIncomingPackets.GGLoginOK: d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_login_ok) self._send_contacts_list() self._ping() elif header.type == GGIncomingPackets.GGLoginFailed: d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_login_failed) elif header.type == GGIncomingPackets.GGNeedEMail: d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_need_email) elif header.type == GGIncomingPackets.GGDisconnecting: in_packet = GGDisconnecting() in_packet.read(data, header.length) d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_disconnecting) elif header.type == GGIncomingPackets.GGNotifyReply60 or header.type == GGIncomingPackets.GGNotifyReply77: in_packet = GGNotifyReply(self.__contacts_list, header.type) in_packet.read(data, header.length) self.__contacts_list = in_packet.contacts d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_notify_reply, self.__contacts_list) #lista elif header.type == GGIncomingPackets.GGUserListReply: in_packet = GGUserListReply() in_packet.read(data, 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 d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_userlist_reply, self.__contacts_list) else: d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_userlist_exported_or_deleted, in_packet.reqtype, in_packet.request) #wiadomosci elif header.type == GGIncomingPackets.GGRecvMsg: in_packet = GGRecvMsg() in_packet.read(data, header.length) d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_msg_recv, 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(data, header.length) d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_msg_ack, in_packet.status, in_packet.recipient, in_packet.seq) #statusy elif header.type == GGIncomingPackets.GGStatus: in_packet = GGStatus() in_packet.read(data, 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 d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_status, self.__contacts_list[uin]) elif header.type == GGIncomingPackets.GGStatus60: in_packet = GGStatus60() in_packet.read(data, 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 d = defer.Deferred() d.callback(self) d.addCallback(self._conn.on_status60, self.__contacts_list[uin]) else: pass
def sendPacket(self, msg): header = GGHeader() header.read(msg) self.transport.write(msg)
sys.path.append(".\\..") # - dla windowsa else: sys.path.append("../") # - dla linuksa from OutgoingPackets import * from HeaderPacket import GGHeader from IncomingPackets import * from Networking import Connection # # 11327271, haslo eto2007 # if __name__ == "__main__": conn = Connection("217.17.45.153", 8074) header = GGHeader() header.read(conn) if header.type != GGIncomingPackets.GGWelcome: raise "Unexpected packet got!" in_packet = GGWelcome() in_packet.read(conn, header.length) seed = in_packet.seed # ... i mamy juz seeda # Logowanie sie out_packet = GGLogin(11327271, "eto2007", 0x0004, seed, "opis :)") out_packet.send(conn) header.read(conn) print "Got Packet:\n \ttype: %d\n\tlength: %d" % (header.type, header.length) if header.type == GGIncomingPackets.GGLoginOK: print 'Logged in'
sys.path.append(".\\..") # - dla windowsa else: sys.path.append("../") # - dla linuksa from OutgoingPackets import * from HeaderPacket import GGHeader from IncomingPackets import * from Networking import Connection # # 11327271, haslo eto2007 # if __name__ == "__main__": conn = Connection("217.17.45.153", 8074) header = GGHeader() header.read(conn) if header.type != GGIncomingPackets.GGWelcome: raise "Unexpected packet got!" in_packet = GGWelcome() in_packet.read(conn, header.length) seed = in_packet.seed # ... i mamy juz seeda # Logowanie sie out_packet = GGLogin(11327271, "eto2007", 0x0004, seed, "opis :)") out_packet.send(conn) header.read(conn) print "Got Packet:\n \ttype: %d\n\tlength: %d" % (header.type, header.length) if header.type == GGIncomingPackets.GGLoginOK: print 'Logged in' else: