Search in sources :

Example 26 with Jid

use of eu.siacs.conversations.xmpp.jid.Jid in project Conversations by siacs.

the class PresenceParser method processConferencePresence.

private void processConferencePresence(PresencePacket packet, Conversation conversation) {
    MucOptions mucOptions = conversation.getMucOptions();
    final Jid from = packet.getFrom();
    if (!from.isBareJid()) {
        final String type = packet.getAttribute("type");
        final Element x = packet.findChild("x", "http://jabber.org/protocol/muc#user");
        Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
        final List<String> codes = getStatusCodes(x);
        if (type == null) {
            if (x != null) {
                Element item = x.findChild("item");
                if (item != null && !from.isBareJid()) {
                    mucOptions.setError(MucOptions.Error.NONE);
                    MucOptions.User user = parseItem(conversation, item, from);
                    if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE)) {
                        mucOptions.setOnline();
                        mucOptions.setSelf(user);
                        if (mucOptions.onRenameListener != null) {
                            mucOptions.onRenameListener.onSuccess();
                            mucOptions.onRenameListener = null;
                        }
                    }
                    mucOptions.updateUser(user);
                    if (codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED) && mucOptions.autoPushConfiguration()) {
                        Log.d(Config.LOGTAG, mucOptions.getAccount().getJid().toBareJid() + ": room '" + mucOptions.getConversation().getJid().toBareJid() + "' created. pushing default configuration");
                        mXmppConnectionService.pushConferenceConfiguration(mucOptions.getConversation(), IqGenerator.defaultRoomConfiguration(), null);
                    }
                    if (mXmppConnectionService.getPgpEngine() != null) {
                        Element signed = packet.findChild("x", "jabber:x:signed");
                        if (signed != null) {
                            Element status = packet.findChild("status");
                            String msg = status == null ? "" : status.getContent();
                            long keyId = mXmppConnectionService.getPgpEngine().fetchKeyId(mucOptions.getAccount(), msg, signed.getContent());
                            if (keyId != 0) {
                                user.setPgpKeyId(keyId);
                            }
                        }
                    }
                    if (avatar != null) {
                        avatar.owner = from;
                        if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
                            if (user.setAvatar(avatar)) {
                                mXmppConnectionService.getAvatarService().clear(user);
                            }
                        } else if (mXmppConnectionService.isDataSaverDisabled()) {
                            mXmppConnectionService.fetchAvatar(mucOptions.getAccount(), avatar);
                        }
                    }
                }
            }
        } else if (type.equals("unavailable")) {
            if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE) || packet.getFrom().equals(mucOptions.getConversation().getJid())) {
                if (codes.contains(MucOptions.STATUS_CODE_KICKED)) {
                    mucOptions.setError(MucOptions.Error.KICKED);
                } else if (codes.contains(MucOptions.STATUS_CODE_BANNED)) {
                    mucOptions.setError(MucOptions.Error.BANNED);
                } else if (codes.contains(MucOptions.STATUS_CODE_LOST_MEMBERSHIP)) {
                    mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
                } else if (codes.contains(MucOptions.STATUS_CODE_AFFILIATION_CHANGE)) {
                    mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
                } else if (codes.contains(MucOptions.STATUS_CODE_SHUTDOWN)) {
                    mucOptions.setError(MucOptions.Error.SHUTDOWN);
                } else if (!codes.contains(MucOptions.STATUS_CODE_CHANGED_NICK)) {
                    mucOptions.setError(MucOptions.Error.UNKNOWN);
                    Log.d(Config.LOGTAG, "unknown error in conference: " + packet);
                }
            } else if (!from.isBareJid()) {
                Element item = x.findChild("item");
                if (item != null) {
                    mucOptions.updateUser(parseItem(conversation, item, from));
                }
                MucOptions.User user = mucOptions.deleteUser(from);
                if (user != null) {
                    mXmppConnectionService.getAvatarService().clear(user);
                }
            }
        } else if (type.equals("error")) {
            Element error = packet.findChild("error");
            if (error != null && error.hasChild("conflict")) {
                if (mucOptions.online()) {
                    if (mucOptions.onRenameListener != null) {
                        mucOptions.onRenameListener.onFailure();
                        mucOptions.onRenameListener = null;
                    }
                } else {
                    mucOptions.setError(MucOptions.Error.NICK_IN_USE);
                }
            } else if (error != null && error.hasChild("not-authorized")) {
                mucOptions.setError(MucOptions.Error.PASSWORD_REQUIRED);
            } else if (error != null && error.hasChild("forbidden")) {
                mucOptions.setError(MucOptions.Error.BANNED);
            } else if (error != null && error.hasChild("registration-required")) {
                mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
            }
        }
    }
}
Also used : MucOptions(eu.siacs.conversations.entities.MucOptions) Jid(eu.siacs.conversations.xmpp.jid.Jid) Element(eu.siacs.conversations.xml.Element) Avatar(eu.siacs.conversations.xmpp.pep.Avatar)

