Search in sources :

Example 6 with MucOptions

use of de.pixart.messenger.entities.MucOptions in project Pix-Art-Messenger by kriztan.

the class PresenceParser method processConferencePresence.

private void processConferencePresence(PresencePacket packet, Conversation conversation) {
    MucOptions mucOptions = conversation.getMucOptions();
    final Jid jid = conversation.getAccount().getJid();
    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) || (codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED) && jid.equals(item.getAttributeAsJid("jid")))) {
                        if (mucOptions.setOnline()) {
                            mXmppConnectionService.getAvatarService().clear(mucOptions);
                        }
                        mucOptions.setSelf(user);
                        mXmppConnectionService.persistSelfNick(user);
                        invokeRenameListener(mucOptions, true);
                    }
                    boolean isNew = mucOptions.updateUser(user);
                    final AxolotlService axolotlService = conversation.getAccount().getAxolotlService();
                    Contact contact = user.getContact();
                    if (isNew && user.getRealJid() != null && mucOptions.isPrivateAndNonAnonymous() && (contact == null || !contact.mutualPresenceSubscription()) && axolotlService.hasEmptyDeviceList(user.getRealJid())) {
                        axolotlService.fetchDeviceIds(user.getRealJid());
                    }
                    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_SHUTDOWN) && from.equals(mucOptions.getSelf().getFullJid())) {
                mucOptions.setError(MucOptions.Error.SHUTDOWN);
            } else if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE)) {
                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")) {
            final Element error = packet.findChild("error");
            if (error == null) {
                return;
            }
            if (error.hasChild("conflict")) {
                if (mucOptions.online()) {
                    invokeRenameListener(mucOptions, false);
                } else {
                    mucOptions.setError(MucOptions.Error.NICK_IN_USE);
                }
            } else if (error.hasChild("not-authorized")) {
                mucOptions.setError(MucOptions.Error.PASSWORD_REQUIRED);
            } else if (error.hasChild("forbidden")) {
                mucOptions.setError(MucOptions.Error.BANNED);
            } else if (error.hasChild("registration-required")) {
                mucOptions.setError(MucOptions.Error.MEMBERS_ONLY);
            } else {
                final String text = error.findChildContent("text");
                if (text != null && text.contains("attribute 'to'")) {
                    if (mucOptions.online()) {
                        invokeRenameListener(mucOptions, false);
                    } else {
                        mucOptions.setError(MucOptions.Error.INVALID_NICK);
                    }
                } else {
                    mucOptions.setError(MucOptions.Error.UNKNOWN);
                    Log.d(Config.LOGTAG, "unknown error in conference: " + packet);
                }
            }
        }
    }
}
Also used : AxolotlService(de.pixart.messenger.crypto.axolotl.AxolotlService) MucOptions(de.pixart.messenger.entities.MucOptions) Jid(de.pixart.messenger.xmpp.jid.Jid) Element(de.pixart.messenger.xml.Element) Avatar(de.pixart.messenger.xmpp.pep.Avatar) Contact(de.pixart.messenger.entities.Contact)

Example 7 with MucOptions

use of de.pixart.messenger.entities.MucOptions in project Pix-Art-Messenger by kriztan.

