aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/MessageUtil.java
blob: 5e6294859c3b05e018daf7ab59a6fedcd7801876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package de.thedevstack.conversationsplus.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.FileParams;
import de.thedevstack.conversationsplus.entities.Message;
import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.enums.FileStatus;
import de.thedevstack.conversationsplus.persistance.DatabaseBackend;
import de.thedevstack.conversationsplus.persistance.FileBackend;

/**
 * Utility class to work with messages.
 */
public final class MessageUtil {

    public static void extractFileParamsFromBody(Message message) {
        if (null == message) {
            return;
        }

        // Ensure that for every message the fileParams are set after calling this method
        FileParams fileParams = message.getFileParams();
        if (null == fileParams) {
            fileParams = new FileParams();
            message.setFileParams(fileParams);
        }

        String body = message.getBody();
        /**
         * there are a few cases where spaces result in an unwanted behavior, e.g.
         * "http://example.com/image.jpg" text that will not be shown /abc.png"
         * or more than one image link in one message.
         */
        if (null == body || body.isEmpty() || body.contains(" ")) {
            return;
        }

        try {
            URL url = new URL(body);
            if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) {
                message.setTreatAsDownloadable(Message.Decision.NEVER);
                return;
            }
            String extension = FileUtils.getRelevantExtension(url);
            if (message.isHttpUploaded()) {
                fileParams.setUrl(url.toString());
                if (null != extension
                        && (Transferable.WELL_KNOWN_EXTENSIONS.contains(extension.toLowerCase()) || Transferable.VALID_IMAGE_EXTENSIONS.contains(extension.toLowerCase()))) {
                    message.setTreatAsDownloadable(Message.Decision.MUST);
                } else {
                    message.setTreatAsDownloadable(Message.Decision.NEVER);
                    fileParams.setFileStatus(FileStatus.UNDEFINED);
                }

                extractFilename(message, url.toString());
                return;
            }

            if (extension == null) {
                message.setTreatAsDownloadable(Message.Decision.NEVER);
                return;
            }
            byte[] ivAndKey = UrlUtil.getIvAndKeyFromURL(url);

            if (null != ivAndKey) {
                if (MimeUtils.guessMimeTypeFromExtension(extension) != null) {
                    message.setTreatAsDownloadable(Message.Decision.MUST);
                    fileParams.setKeyAndIv(ivAndKey);
                } else {
                    message.setTreatAsDownloadable(Message.Decision.NEVER);
                }
            } else if (Transferable.VALID_IMAGE_EXTENSIONS.contains(extension)
                    || Transferable.WELL_KNOWN_EXTENSIONS.contains(extension)) {
                message.setTreatAsDownloadable(Message.Decision.SHOULD);
            } else {
                message.setTreatAsDownloadable(Message.Decision.NEVER);
            }

            if (message.treatAsDownloadable() == Message.Decision.MUST
                    || message.treatAsDownloadable() == Message.Decision.SHOULD) {
                fileParams.setUrl(url.toString());
                extractFilename(message, url.toString());
            }
        } catch (MalformedURLException e) {
            message.setTreatAsDownloadable(Message.Decision.NEVER);
        }
    }

    private static void extractFilename(Message message, String url) {
        String originalFilename = FileUtils.getFilenameFromPath(url);
        final String lowerCaseFilename = originalFilename.toLowerCase();
        final String lastPart = FileUtils.getLastExtension(lowerCaseFilename);

        detectAndSetEncryption(lastPart, message);

        String filenameExtension;
        if (!lastPart.isEmpty() && Transferable.VALID_CRYPTO_EXTENSIONS.contains(lastPart)) {
            filenameExtension = FileUtils.getSecondToLastExtension(lowerCaseFilename);
            originalFilename = originalFilename.replace("." + lastPart, "");
        } else {
            filenameExtension = lastPart;
        }
        message.setRelativeFilePath(message.getUuid() + "." + filenameExtension);

        message.getFileParams().setOriginalFilename(originalFilename);
    }

    private static void detectAndSetEncryption(String lastPart, Message message) {
        if (!lastPart.isEmpty() && ("pgp".equals(lastPart) || "gpg".equals(lastPart))) {
            message.setEncryption(Message.ENCRYPTION_PGP);
        } else if (message.getEncryption() != Message.ENCRYPTION_OTR
                && message.getEncryption() != Message.ENCRYPTION_AXOLOTL) {
            message.setEncryption(Message.ENCRYPTION_NONE);
        } else if ((message.getEncryption() == Message.ENCRYPTION_OTR
                || message.getEncryption() == Message.ENCRYPTION_AXOLOTL)
                && message.getFileParams() != null && message.getFileParams().getKey() == null) {
            // If an encryption is set for the message, but no key given -> decryption not possible
            message.setEncryption(Message.ENCRYPTION_NONE);
        }
    }

    /**
     * Checks if an attached file is an image or not.
     * Prerequisite for calling this method: The check if a file is attached is done before.
     * @param message
     * @return
     */
    public static boolean isAttachedFileAnImage(Message message) {
        final FileParams fileParams = message.getFileParams();
        String mimeType = (null != fileParams) ? fileParams.getMimeType() : null;
        return message.getType() == Message.TYPE_IMAGE
                || (fileParams != null && fileParams.getWidth() > 0)
                || (null != mimeType && mimeType.startsWith("image/"));
    }

    public static boolean isTypeFileAndDecrypted(Message message) {
        return message.getType() == Message.TYPE_FILE
                && message.getEncryption() != Message.ENCRYPTION_PGP
                && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED;
    }

    public static boolean isDecrypted(Message message) {
        return message.getEncryption() != Message.ENCRYPTION_PGP && message.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED;
    }

    /**
     * Checks if the file status of an message is <b>NOT</b> one of:
     * <ul>
     *     <li>FileStatus#NOT_FOUND</li>
     *     <li>FileStatus#DELETED</li>
     * </ul>
     * The file status does not guarantee that a file is really available or not.
     * @param message
     * @return
     */
    public static boolean mayFileRemoteAvailable(Message message) {
        FileStatus fileStatus = (null != message.getFileParams()) ? message.getFileParams().getFileStatus() : null;
        return null != fileStatus
                && FileStatus.NOT_FOUND != fileStatus
                && FileStatus.DELETED != fileStatus;
    }

    public static boolean needsDownload(Message message) {
        FileStatus fileStatus = (null != message.getFileParams()) ? message.getFileParams().getFileStatus() : null;
        return (null == fileStatus
                || (null != fileStatus && (fileStatus == FileStatus.NEEDS_DOWNLOAD || fileStatus == FileStatus.UNDEFINED)))
                && message.treatAsDownloadable() != Message.Decision.NEVER;
    }

    public static boolean isMessageSent(Message message) {
        switch (message.getStatus()) {
            case Message.STATUS_SEND:
            case Message.STATUS_SEND_DISPLAYED:
            case Message.STATUS_SEND_FAILED:
            case Message.STATUS_SEND_RECEIVED:
                return true;
            default:
                return false;
        }
    }

    public static void setAndSaveFileStatus(Message message, FileStatus fileStatus) {
        message.getFileParams().setFileStatus(fileStatus);
        DatabaseBackend.getInstance().updateMessage(message);
        UiUpdateHelper.updateConversationUi();
    }

    public static boolean markMessage(Conversation conversation, String uuid, int status) {
        if (uuid == null) {
            return false;
        } else {
            Message message = conversation.findSentMessageWithUuid(uuid);
            if (message != null) {
                markMessage(message, status);
                return true;
            } else {
                return false;
            }
        }
    }

    public static void markMessage(Message message, int status) {
        if (status == Message.STATUS_SEND_FAILED
                && (message.getStatus() == Message.STATUS_SEND_RECEIVED || message
                .getStatus() == Message.STATUS_SEND_DISPLAYED)) {
            return;
        }
        message.setStatus(status);
        DatabaseBackend.getInstance(ConversationsPlusApplication.getAppContext()).updateMessage(message);
        UiUpdateHelper.updateConversationUi();
    }

    public static boolean wasHighlightedOrPrivate(final Message message) {
        final String nick = message.getConversation().getMucOptions().getActualNick();
        final Pattern highlight = generateNickHighlightPattern(nick);
        if (message.getBody() == null || nick == null) {
            return false;
        }
        final Matcher m = highlight.matcher(message.getBody());
        return (m.find() || message.getType() == Message.TYPE_PRIVATE);
    }

    private static Pattern generateNickHighlightPattern(final String nick) {
        // We expect a word boundary, i.e. space or start of string, followed by
        // the
        // nick (matched in case-insensitive manner), followed by optional
        // punctuation (for example "bob: i disagree" or "how are you alice?"),
        // followed by another word boundary.
        return Pattern.compile("\\b" + Pattern.quote(nick) + "\\p{Punct}?\\b",
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    }

    public static void updateMessageWithImageDetails(Message message, String filePath, long size, int imageWidth, int imageHeight) {
        message.setRelativeFilePath(filePath);
        MessageUtil.updateMessageWithFileParams(message, null, size, imageWidth, imageHeight);
    }

    public static void updateFileParams(Message message) {
        updateFileParams(message, null);
    }

    public static void updateFileParams(Message message, URL url) {
        DownloadableFile file = FileBackend.getFile(message);
        int imageWidth = -1;
        int imageHeight = -1;
        if (message.getType() == Message.TYPE_IMAGE || file.getMimeType().startsWith("image/")) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
            imageHeight = options.outHeight;
            imageWidth = options.outWidth;
            if (null != bmp) {
                bmp.recycle();
            }
        }

        MessageUtil.updateMessageWithFileParams(message, url, file.getSize(), imageWidth, imageHeight);
    }

    private static void updateMessageWithFileParams(Message message, URL url, long size, int imageWidth, int imageHeight) {
        FileParams fileParams = message.getFileParams();
        if (null == fileParams) {
            fileParams = new FileParams();
            message.setFileParams(fileParams);
        }
        fileParams.setSize(size);
        if (null != url) {
            fileParams.setUrl(url.toString());
        }
        if (-1 < imageWidth) {
            fileParams.setWidth(imageWidth);
        }
        if (-1 < imageHeight) {
            fileParams.setHeight(imageHeight);
        }
        String relativeFilePathFromMessage = message.getRelativeFilePath();
        if (null != relativeFilePathFromMessage && relativeFilePathFromMessage.startsWith("/") && (null == fileParams.getPath() || !relativeFilePathFromMessage.equals(fileParams.getPath()))) {
            fileParams.setPath(relativeFilePathFromMessage);
        }
    }

    private MessageUtil() {
        // Static helper class
    }
}