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)));
}
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();
}
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;
}
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());
}
}
Aggregations