Ejemplo n.º 1
0
    def __init__(self, domain, resource, user, push_name, password):

        self.domain = domain
        self.resource = resource
        self.user = user
        self.push_name = push_name
        self.password = password
        self.jid = user + '@' + domain
        self.fromm = user + '@' + domain + '/' + resource
        self.supports_receipt_acks = False
        self.msg_id = 0
        self.state = 0  #0 disconnected 1 connecting 2 connected
        self.retry = True
        self.eventHandler = WAEventHandler(self)
        self.conn = MySocketConnection()
        self.stanzaReader = None
        self.login = None

        self.disconnectRequested = False

        self.connTries = 0

        self.verbose = True
        self.iqId = 0
        self.lock = threading.Lock()

        self.waiting = 0

        #super(WAXMPP,self).__init__();

        self.eventHandler.initialConnCheck()
Ejemplo n.º 2
0
	def __init__(self,domain,resource,user,push_name,password):
		
		WADebug.attach(self);
		
		self.domain = domain;
		self.resource = resource;
		self.user=user;
		self.push_name=push_name;
		self.password = password;
		self.jid = user+'@'+domain
		self.fromm = user+'@'+domain+'/'+resource;
		self.supports_receipt_acks = False;
		self.msg_id = 0;
		self.state = 0 #0 disconnected 1 connecting 2 connected
		self.retry = True
		self.eventHandler = WAEventHandler(self);
		self.conn =MySocketConnection();		
		self.stanzaReader = None
		self.login = None;
		
		self.disconnectRequested = False
		
		self.connTries = 0;
		
		self.verbose = True
		self.iqId = 0;
		self.lock = threading.Lock()
		
		self.waiting = 0;
		
		#super(WAXMPP,self).__init__();
		
		self.eventHandler.initialConnCheck();
Ejemplo n.º 3
0
    def login(self):
        self.conn = MySocketConnection()
        self.inn = BinTreeNodeReader(self.conn, Login.dictionary)
        self.out = BinTreeNodeWriter(self.conn, Login.dictionary)

        self.walogin = Login(self)
        self.walogin.start()