Example 27 with Jid

use of eu.siacs.conversations.xmpp.jid.Jid in project Conversations by siacs.

the class PresenceParser method parseContactPresence.

public void parseContactPresence(final PresencePacket packet, final Account account) {
    final PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator();
    final Jid from = packet.getFrom();
    if (from == null || from.equals(account.getJid())) {
        return;
    }
    final String type = packet.getAttribute("type");
    final Contact contact = account.getRoster().getContact(from);
    if (type == null) {
        final String resource = from.isBareJid() ? "" : from.getResourcepart();
        contact.setPresenceName(packet.findChildContent("nick", "http://jabber.org/protocol/nick"));
        Avatar avatar = Avatar.parsePresence(packet.findChild("x", "vcard-temp:x:update"));
        if (avatar != null && (!contact.isSelf() || account.getAvatar() == null)) {
            avatar.owner = from.toBareJid();
            if (mXmppConnectionService.getFileBackend().isAvatarCached(avatar)) {
                if (avatar.owner.equals(account.getJid().toBareJid())) {
                    account.setAvatar(avatar.getFilename());
                    mXmppConnectionService.databaseBackend.updateAccount(account);
                    mXmppConnectionService.getAvatarService().clear(account);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateAccountUi();
                } else if (contact.setAvatar(avatar)) {
                    mXmppConnectionService.getAvatarService().clear(contact);
                    mXmppConnectionService.updateConversationUi();
                    mXmppConnectionService.updateRosterUi();
                }
            } else if (mXmppConnectionService.isDataSaverDisabled()) {
                mXmppConnectionService.fetchAvatar(account, avatar);
            }
        }
        int sizeBefore = contact.getPresences().size();
        final String show = packet.findChildContent("show");
        final Element caps = packet.findChild("c", "http://jabber.org/protocol/caps");
        final String message = packet.findChildContent("status");
        final Presence presence = Presence.parse(show, caps, message);
        contact.updatePresence(resource, presence);
        if (presence.hasCaps()) {
            mXmppConnectionService.fetchCaps(account, from, presence);
        }
        final Element idle = packet.findChild("idle", "urn:xmpp:idle:1");
        if (idle != null) {
            contact.flagInactive();
            String since = idle.getAttribute("since");
            try {
                contact.setLastseen(AbstractParser.parseTimestamp(since));
            } catch (NullPointerException | ParseException e) {
                contact.setLastseen(System.currentTimeMillis());
            }
        } else {
            contact.flagActive();
            contact.setLastseen(AbstractParser.parseTimestamp(packet));
        }
        PgpEngine pgp = mXmppConnectionService.getPgpEngine();
        Element x = packet.findChild("x", "jabber:x:signed");
        if (pgp != null && x != null) {
            Element status = packet.findChild("status");
            String msg = status != null ? status.getContent() : "";
            contact.setPgpKeyId(pgp.fetchKeyId(account, msg, x.getContent()));
        }
        boolean online = sizeBefore < contact.getPresences().size();
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online);
    } else if (type.equals("unavailable")) {
        if (from.isBareJid()) {
            contact.clearPresences();
        } else {
            contact.removePresence(from.getResourcepart());
        }
        mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false);
    } else if (type.equals("subscribe")) {
        if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) {
            mXmppConnectionService.sendPresencePacket(account, mPresenceGenerator.sendPresenceUpdatesTo(contact));
        } else {
            contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST);
            final Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, contact.getJid().toBareJid(), false);
            final String statusMessage = packet.findChildContent("status");
            if (statusMessage != null && !statusMessage.isEmpty() && conversation.countMessages() == 0) {
                conversation.add(new Message(conversation, statusMessage, Message.ENCRYPTION_NONE, Message.STATUS_RECEIVED));
            }
        }
    }
    mXmppConnectionService.updateRosterUi();
}
Also used : PresenceGenerator(eu.siacs.conversations.generator.PresenceGenerator) Jid(eu.siacs.conversations.xmpp.jid.Jid) Message(eu.siacs.conversations.entities.Message) Element(eu.siacs.conversations.xml.Element) PgpEngine(eu.siacs.conversations.crypto.PgpEngine) Conversation(eu.siacs.conversations.entities.Conversation) Avatar(eu.siacs.conversations.xmpp.pep.Avatar) Contact(eu.siacs.conversations.entities.Contact) Presence(eu.siacs.conversations.entities.Presence) ParseException(java.text.ParseException)

