示例#1
0
def get_presence(jid, from_jid=None, get_show=False):
    """Gets the presence for a JID.

  Args:
    jid: The JID of the contact whose presence is requested. This may also be a
      list of JIDs, which also implies get_show (below).
    from_jid: The optional custom JID to use for sending. Currently, the default
      is <appid>@appspot.com. This is supported as a value. Custom JIDs can be
      of the form <anything>@<appid>.appspotchat.com.
    get_show: if True, return a tuple of (is_available, show). If a list of jids
      is given, this will always be True.

  Returns:
    At minimum, a boolean is_available representing whether the requested JID
    is available.

    If get_show is specified, a tuple (is_available, show) will be given.

    If a list of JIDs is given, a list of tuples will be returned, including
    is_available, show, and an additional boolean indicating if that JID was
    valid.

  Raises:
    InvalidJidError: Raised if no JID passed in is valid.
    Error: if an unspecified error happens processing the request.
  """
    if not jid:
        raise InvalidJidError()

    request = xmpp_service_pb.BulkPresenceRequest()
    response = xmpp_service_pb.BulkPresenceResponse()

    if isinstance(jid, basestring):
        single_jid = True
        jidlist = [jid]
    else:

        single_jid = False
        get_show = True
        jidlist = jid

    for given_jid in jidlist:
        request.add_jid(_to_str(given_jid))

    if from_jid:
        request.set_from_jid(_to_str(from_jid))

    try:
        apiproxy_stub_map.MakeSyncCall("xmpp", "BulkGetPresence", request,
                                       response)
    except apiproxy_errors.ApplicationError, e:
        if (e.application_error == xmpp_service_pb.XmppServiceError.INVALID_JID
            ):

            raise InvalidJidError()
        elif (e.application_error ==
              xmpp_service_pb.XmppServiceError.NONDEFAULT_MODULE):
            raise NondefaultModuleError()
        else:
            raise Error()
示例#2
0
def get_presence(jid, from_jid=None, get_show=False):
  """Gets the presence for a Jabber identifier (JID).

  Args:
    jid: The JID of the contact whose presence is requested. This can also be a
        list of JIDs, which also implies the `get_show` argument.
    from_jid: The optional custom JID to use. Currently, the default JID is
        `<appid>@appspot.com`. This JID is supported as a value. Custom JIDs can
        be of the form `<anything>@<appid>.appspotchat.com`.
    get_show: If True, this argument returns a tuple of `(is_available, show)`.
        If a list of JIDs is given, this argument will always be True.

  Returns:
    At minimum, a boolean `is_available` representing whether the requested JID
    is available is returned.

    If `get_show` is specified, a tuple `(is_available, show)` will be given.

    If a list of JIDs is given, a list of tuples will be returned, including
    `is_available`, `show`, and an additional boolean indicating if that JID was
    valid.

  Raises:
    InvalidJidError: If the specified `jid` is invalid.
    Error: If an unspecified error happens while processing the request.
  """
  if not jid:
    raise InvalidJidError()

  request = xmpp_service_pb.BulkPresenceRequest()
  response = xmpp_service_pb.BulkPresenceResponse()

  if isinstance(jid, basestring):
    single_jid = True
    jidlist = [jid]
  else:

    single_jid = False
    get_show = True
    jidlist = jid

  for given_jid in jidlist:
    request.add_jid(_to_str(given_jid))

  if from_jid:
    request.set_from_jid(_to_str(from_jid))

  try:
    apiproxy_stub_map.MakeSyncCall("xmpp",
                                   "BulkGetPresence",
                                   request,
                                   response)
  except apiproxy_errors.ApplicationError as e:
    if (e.application_error ==
        xmpp_service_pb.XmppServiceError.INVALID_JID):





      raise InvalidJidError()
    elif (e.application_error ==
          xmpp_service_pb.XmppServiceError.NONDEFAULT_MODULE):
      raise NondefaultModuleError()
    else:
      raise Error()

  def HandleSubresponse(subresponse):
    if get_show:
      if subresponse.has_presence():
        presence = subresponse.presence()
        show = _PRESENCE_SHOW_MAPPING.get(presence, None)
      else:
        show = None
      return bool(subresponse.is_available()), show, subresponse.valid()
    else:
      return bool(subresponse.is_available()), subresponse.valid()

  results = [HandleSubresponse(s) for s in response.presence_response_list()]


  if not any(t[-1] for t in results):
    raise InvalidJidError()

  if single_jid:
    if get_show:
      return results[0][:-1]
    else:
      return results[0][0]
  else:
    return results