Пример #1
0
 def process(self, content: Content, msg: ReliableMessage) -> Optional[Content]:
     assert isinstance(content, TextContent), 'text content error: %s' % content
     sender = msg.sender
     nickname = self.facebook.name(identifier=sender)
     if self.__ignored(content=content, sender=sender, msg=msg):
         return None
     Log.debug('Received text message from %s: %s' % (nickname, content))
     response = self.__query(content=content, sender=sender)
     if response is not None:
         assert isinstance(response, TextContent)
         question = content.text
         answer = response.text
         group = content.group
         if group is None:
             # personal message
             Log.debug('Dialog > %s(%s): "%s" -> "%s"' % (nickname, sender, question, answer))
         else:
             # group message
             Log.debug('Group Dialog > %s(%s)@%s: "%s" -> "%s"' % (nickname, sender, group.name, question, answer))
             if self.messenger.send_content(sender=None, receiver=group, content=response):
                 text = 'Group message responded'
                 return ReceiptCommand(message=text)
             else:
                 text = 'Group message respond failed'
                 return ReceiptCommand(message=text)
         return response
Пример #2
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)
Пример #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')
Пример #4
0
 def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]:
     assert isinstance(cmd, Command), 'command error: %s' % cmd
     # submit device token for APNs
     token = cmd.get('device_token')
     if token is not None:
         self.database.save_device_token(token=token, identifier=msg.sender)
         return ReceiptCommand(message='Token received')
Пример #5
0
 def __split_group_message(self, msg: ReliableMessage,
                           members: List[ID]) -> Optional[Content]:
     """ Split group message for each member """
     messages = msg.split(members=members)
     success_list = []
     failed_list = []
     for item in messages:
         if item.delegate is None:
             item.delegate = msg.delegate
         if self.__forward_group_message(msg=item):
             success_list.append(item.receiver)
         else:
             failed_list.append(item.receiver)
     response = ReceiptCommand(message='Group message delivering')
     if len(success_list) > 0:
         response['success'] = ID.revert(success_list)
     if len(failed_list) > 0:
         response['failed'] = ID.revert(failed_list)
         # failed to get keys for this members,
         # query from sender by invite members
         sender = msg.sender
         group = msg.receiver
         cmd = GroupCommand.invite(group=group, members=failed_list)
         self.send_content(sender=None, receiver=sender, content=cmd)
     return response
Пример #6
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(message='Block command of %s received!' %
                               sender)
     else:
         return TextContent(text='Sorry, block-list not stored: %s!' % cmd)
Пример #7
0
 def __put(self, cmd: MuteCommand, sender: ID) -> Content:
     # receive mute command, save it
     if self.database.save_mute_command(cmd=cmd, sender=sender):
         return ReceiptCommand(message='Mute command of %s received!' %
                               sender)
     else:
         return TextContent(text='Sorry, mute-list not stored %s!' % cmd)
Пример #8
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
Пример #9
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')
Пример #10
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')
Пример #11
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)
Пример #12
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)
Пример #13
0
 def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]:
     assert isinstance(
         cmd, ReportCommand), 'offline report command error: %s' % cmd
     # goodbye!
     session = self.messenger.current_session(identifier=msg.sender)
     if isinstance(session, Session):
         session.active = False
     # post notification
     post_notification(session=session, sender=self)
     return ReceiptCommand(message='Client offline received')
Пример #14
0
 def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]:
     assert isinstance(
         cmd, ReportCommand), 'online report command error: %s' % cmd
     # welcome back!
     session = self.messenger.current_session(identifier=msg.sender)
     if isinstance(session, Session):
         session.active = True
     # post notification
     post_notification(session=session, sender=self)
     # TODO: notification for pushing offline message(s) from 'last_time'
     return ReceiptCommand(message='Client online received')
Пример #15
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')
Пример #16
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')
Пример #17
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')
Пример #18
0
 def execute(self, cmd: Command, msg: ReliableMessage) -> Optional[Content]:
     assert isinstance(cmd, LoginCommand), 'command error: %s' % cmd
     sender = msg.sender
     # check roaming
     sid = self.__roaming(cmd=cmd, sender=sender)
     if sid is not None:
         # post notification: USER_ONLINE
         NotificationCenter().post(name=NotificationNames.USER_ONLINE,
                                   sender=self,
                                   info={
                                       'ID': sender,
                                       'station': sid,
                                   })
     # update login info
     if not self.database.save_login(cmd=cmd, msg=msg):
         return None
     # response
     return ReceiptCommand(message='Login received')
Пример #19
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
Пример #20
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')