Search in sources :

Example 26 with MessagePacket

use of de.pixart.messenger.xmpp.stanzas.MessagePacket in project Pix-Art-Messenger by kriztan.

the class MessageGenerator method received.

public MessagePacket received(Account account, MessagePacket originalMessage, ArrayList<String> namespaces, int type) {
    MessagePacket receivedPacket = new MessagePacket();
    receivedPacket.setType(type);
    receivedPacket.setTo(originalMessage.getFrom());
    receivedPacket.setFrom(account.getJid());
    for (String namespace : namespaces) {
        receivedPacket.addChild("received", namespace).setAttribute("id", originalMessage.getId());
    }
    receivedPacket.addChild("store", "urn:xmpp:hints");
    return receivedPacket;
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket)

Example 27 with MessagePacket

use of de.pixart.messenger.xmpp.stanzas.MessagePacket in project Pix-Art-Messenger by kriztan.

the class OtrService method injectMessage.

@Override
public void injectMessage(SessionID session, String body) throws OtrException {
    MessagePacket packet = new MessagePacket();
    packet.setFrom(account.getJid());
    if (session.getUserID().isEmpty()) {
        packet.setAttribute("to", session.getAccountID());
    } else {
        packet.setAttribute("to", session.getAccountID() + "/" + session.getUserID());
    }
    packet.setBody(body);
    MessageGenerator.addMessageHints(packet);
    try {
        Jid jid = Jid.fromSessionID(session);
        Conversation conversation = mXmppConnectionService.find(account, jid);
        if (conversation != null && conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
            if (mXmppConnectionService.sendChatStates()) {
                packet.addChild(ChatState.toElement(conversation.getOutgoingChatState()));
            }
        }
    } catch (final InvalidJidException ignored) {
    }
    packet.setType(MessagePacket.TYPE_CHAT);
    packet.addChild("encryption", "urn:xmpp:eme:0").setAttribute("namespace", "urn:xmpp:otr:0");
    account.getXmppConnection().sendMessagePacket(packet);
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) Conversation(de.pixart.messenger.entities.Conversation)

Example 28 with MessagePacket

use of de.pixart.messenger.xmpp.stanzas.MessagePacket in project Pix-Art-Messenger by kriztan.

the class AxolotlService method completeSession.

private void completeSession(XmppAxolotlSession session) {
    final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().toBareJid(), getOwnDeviceId());
    axolotlMessage.addDevice(session);
    try {
        Jid jid = Jid.fromString(session.getRemoteAddress().getName());
        MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateKeyTransportMessage(jid, axolotlMessage);
        mXmppConnectionService.sendMessagePacket(account, packet);
    } catch (InvalidJidException e) {
        throw new Error("Remote addresses are created from jid and should convert back to jid", e);
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Jid(de.pixart.messenger.xmpp.jid.Jid) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException)

Example 29 with MessagePacket

use of de.pixart.messenger.xmpp.stanzas.MessagePacket in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method sendReadMarker.

public void sendReadMarker(final Conversation conversation) {
    final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous();
    final Message markable = conversation.getLatestMarkableMessage(isPrivateAndNonAnonymousMuc);
    if (this.markRead(conversation)) {
        updateConversationUi();
    }
    if (confirmMessages() && markable != null && (markable.trusted() || isPrivateAndNonAnonymousMuc) && markable.getRemoteMsgId() != null) {
        Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
        Account account = conversation.getAccount();
        final Jid to = markable.getCounterpart();
        final boolean groupChat = conversation.getMode() == Conversation.MODE_MULTI;
        MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId(), markable.getCounterpart(), groupChat);
        this.sendMessagePacket(conversation.getAccount(), packet);
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) Account(de.pixart.messenger.entities.Account) XmppAxolotlMessage(de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage) Message(de.pixart.messenger.entities.Message) Jid(de.pixart.messenger.xmpp.jid.Jid)

Example 30 with MessagePacket

use of de.pixart.messenger.xmpp.stanzas.MessagePacket in project Pix-Art-Messenger by kriztan.

the class XmppConnection method processStream.

