use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
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 XMPPException if someone without appropriate privileges attempts to change the
* room subject will throw an error with code 403 (i.e. Forbidden)
*/
public void changeSubject(final String subject) throws XMPPException {
Message message = new Message(room, Message.Type.groupchat);
message.setSubject(subject);
// Wait for an error or confirmation message back from the server.
PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room), new PacketTypeFilter(Message.class));
responseFilter = new AndFilter(responseFilter, new PacketFilter() {
public boolean accept(Packet packet) {
Message msg = (Message) packet;
return subject.equals(msg.getSubject());
}
});
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send change subject packet.
connection.sendPacket(message);
// Wait up to a certain number of seconds for a reply.
Message answer = (Message) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method sendConfigurationForm.
/**
* Sends the completed configuration form to the server. The room will be configured
* with the new settings defined in the form. If the form is empty then the server
* will create an instant room (will use default configuration).
*
* @param form the form with the new settings.
* @throws XMPPException if an error occurs setting the new rooms' configuration.
*/
public void sendConfigurationForm(Form form) throws XMPPException {
MUCOwner iq = new MUCOwner();
iq.setTo(room);
iq.setType(IQ.Type.SET);
iq.addExtension(form.getDataFormToSend());
// Filter packets looking for an answer from the server.
PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send the completed configuration form to the server.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method getOccupants.
/**
* Returns a collection of <code>Occupant</code> that have the specified room role.
*
* @param role the role of the occupant in the room.
* @return a collection of <code>Occupant</code> that have the specified room role.
* @throws XMPPException if an error occured while performing the request to the server or you
* don't have enough privileges to get this information.
*/
private Collection<Occupant> getOccupants(String role) throws XMPPException {
MUCAdmin iq = new MUCAdmin();
iq.setTo(room);
iq.setType(IQ.Type.GET);
// Set the specified role. This may request the list of moderators/participants.
MUCAdmin.Item item = new MUCAdmin.Item(null, role);
iq.addItem(item);
// Wait for a response packet back from the server.
PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send the request to the server.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
MUCAdmin answer = (MUCAdmin) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
// Get the list of participants from the server's answer
List<Occupant> participants = new ArrayList<Occupant>();
for (Iterator<MUCAdmin.Item> it = answer.getItems(); it.hasNext(); ) {
participants.add(new Occupant(it.next()));
}
return participants;
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method join.
/**
* Joins the chat room using the specified nickname and password. If already joined
* using another nickname, this method will first leave the room and then
* re-join using the new nickname.<p>
*
* To control the amount of history to receive while joining a room you will need to provide
* a configured DiscussionHistory object.<p>
*
* A password is required when joining password protected rooms. If the room does
* not require a password there is no need to provide one.<p>
*
* If the room does not already exist when the user seeks to enter it, the server will
* decide to create a new room or not.
*
* @param nickname the nickname to use.
* @param password the password to use.
* @param history the amount of discussion history to receive while joining a room.
* @param timeout the amount of time to wait for a reply from the MUC service(in milleseconds).
* @throws XMPPException if an error occurs joining the room. In particular, a
* 401 error can occur if no password was provided and one is required; or a
* 403 error can occur if the user is banned; or a
* 404 error can occur if the room does not exist or is locked; or a
* 407 error can occur if user is not on the member list; or a
* 409 error can occur if someone is already in the group chat with the same nickname.
*/
public synchronized void join(String nickname, String password, DiscussionHistory history, long timeout) throws XMPPException {
if (nickname == null || nickname.equals("")) {
throw new IllegalArgumentException("Nickname must not be null or blank.");
}
// nickname.
if (joined) {
leave();
}
// We join a room by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(room + "/" + nickname);
// Indicate the the client supports MUC
MUCInitialPresence mucInitialPresence = new MUCInitialPresence();
if (password != null) {
mucInitialPresence.setPassword(password);
}
if (history != null) {
mucInitialPresence.setHistory(history.getMUCHistory());
}
joinPresence.addExtension(mucInitialPresence);
// Invoke presence interceptors so that extra information can be dynamically added
for (PacketInterceptor packetInterceptor : presenceInterceptors) {
packetInterceptor.interceptPacket(joinPresence);
}
// Wait for a presence packet back from the server.
PacketFilter responseFilter = new AndFilter(new FromMatchesFilter(room + "/" + nickname), new PacketTypeFilter(Presence.class));
PacketCollector response = null;
Presence presence;
try {
response = connection.createPacketCollector(responseFilter);
// Send join packet.
connection.sendPacket(joinPresence);
// Wait up to a certain number of seconds for a reply.
presence = (Presence) response.nextResult(timeout);
} finally {
// Stop queuing results
if (response != null) {
response.cancel();
}
}
if (presence == null) {
throw new XMPPException("No response from server.");
} else if (presence.getError() != null) {
throw new XMPPException(presence.getError());
}
this.nickname = nickname;
joined = true;
userHasJoined();
}
use of org.jivesoftware.smack.PacketCollector in project ecf by eclipse.
the class MultiUserChat method destroy.
/**
* Sends a request to the server to destroy the room. The sender of the request
* should be the room's owner. If the sender of the destroy request is not the room's owner
* then the server will answer a "Forbidden" error (403).
*
* @param reason the reason for the room destruction.
* @param alternateJID the JID of an alternate location.
* @throws XMPPException if an error occurs while trying to destroy the room.
* An error can occur which will be wrapped by an XMPPException --
* XMPP error code 403. The error code can be used to present more
* appropiate error messages to end-users.
*/
public void destroy(String reason, String alternateJID) throws XMPPException {
MUCOwner iq = new MUCOwner();
iq.setTo(room);
iq.setType(IQ.Type.SET);
// Create the reason for the room destruction
MUCOwner.Destroy destroy = new MUCOwner.Destroy();
destroy.setReason(reason);
destroy.setJid(alternateJID);
iq.setDestroy(destroy);
// Wait for a presence packet back from the server.
PacketFilter responseFilter = new PacketIDFilter(iq.getPacketID());
PacketCollector response = connection.createPacketCollector(responseFilter);
// Send the room destruction request.
connection.sendPacket(iq);
// Wait up to a certain number of seconds for a reply.
IQ answer = (IQ) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
response.cancel();
if (answer == null) {
throw new XMPPException("No response from server.");
} else if (answer.getError() != null) {
throw new XMPPException(answer.getError());
}
// Reset occupant information.
occupantsMap.clear();
nickname = null;
joined = false;
userHasLeft();
}
Aggregations