Example 28 with Jid

use of eu.siacs.conversations.xmpp.jid.Jid in project Conversations by siacs.

the class AbstractParser method parseItem.

public static MucOptions.User parseItem(Conversation conference, Element item, Jid fullJid) {
    final String local = conference.getJid().getLocalpart();
    final String domain = conference.getJid().getDomainpart();
    String affiliation = item.getAttribute("affiliation");
    String role = item.getAttribute("role");
    String nick = item.getAttribute("nick");
    if (nick != null && fullJid == null) {
        try {
            fullJid = Jid.fromParts(local, domain, nick);
        } catch (InvalidJidException e) {
            fullJid = null;
        }
    }
    Jid realJid = item.getAttributeAsJid("jid");
    MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid);
    user.setRealJid(realJid);
    user.setAffiliation(affiliation);
    user.setRole(role);
    return user;
}
Also used : MucOptions(eu.siacs.conversations.entities.MucOptions) Jid(eu.siacs.conversations.xmpp.jid.Jid) InvalidJidException(eu.siacs.conversations.xmpp.jid.InvalidJidException)

Example 29 with Jid

use of eu.siacs.conversations.xmpp.jid.Jid in project Conversations by siacs.

the class IqParser method onIqPacketReceived.

@Override
public void onIqPacketReceived(final Account account, final IqPacket packet) {
    final boolean isGet = packet.getType() == IqPacket.TYPE.GET;
    if (packet.getType() == IqPacket.TYPE.ERROR || packet.getType() == IqPacket.TYPE.TIMEOUT) {
        return;
    } else if (packet.hasChild("query", Namespace.ROSTER) && packet.fromServer(account)) {
        final Element query = packet.findChild("query");
        // If this is in response to a query for the whole roster:
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            account.getRoster().markAllAsNotInRoster();
        }
        this.rosterItems(account, query);
    } else if ((packet.hasChild("block", Namespace.BLOCKING) || packet.hasChild("blocklist", Namespace.BLOCKING)) && packet.fromServer(account)) {
        // Block list or block push.
        Log.d(Config.LOGTAG, "Received blocklist update from server");
        final Element blocklist = packet.findChild("blocklist", Namespace.BLOCKING);
        final Element block = packet.findChild("block", Namespace.BLOCKING);
        final Collection<Element> items = blocklist != null ? blocklist.getChildren() : (block != null ? block.getChildren() : null);
        // Otherwise, just update the existing blocklist.
        if (packet.getType() == IqPacket.TYPE.RESULT) {
            account.clearBlocklist();
            account.getXmppConnection().getFeatures().setBlockListRequested(true);
        }
        if (items != null) {
            final Collection<Jid> jids = new ArrayList<>(items.size());
            // Create a collection of Jids from the packet
            for (final Element item : items) {
                if (item.getName().equals("item")) {
                    final Jid jid = item.getAttributeAsJid("jid");
                    if (jid != null) {
                        jids.add(jid);
                    }
                }
            }
            account.getBlocklist().addAll(jids);
            if (packet.getType() == IqPacket.TYPE.SET) {
                boolean removed = false;
                for (Jid jid : jids) {
                    removed |= mXmppConnectionService.removeBlockedConversations(account, jid);
                }
                if (removed) {
                    mXmppConnectionService.updateConversationUi();
                }
            }
        }
        // Update the UI
        mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED);
        if (packet.getType() == IqPacket.TYPE.SET) {
            final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
            mXmppConnectionService.sendIqPacket(account, response, null);
        }
    } else if (packet.hasChild("unblock", Namespace.BLOCKING) && packet.fromServer(account) && packet.getType() == IqPacket.TYPE.SET) {
        Log.d(Config.LOGTAG, "Received unblock update from server");
        final Collection<Element> items = packet.findChild("unblock", Namespace.BLOCKING).getChildren();
        if (items.size() == 0) {
            // No children to unblock == unblock all
            account.getBlocklist().clear();
        } else {
            final Collection<Jid> jids = new ArrayList<>(items.size());
            for (final Element item : items) {
                if (item.getName().equals("item")) {
                    final Jid jid = item.getAttributeAsJid("jid");
                    if (jid != null) {
                        jids.add(jid);
                    }
                }
            }
            account.getBlocklist().removeAll(jids);
        }
        mXmppConnectionService.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED);
        final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("open", "http://jabber.org/protocol/ibb") || packet.hasChild("data", "http://jabber.org/protocol/ibb") || packet.hasChild("close", "http://jabber.org/protocol/ibb")) {
        mXmppConnectionService.getJingleConnectionManager().deliverIbbPacket(account, packet);
    } else if (packet.hasChild("query", "http://jabber.org/protocol/disco#info")) {
        final IqPacket response = mXmppConnectionService.getIqGenerator().discoResponse(packet);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("query", "jabber:iq:version") && isGet) {
        final IqPacket response = mXmppConnectionService.getIqGenerator().versionResponse(packet);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("ping", "urn:xmpp:ping") && isGet) {
        final IqPacket response = packet.generateResponse(IqPacket.TYPE.RESULT);
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else if (packet.hasChild("time", "urn:xmpp:time") && isGet) {
        final IqPacket response;
        if (mXmppConnectionService.useTorToConnect()) {
            response = packet.generateResponse(IqPacket.TYPE.ERROR);
            final Element error = response.addChild("error");
            error.setAttribute("type", "cancel");
            error.addChild("not-allowed", "urn:ietf:params:xml:ns:xmpp-stanzas");
        } else {
            response = mXmppConnectionService.getIqGenerator().entityTimeResponse(packet);
        }
        mXmppConnectionService.sendIqPacket(account, response, null);
    } else {
        if (packet.getType() == IqPacket.TYPE.GET || packet.getType() == IqPacket.TYPE.SET) {
            final IqPacket response = packet.generateResponse(IqPacket.TYPE.ERROR);
            final Element error = response.addChild("error");
            error.setAttribute("type", "cancel");
            error.addChild("feature-not-implemented", "urn:ietf:params:xml:ns:xmpp-stanzas");
            account.getXmppConnection().sendIqPacket(response, null);
        }
    }
}
Also used : Jid(eu.siacs.conversations.xmpp.jid.Jid) Element(eu.siacs.conversations.xml.Element) ArrayList(java.util.ArrayList) Collection(java.util.Collection) IqPacket(eu.siacs.conversations.xmpp.stanzas.IqPacket)

