use of org.jivesoftware.smackx.muc.packet.MUCUser 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.smackx.muc.packet.MUCUser in project xabber-android by redsolution.
the class MUCManager method invite.
/**
* Sends invitation.
*
* @throws NetworkException
*/
public void invite(AccountJid account, EntityBareJid room, UserJid user) throws NetworkException {
RoomChat roomChat = getRoomChat(account, room);
if (roomChat == null || roomChat.getState() != RoomState.available) {
Application.getInstance().onError(R.string.NOT_CONNECTED);
return;
}
Message message = new Message(room);
MUCUser mucUser = new MUCUser();
MUCUser.Invite invite = new MUCUser.Invite("", null, user.getBareJid().asEntityBareJidIfPossible());
mucUser.setInvite(invite);
message.addExtension(mucUser);
StanzaSender.sendStanza(account, message);
roomChat.putInvite(message.getStanzaId(), user);
roomChat.newAction(roomChat.getNickname(), user.toString(), ChatAction.invite_sent);
}
use of org.jivesoftware.smackx.muc.packet.MUCUser in project xabber-android by redsolution.
the class MUCManager method onStanza.
@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
if (!(connection instanceof AccountItem)) {
return;
}
AccountJid account = ((AccountItem) connection).getAccount();
Jid from = stanza.getFrom();
if (from == null || !(stanza instanceof Message)) {
return;
}
Message message = (Message) stanza;
if (message.getType() != Message.Type.normal && message.getType() != Message.Type.chat) {
return;
}
MUCUser mucUser = MUCUser.from(stanza);
if (mucUser == null || mucUser.getInvite() == null) {
return;
}
RoomChat roomChat = getRoomChat(account, from.asEntityBareJidIfPossible());
if (roomChat == null || !roomChat.getState().inUse()) {
UserJid inviter = null;
try {
inviter = UserJid.from(mucUser.getInvite().getFrom());
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
if (inviter == null) {
try {
inviter = UserJid.from(from);
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
}
try {
inviteProvider.add(new RoomInvite(account, UserJid.from(from), inviter, mucUser.getInvite().getReason(), mucUser.getPassword()), true);
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
}
}
use of org.jivesoftware.smackx.muc.packet.MUCUser in project xabber-android by redsolution.
the class RoomChat method createOccupant.
/**
* Warning: this method should be placed with packet provider.
*
* @return New occupant based on presence information.
*/
private Occupant createOccupant(Resourcepart resource, Presence presence) {
Occupant occupant = new Occupant(resource);
org.jxmpp.jid.Jid jid = null;
MUCAffiliation affiliation = MUCAffiliation.none;
MUCRole role = MUCRole.none;
StatusMode statusMode = StatusMode.unavailable;
String statusText = null;
MUCUser mucUser = MUCUser.from(presence);
if (mucUser != null) {
MUCItem item = mucUser.getItem();
if (item != null) {
jid = item.getJid();
try {
affiliation = item.getAffiliation();
} catch (NoSuchElementException e) {
}
try {
role = item.getRole();
} catch (NoSuchElementException e) {
}
statusMode = StatusMode.createStatusMode(presence);
statusText = presence.getStatus();
}
}
if (statusText == null) {
statusText = "";
}
occupant.setJid(jid);
occupant.setAffiliation(affiliation);
occupant.setRole(role);
occupant.setStatusMode(statusMode);
occupant.setStatusText(statusText);
return occupant;
}
use of org.jivesoftware.smackx.muc.packet.MUCUser in project xabber-android by redsolution.
the class RoomChat method onPacket.
@Override
protected boolean onPacket(UserJid bareAddress, Stanza stanza, boolean isCarbons) {
if (!super.onPacket(bareAddress, stanza, isCarbons)) {
return false;
}
// MUCUser mucUserExtension = MUCUser.from(stanza);
// if (mucUserExtension != null && mucUserExtension.getInvite() != null) {
// return false;
// }
final org.jxmpp.jid.Jid from = stanza.getFrom();
final Resourcepart resource = from.getResourceOrNull();
if (stanza instanceof Message) {
final Message message = (Message) stanza;
if (message.getType() == Message.Type.error) {
UserJid invite = invites.remove(message.getStanzaId());
if (invite != null) {
newAction(nickname, invite.toString(), ChatAction.invite_error);
}
return true;
}
MUCUser mucUser = MUCUser.from(stanza);
if (mucUser != null && mucUser.getDecline() != null) {
onInvitationDeclined(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
return true;
}
if (mucUser != null && mucUser.getStatus() != null && mucUser.getStatus().contains(MUCUser.Status.create("100")) && ChatManager.getInstance().isSuppress100(account, user)) {
// 'This room is not anonymous'
return true;
}
final String text = message.getBody();
final String subject = message.getSubject();
if (text == null && subject == null) {
return true;
}
if (subject != null) {
if (this.subject.equals(subject)) {
return true;
}
this.subject = subject;
RosterManager.onContactChanged(account, bareAddress);
newAction(resource, subject, ChatAction.subject);
} else {
boolean notify = true;
String stanzaId = message.getStanzaId();
// disabling because new messages without stanza will be repeated
// if (stanzaId == null) stanzaId = UUID.randomUUID().toString();
DelayInformation delayInformation = DelayInformation.from(message);
Date delay = null;
if (delayInformation != null) {
delay = delayInformation.getStamp();
}
if (delay != null) {
notify = false;
}
final long startTime = System.currentTimeMillis();
Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
final MessageItem sameMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.STANZA_ID, stanzaId).findFirst();
// Server send our own message back
if (sameMessage != null) {
// realm.beginTransaction();
// sameMessage.setDelivered(true);
// realm.commitTransaction();
LogManager.d("REALM", Thread.currentThread().getName() + " save message delivered: " + (System.currentTimeMillis() - startTime));
return true;
}
if (isSelf(resource)) {
// Own message from other client
notify = false;
}
updateThreadId(message.getThread());
createAndSaveNewMessage(resource, text, null, delay, true, notify, false, false, stanzaId);
}
} else if (stanza instanceof Presence) {
Presence presence = (Presence) stanza;
if (presence.getType() == Presence.Type.available) {
Occupant oldOccupant = occupants.get(resource);
Occupant newOccupant = createOccupant(resource, presence);
newOccupant.setJid(from);
occupants.put(resource, newOccupant);
if (oldOccupant == null) {
onAvailable(resource);
RosterManager.onContactChanged(account, user);
} else {
boolean changed = false;
if (oldOccupant.getAffiliation() != newOccupant.getAffiliation()) {
changed = true;
onAffiliationChanged(resource, newOccupant.getAffiliation());
}
if (oldOccupant.getRole() != newOccupant.getRole()) {
changed = true;
onRoleChanged(resource, newOccupant.getRole());
}
if (oldOccupant.getStatusMode() != newOccupant.getStatusMode() || !oldOccupant.getStatusText().equals(newOccupant.getStatusText())) {
changed = true;
onStatusChanged(resource, newOccupant.getStatusMode(), newOccupant.getStatusText());
}
if (changed) {
RosterManager.onContactChanged(account, user);
}
}
} else if (presence.getType() == Presence.Type.unavailable && state == RoomState.available) {
occupants.remove(resource);
MUCUser mucUser = MUCUser.from(presence);
if (mucUser != null && mucUser.getStatus() != null) {
if (mucUser.getStatus().contains(MUCUser.Status.KICKED_307)) {
onKick(resource, mucUser.getItem().getActor());
} else if (mucUser.getStatus().contains(MUCUser.Status.BANNED_301)) {
onBan(resource, mucUser.getItem().getActor());
} else if (mucUser.getStatus().contains(MUCUser.Status.NEW_NICKNAME_303)) {
Resourcepart newNick = mucUser.getItem().getNick();
if (newNick == null) {
return true;
}
onRename(resource, newNick);
Occupant occupant = createOccupant(newNick, presence);
occupants.put(newNick, occupant);
} else if (mucUser.getStatus().contains(MUCUser.Status.REMOVED_AFFIL_CHANGE_321)) {
onRevoke(resource, mucUser.getItem().getActor());
}
} else {
onLeave(resource);
}
RosterManager.onContactChanged(account, user);
}
}
return true;
}
Aggregations