Example #1
0
 public void sendPresenceRequest(String user, String type) {
   // HACK: remove the fake roster entry added by handleIncomingSubscribe()
   if ("unsubscribed".equals(type)) deleteRosterEntryFromDB(user);
   Presence response = new Presence(Presence.Type.valueOf(type));
   response.setTo(user);
   mXMPPConnection.sendPacket(response);
 }
Example #2
0
 public void sendPresenceRequest(String user, String type) {
   if ("unsubscribed".equals(type)) deleteRosterEntryFromDB(user);
   Presence response = new Presence(Presence.Type.valueOf(type));
   response.setTo(user);
   extXMPPConnection.sendPacket(response);
 }
  /**
   * Parses a presence packet.
   *
   * @param parser the XML parser, positioned at the start of a presence packet.
   * @return a Presence packet.
   * @throws Exception if an exception occurs while parsing the packet.
   */
  public static Presence parsePresence(XmlPullParser parser) throws Exception {
    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
      try {
        type = Presence.Type.valueOf(typeString);
      } catch (IllegalArgumentException iae) {
        System.err.println("Found invalid presence type " + typeString);
      }
    }
    Presence presence = new Presence(type);
    presence.setTo(parser.getAttributeValue("", "to"));
    presence.setFrom(parser.getAttributeValue("", "from"));
    String id = parser.getAttributeValue("", "id");
    presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);

    String language = getLanguageAttribute(parser);
    if (language != null && !"".equals(language.trim())) {
      presence.setLanguage(language);
    }
    presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);

    // Parse sub-elements
    boolean done = false;
    while (!done) {
      int eventType = parser.next();
      if (eventType == XmlPullParser.START_TAG) {
        String elementName = parser.getName();
        String namespace = parser.getNamespace();
        if (elementName.equals("status")) {
          presence.setStatus(parser.nextText());
        } else if (elementName.equals("priority")) {
          try {
            int priority = Integer.parseInt(parser.nextText());
            presence.setPriority(priority);
          } catch (NumberFormatException nfe) {
            // Ignore.
          } catch (IllegalArgumentException iae) {
            // Presence priority is out of range so assume priority to be zero
            presence.setPriority(0);
          }
        } else if (elementName.equals("show")) {
          String modeText = parser.nextText();
          try {
            presence.setMode(Presence.Mode.valueOf(modeText));
          } catch (IllegalArgumentException iae) {
            System.err.println("Found invalid presence mode " + modeText);
          }
        } else if (elementName.equals("error")) {
          presence.setError(parseError(parser));
        } else if (elementName.equals("properties") && namespace.equals(PROPERTIES_NAMESPACE)) {
          Map<String, Object> properties = parseProperties(parser);
          // Set packet properties.
          for (String name : properties.keySet()) {
            presence.setProperty(name, properties.get(name));
          }
        }
        // Otherwise, it must be a packet extension.
        else {
          try {
            presence.addExtension(
                PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
          } catch (Exception e) {
            System.err.println("Failed to parse extension packet in Presence packet.");
          }
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("presence")) {
          done = true;
        }
      }
    }
    return presence;
  }
Example #4
0
 public static Presence parsePresence(XmlPullParser xmlPullParser)
     throws XmlPullParserException, IOException, SmackException {
   ParserUtils.assertAtStartTag(xmlPullParser);
   int depth = xmlPullParser.getDepth();
   Presence.Type type = Presence.Type.available;
   String attributeValue = xmlPullParser.getAttributeValue("", "type");
   if (!(attributeValue == null || attributeValue.equals(""))) {
     type = Presence.Type.fromString(attributeValue);
   }
   Stanza presence = new Presence(type);
   presence.setTo(xmlPullParser.getAttributeValue("", PrivacyItem.SUBSCRIPTION_TO));
   presence.setFrom(xmlPullParser.getAttributeValue("", PrivacyItem.SUBSCRIPTION_FROM));
   presence.setStanzaId(xmlPullParser.getAttributeValue("", "id"));
   String languageAttribute = getLanguageAttribute(xmlPullParser);
   if (!(languageAttribute == null || "".equals(languageAttribute.trim()))) {
     presence.setLanguage(languageAttribute);
   }
   while (true) {
     switch (xmlPullParser.next()) {
       case VideoSize.HVGA /*2*/:
         String name = xmlPullParser.getName();
         String namespace = xmlPullParser.getNamespace();
         Object obj = -1;
         switch (name.hashCode()) {
           case -1165461084:
             if (name.equals("priority")) {
               obj = 1;
               break;
             }
             break;
           case -892481550:
             if (name.equals(Status.ELEMENT)) {
               obj = null;
               break;
             }
             break;
           case 3529469:
             if (name.equals("show")) {
               obj = 2;
               break;
             }
             break;
           case 96784904:
             if (name.equals(XMPPError.ERROR)) {
               obj = 3;
               break;
             }
             break;
         }
         switch (obj) {
           case VideoSize.QCIF /*0*/:
             presence.setStatus(xmlPullParser.nextText());
             break;
           case VideoSize.CIF /*1*/:
             presence.setPriority(Integer.parseInt(xmlPullParser.nextText()));
             break;
           case VideoSize.HVGA /*2*/:
             obj = xmlPullParser.nextText();
             if (!StringUtils.isNotEmpty(obj)) {
               LOGGER.warning(
                   "Empty or null mode text in presence show element form "
                       + presence.getFrom()
                       + " with id '"
                       + presence.getStanzaId()
                       + "' which is invalid according to RFC6121 4.7.2.1");
               break;
             }
             presence.setMode(Mode.fromString(obj));
             break;
           case Version.API03_CUPCAKE_15 /*3*/:
             presence.setError(parseError(xmlPullParser));
             break;
           default:
             try {
               addExtensionElement(presence, xmlPullParser, name, namespace);
               break;
             } catch (Throwable e) {
               LOGGER.log(
                   Level.WARNING,
                   "Failed to parse extension packet in Presence packet. Attributes: from="
                       + presence.getFrom()
                       + " id="
                       + presence.getStanzaId(),
                   e);
               break;
             }
         }
       case Version.API03_CUPCAKE_15 /*3*/:
         if (xmlPullParser.getDepth() != depth) {
           break;
         }
         return presence;
       default:
         break;
     }
   }
 }