示例#1
0
 def handshake(self):
     user = self.facebook.current_user
     assert user is not None, 'current user not set yet'
     server = self.messenger.station
     cmd = HandshakeCommand.start()
     msg = InstantMessage.new(content=cmd,
                              sender=user.identifier,
                              receiver=server.identifier)
     msg = self.messenger.sign_message(
         self.messenger.encrypt_message(msg=msg))
     # carry meta for first handshake
     msg.meta = user.meta
     data = self.messenger.serialize_message(msg=msg)
     # send out directly
     handler: CompletionHandler = None
     self.messenger.delegate.send_package(data=data, handler=handler)
示例#2
0
 def forward_message(self, msg: ReliableMessage) -> Optional[Content]:
     """ Re-pack and deliver (Top-Secret) message to the real receiver """
     if self.filter.allow_forward(msg=msg):
         # call dispatcher to deliver this message
         # repack the top-secret message
         sender = self.messenger.current_user.identifier
         receiver = g_facebook.identifier(msg.envelope.receiver)
         content = ForwardContent.new(message=msg)
         i_msg = InstantMessage.new(content=content,
                                    sender=sender,
                                    receiver=receiver)
         # encrypt, sign & deliver it
         r_msg = self.messenger.encrypt_sign(msg=i_msg)
         return self.deliver_message(msg=r_msg)
     else:
         text = 'Message forward to "%s" is not allowed' % msg.envelope.receiver
         return TextContent.new(text=text)
示例#3
0
 def send_report(self, text: str, receiver: ID) -> bool:
     if self.sender is None:
         self.error('sender not set yet')
         return False
     sender = self.facebook.identifier(self.sender)
     receiver = self.facebook.identifier(receiver)
     timestamp = int(time.time())
     content = TextContent.new(text=text)
     i_msg = InstantMessage.new(content=content,
                                sender=sender,
                                receiver=receiver,
                                time=timestamp)
     s_msg = self.messenger.encrypt_message(msg=i_msg)
     r_msg = self.messenger.sign_message(msg=s_msg)
     # try for online user
     sessions = self.session_server.all(identifier=receiver)
     if sessions and len(sessions) > 0:
         self.info('%s is online(%d), try to push report: %s' %
                   (receiver, len(sessions), text))
         success = 0
         for sess in sessions:
             if sess.valid is False or sess.active is False:
                 # self.info('session invalid %s' % sess)
                 continue
             request_handler = self.session_server.get_handler(
                 client_address=sess.client_address)
             if request_handler is None:
                 self.error('handler lost: %s' % sess)
                 continue
             if request_handler.push_message(r_msg):
                 success = success + 1
             else:
                 self.error(
                     'failed to push report via connection (%s, %s)' %
                     sess.client_address)
         if success > 0:
             self.info(
                 'report pushed to activated session(%d) of user: %s' %
                 (success, receiver))
             return True
     # store in local cache file
     self.info('%s is offline, store report: %s' % (receiver, text))
     self.database.store_message(r_msg)
     # push notification
     return self.apns.push(identifier=receiver, message=text)
示例#4
0
 def send_content(self, content: Content, receiver: ID) -> bool:
     sender = self.current_user.identifier
     msg = InstantMessage.new(content=content,
                              sender=sender,
                              receiver=receiver)
     return self.send_message(msg=msg)