Esempio n. 1
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content,
                       TextContent), 'text content error: %s' % content
     nickname = self.facebook.nickname(identifier=sender)
     self.info('Received text message from %s: %s' % (nickname, content))
     if self.__ignored(content=content, sender=sender, msg=msg):
         return None
     response = self.dialog.query(content=content, sender=sender)
     if response is not None:
         assert isinstance(response, TextContent)
         question = content.text
         answer = response.text
         group = self.facebook.identifier(content.group)
         if group is None:
             # personal message
             self.info('Dialog > %s(%s): "%s" -> "%s"' %
                       (nickname, sender, question, answer))
         else:
             # group message
             self.info('Group Dialog > %s(%s)@%s: "%s" -> "%s"' %
                       (nickname, sender, group.name, question, answer))
             if self.messenger.send_content(content=response,
                                            receiver=group):
                 text = 'Group message responded'
                 return ReceiptCommand.new(message=text)
             else:
                 text = 'Group message respond failed'
                 return ReceiptCommand.new(message=text)
         return response
Esempio n. 2
0
 def __receipt(message: str, msg: ReliableMessage) -> Content:
     receipt = ReceiptCommand.new(message=message)
     for key in ['sender', 'receiver', 'time', 'group', 'signature']:
         value = msg.get(key)
         if value is not None:
             receipt[key] = value
     return receipt
Esempio n. 3
0
 def process(self, content: Content, sender: ID, msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # submit device token for APNs
     token = content.get('device_token')
     if token is not None:
         self.database.save_device_token(token=token, identifier=sender)
         return ReceiptCommand.new(message='Token received')
Esempio n. 4
0
 def process(self, content: Content, sender: ID, msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # goodbye!
     session = self.messenger.current_session(identifier=sender)
     if isinstance(session, Session):
         session.active = False
     return ReceiptCommand.new(message='Client offline received')
Esempio n. 5
0
 def __put_contacts(self, cmd: StorageCommand, sender: ID) -> Content:
     # receive encrypted contacts, save it
     if self.database.save_contacts_command(cmd=cmd, sender=sender):
         return ReceiptCommand.new(message='Contacts of %s received!' %
                                   sender)
     else:
         return TextContent.new(text='Contacts not stored %s!' % cmd)
Esempio n. 6
0
 def process(self, content: Content, sender: ID, msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # welcome back!
     self.receptionist.add_guest(identifier=sender)
     session = self.messenger.current_session(identifier=sender)
     if isinstance(session, Session):
         session.active = True
     return ReceiptCommand.new(message='Client online received')
Esempio n. 7
0
 def __put(self, cmd: Command, sender: ID) -> Content:
     # receive encrypted contacts, save it
     if self.database.save_contacts_command(cmd=cmd, sender=sender):
         self.info('contacts command saved for %s' % sender)
         return ReceiptCommand.new(message='Contacts of %s received!' %
                                   sender)
     else:
         self.error('failed to save contacts command: %s' % cmd)
         return TextContent.new(text='Contacts not stored %s!' % cmd)
Esempio n. 8
0
 def __put(self, cmd: Command, sender: ID) -> Content:
     # receive block command, save it
     if self.database.save_block_command(cmd=cmd, sender=sender):
         self.info('block command saved for %s' % sender)
         return ReceiptCommand.new(message='Block command of %s received!' %
                                   sender)
     else:
         self.error('failed to save block command: %s' % cmd)
         return TextContent.new(text='Block-list not stored %s!' % cmd)
Esempio n. 9
0
 def forward_message(self, msg: ReliableMessage) -> Optional[Content]:
     """ Re-pack and deliver (Top-Secret) message to the real receiver """
     res = self.filter.check_forward(msg=msg)
     if res is not None:
         # forward is not allowed
         return res
     forward = ForwardContent.new(message=msg)
     receiver = self.facebook.identifier(string=msg.envelope.receiver)
     if self.send_content(content=forward, receiver=receiver):
         return ReceiptCommand.new(message='Message forwarded', envelope=msg.envelope)
     else:
         return TextContent.new(text='Failed to forward your message')
Esempio n. 10
0
 def __process_old_report(self, cmd: Command, sender: ID) -> Optional[Content]:
     # compatible with v1.0
     state = cmd.get('state')
     if state is not None:
         session = self.messenger.current_session(identifier=sender)
         if 'background' == state:
             session.active = False
         elif 'foreground' == state:
             # welcome back!
             self.receptionist.add_guest(identifier=session.identifier)
             session.active = True
         else:
             session.active = True
         return ReceiptCommand.new(message='Client state received')
Esempio n. 11
0
 def forward(self, content: ForwardContent,
             sender: ID) -> Optional[Content]:
     if not self.update(identifier=sender):
         return None
     self.info('forwarding message from: %s' % sender)
     # forwarding
     messenger = self.messenger
     users = self.__users.copy()
     users.reverse()
     for item in users:
         if item == sender:
             continue
         messenger.send_content(content=content, receiver=item)
     return ReceiptCommand.new(message='message forwarded')
Esempio n. 12
0
 def __split_group_message(self, msg: ReliableMessage) -> Optional[Content]:
     receiver = self.facebook.identifier(msg.envelope.receiver)
     assert receiver.type.is_group(), 'receiver not a group: %s' % receiver
     members = self.facebook.members(identifier=receiver)
     if members is not None:
         messages = msg.split(members=members)
         success_list = []
         failed_list = []
         for item in messages:
             if self.deliver(msg=item) is None:
                 failed_list.append(item.envelope.receiver)
             else:
                 success_list.append(item.envelope.receiver)
         response = ReceiptCommand.new(
             message='Message split and delivering')
         if len(success_list) > 0:
             response['success'] = success_list
         if len(failed_list) > 0:
             response['failed'] = failed_list
         return response
Esempio n. 13
0
 def process(self, content: Content, sender: ID,
             msg: InstantMessage) -> Optional[Content]:
     assert isinstance(content, Command), 'command error: %s' % content
     # TODO: update login status
     return ReceiptCommand.new(message='Login received')
Esempio n. 14
0
 def __put(self, cmd: BlockCommand, sender: ID) -> Content:
     # receive block command, save it
     if self.database.save_block_command(cmd=cmd, sender=sender):
         return ReceiptCommand.new(message='Block command of %s received!' % sender)
     else:
         return TextContent.new(text='Sorry, block-list not stored: %s!' % cmd)