Example 30 with Jid

use of eu.siacs.conversations.xmpp.jid.Jid in project Conversations by siacs.

the class MessageParser method extractChatState.

private boolean extractChatState(Conversation c, final boolean isTypeGroupChat, final MessagePacket packet) {
    ChatState state = ChatState.parse(packet);
    if (state != null && c != null) {
        final Account account = c.getAccount();
        Jid from = packet.getFrom();
        if (from.toBareJid().equals(account.getJid().toBareJid())) {
            c.setOutgoingChatState(state);
            if (state == ChatState.ACTIVE || state == ChatState.COMPOSING) {
                mXmppConnectionService.markRead(c);
                activateGracePeriod(account);
            }
            return false;
        } else {
            if (isTypeGroupChat) {
                MucOptions.User user = c.getMucOptions().findUserByFullJid(from);
                if (user != null) {
                    return user.setChatState(state);
                } else {
                    return false;
                }
            } else {
                return c.setIncomingChatState(state);
            }
        }
    }
    return false;
}
Also used : ChatState(eu.siacs.conversations.xmpp.chatstate.ChatState) Account(eu.siacs.conversations.entities.Account) MucOptions(eu.siacs.conversations.entities.MucOptions) Jid(eu.siacs.conversations.xmpp.jid.Jid)

Aggregations

Jid (eu.siacs.conversations.xmpp.jid.Jid)59 Account (eu.siacs.conversations.entities.Account)22 InvalidJidException (eu.siacs.conversations.xmpp.jid.InvalidJidException)19 Element (eu.siacs.conversations.xml.Element)17 IqPacket (eu.siacs.conversations.xmpp.stanzas.IqPacket)14 Conversation (eu.siacs.conversations.entities.Conversation)13 Contact (eu.siacs.conversations.entities.Contact)9 MucOptions (eu.siacs.conversations.entities.MucOptions)9 OnIqPacketReceived (eu.siacs.conversations.xmpp.OnIqPacketReceived)9 Message (eu.siacs.conversations.entities.Message)7 ArrayList (java.util.ArrayList)6 Intent (android.content.Intent)5 SuppressLint (android.annotation.SuppressLint)4 AlertDialog (android.app.AlertDialog)4 TextView (android.widget.TextView)4 AxolotlService (eu.siacs.conversations.crypto.axolotl.AxolotlService)4 Bookmark (eu.siacs.conversations.entities.Bookmark)4 MessagePacket (eu.siacs.conversations.xmpp.stanzas.MessagePacket)4 HashMap (java.util.HashMap)4 PendingIntent (android.app.PendingIntent)3