Esempio n. 1
0
 /** 更改用户状态 */
 public void setPresence(int code) {
   if (connection == null) return;
   Presence presence;
   switch (code) {
     case 0:
       presence = new Presence(Presence.Type.available);
       connection.sendPacket(presence);
       Log.v("state", "设置在线");
       break;
     case 1:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.chat);
       connection.sendPacket(presence);
       Log.v("state", "设置Q我吧");
       System.out.println(presence.toXML());
       break;
     case 2:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.dnd);
       connection.sendPacket(presence);
       Log.v("state", "设置忙碌");
       System.out.println(presence.toXML());
       break;
     case 3:
       presence = new Presence(Presence.Type.available);
       presence.setMode(Presence.Mode.away);
       connection.sendPacket(presence);
       Log.v("state", "设置离开");
       System.out.println(presence.toXML());
       break;
     case 4:
       Roster roster = connection.getRoster();
       Collection<RosterEntry> entries = roster.getEntries();
       for (RosterEntry entry : entries) {
         presence = new Presence(Presence.Type.unavailable);
         presence.setPacketID(Packet.ID_NOT_AVAILABLE);
         presence.setFrom(connection.getUser());
         presence.setTo(entry.getUser());
         connection.sendPacket(presence);
         System.out.println(presence.toXML());
       }
       // 向同一用户的其他客户端发送隐身状态
       presence = new Presence(Presence.Type.unavailable);
       presence.setPacketID(Packet.ID_NOT_AVAILABLE);
       presence.setFrom(connection.getUser());
       presence.setTo(StringUtils.parseBareAddress(connection.getUser()));
       connection.sendPacket(presence);
       Log.v("state", "设置隐身");
       break;
     case 5:
       presence = new Presence(Presence.Type.unavailable);
       connection.sendPacket(presence);
       Log.v("state", "设置离线");
       break;
     default:
       break;
   }
 }
Esempio n. 2
0
  /*
   * (non-Javadoc)
   *
   * @see
   * fr.univartois.ili.fsnet.commons.talk.ITalk#subscribe(java.lang.String)
   */
  @Override
  public void subscribe(String user) {

    Presence presence = new Presence(Presence.Type.subscribe, "subscribe", 10, Mode.available);
    presence.setTo(user);
    connection.sendPacket(presence);

    presence = new Presence(Presence.Type.subscribed);
    presence.setTo(user);
    connection.sendPacket(presence);
  }
Esempio n. 3
0
  /*
   * (non-Javadoc)
   *
   * @see
   * fr.univartois.ili.fsnet.commons.talk.ITalk#unSubscribe(java.lang.String)
   */
  @Override
  public void unSubscribe(String user) {
    /*
     * The Mode referring one of the 5 Mode available in XMPP server. The text message is just a description.
     */
    Presence presence = new Presence(Presence.Type.unsubscribe, "unsubscribe", 10, Mode.available);
    presence.setTo(user);
    connection.sendPacket(presence);

    presence = new Presence(Presence.Type.unsubscribed);
    presence.setTo(user);
    connection.sendPacket(presence);
  }
Esempio n. 4
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);
 }
Esempio n. 5
0
  /** @see net.sf.kraken.session.TransportSession#acceptAddContact(JID) */
  @Override
  public void acceptAddContact(JID jid) {
    final String userID = getTransport().convertJIDToID(jid);
    Log.debug("XMPP: accept-add contact: " + userID);

    final Presence accept = new Presence(Type.subscribed);
    accept.setTo(userID);
    conn.sendPacket(accept);
  }
Esempio n. 6
0
  /**
   * Returns true if the workgroup is available for receiving new requests. The workgroup will be
   * available only when agents are available for this workgroup.
   *
   * @return true if the workgroup is available for receiving new requests.
   * @throws XMPPException
   */
  public boolean isAvailable() throws XMPPException {
    Presence directedPresence = new Presence(Presence.Type.available);
    directedPresence.setTo(workgroupJID);
    PacketFilter typeFilter = new PacketTypeFilter(Presence.class);
    PacketFilter fromFilter = FromMatchesFilter.create(workgroupJID);
    PacketCollector collector =
        connection.createPacketCollector(new AndFilter(fromFilter, typeFilter));

    connection.sendPacket(directedPresence);

    Presence response = (Presence) collector.nextResultOrThrow();
    return Presence.Type.available == response.getType();
  }
Esempio n. 7
0
  private void tryToRemoveRosterEntry(String user) throws ClientException {
    try {
      RosterEntry rosterEntry = roster.getEntry(user);

      if (rosterEntry != null) {

        Presence unsub = new Presence(Presence.Type.unsubscribed);
        unsub.setTo(rosterEntry.getUser());
        extXMPPConnection.sendPacket(unsub);
        roster.removeEntry(rosterEntry);
      }
    } catch (XMPPException e) {
      throw new ClientException("tryToRemoveRosterEntry", e);
    }
  }
Esempio n. 8
0
  private void tryToRemoveRosterEntry(String user) throws YaximXMPPException {
    try {
      RosterEntry rosterEntry = mRoster.getEntry(user);

      if (rosterEntry != null) {
        // first, unsubscribe the user
        Presence unsub = new Presence(Presence.Type.unsubscribed);
        unsub.setTo(rosterEntry.getUser());
        mXMPPConnection.sendPacket(unsub);
        // then, remove from roster
        mRoster.removeEntry(rosterEntry);
      }
    } catch (XMPPException e) {
      throw new YaximXMPPException("tryToRemoveRosterEntry", e);
    }
  }
Esempio n. 9
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);
 }
Esempio n. 10
0
 private static void sendPresenceTo(String to, Presence presence, XMPPConnection connection) {
   presence.setTo(to);
   connection.sendPacket(presence);
 }
Esempio n. 11
0
  /**
   * 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;
  }