summaryrefslogtreecommitdiffstats
blob: 1a822a17b283d0a9837fc6ba04611881999e34a8 (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
/*
 * Copyright (c) 2013-2017 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/Controllers/Chat/ChatMessageParser.h>

#include <algorithm>
#include <memory>
#include <utility>
#include <vector>

#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>

#include <Swiften/Base/Regex.h>
#include <Swiften/Base/String.h>

#include <SwifTools/Linkify.h>

namespace Swift {

    ChatMessageParser::ChatMessageParser(const std::map<std::string, std::string>& emoticons, std::shared_ptr<HighlightConfiguration> highlightConfiguration, Mode mode) : emoticons_(emoticons), highlightConfiguration_(highlightConfiguration), mode_(mode) {
    }

    typedef std::pair<std::string, std::string> StringPair;

    ChatWindow::ChatMessage ChatMessageParser::parseMessageBody(const std::string& body, const std::string& senderNickname, bool senderIsSelf) {
        ChatWindow::ChatMessage parsedMessage;

        std::string remaining = body;
        if (boost::starts_with(body, "/me ")) {
           remaining = String::getSplittedAtFirst(body, ' ').second;
           parsedMessage.setIsMeCommand(true);
        }

        /* Parse one, URLs */
        while (!remaining.empty()) {
            bool found = false;
            std::pair<std::vector<std::string>, size_t> links = Linkify::splitLink(remaining);
            remaining = "";
            for (size_t i = 0; i < links.first.size(); i++) {
                const std::string& part = links.first[i];
                if (found) {
                    // Must be on the last part, then
                    remaining = part;
                }
                else {
                    if (i == links.second) {
                        found = true;
                        parsedMessage.append(std::make_shared<ChatWindow::ChatURIMessagePart>(part));
                    }
                    else {
                        parsedMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(part));
                    }
                }
            }
        }

        /* do emoticon substitution */
        parsedMessage = emoticonHighlight(parsedMessage);

        if (!senderIsSelf) { /* do not highlight our own messsages */
            // Highlight keywords and own mentions.
            parsedMessage = splitHighlight(parsedMessage);

            // Highlight full message events like, specific sender, general
            // incoming group message, or general incoming direct message.
            parsedMessage = fullMessageHighlight(parsedMessage, senderNickname);
        }

        return parsedMessage;
    }

    ChatWindow::ChatMessage ChatMessageParser::emoticonHighlight(const ChatWindow::ChatMessage& message) {
        ChatWindow::ChatMessage parsedMessage = message;

        std::string regexString;
        /* Parse two, emoticons */
        for (StringPair emoticon : emoticons_) {
            /* Construct a regexp that finds an instance of any of the emoticons inside a group
             * at the start or end of the line, or beside whitespace.
             */
            regexString += regexString.empty() ? "" : "|";
            std::string escaped = "(" + Regex::escape(emoticon.first) + ")";
            regexString += "^" + escaped + "|";
            regexString += escaped + "$|";
            regexString += "\\s" + escaped + "|";
            regexString += escaped + "\\s";

        }
        if (!regexString.empty()) {
            regexString += "";
            boost::regex emoticonRegex(regexString);

            ChatWindow::ChatMessage newMessage;
            for (const auto& part : parsedMessage.getParts()) {
                std::shared_ptr<ChatWindow::ChatTextMessagePart> textPart;
                if ((textPart = std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(part))) {
                    try {
                        boost::match_results<std::string::const_iterator> match;
                        const std::string& text = textPart->text;
                        std::string::const_iterator start = text.begin();
                        while (regex_search(start, text.end(), match, emoticonRegex)) {
                            int matchIndex = 0;
                            for (matchIndex = 1; matchIndex < static_cast<int>(match.size()); matchIndex++) {
                                if (match[matchIndex].length() > 0) {
                                    //This is the matching subgroup
                                    break;
                                }
                            }
                            std::string::const_iterator matchStart = match[matchIndex].first;
                            std::string::const_iterator matchEnd = match[matchIndex].second;
                            if (start != matchStart) {
                                /* If we're skipping over plain text since the previous emoticon, record it as plain text */
                                newMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, matchStart)));
                            }
                            std::shared_ptr<ChatWindow::ChatEmoticonMessagePart> emoticonPart = std::make_shared<ChatWindow::ChatEmoticonMessagePart>();
                            std::string matchString = match[matchIndex].str();
                            std::map<std::string, std::string>::const_iterator emoticonIterator = emoticons_.find(matchString);
                            assert (emoticonIterator != emoticons_.end());
                            const StringPair& emoticon = *emoticonIterator;
                            emoticonPart->imagePath = emoticon.second;
                            emoticonPart->alternativeText = emoticon.first;
                            newMessage.append(emoticonPart);
                            start = matchEnd;
                        }
                        if (start != text.end()) {
                            /* If there's plain text after the last emoticon, record it */
                            newMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, text.end())));
                        }

                    }
                    catch (std::runtime_error) {
                        /* Basically too expensive to compute the regex results and it gave up, so pass through as text */
                        newMessage.append(part);
                    }
                }
                else {
                    newMessage.append(part);
                }
            }
            parsedMessage.setParts(newMessage.getParts());
        }

        return parsedMessage;
    }

    ChatWindow::ChatMessage ChatMessageParser::splitHighlight(const ChatWindow::ChatMessage& message) {
        auto keywordToRegEx = [](const std::string& keyword, bool matchCaseSensitive) {
            std::string escaped = Regex::escape(keyword);
            boost::regex::flag_type flags = boost::regex::normal;
            if (!matchCaseSensitive) {
                flags |= boost::regex::icase;
            }
            return boost::regex("\\b" + escaped + "\\b", flags);
        };

        auto highlightKeywordInChatMessage = [&](const ChatWindow::ChatMessage& message, const std::string& keyword, bool matchCaseSensitive, const HighlightAction& action) {
            ChatWindow::ChatMessage resultMessage;

            for (const auto& part : message.getParts()) {
                std::shared_ptr<ChatWindow::ChatTextMessagePart> textPart;
                if ((textPart = std::dynamic_pointer_cast<ChatWindow::ChatTextMessagePart>(part))) {
                    try {
                        boost::match_results<std::string::const_iterator> match;
                        const std::string& text = textPart->text;
                        std::string::const_iterator start = text.begin();
                        while (regex_search(start, text.end(), match, keywordToRegEx(keyword, matchCaseSensitive))) {
                            std::string::const_iterator matchStart = match[0].first;
                            std::string::const_iterator matchEnd = match[0].second;
                            if (start != matchStart) {
                                /* If we're skipping over plain text since the previous emoticon, record it as plain text */
                                resultMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, matchStart)));
                            }
                            std::shared_ptr<ChatWindow::ChatHighlightingMessagePart> highlightPart = std::make_shared<ChatWindow::ChatHighlightingMessagePart>();
                            highlightPart->text = match.str();
                            highlightPart->action = action;
                            resultMessage.append(highlightPart);
                            start = matchEnd;
                        }
                        if (start != text.end()) {
                            /* If there's plain text after the last emoticon, record it */
                            resultMessage.append(std::make_shared<ChatWindow::ChatTextMessagePart>(std::string(start, text.end())));
                        }
                    }
                    catch (std::runtime_error) {
                        /* Basically too expensive to compute the regex results and it gave up, so pass through as text */
                        resultMessage.append(part);
                    }
                } else {
                    resultMessage.append(part);
                }
            }
            return resultMessage;
        };

        ChatWindow::ChatMessage parsedMessage = message;

        // detect mentions of own nickname
        HighlightAction ownMentionKeywordAction = highlightConfiguration_->ownMentionAction;
        ownMentionKeywordAction.setSoundFilePath(boost::optional<std::string>());
        ownMentionKeywordAction.setSystemNotificationEnabled(false);
        if (!getNick().empty() && !highlightConfiguration_->ownMentionAction.isEmpty()) {
            auto nicknameHighlightedMessage = highlightKeywordInChatMessage(parsedMessage, nick_, false, ownMentionKeywordAction);
            auto highlightedParts = nicknameHighlightedMessage.getParts();
            auto ownNicknamePart = std::find_if(highlightedParts.begin(), highlightedParts.end(), [&](std::shared_ptr<ChatWindow::ChatMessagePart>& part){
                auto highlightPart = std::dynamic_pointer_cast<ChatWindow::ChatHighlightingMessagePart>(part);
                if (highlightPart && highlightPart->text == nick_) {
                    return true;
                }
                return false;
            });
            if (ownNicknamePart != highlightedParts.end()) {
                parsedMessage.setHighlightActionOwnMention(highlightConfiguration_->ownMentionAction);
            }
            parsedMessage.setParts(nicknameHighlightedMessage.getParts());
        }

        // detect keywords
        for (const auto& keywordHighlight : highlightConfiguration_->keywordHighlights) {
            if (keywordHighlight.keyword.empty() || keywordHighlight.action.isEmpty()) {
                continue;
            }
            auto newMessage = highlightKeywordInChatMessage(parsedMessage, keywordHighlight.keyword, keywordHighlight.matchCaseSensitive, keywordHighlight.action);
            parsedMessage.setParts(newMessage.getParts());
        }

        return parsedMessage;
    }

    ChatWindow::ChatMessage ChatMessageParser::fullMessageHighlight(const ChatWindow::ChatMessage& parsedMessage, const std::string& sender) {
        auto fullHighlightedMessage = parsedMessage;

        // contact highlighting
        for (const auto& contactHighlight : highlightConfiguration_->contactHighlights) {
            if (sender == contactHighlight.name) {
                fullHighlightedMessage.setHighlightActionSender(contactHighlight.action);
                break;
            }
        }

        // general incoming messages
        HighlightAction groupAction;
        HighlightAction chatAction;

        switch (mode_) {
            case Mode::GroupChat:
                groupAction.setSoundFilePath(highlightConfiguration_->playSoundOnIncomingGroupchatMessages ? boost::optional<std::string>("") : boost::optional<std::string>());
                groupAction.setSystemNotificationEnabled(highlightConfiguration_->showNotificationOnIncomingGroupchatMessages);
                fullHighlightedMessage.setHighlightActionGroupMessage(groupAction);
                break;

            case Mode::Chat:
                chatAction.setSoundFilePath(highlightConfiguration_->playSoundOnIncomingDirectMessages ? boost::optional<std::string>("") : boost::optional<std::string>());
                chatAction.setSystemNotificationEnabled(highlightConfiguration_->showNotificationOnIncomingDirectMessages);
                fullHighlightedMessage.setHighlightActonDirectMessage(chatAction);
                break;
        }

        return fullHighlightedMessage;
    }
}