the class ConversationFragment method onCreateView.

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.binding = DataBindingUtil.inflate(inflater, R.layout.fragment_conversation, container, false);
    // TODO why the fuck did we do this?
    binding.getRoot().setOnClickListener(null);
    binding.textinput.addTextChangedListener(new StylingHelper.MessageEditorStyler(binding.textinput));
    binding.textinput.setOnEditorActionListener(mEditorActionListener);
    binding.textinput.setRichContentListener(new String[] { "image/*" }, mEditorContentListener);
    binding.textSendButton.setOnClickListener(this.mSendButtonListener);
    binding.textSendButton.setOnLongClickListener(this.mSendButtonLongListener);
    binding.messagesView.setOnScrollListener(mOnScrollListener);
    binding.messagesView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
    messageListAdapter = new MessageAdapter((XmppActivity) getActivity(), this.messageList);
    messageListAdapter.setOnContactPictureClicked(message -> {
        final boolean received = message.getStatus() <= Message.STATUS_RECEIVED;
        if (received) {
            if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
                Jid user = message.getCounterpart();
                if (user != null && !user.isBareJid()) {
                    if (!message.getConversation().getMucOptions().isUserInRoom(user)) {
                        Toast.makeText(activity, getActivity().getString(R.string.user_has_left_conference, user.getResourcepart()), Toast.LENGTH_SHORT).show();
                    }
                    highlightInConference(user.getResourcepart());
                }
                return;
            } else {
                if (!message.getContact().isSelf()) {
                    String fingerprint;
                    if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
                        fingerprint = "pgp";
                    } else {
                        fingerprint = message.getFingerprint();
                    }
                    activity.switchToContactDetails(message.getContact(), fingerprint);
                    return;
                }
            }
        }
        Account account = message.getConversation().getAccount();
        Intent intent = new Intent(activity, EditAccountActivity.class);
        intent.putExtra("jid", account.getJid().toBareJid().toString());
        String fingerprint;
        if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
            fingerprint = "pgp";
        } else {
            fingerprint = message.getFingerprint();
        }
        intent.putExtra("fingerprint", fingerprint);
        startActivity(intent);
    });
    messageListAdapter.setOnContactPictureLongClicked(message -> {
        if (message.getStatus() <= Message.STATUS_RECEIVED) {
            if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
                final MucOptions mucOptions = conversation.getMucOptions();
                if (!mucOptions.allowPm()) {
                    Toast.makeText(getActivity(), R.string.private_messages_are_disabled, Toast.LENGTH_SHORT).show();
                    return;
                }
                Jid user = message.getCounterpart();
                if (user != null && !user.isBareJid()) {
                    if (mucOptions.isUserInRoom(user)) {
                        privateMessageWith(user);
                    } else {
                        Toast.makeText(getActivity(), activity.getString(R.string.user_has_left_conference, user.getResourcepart()), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        } else {
            activity.showQrCode(conversation.getAccount().getShareableUri());
        }
    });
    messageListAdapter.setOnQuoteListener(this::quoteText);
    binding.messagesView.setAdapter(messageListAdapter);
    registerForContextMenu(binding.messagesView);
    return binding.getRoot();
}
Also used : MessageAdapter(de.pixart.messenger.ui.adapter.MessageAdapter) Account(de.pixart.messenger.entities.Account) MucOptions(de.pixart.messenger.entities.MucOptions) Jid(de.pixart.messenger.xmpp.jid.Jid) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) StylingHelper(de.pixart.messenger.utils.StylingHelper)

Example 8 with MucOptions

use of de.pixart.messenger.entities.MucOptions in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method renameInMuc.

public boolean renameInMuc(final Conversation conversation, final String nick, final UiCallback<Conversation> callback) {
    final MucOptions options = conversation.getMucOptions();
    final Jid joinJid = options.createJoinJid(nick);
    if (joinJid == null) {
        return false;
    }
    if (options.online()) {
        Account account = conversation.getAccount();
        options.setOnRenameListener(new OnRenameListener() {

            @Override
            public void onSuccess() {
                callback.success(conversation);
            }

            @Override
            public void onFailure() {
                callback.error(R.string.nick_in_use, conversation);
            }
        });
        PresencePacket packet = new PresencePacket();
        packet.setTo(joinJid);
        packet.setFrom(conversation.getAccount().getJid());
        String sig = account.getPgpSignature();
        if (sig != null) {
            packet.addChild("status").setContent("online");
            packet.addChild("x", "jabber:x:signed").setContent(sig);
        }
        sendPresencePacket(account, packet);
    } else {
        conversation.setContactJid(joinJid);
        databaseBackend.updateConversation(conversation);
        if (conversation.getAccount().getStatus() == Account.State.ONLINE) {
            Bookmark bookmark = conversation.getBookmark();
            if (bookmark != null) {
                bookmark.setNick(nick);
                pushBookmarks(bookmark.getAccount());
            }
            joinMuc(conversation);
        }
    }
    return true;
}
Also used : Account(de.pixart.messenger.entities.Account) OnRenameListener(de.pixart.messenger.entities.MucOptions.OnRenameListener) MucOptions(de.pixart.messenger.entities.MucOptions) Jid(de.pixart.messenger.xmpp.jid.Jid) Bookmark(de.pixart.messenger.entities.Bookmark) PresencePacket(de.pixart.messenger.xmpp.stanzas.PresencePacket)

Aggregations

MucOptions (de.pixart.messenger.entities.MucOptions)8 Jid (de.pixart.messenger.xmpp.jid.Jid)5 Account (de.pixart.messenger.entities.Account)4 Conversation (de.pixart.messenger.entities.Conversation)3 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 Contact (de.pixart.messenger.entities.Contact)2 Element (de.pixart.messenger.xml.Element)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 SendIntentException (android.content.IntentSender.SendIntentException)1 Resources (android.content.res.Resources)1 DataBindingUtil (android.databinding.DataBindingUtil)1 Bitmap (android.graphics.Bitmap)1 PorterDuff (android.graphics.PorterDuff)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Drawable (android.graphics.drawable.Drawable)1