Search in sources :

Example 31 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project Smack by igniterealtime.

the class CarbonManager method addCarbonsListener.

private void addCarbonsListener(XMPPConnection connection) {
    EntityFullJid localAddress = connection.getUser();
    if (localAddress == null) {
        // carbons securely. Abort here. The ConnectionListener above will eventually setup the carbons listener.
        return;
    }
    // XEP-0280 ยง 11. Security Considerations "Any forwarded copies received by a Carbons-enabled client MUST be
    // from that user's bare JID; any copies that do not meet this requirement MUST be ignored." Otherwise, if
    // those copies do not get ignored, malicious users may be able to impersonate other users. That is why the
    // 'from' matcher is important here.
    connection.addSyncStanzaListener(carbonsListener, new AndFilter(CARBON_EXTENSION_FILTER, FromMatchesFilter.createBare(localAddress)));
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) EntityFullJid(org.jxmpp.jid.EntityFullJid)

Example 32 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project Smack by igniterealtime.

the class MultiUserChat method changeSubject.

/**
 * Changes the subject within the room. As a default, only users with a role of "moderator"
 * are allowed to change the subject in a room. Although some rooms may be configured to
 * allow a mere participant or even a visitor to change the subject.
 *
 * @param subject the new room's subject to set.
 * @throws XMPPErrorException if someone without appropriate privileges attempts to change the
 *          room subject will throw an error with code 403 (i.e. Forbidden)
 * @throws NoResponseException if there was no response from the server.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
public void changeSubject(final String subject) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    MessageBuilder message = buildMessage();
    message.setSubject(subject);
    // Wait for an error or confirmation message back from the server.
    StanzaFilter responseFilter = new AndFilter(fromRoomGroupchatFilter, new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            Message msg = (Message) packet;
            return subject.equals(msg.getSubject());
        }
    });
    StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, message.build());
    // Wait up to a certain number of seconds for a reply.
    response.nextResultOrThrow();
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) MessageBuilder(org.jivesoftware.smack.packet.MessageBuilder) Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 33 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project Smack by igniterealtime.

the class MultiUserChat method leave.

/**
 * Leave the chat room.
 *
 * @return the leave presence as reflected by the MUC.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws MucNotJoinedException if not joined to the Multi-User Chat.
 */
public synchronized Presence leave() throws NotConnectedException, InterruptedException, NoResponseException, XMPPErrorException, MucNotJoinedException {
    // Note that this method is intentionally not guarded by
    // "if  (!joined) return" because it should be always be possible to leave the room in case the instance's
    // state does not reflect the actual state.
    final EntityFullJid myRoomJid = getMyRoomJid();
    if (myRoomJid == null) {
        throw new MucNotJoinedException(this);
    }
    // TODO: Consider adding a origin-id to the presence, once it is moved form smack-experimental into
    // smack-extensions, in case the MUC service does not support stable IDs, and modify
    // reflectedLeavePresenceFilters accordingly.
    // We leave a room by sending a presence packet where the "to"
    // field is in the form "roomName@service/nickname"
    Presence leavePresence = connection.getStanzaFactory().buildPresenceStanza().ofType(Presence.Type.unavailable).to(myRoomJid).build();
    List<StanzaFilter> reflectedLeavePresenceFilters = new ArrayList<>(3);
    reflectedLeavePresenceFilters.add(StanzaTypeFilter.PRESENCE);
    reflectedLeavePresenceFilters.add(new OrFilter(new AndFilter(FromMatchesFilter.createFull(myRoomJid), PresenceTypeFilter.UNAVAILABLE, MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF), new AndFilter(fromRoomFilter, PresenceTypeFilter.ERROR)));
    if (serviceSupportsStableIds()) {
        reflectedLeavePresenceFilters.add(new StanzaIdFilter(leavePresence));
    }
    StanzaFilter reflectedLeavePresenceFilter = new AndFilter(reflectedLeavePresenceFilters);
    Presence reflectedLeavePresence;
    try {
        reflectedLeavePresence = connection.createStanzaCollectorAndSend(reflectedLeavePresenceFilter, leavePresence).nextResultOrThrow();
    } finally {
        // Reset occupant information after we send the leave presence. This ensures that we only call userHasLeft()
        // and reset the local MUC state after we successfully left the MUC (or if an exception occurred).
        userHasLeft();
    }
    return reflectedLeavePresence;
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) StanzaIdFilter(org.jivesoftware.smack.filter.StanzaIdFilter) EntityFullJid(org.jxmpp.jid.EntityFullJid) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence) MucNotJoinedException(org.jivesoftware.smackx.muc.MultiUserChatException.MucNotJoinedException) OrFilter(org.jivesoftware.smack.filter.OrFilter)

