use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MultiUserChat method changeNickname.
/**
* Changes the occupant's nickname to a new nickname within the room. Each room occupant
* will receive two presence packets. One of type "unavailable" for the old nickname and one
* indicating availability for the new nickname. The unavailable presence will contain the new
* nickname and an appropriate status code (namely 303) as extended presence information. The
* status code 303 indicates that the occupant is changing his/her nickname.
*
* @param nickname the new nickname within the room.
* @throws XMPPErrorException if the new nickname is already in use by another occupant.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
* @throws MucNotJoinedException
*/
public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
// nickname.
if (!joined) {
throw new MucNotJoinedException(this);
}
final EntityFullJid jid = JidCreate.fullFrom(room, nickname);
// We change the nickname by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
// We don't have to signal the MUC support again
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(jid);
// Wait for a presence packet back from the server.
StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(jid), new StanzaTypeFilter(Presence.class));
StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, joinPresence);
// Wait up to a certain number of seconds for a reply. If there is a negative reply, an
// exception will be thrown
response.nextResultOrThrow();
this.nickname = nickname;
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MultiUserChat method createOrJoin.
/**
* Like {@link #create(Resourcepart)}, but will return a {@link MucCreateConfigFormHandle} if the room creation was acknowledged by
* the service (with an 201 status code). It's up to the caller to decide, based on the return
* value, if he needs to continue sending the room configuration. If {@code null} is returned, the room
* already existed and the user is able to join right away, without sending a form.
*
* @param mucEnterConfiguration the configuration used to enter the MUC.
* @return A {@link MucCreateConfigFormHandle} if the room was created while joining, or {@code null} if the room was just joined.
* @throws XMPPErrorException if the room couldn't be created for some reason (e.g. 405 error if
* the user is not allowed to create the room)
* @throws NoResponseException if there was no response from the server.
* @throws InterruptedException
* @throws MucAlreadyJoinedException if the MUC is already joined
* @throws NotConnectedException
* @throws NotAMucServiceException
*/
public synchronized MucCreateConfigFormHandle createOrJoin(MucEnterConfiguration mucEnterConfiguration) throws NoResponseException, XMPPErrorException, InterruptedException, MucAlreadyJoinedException, NotConnectedException, NotAMucServiceException {
if (joined) {
throw new MucAlreadyJoinedException();
}
Presence presence = enter(mucEnterConfiguration);
// Look for confirmation of room creation from the server
MUCUser mucUser = MUCUser.from(presence);
if (mucUser != null && mucUser.getStatus().contains(Status.ROOM_CREATED_201)) {
// Room was created and the user has joined the room
return new MucCreateConfigFormHandle();
}
return null;
}
use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.
the class MultiUserChat method enter.
/**
* Enter a room, as described in XEP-45 7.2.
*
* @param conf the configuration used to enter the room.
* @return the returned presence by the service after the client send the initial presence in order to enter the room.
* @throws NotConnectedException
* @throws NoResponseException
* @throws XMPPErrorException
* @throws InterruptedException
* @throws NotAMucServiceException
* @see <a href="http://xmpp.org/extensions/xep-0045.html#enter">XEP-45 7.2 Entering a Room</a>
*/
private Presence enter(MucEnterConfiguration conf) throws NotConnectedException, NoResponseException, XMPPErrorException, InterruptedException, NotAMucServiceException {
final DomainBareJid mucService = room.asDomainBareJid();
if (!KNOWN_MUC_SERVICES.containsKey(mucService)) {
if (multiUserChatManager.providesMucService(mucService)) {
KNOWN_MUC_SERVICES.put(mucService, null);
} else {
throw new NotAMucServiceException(this);
}
}
// We enter a room by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
Presence joinPresence = conf.getJoinPresence(this);
// Setup the messageListeners and presenceListeners *before* the join presence is send.
connection.addSyncStanzaListener(messageListener, fromRoomGroupchatFilter);
connection.addSyncStanzaListener(presenceListener, new AndFilter(fromRoomFilter, StanzaTypeFilter.PRESENCE));
connection.addSyncStanzaListener(subjectListener, new AndFilter(fromRoomFilter, MessageWithSubjectFilter.INSTANCE, new NotFilter(MessageTypeFilter.ERROR)));
connection.addSyncStanzaListener(declinesListener, DECLINE_FILTER);
connection.addPacketInterceptor(presenceInterceptor, new AndFilter(ToMatchesFilter.create(room), StanzaTypeFilter.PRESENCE));
messageCollector = connection.createStanzaCollector(fromRoomGroupchatFilter);
// Wait for a presence packet back from the server.
// @formatter:off
StanzaFilter responseFilter = new AndFilter(StanzaTypeFilter.PRESENCE, new OrFilter(// We use a bare JID filter for positive responses, since the MUC service/room may rewrite the nickname.
new AndFilter(FromMatchesFilter.createBare(getRoom()), MUCUserStatusCodeFilter.STATUS_110_PRESENCE_TO_SELF), // JID we send the join presence to.
new AndFilter(FromMatchesFilter.createFull(joinPresence.getTo()), new StanzaIdFilter(joinPresence), PresenceTypeFilter.ERROR)));
// @formatter:on
Presence presence;
try {
presence = connection.createStanzaCollectorAndSend(responseFilter, joinPresence).nextResultOrThrow(conf.getTimeout());
} catch (NotConnectedException | InterruptedException | NoResponseException | XMPPErrorException e) {
// Ensure that all callbacks are removed if there is an exception
removeConnectionCallbacks();
throw e;
}
// This presence must be send from a full JID. We use the resourcepart of this JID as nick, since the room may
// performed roomnick rewriting
this.nickname = presence.getFrom().asEntityFullJidIfPossible().getResourcepart();
joined = true;
// Update the list of joined rooms
multiUserChatManager.addJoinedRoom(room);
return presence;
}
use of org.jivesoftware.smack.packet.Presence in project intellij-plugins by JetBrains.
the class JabberTransport_ConnectionTest method createLocalConnectionWithJabberUser.
private XMPPConnection createLocalConnectionWithJabberUser(String userName, String resource) throws XMPPException, InterruptedException {
final XMPPConnection conn = new XMPPConnection("localhost");
conn.getAccountManager().createAccount(userName, "123456");
if (resource == null) {
resource = "test";
}
conn.login(userName, "123456", resource);
conn.getRoster().setSubscriptionMode(Roster.SubscriptionMode.accept_all);
Presence presence = new Presence(Presence.Type.available);
conn.sendPacket(presence);
disposeOnTearDown(new Disposable() {
@Override
public void dispose() {
try {
if (conn.isConnected()) {
conn.getAccountManager().deleteAccount();
}
} catch (XMPPException e) {
throw new RuntimeException(e);
} finally {
Thread thread = new Thread("jabber conn test") {
@Override
public void run() {
conn.close();
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
return conn;
}
use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.
the class PresenceManager method requestSubscription.
/**
* Requests subscription to the contact.
*
* @param account
* @param bareAddress
* @throws NetworkException
*/
public void requestSubscription(String account, String bareAddress) throws NetworkException {
Presence packet = new Presence(Presence.Type.subscribe);
packet.setTo(bareAddress);
ConnectionManager.getInstance().sendStanza(account, packet);
HashSet<String> set = requestedSubscriptions.get(account);
if (set == null) {
set = new HashSet<>();
requestedSubscriptions.put(account, set);
}
set.add(bareAddress);
}
Aggregations