1/*
  2 * Copyright (c) 2017, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.utils;
 31
 32import com.google.common.base.Strings;
 33import eu.siacs.conversations.entities.Conversational;
 34import eu.siacs.conversations.entities.Message;
 35import eu.siacs.conversations.http.AesGcmURL;
 36import eu.siacs.conversations.http.URL;
 37import eu.siacs.conversations.ui.util.QuoteHelper;
 38import java.net.URI;
 39import java.net.URISyntaxException;
 40import java.util.regex.Pattern;
 41
 42public class MessageUtils {
 43
 44    private static final Pattern LTR_RTL = Pattern.compile("(\\u200E[^\\u200F]*\\u200F){3,}");
 45
 46    public static final String EMPTY_STRING = "";
 47
 48    public static String prepareQuote(final Message message) {
 49        final StringBuilder builder = new StringBuilder();
 50        final String body;
 51        if (message.hasMeCommand()) {
 52            final String nick;
 53            if (message.getStatus() == Message.STATUS_RECEIVED) {
 54                if (message.getConversation().getMode() == Conversational.MODE_MULTI) {
 55                    nick = Strings.nullToEmpty(message.getCounterpart().getResource());
 56                } else {
 57                    nick = message.getContact().getPublicDisplayName();
 58                }
 59            } else {
 60                nick = UIHelper.getMessageDisplayName(message);
 61            }
 62            body = nick + " " + message.getQuoteableBody().substring(Message.ME_COMMAND.length());
 63        } else {
 64            body = message.getQuoteableBody();
 65        }
 66        for (String line : body.split("\n")) {
 67            if (!(line.length() <= 0) && QuoteHelper.isNestedTooDeeply(line)) {
 68                continue;
 69            }
 70            if (builder.length() != 0) {
 71                builder.append('\n');
 72            }
 73            builder.append(line.trim());
 74        }
 75        return builder.toString();
 76    }
 77
 78    public static boolean treatAsDownloadable(final String body, final boolean oob, final boolean legacyEncryption) {
 79        if (oob) return true;
 80
 81        final String[] lines = body.split("\n");
 82        if (lines.length == 0) {
 83            return false;
 84        }
 85        for (final String line : lines) {
 86            if (line.contains("\\s+")) {
 87                return false;
 88            }
 89        }
 90        final URI uri;
 91        try {
 92            uri = new URI(lines[0]);
 93        } catch (final URISyntaxException e) {
 94            return false;
 95        }
 96        if (!URL.WELL_KNOWN_SCHEMES.contains(uri.getScheme())) {
 97            return false;
 98        }
 99        final String ref = uri.getFragment();
100        final String protocol = uri.getScheme();
101        final boolean encrypted = ref != null && AesGcmURL.IV_KEY.matcher(ref).matches();
102        final boolean followedByDataUri = lines.length == 2 && lines[1].startsWith("data:");
103        final boolean validAesGcm =
104                AesGcmURL.PROTOCOL_NAME.equalsIgnoreCase(protocol)
105                        && encrypted
106                        && (lines.length == 1 || followedByDataUri);
107        final boolean validProtocol =
108                "http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol);
109        final boolean validOob = validProtocol && (oob || encrypted || (legacyEncryption && uri.getPath() != null && (uri.getPath().endsWith(".xdc") || uri.getPath().endsWith(".webp") || uri.getPath().endsWith(".gif") || uri.getPath().endsWith(".png")))) && lines.length == 1;
110        return validAesGcm || validOob;
111    }
112
113    public static String aesgcmDownloadable(final String body) {
114        final String[] lines = body.split("\n");
115        if (lines.length == 0) {
116            return null;
117        }
118        for (final String line : lines) {
119            if (line.contains("\\s+")) {
120                return null;
121            }
122        }
123        final URI uri;
124        try {
125            uri = new URI(lines[0]);
126        } catch (final URISyntaxException e) {
127            return null;
128        }
129        if (!URL.WELL_KNOWN_SCHEMES.contains(uri.getScheme())) {
130            return null;
131        }
132        final String ref = uri.getFragment();
133        final String protocol = uri.getScheme();
134        final boolean encrypted = ref != null && AesGcmURL.IV_KEY.matcher(ref).matches();
135        final boolean followedByDataUri = lines.length == 2 && lines[1].startsWith("data:");
136        final boolean validAesGcm = AesGcmURL.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted && (lines.length == 1 || followedByDataUri);
137        return validAesGcm ? lines[0] : null;
138    }
139
140    public static String filterLtrRtl(String body) {
141        return LTR_RTL.matcher(body).replaceFirst(EMPTY_STRING);
142    }
143
144    public static boolean unInitiatedButKnownSize(Message message) {
145        return message.getType() == Message.TYPE_TEXT
146                && message.getTransferable() == null
147                && message.isOOb()
148                && (message.getFileParams().size != null || (message.getOob() != null && message.getOob().getScheme() != null && message.getOob().getScheme().equalsIgnoreCase("cid")))
149                && message.getFileParams().url != null;
150    }
151}