예제 #1
0
 def _wrap_participant_id(id_):
     """Convert a user ID into a hangouts_pb2.ParticipantId object."""
     if isinstance(id_, hangouts_pb2.ParticipantId):
         return id_
     elif isinstance(id_, user.UserID):
         return hangouts_pb2.ParticipantId(gaia_id=id_.gaia_id)
     else:
         return hangouts_pb2.ParticipantId(gaia_id=id_)
예제 #2
0
파일: hangouts.py 프로젝트: raveinid/IMMP
 async def channel_remove(self, channel, user):
     try:
         conv = self._convs.get(channel.source)
     except KeyError:
         return
     request = hangouts_pb2.RemoveUserRequest(
         request_header=self._client.get_request_header(),
         event_request_header=conv._get_event_request_header(),
         participant_id=hangouts_pb2.ParticipantId(gaia_id=user.id))
     await self._client.remove_user(request)
예제 #3
0
    def update_online_status(self):
        global client
        if len(self.conv.users) == 2:
            for user in self.conv.users:
                if not user.is_self:
                    request = hangouts_pb2.QueryPresenceRequest(
                        request_header=client.get_request_header(),
                        participant_id=[hangouts_pb2.ParticipantId(gaia_id=user.id_.chat_id)],
                        field_mask=[hangouts_pb2.FIELD_MASK_REACHABLE,
                                    hangouts_pb2.FIELD_MASK_AVAILABLE,
                                    hangouts_pb2.FIELD_MASK_DEVICE],
                    )

                    asyncio.async(client.query_presence(request)).add_done_callback(self.online_status_updated)
                    break
예제 #4
0
    def querypresence(self, gaia_id):
        """Check someone's presence status.

        Raises hangups.NetworkError if the request fails.
        """
        request = hangouts_pb2.QueryPresenceRequest(
            request_header=self._get_request_header_pb(),
            participant_id=[hangouts_pb2.ParticipantId(gaia_id=gaia_id)],
            field_mask=[hangouts_pb2.FIELD_MASK_REACHABLE,
                        hangouts_pb2.FIELD_MASK_AVAILABLE,
                        hangouts_pb2.FIELD_MASK_DEVICE],
        )
        response = hangouts_pb2.QueryPresenceResponse()
        yield from self._pb_request('presence/querypresence', request,
                                    response)
        return response
예제 #5
0
    def send_presence(self):
        participant_ids = []
        for user_id in self.user_list._user_dict.keys():
            if user_id.gaia_id == '':
                continue
            participant_ids.append(hangouts_pb2.ParticipantId(gaia_id=user_id.gaia_id, chat_id=user_id.chat_id))

        presence_request = hangouts_pb2.QueryPresenceRequest(
            request_header=self.client.get_request_header(),
            participant_id=participant_ids,
            field_mask=[
                hangouts_pb2.FIELD_MASK_REACHABLE,
                hangouts_pb2.FIELD_MASK_AVAILABLE,
                hangouts_pb2.FIELD_MASK_DEVICE,
                hangouts_pb2.FIELD_MASK_MOOD])
        presence_response = yield from self.client.query_presence(presence_request)
        for presence_result in presence_response.presence_result:
            self.send_message_to_xmpp({'what': 'presence',
                                       'gaia_id': presence_result.user_id.gaia_id,
                                       'status': presence_to_status(presence_result.presence),
                                       'status_message': ''})
예제 #6
0
def to_participantid(user_id):
    """Convert UserID to hangouts_pb2.ParticipantId."""
    return hangouts_pb2.ParticipantId(chat_id=user_id.chat_id,
                                      gaia_id=user_id.gaia_id)
예제 #7
0
    def on_connect(self):
        """Hangouts is connected."""
        self.send_message_to_xmpp({'what': 'connected'})

        # Get the list of users and conversations
        self.user_list, self.conv_list = (
            yield from hangups.build_user_conversation_list(self.client)
        )

        #self.user_list.on_presence.add_observer(self.on_presence)

        self.conv_list.on_event.add_observer(self.on_event)
        self.conv_list.on_typing.add_observer(self.on_typing)

        # Query presence information for user list
        participant_ids = []
        for user_id in self.user_list._user_dict.keys():
            if user_id.gaia_id == '':
                continue
            participant_ids.append(hangouts_pb2.ParticipantId(gaia_id=user_id.gaia_id, chat_id=user_id.chat_id))

        presence_request = hangouts_pb2.QueryPresenceRequest(
            request_header=self.client.get_request_header(),
            participant_id=participant_ids,
            field_mask=[
                hangouts_pb2.FIELD_MASK_REACHABLE,
                hangouts_pb2.FIELD_MASK_AVAILABLE,
                hangouts_pb2.FIELD_MASK_DEVICE,
                hangouts_pb2.FIELD_MASK_MOOD])
        presence_response = yield from self.client.query_presence(presence_request)

        # Send user list to XMPP
        user_list_dict = {}
        for user in self.user_list.get_all():
            user_list_dict[user.id_.gaia_id] = {
                'chat_id': user.id_.chat_id,
                'gaia_id': user.id_.gaia_id,
                'first_name': user.first_name,
                'full_name': user.full_name,
                'is_self': user.is_self,
                'emails': list(user.emails) if user.emails is not None else [],
                'photo_url': user.photo_url,
                'status': 'unknown',
            }

        for presence_result in presence_response.presence_result:
            user_list_dict[presence_result.user_id.gaia_id]['status'] = presence_to_status(presence_result.presence)

        self.send_message_to_xmpp({'what': 'user_list', 'user_list': user_list_dict})

        # Send conversation list to XMPP
        conv_list_dict = {}
        for conv in self.conv_list.get_all():
            self.known_conservations.add(conv.id_)
            if conv._conversation.type == hangouts_pb2.CONVERSATION_TYPE_GROUP:
                user_list = {}
                self_gaia_id = None
                for user in conv.users:
                    user_list[user.id_.gaia_id] = user_list_dict[user.id_.gaia_id]['full_name']\
                        if user.id_.gaia_id in user_list_dict else user.id_.gaia_id
                    if user.is_self:
                        # XMPP needs to know which user is itself.
                        self_gaia_id = user.id_.gaia_id
                # If the conversation does not have a name, create one by joining the list of participants.
                conv_name = conv.name
                if conv_name is None:
                    conv_name = ', '.join(user_list.values())
                conv_list_dict[conv.id_] = {
                    'conv_id': conv.id_,
                    'topic': conv_name,
                    'user_list': user_list,
                    'self_id': self_gaia_id,
                }
        self.send_message_to_xmpp({'what': 'conv_list',
                                   'conv_list': conv_list_dict,
                                   'self_gaia': self.user_list._self_user.id_.gaia_id})