Example 34 with AndFilter

use of org.jivesoftware.smack.filter.AndFilter in project Openfire by igniterealtime.

the class ThrottleTestReader method main.

/**
 * Starts the throttle test reader client.
 *
 * @param args application arguments.
 */
public static void main(String[] args) {
    if (args.length != 3) {
        System.out.println("Usage: java ThrottleTestReader [server] [username] [password]");
        System.exit(0);
    }
    String server = args[0];
    String username = args[1];
    String password = args[2];
    try {
        // Connect to the server, without TLS encryption.
        ConnectionConfiguration config = new ConnectionConfiguration(server);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        final XMPPConnection con = new XMPPConnection(config);
        System.out.print("Connecting to " + server + "... ");
        con.connect();
        con.login(username, password, "reader");
        System.out.print("success.");
        System.out.println("");
        // Get the "real" server address.
        server = con.getServiceName();
        final String writerAddress = username + "@" + server + "/writer";
        String readerAddress = username + "@" + server + "/reader";
        System.out.println("Registered as " + readerAddress);
        // Look for the reader process.
        System.out.print("Waiting for " + writerAddress + "...");
        PacketCollector collector = con.createPacketCollector(new AndFilter(new FromMatchesFilter(writerAddress), new PacketTypeFilter(Time.class)));
        Time timeRequest = (Time) collector.nextResult();
        Time timeReply = new Time(Calendar.getInstance());
        timeReply.setPacketID(timeRequest.getPacketID());
        timeReply.setType(IQ.Type.RESULT);
        timeReply.setTo(timeRequest.getFrom());
        con.sendPacket(timeReply);
        System.out.println(" found writer. Now in reading mode.");
        // Track how many packets we've read.
        con.addPacketListener(new PacketListener() {

            public void processPacket(Packet packet) {
                packetCount.getAndIncrement();
            }
        }, new PacketTypeFilter(Message.class));
        while (!done) {
            Thread.sleep(5000);
            int count = packetCount.getAndSet(0);
            System.out.println("Packets per second: " + (count / 5));
        }
        // Sleep while we're reading packets.
        Thread.sleep(Integer.MAX_VALUE);
    } catch (Exception e) {
        System.out.println("\nError: " + e.getMessage());
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) Time(org.jivesoftware.smackx.packet.Time) AndFilter(org.jivesoftware.smack.filter.AndFilter) FromMatchesFilter(org.jivesoftware.smack.filter.FromMatchesFilter)

Aggregations

AndFilter (org.jivesoftware.smack.filter.AndFilter)34 PacketTypeFilter (org.jivesoftware.smack.filter.PacketTypeFilter)15 Presence (org.jivesoftware.smack.packet.Presence)15 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)13 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)9 StanzaCollector (org.jivesoftware.smack.StanzaCollector)8 Message (org.jivesoftware.smack.packet.Message)8 XMPPException (org.jivesoftware.smack.XMPPException)7 FromMatchesFilter (org.jivesoftware.smack.filter.FromMatchesFilter)7 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)7 IQ (org.jivesoftware.smack.packet.IQ)7 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)6 Registration (org.jivesoftware.smack.packet.Registration)6 PacketCollector (org.jivesoftware.smack.PacketCollector)5 Packet (org.jivesoftware.smack.packet.Packet)5 Stanza (org.jivesoftware.smack.packet.Stanza)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 SmackIntegrationTest (org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest)4 MUCInitialPresence (org.jivesoftware.smackx.packet.MUCInitialPresence)4