Search in sources :

Example 6 with MUCUser

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;
}
Also used : MucAlreadyJoinedException(org.jivesoftware.smackx.muc.MultiUserChatException.MucAlreadyJoinedException) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence)

Example 7 with MUCUser

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);
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message)

Example 8 with MUCUser

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);
        }
    }
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) UserJid(com.xabber.android.data.entity.UserJid) AccountJid(com.xabber.android.data.entity.AccountJid) DomainBareJid(org.jxmpp.jid.DomainBareJid) EntityBareJid(org.jxmpp.jid.EntityBareJid) Jid(org.jxmpp.jid.Jid) Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) AccountJid(com.xabber.android.data.entity.AccountJid) UserJid(com.xabber.android.data.entity.UserJid)

Example 9 with MUCUser

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;
}
Also used : MUCItem(org.jivesoftware.smackx.muc.packet.MUCItem) MUCAffiliation(org.jivesoftware.smackx.muc.MUCAffiliation) StatusMode(com.xabber.android.data.account.StatusMode) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) MUCRole(org.jivesoftware.smackx.muc.MUCRole) NoSuchElementException(java.util.NoSuchElementException)

Example 10 with MUCUser

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;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) Message(org.jivesoftware.smack.packet.Message) UserJid(com.xabber.android.data.entity.UserJid) Date(java.util.Date) Resourcepart(org.jxmpp.jid.parts.Resourcepart) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Presence(org.jivesoftware.smack.packet.Presence) Realm(io.realm.Realm)

Aggregations

MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)17 Message (org.jivesoftware.smack.packet.Message)13 Presence (org.jivesoftware.smack.packet.Presence)6 AccountItem (com.xabber.android.data.account.AccountItem)3 UserJid (com.xabber.android.data.entity.UserJid)3 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)3 Resourcepart (org.jxmpp.jid.parts.Resourcepart)3 StatusMode (com.xabber.android.data.account.StatusMode)2 AccountJid (com.xabber.android.data.entity.AccountJid)2 OTRUnencryptedException (com.xabber.android.data.extension.otr.OTRUnencryptedException)2 MucPrivateChatNotification (com.xabber.android.data.message.chat.MucPrivateChatNotification)2 Date (java.util.Date)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 OtrException (net.java.otr4j.OtrException)2 MUCItem (org.jivesoftware.smackx.muc.packet.MUCItem)2 MetaData (org.jivesoftware.smackx.workgroup.MetaData)2 SessionID (org.jivesoftware.smackx.workgroup.packet.SessionID)2 NetworkException (com.xabber.android.data.NetworkException)1 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1