Ejemplo n.º 4
0
    def do_login(self):

        self.conn = conn = MySocketConnection()
        #conn.connect((HOST, PORT));

        self.inn = BinTreeNodeReader(conn, WALogin.dictionary)
        self.out = BinTreeNodeWriter(conn, WALogin.dictionary)

        self.login = WALogin(conn, self.inn, self.out, S40MD5Digest())

        self.login.setConnection(self)

        self.login.loginSuccess.connect(self.onLoginSuccess)
        self.login.loginFailed.connect(self.eventHandler.onLoginFailed)

        self.login.connectionError.connect(self.onConnectionError)
        self.login.start()
        '''try:
Ejemplo n.º 5
0
class WAXMPP():
	message_store = None
	def __init__(self,domain,resource,user,push_name,password):
		
		WADebug.attach(self);
		
		self.domain = domain;
		self.resource = resource;
		self.user=user;
		self.push_name=push_name;
		self.password = password;
		self.jid = user+'@'+domain
		self.fromm = user+'@'+domain+'/'+resource;
		self.supports_receipt_acks = False;
		self.msg_id = 0;
		self.state = 0 #0 disconnected 1 connecting 2 connected
		self.retry = True
		self.eventHandler = WAEventHandler(self);
		self.conn =MySocketConnection();		
		self.stanzaReader = None
		self.login = None;
		
		self.disconnectRequested = False
		
		self.connTries = 0;
		
		self.verbose = True
		self.iqId = 0;
		self.lock = threading.Lock()
		
		self.waiting = 0;
		
		#super(WAXMPP,self).__init__();
		
		self.eventHandler.initialConnCheck();
		
		#self.do_login();
		
	
	
	
	def setContactsManager(self,contactsManager):
		self.contactsManager = contactsManager
		
	def setReceiptAckCapable(self,can):
		#print "Switching to True"
		self.supports_receipt_acks = True;
		#print self.supports_receipt_acks

		
	
	
	
	def onLoginSuccess(self):
		self.changeState(4)
		
		self.connectionTries = 0
		c = StanzaReader(self);
		
		c.setEventHandler(self.eventHandler);
		
		#initial presence
		self.stanzaReader = c
		
		self.stanzaReader.start();
		
		
		self.sendClientConfig('','',False,'');
		self.sendAvailableForChat();
		self.eventHandler.connected.emit();
		
		

	def onConnectionError(self):
		self.login.wait()
		self.conn.close()	

		self.changeState(3)
		
		'''
		if self.connTries < 4:
			print "trying connect "+str(self.connTries)
			self.retryLogin()
		else:
			print "Too many tries, trying in 30000"
			t = QTimer.singleShot(30000,self.retryLogin)
		'''
	
	
	
	def disconnect(self):
		self.conn.close()
	
	def retryLogin(self):
		self.changeState(3);
	
	def changeState(self,newState):
		self._d("Entering critical area")
		self.waiting+=1
		self.lock.acquire()
		self.waiting-=1
		self._d("inside critical area")
		
		if self.state == 0:
			if newState == 2:
				self.state = 1
				self.do_login();
				
		elif self.state == 1:
			#raise Exception("mutex violated! I SHOULDN'T BE HERE !!!")
			if newState == 0:
				self.retry = False
			elif newState == 2:
				self.retry = True
			elif newState == 3: #failed
				if self.retry:
					
					
					if self.connTries >= 3:
						self._d("%i or more failed connections. Will try again in 30 seconds" % self.connTries)
						QTimer.singleShot(30000,self.retryLogin)
						self.connTries-=1
						
					else:	
						self.do_login()
						self.connTries+=1
				else:
					self.connTries = 0
					self.state = 0
					self.retry = True
					
			elif newState == 4:#connected
				self.connTries = 0
				self.retry = True
				self.state = 2
		elif self.state == 2:
			if newState == 2:
				self.disconnect()
				self.state = 1
				self.do_login()
			elif newState == 0:
				self.disconnect()
				self.state = 0
		
		
		self._d("Releasing lock")
		self.lock.release()
		
		
			

	def do_login(self):
		
		self.conn = conn = MySocketConnection();
		#conn.connect((HOST, PORT));

		self.inn = BinTreeNodeReader(conn,WALogin.dictionary);
		self.out = BinTreeNodeWriter(conn,WALogin.dictionary);
		
		
		self.login = WALogin(conn,self.inn,self.out,S40MD5Digest());
		
		
		self.login.setConnection(self);
		
		self.login.loginSuccess.connect(self.onLoginSuccess)
		self.login.loginFailed.connect(self.eventHandler.onLoginFailed);
		
		self.login.connectionError.connect(self.onConnectionError)
		self.login.start();
		
		'''try:
			self.login.login();
		except:
			print "LOGIN FAILED"
			#sys.exit()
			return
		'''
		



		#fmsg = FMessage();
		#fmsg.setData('*****@*****.**',True,"Hello World");
		
		#self.sendIq();
		#self.inn.nextTree();
		#print self.inn.inn.buf;
		#exit();
		#self.inn.nextTree();
		
		
		
		#self.sendMessageWithBody("ok");
		#node = self.inn.nextTree();
		#print node.toString();

		#self.sendSubscribe("*****@*****.**");
		
		#self.sendMessageWithBody("OK");
		#self.sendMessageWithBody("OK");
		#node = self.inn.nextTree();
		#print node.toString();
		#raw_input();
		#self.sendMessageWithBody(fmsg);
	
	
	def resendUnsent(self):
		'''
			Resends all unsent messages, should invoke on connect
		'''
		
		
		messages = WAXMPP.message_store.getUnsent();
		self._d("Resending %i old messages"%(len(messages)))
		for m in messages:
			self.sendMessageWithBody(m);
		
		
	
	def sendTyping(self,jid):
		self._d("SEND TYPING TO JID")
		composing = ProtocolTreeNode("composing",{"xmlns":"http://jabber.org/protocol/chatstates"})
		message = ProtocolTreeNode("message",{"to":jid,"type":"chat"},[composing]);
		self.out.write(message);
		
	
		
	def sendPaused(self,jid):
		self._d("SEND PAUSED TO JID")
		composing = ProtocolTreeNode("paused",{"xmlns":"http://jabber.org/protocol/chatstates"})
		message = ProtocolTreeNode("message",{"to":jid,"type":"chat"},[composing]);
		self.out.write(message);

	
	
	def getSubjectMessage(self,to,msg_id,child):
		messageNode = ProtocolTreeNode("message",{"to":to,"type":"subject","id":msg_id},[child]);
		
		return messageNode
	
	def sendSubjectReceived(self,to,msg_id):
		self._d("Sending subject recv receipt")
		receivedNode = ProtocolTreeNode("received",{"xmlns": "urn:xmpp:receipts"});
		messageNode = self.getSubjectMessage(to,msg_id,receivedNode);
		self.out.write(messageNode);

	def sendMessageReceived(self,fmsg):
		receivedNode = ProtocolTreeNode("received",{"xmlns": "urn:xmpp:receipts"})
		messageNode = ProtocolTreeNode("message",{"to":fmsg.key.remote_jid,"type":"chat","id":fmsg.key.id},[receivedNode]);
		self.out.write(messageNode);


	def sendDeliveredReceiptAck(self,to,msg_id):
		self.out.write(self.getReceiptAck(to,msg_id,"delivered"));
	
	def sendVisibleReceiptAck(self,to,msg_id):
		self.out.write(self.getReceiptAck(to,msg_id,"visible"));
	
	def getReceiptAck(self,to,msg_id,receiptType):
		ackNode = ProtocolTreeNode("ack",{"xmlns":"urn:xmpp:receipts","type":receiptType})
		messageNode = ProtocolTreeNode("message",{"to":to,"type":"chat","id":msg_id},[ackNode]);
		return messageNode;

	def makeId(self,prefix):
		self.iqId += 1
		idx = ""
		if self.verbose:
			idx += prefix + str(self.iqId);
		else:
			idx = "%x" % self.iqId
		
		return idx
		 	
	
	def sendPing(self):
		
		idx = self.makeId("ping_")
		self.stanzaReader.requests[idx] = self.stanzaReader.handlePingResponse;
		
		pingNode = ProtocolTreeNode("ping",{"xmlns":"w:p"});
		iqNode = ProtocolTreeNode("iq",{"id":idx,"type":"get","to":self.domain},[pingNode]);
		self.out.write(iqNode);
		
	
	def sendPong(self,idx):
		iqNode = ProtocolTreeNode("iq",{"type":"result","to":self.domain,"id":idx})
		self.out.write(iqNode);
	
	def getLastOnline(self,jid):
		
		if len(jid.split('-')) == 2: #SUPER CANCEL SUBSCRIBE TO GROUP
			return
		
		self.sendSubscribe(jid);
		
		self._d("presence request Initiated for %s"%(jid))
		idx = self.makeId("last_")
		self.stanzaReader.requests[idx] = self.stanzaReader.handleLastOnline;
		
		query = ProtocolTreeNode("query",{"xmlns":"jabber:iq:last"});
		iqNode = ProtocolTreeNode("iq",{"id":idx,"type":"get","to":jid},[query]);
		self.out.write(iqNode)
	
	
	def sendIq(self):
		node = ProtocolTreeNode("iq",{"to":"g.us","type":"get","id":str(int(time.time()))+"-0"},None,'expired');
		self.out.write(node);

		node = ProtocolTreeNode("iq",{"to":"s.whatsapp.net","type":"set","id":str(int(time.time()))+"-1"},None,'expired');
		self.out.write(node);

	def sendAvailableForChat(self):
		presenceNode = ProtocolTreeNode("presence",{"name":self.push_name})
		self.out.write(presenceNode);
		
	def sendAvailable(self):
		if self.state != 2:
			return
		presenceNode = ProtocolTreeNode("presence",{"type":"available"})
		self.out.write(presenceNode);
	
	
	def sendUnavailable(self):
		if self.state != 2:
			return
		presenceNode = ProtocolTreeNode("presence",{"type":"unavailable"})
		self.out.write(presenceNode);
		

	def sendSubscribe(self,to):
			presenceNode = ProtocolTreeNode("presence",{"type":"subscribe","to":to});
			
			self.out.write(presenceNode);

	def sendMessageWithBody(self,fmsg):
			#bodyNode = ProtocolTreeNode("body",None,message.data);
			#self.out.write(self.getMessageNode(message,bodyNode));

			bodyNode = ProtocolTreeNode("body",None,None,fmsg.content);
			self.out.write(self.getMessageNode(fmsg,bodyNode));
			self.msg_id+=1;

	
	def sendClientConfig(self,sound,pushID,preview,platform):
		idx = self.makeId("config_");
		configNode = ProtocolTreeNode("config",{"xmlns":"urn:xmpp:whatsapp:push","sound":sound,"id":pushID,"preview":"1" if preview else "0","platform":platform})
		iqNode = ProtocolTreeNode("iq",{"id":idx,"type":"set","to":self.domain},[configNode]);
		
		self.out.write(iqNode);
		
	
	
	def sendGetGroupInfo(self,jid):
		idx = self.makeId("get_g_info_")
		self.stanzaReader.requests[idx] = self.stanzaReader.handleGroupInfo;
		
		queryNode = ProtocolTreeNode("query",{"xmlns":"w:g"})
		iqNode = ProtocolTreeNode("iq",{"id":idx,"type":"get","to":jid},[queryNode])
		
		self.out.write(iqNode)

	
	def getMessageNode(self,fmsg,child):
			requestNode = None;
			serverNode = ProtocolTreeNode("server",None);
			xNode = ProtocolTreeNode("x",{"xmlns":"jabber:x:event"},[serverNode]);
			childCount = (0 if requestNode is None else 1) +2;
			messageChildren = [None]*childCount;
			i = 0;
			if requestNode is not None:
				messageChildren[i] = requestNode;
				i+=1;
			#System.currentTimeMillis() / 1000L + "-"+1
			messageChildren[i] = xNode;
			i+=1;
			messageChildren[i]= child;
			i+=1;
			
			key = eval(fmsg.key)
			messageNode = ProtocolTreeNode("message",{"to":key.remote_jid,"type":"chat","id":key.id},messageChildren)
			
			
			return messageNode;
Ejemplo n.º 6
0
class WAXMPP():
    message_store = None

    def __init__(self, domain, resource, user, push_name, password):

        self.domain = domain
        self.resource = resource
        self.user = user
        self.push_name = push_name
        self.password = password
        self.jid = user + '@' + domain
        self.fromm = user + '@' + domain + '/' + resource
        self.supports_receipt_acks = False
        self.msg_id = 0
        self.state = 0  #0 disconnected 1 connecting 2 connected
        self.retry = True
        self.eventHandler = WAEventHandler(self)
        self.conn = MySocketConnection()
        self.stanzaReader = None
        self.login = None

        self.disconnectRequested = False

        self.connTries = 0

        self.verbose = True
        self.iqId = 0
        self.lock = threading.Lock()

        self.waiting = 0

        #super(WAXMPP,self).__init__();

        self.eventHandler.initialConnCheck()

        #self.do_login();

    def setContactsManager(self, contactsManager):
        self.contactsManager = contactsManager

    def setReceiptAckCapable(self, can):
        #print "Switching to True"
        self.supports_receipt_acks = True
        #print self.supports_receipt_acks

    def onLoginSuccess(self):
        self.changeState(4)

        self.connectionTries = 0
        c = StanzaReader(self)

        c.setEventHandler(self.eventHandler)

        #initial presence
        self.stanzaReader = c

        self.stanzaReader.start()

        self.sendClientConfig('', '', False, '')
        self.sendAvailableForChat()
        self.eventHandler.connected.emit()

    def onConnectionError(self):
        self.login.wait()
        self.conn.close()

        self.changeState(3)
        '''
		if self.connTries < 4:
			print "trying connect "+str(self.connTries)
			self.retryLogin()
		else:
			print "Too many tries, trying in 30000"
			t = QTimer.singleShot(30000,self.retryLogin)
		'''

    def disconnect(self):
        self.conn.close()

    def retryLogin(self):
        self.changeState(3)

    def changeState(self, newState):
        print "Entering critical area"
        self.waiting += 1
        self.lock.acquire()
        self.waiting -= 1
        print "inside critical area"

        if self.state == 0:
            if newState == 2:
                self.state = 1
                self.do_login()

        elif self.state == 1:
            #raise Exception("mutex violated! I SHOULDN'T BE HERE !!!")
            if newState == 0:
                self.retry = False
            elif newState == 2:
                self.retry = True
            elif newState == 3:  #failed
                if self.retry:

                    if self.connTries >= 3:
                        print "%i or more failed connections. Will try again in 30 seconds" % self.connTries
                        QTimer.singleShot(30000, self.retryLogin)
                        self.connTries -= 1

                    else:
                        self.do_login()
                        self.connTries += 1
                else:
                    self.connTries = 0
                    self.state = 0
                    self.retry = True

            elif newState == 4:  #connected
                self.connTries = 0
                self.retry = True
                self.state = 2
        elif self.state == 2:
            if newState == 2:
                self.disconnect()
                self.state = 1
                self.do_login()
            elif newState == 0:
                self.disconnect()
                self.state = 0

        print "Releasing lock"
        self.lock.release()

    def do_login(self):

        self.conn = conn = MySocketConnection()
        #conn.connect((HOST, PORT));

        self.inn = BinTreeNodeReader(conn, WALogin.dictionary)
        self.out = BinTreeNodeWriter(conn, WALogin.dictionary)

        self.login = WALogin(conn, self.inn, self.out, S40MD5Digest())

        self.login.setConnection(self)

        self.login.loginSuccess.connect(self.onLoginSuccess)
        self.login.loginFailed.connect(self.eventHandler.onLoginFailed)

        self.login.connectionError.connect(self.onConnectionError)
        self.login.start()
        '''try:
			self.login.login();
		except:
			print "LOGIN FAILED"
			#sys.exit()
			return
		'''

        #fmsg = FMessage();
        #fmsg.setData('*****@*****.**',True,"Hello World");

        #self.sendIq();
        #self.inn.nextTree();
        #print self.inn.inn.buf;
        #exit();
        #self.inn.nextTree();

        #self.sendMessageWithBody("ok");
        #node = self.inn.nextTree();
        #print node.toString();

        #self.sendSubscribe("*****@*****.**");

        #self.sendMessageWithBody("OK");
        #self.sendMessageWithBody("OK");
        #node = self.inn.nextTree();
        #print node.toString();
        #raw_input();
        #self.sendMessageWithBody(fmsg);

    def resendUnsent(self):
        '''
			Resends all unsent messages, should invoke on connect
		'''

        messages = WAXMPP.message_store.getUnsent()
        print "Resending %i old messages" % (len(messages))
        for m in messages:
            self.sendMessageWithBody(m)

    def sendTyping(self, jid):
        print "SEND TYPING TO JID"
        composing = ProtocolTreeNode(
            "composing", {"xmlns": "http://jabber.org/protocol/chatstates"})
        message = ProtocolTreeNode("message", {
            "to": jid,
            "type": "chat"
        }, [composing])
        self.out.write(message)

    def sendPaused(self, jid):
        print "SEND PAUSED TO JID"
        composing = ProtocolTreeNode(
            "paused", {"xmlns": "http://jabber.org/protocol/chatstates"})
        message = ProtocolTreeNode("message", {
            "to": jid,
            "type": "chat"
        }, [composing])
        self.out.write(message)

    def getSubjectMessage(self, to, msg_id, child):
        messageNode = ProtocolTreeNode("message", {
            "to": to,
            "type": "subject",
            "id": msg_id
        }, [child])

        return messageNode

    def sendSubjectReceived(self, to, msg_id):
        print "Sending subject recv receipt"
        receivedNode = ProtocolTreeNode("received",
                                        {"xmlns": "urn:xmpp:receipts"})
        messageNode = self.getSubjectMessage(to, msg_id, receivedNode)
        self.out.write(messageNode)

    def sendMessageReceived(self, fmsg):
        receivedNode = ProtocolTreeNode("received",
                                        {"xmlns": "urn:xmpp:receipts"})
        messageNode = ProtocolTreeNode("message", {
            "to": fmsg.key.remote_jid,
            "type": "chat",
            "id": fmsg.key.id
        }, [receivedNode])
        self.out.write(messageNode)

    def sendDeliveredReceiptAck(self, to, msg_id):
        self.out.write(self.getReceiptAck(to, msg_id, "delivered"))

    def sendVisibleReceiptAck(self, to, msg_id):
        self.out.write(self.getReceiptAck(to, msg_id, "visible"))

    def getReceiptAck(self, to, msg_id, receiptType):
        ackNode = ProtocolTreeNode("ack", {
            "xmlns": "urn:xmpp:receipts",
            "type": receiptType
        })
        messageNode = ProtocolTreeNode("message", {
            "to": to,
            "type": "chat",
            "id": msg_id
        }, [ackNode])
        return messageNode

    def makeId(self, prefix):
        self.iqId += 1
        idx = ""
        if self.verbose:
            idx += prefix + str(self.iqId)
        else:
            idx = "%x" % self.iqId

        return idx

    def sendPing(self):

        idx = self.makeId("ping_")
        self.stanzaReader.requests[idx] = self.stanzaReader.handlePingResponse

        pingNode = ProtocolTreeNode("ping", {"xmlns": "w:p"})
        iqNode = ProtocolTreeNode("iq", {
            "id": idx,
            "type": "get",
            "to": self.domain
        }, [pingNode])
        self.out.write(iqNode)

    def sendPong(self, idx):
        iqNode = ProtocolTreeNode("iq", {
            "type": "result",
            "to": self.domain,
            "id": idx
        })
        self.out.write(iqNode)

    def getLastOnline(self, jid):

        if len(jid.split('-')) == 2:  #SUPER CANCEL SUBSCRIBE TO GROUP
            return

        self.sendSubscribe(jid)

        print "presence request Initiated for %s" % (jid)
        idx = self.makeId("last_")
        self.stanzaReader.requests[idx] = self.stanzaReader.handleLastOnline

        query = ProtocolTreeNode("query", {"xmlns": "jabber:iq:last"})
        iqNode = ProtocolTreeNode("iq", {
            "id": idx,
            "type": "get",
            "to": jid
        }, [query])
        self.out.write(iqNode)

    def sendIq(self):
        node = ProtocolTreeNode("iq", {
            "to": "g.us",
            "type": "get",
            "id": str(int(time.time())) + "-0"
        }, None, 'expired')
        self.out.write(node)

        node = ProtocolTreeNode(
            "iq", {
                "to": "s.whatsapp.net",
                "type": "set",
                "id": str(int(time.time())) + "-1"
            }, None, 'expired')
        self.out.write(node)

    def sendAvailableForChat(self):
        presenceNode = ProtocolTreeNode("presence", {"name": self.push_name})
        self.out.write(presenceNode)

    def sendAvailable(self):
        if self.state != 2:
            return
        presenceNode = ProtocolTreeNode("presence", {"type": "available"})
        self.out.write(presenceNode)

    def sendUnavailable(self):
        if self.state != 2:
            return
        presenceNode = ProtocolTreeNode("presence", {"type": "unavailable"})
        self.out.write(presenceNode)

    def sendSubscribe(self, to):
        presenceNode = ProtocolTreeNode("presence", {
            "type": "subscribe",
            "to": to
        })

        self.out.write(presenceNode)

    def sendMessageWithBody(self, fmsg):
        #bodyNode = ProtocolTreeNode("body",None,message.data);
        #self.out.write(self.getMessageNode(message,bodyNode));

        bodyNode = ProtocolTreeNode("body", None, None, fmsg.content)
        self.out.write(self.getMessageNode(fmsg, bodyNode))
        self.msg_id += 1

    def sendClientConfig(self, sound, pushID, preview, platform):
        idx = self.makeId("config_")
        configNode = ProtocolTreeNode(
            "config", {
                "xmlns": "urn:xmpp:whatsapp:push",
                "sound": sound,
                "id": pushID,
                "preview": "1" if preview else "0",
                "platform": platform
            })
        iqNode = ProtocolTreeNode("iq", {
            "id": idx,
            "type": "set",
            "to": self.domain
        }, [configNode])

        self.out.write(iqNode)

    def getMessageNode(self, fmsg, child):
        requestNode = None
        serverNode = ProtocolTreeNode("server", None)
        xNode = ProtocolTreeNode("x", {"xmlns": "jabber:x:event"},
                                 [serverNode])
        childCount = (0 if requestNode is None else 1) + 2
        messageChildren = [None] * childCount
        i = 0
        if requestNode is not None:
            messageChildren[i] = requestNode
            i += 1
        #System.currentTimeMillis() / 1000L + "-"+1
        messageChildren[i] = xNode
        i += 1
        messageChildren[i] = child
        i += 1

        key = eval(fmsg.key)
        messageNode = ProtocolTreeNode("message", {
            "to": key.remote_jid,
            "type": "chat",
            "id": key.id
        }, messageChildren)

        return messageNode
Ejemplo n.º 7
0
	def push(self,image):
		#image = urllib.quote(image)
		image = image.replace("file://","")

		self.sock = MySocketConnection();
		HOST, PORT = 'mms.whatsapp.net', 443
		self.sock.connect((HOST, PORT));
		ssl_sock = ssl.wrap_socket(self.sock)

		filename = os.path.basename(image)
		filetype = mimetypes.guess_type(filename)[0]
		filesize = os.path.getsize(image)

		if self.resizeImages is True and "image" in filetype:
			user_img = QImage(image)
			preimg = user_img
			if user_img.height() > user_img.width() and user_img.width() > 600:
				preimg = user_img.scaledToWidth(600, Qt.SmoothTransformation)
			elif user_img.height() < user_img.width() and user_img.height() > 800:
				preimg = user_img.scaledToHeight(800, Qt.SmoothTransformation)
			elif user_img.height() == user_img.width() and user_img.height() > 600:
				preimg = user_img.scaled(600, 600, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation)
			preimg.save("/home/user/.cache/wazapp/" + os.path.basename(image))
			image = "/home/user/.cache/wazapp/" + os.path.basename(image)

			filename = os.path.basename(image)
			filetype = mimetypes.guess_type(filename)[0]
			filesize = os.path.getsize(image)

		print "Uploading " + image + " - type: " + filetype + " - resize:" + str(self.resizeImages);


		m = hashlib.md5()
		m.update(filename)
		crypto = m.hexdigest() + os.path.splitext(filename)[1]

		boundary = "-------" + m.hexdigest() #"zzXXzzYYzzXXzzQQ"
		contentLength = 0
		
		hBAOS = bytearray()
		hBAOS += "--" + boundary + "\r\n"
		hBAOS += "Content-Disposition: form-data; name=\"to\"\r\n\r\n"
		hBAOS += self.jid + "\r\n"
		hBAOS += "--" + boundary + "\r\n"
		hBAOS += "Content-Disposition: form-data; name=\"from\"\r\n\r\n"
		hBAOS += self.account.replace("@whatsapp.net","").encode() + "\r\n"

		hBAOS += "--" + boundary + "\r\n"
		hBAOS += "Content-Disposition: form-data; name=\"file\"; filename=\"" + crypto.encode() + "\"\r\n"
		hBAOS += "Content-Type: " + filetype + "\r\n\r\n"

		fBAOS = bytearray()
		fBAOS += "\r\n--" + boundary + "--\r\n"
		
		contentLength += len(hBAOS)
		contentLength += len(fBAOS)
		contentLength += filesize

		userAgent = "WhatsApp/2.8.4 S60Version/5.2 Device/C7-00"

		POST = bytearray()
		POST += "POST https://mms.whatsapp.net/client/iphone/upload.php HTTP/1.1\r\n"
		POST += "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n"
		POST += "Host: mms.whatsapp.net\r\n"
		POST += "User-Agent: WhatsApp/2.8.14 S60Version/5.3 Device/C7-00\r\n"
		POST += "Content-Length: " + str(contentLength) + "\r\n\r\n"

		print "sending REQUEST "
		print hBAOS
		ssl_sock.write(str(POST))
		ssl_sock.write(str(hBAOS))

		totalsent = 0
		buf = 1024
		f = open(image, 'r')
		stream = f.read()
		f.close()
		status = 0
		lastEmit = 0

		while totalsent < int(filesize):
			#print "sending " + str(totalsent) + " to " + str(totalsent+buf) + " - real: " + str(len(stream[:buf]))
			ssl_sock.write(str(stream[:buf]))
			status = totalsent * 100 / filesize
			if lastEmit!=status and status!=100 and filesize>12288:
				self.progressUpdated.emit(status)
			lastEmit = status
			stream = stream[buf:]
			totalsent = totalsent + buf

		ssl_sock.write(str(fBAOS))

		if self.resizeImages is True and "image" in filetype:
			os.remove("/home/user/.cache/wazapp/" + os.path.basename(image))

		sleep(1)
		print "Done!"
		print "Reading response..."
		data = ssl_sock.recv(8192)
		data += ssl_sock.recv(8192)
		data += ssl_sock.recv(8192)
		data += ssl_sock.recv(8192)
		data += ssl_sock.recv(8192)
		data += ssl_sock.recv(8192)
		data += ssl_sock.recv(8192)
		print data;
		self.progressUpdated.emit(100)

		if "<string>https://mms" in data:
			n = data.find("<string>https://mms") +8
			url = data[n:]
			n = url.find("</string>")
			url = url[:n]
			#print "MMS ADDRESS: "+url
			self.success.emit(url + "," + filename + "," + str(filesize), "upload")

		else:
			self.error.emit()