private void processStream() throws XmlPullParserException, IOException, NoSuchAlgorithmException {
    final CountDownLatch streamCountDownLatch = new CountDownLatch(1);
    this.mStreamCountDownLatch = streamCountDownLatch;
    Tag nextTag = tagReader.readTag();
    while (nextTag != null && !nextTag.isEnd("stream")) {
        if (nextTag.isStart("error")) {
            processStreamError(nextTag);
        } else if (nextTag.isStart("features")) {
            processStreamFeatures(nextTag);
        } else if (nextTag.isStart("proceed")) {
            switchOverToTls(nextTag);
        } else if (nextTag.isStart("success")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            try {
                saslMechanism.getResponse(challenge);
            } catch (final SaslMechanism.AuthenticationException e) {
                Log.e(Config.LOGTAG, String.valueOf(e));
                throw new StateChangingException(Account.State.UNAUTHORIZED);
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
            account.setKey(Account.PINNED_MECHANISM_KEY, String.valueOf(saslMechanism.getPriority()));
            tagReader.reset();
            sendStartStream();
            final Tag tag = tagReader.readTag();
            if (tag != null && tag.isStart("stream")) {
                processStream();
            } else {
                throw new IOException("server didn't restart stream after successful auth");
            }
            break;
        } else if (nextTag.isStart("failure")) {
            final Element failure = tagReader.readElement(nextTag);
            if (Namespace.SASL.equals(failure.getNamespace())) {
                final String text = failure.findChildContent("text");
                if (failure.hasChild("account-disabled") && text != null) {
                    Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(text);
                    if (matcher.find()) {
                        try {
                            URL url = new URL(text.substring(matcher.start(), matcher.end()));
                            if (url.getProtocol().equals("https")) {
                                this.redirectionUrl = url;
                                throw new StateChangingException(Account.State.PAYMENT_REQUIRED);
                            }
                        } catch (MalformedURLException e) {
                            throw new StateChangingException(Account.State.UNAUTHORIZED);
                        }
                    }
                }
                throw new StateChangingException(Account.State.UNAUTHORIZED);
            } else if (Namespace.TLS.equals(failure.getNamespace())) {
                throw new StateChangingException(Account.State.TLS_ERROR);
            } else {
                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
            }
        } else if (nextTag.isStart("challenge")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            final Element response = new Element("response", Namespace.SASL);
            try {
                response.setContent(saslMechanism.getResponse(challenge));
            } catch (final SaslMechanism.AuthenticationException e) {
                // TODO: Send auth abort tag.
                Log.e(Config.LOGTAG, e.toString());
            }
            tagWriter.writeElement(response);
        } else if (nextTag.isStart("enabled")) {
            final Element enabled = tagReader.readElement(nextTag);
            if ("true".equals(enabled.getAttribute("resume"))) {
                this.streamId = enabled.getAttribute("id");
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled (resumable)");
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled");
            }
            this.stanzasReceived = 0;
            this.inSmacksSession = true;
            final RequestPacket r = new RequestPacket(smVersion);
            tagWriter.writeStanzaAsync(r);
        } else if (nextTag.isStart("resumed")) {
            this.inSmacksSession = true;
            this.isBound = true;
            this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
            lastPacketReceived = SystemClock.elapsedRealtime();
            final Element resumed = tagReader.readElement(nextTag);
            final String h = resumed.getAttribute("h");
            try {
                ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
                synchronized (this.mStanzaQueue) {
                    final int serverCount = Integer.parseInt(h);
                    if (serverCount < stanzasSent) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed with lost packages");
                        stanzasSent = serverCount;
                    } else {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed");
                    }
                    acknowledgeStanzaUpTo(serverCount);
                    for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
                        failedStanzas.add(mStanzaQueue.valueAt(i));
                    }
                    mStanzaQueue.clear();
                }
                Log.d(Config.LOGTAG, "resending " + failedStanzas.size() + " stanzas");
                for (AbstractAcknowledgeableStanza packet : failedStanzas) {
                    if (packet instanceof MessagePacket) {
                        MessagePacket message = (MessagePacket) packet;
                        mXmppConnectionService.markMessage(account, message.getTo().toBareJid(), message.getId(), Message.STATUS_UNSEND);
                    }
                    sendPacket(packet);
                }
            } catch (final NumberFormatException ignored) {
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource());
            changeStatus(Account.State.ONLINE);
        } else if (nextTag.isStart("r")) {
            tagReader.readElement(nextTag);
            if (Config.EXTENDED_SM_LOGGING) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
            }
            final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
            tagWriter.writeStanzaAsync(ack);
        } else if (nextTag.isStart("a")) {
            boolean accountUiNeedsRefresh = false;
            synchronized (NotificationService.CATCHUP_LOCK) {
                if (mWaitingForSmCatchup.compareAndSet(true, false)) {
                    int count = mSmCatchupMessageCounter.get();
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": SM catchup complete (" + count + ")");
                    accountUiNeedsRefresh = true;
                    if (count > 0) {
                        mXmppConnectionService.getNotificationService().finishBacklog(true, account);
                    }
                }
            }
            if (accountUiNeedsRefresh) {
                mXmppConnectionService.updateAccountUi();
            }
            final Element ack = tagReader.readElement(nextTag);
            lastPacketReceived = SystemClock.elapsedRealtime();
            try {
                synchronized (this.mStanzaQueue) {
                    final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
                    acknowledgeStanzaUpTo(serverSequence);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server send ack without sequence number");
            }
        } else if (nextTag.isStart("failed")) {
            Element failed = tagReader.readElement(nextTag);
            try {
                final int serverCount = Integer.parseInt(failed.getAttribute("h"));
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount);
                synchronized (this.mStanzaQueue) {
                    acknowledgeStanzaUpTo(serverCount);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed");
            }
            resetStreamId();
            sendBindRequest();
        } else if (nextTag.isStart("iq")) {
            processIq(nextTag);
        } else if (nextTag.isStart("message")) {
            processMessage(nextTag);
        } else if (nextTag.isStart("presence")) {
            processPresence(nextTag);
        }
        nextTag = tagReader.readTag();
    }
    if (nextTag != null && nextTag.isEnd("stream")) {
        streamCountDownLatch.countDown();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RequestPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket) Matcher(java.util.regex.Matcher) Element(de.pixart.messenger.xml.Element) ArrayList(java.util.ArrayList) AckPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.AckPacket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) AbstractAcknowledgeableStanza(de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza) Tag(de.pixart.messenger.xml.Tag)

Aggregations

MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)32 Element (de.pixart.messenger.xml.Element)8 Account (de.pixart.messenger.entities.Account)6 Conversation (de.pixart.messenger.entities.Conversation)6 XmppAxolotlMessage (de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage)5 Message (de.pixart.messenger.entities.Message)5 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)5 Jid (de.pixart.messenger.xmpp.jid.Jid)5 AbstractAcknowledgeableStanza (de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza)3 Session (net.java.otr4j.session.Session)3 MucOptions (de.pixart.messenger.entities.MucOptions)2 ReceiptRequest (de.pixart.messenger.entities.ReceiptRequest)2 Tag (de.pixart.messenger.xml.Tag)2 RequestPacket (de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 OtrException (net.java.otr4j.OtrException)2 Bundle (android.os.Bundle)1 Bookmark (de.pixart.messenger.entities.Bookmark)1 Contact (de.pixart.messenger.entities.Contact)1