Esempio n. 1
0
    def identify(self, name,address):
        """called upon receival of a chat message"""
        check.check_is_opt_address(address)  #=@R32

        def identify_thread(self, result, name, address):

            if self.acquaintances.has_key(name):
                acq = self.acquaintances[name]
                acq_online = acq.online
                acq_address = acq.address

                # May as well test the address
                if address is not None and \
                       (not acq_online or address != acq_address):
                    dummy_result, subthread = try_address(acq,address,self.node)
                    yield 'wait', subthread
                result.append(acq)
                return

            if address is None:
                return
                #raise error.Error('no address')

            ticket, template, wait = self.node.call(address, ('identity query',))
            if wait: yield 'call', (self.node,ticket)
            try:
                info = self.node.get_reply(ticket,template)
            except:
                print "cannot identify"
                result.append(None)
                return
            
            acq_result, thread = self.make_acquaintance(info,address)
            yield 'wait',thread
            result.append(acq_result[0])
            return

        result = []
        thread = identify_thread(self,result,name,address)
        return result,thread
Esempio n. 2
0
    def receive(self, request, address, quiet, send_time, add_to_history=1):
        """send_time should be as if from time.time() (i.e. a float expressing seconds
           since the local machine's epoch) rather than the standardized timestamps
           sent over the network (i.e. a long expressing seconds since the Unix epoch
           of 1970)."""

        # TODO: more checking of message format
        # TODO: search for people not in acq list

        check.check_is_opt_address(address)  #=@R33  fixme callers
        check.check_has_type(send_time, types.FloatType)  #=@R35  fixme callers
        # fixme: should probably require that request be a tuple with 5+ elems.

        if 1:
            if not self.is_visible():
                self.n_unseen_messages = self.n_unseen_messages + 1
                if self.n_unseen_messages == 1:
                    title = _('1 unseen message')
                else:
                    title = _('%d unseen messages') % self.n_unseen_messages
                self.app.set_title(title)

            if request[0] == 'chat message 2':
                package = safe_pickle.loads(request[1])
                text = package['text']
                augmentation = package.get('aug')
                has_attachment = package.has_key('attach')
                attachment = package.get('attach')
                sender_key_name, sender_name = package['from']
                recipients = package['to']

                verify_text = request[1]
                verify_sig = request[2]

            else:
                # Legacy message format
                text = request[3]
                augmentation = None
                has_attachment = (len(request) == 6)
                if has_attachment:
                    attachment = safe_pickle.loads(request[4])

                recipients = request[2]

                if type(request[1]) == type(()):
                    sender_key_name = request[1][0]
                    sender_name = request[1][1]
                else:
                    # Used not to send username with key name
                    sender_key_name = request[1]
                    sender_name = 'unknown'

                if has_attachment:
                    verify_text = request[3].encode('utf8')+request[4]
                    verify_sig = request[5]
                else:
                    verify_text = request[3]
                    verify_sig = request[4]

            if not augmentation:
                augmentation = chr(128) * len(text)


            self.show_received_message(sender_name,sender_key_name,address,recipients,
                                       text,verify_text,verify_sig,quiet,
                                       send_time,augmentation,has_attachment,attachment)

        #except error.Error:
        #    self.show(_('Bad message received from ') + repr(address) + '\n')

        if add_to_history:
            self.lock.acquire()
            try:
                new_item = (request, send_time)
                check.check_matches(new_item, incoming_message_history_item_tmpl)
                # Proof: @R35, immutability of float, send_time not reassigned
                # in this function.
                # Relevance: @I16.
                self.incoming_message_history.append(new_item)

                while len(self.incoming_message_history) > settings.incoming_message_history_size:
                    del self.incoming_message_history[0]
            finally:
                self.lock.release()