1 /*
2     Copyright © 2019 by The qTox Project Contributors
3 
4     This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6     qTox is libre software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     qTox is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with qTox.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef MESSAGE_H
21 #define MESSAGE_H
22 
23 #include <QDateTime>
24 #include <QRegularExpression>
25 #include <QString>
26 
27 #include <vector>
28 
29 class Friend;
30 
31 // NOTE: This could be extended in the future to handle all text processing (see
32 // ChatMessage::createChatMessage)
33 enum class MessageMetadataType
34 {
35     selfMention,
36 };
37 
38 // May need to be extended in the future to have a more varianty type (imagine
39 // if we wanted to add message replies and shoved a reply id in here)
40 struct MessageMetadata
41 {
42     MessageMetadataType type;
43     // Indicates start position within a Message::content
44     size_t start;
45     // Indicates end position within a Message::content
46     size_t end;
47 };
48 
49 struct Message
50 {
51     bool isAction;
52     QString content;
53     QDateTime timestamp;
54     std::vector<MessageMetadata> metadata;
55 };
56 
57 
58 class MessageProcessor
59 {
60 
61 public:
62     /**
63      * Parameters needed by all message processors. Used to reduce duplication
64      * of expensive data looked at by all message processors
65      */
66     class SharedParams
67     {
68 
69     public:
GetNameMention()70         QRegularExpression GetNameMention() const
71         {
72             return nameMention;
73         }
GetSanitizedNameMention()74         QRegularExpression GetSanitizedNameMention() const
75         {
76             return sanitizedNameMention;
77         }
GetPublicKeyMention()78         QRegularExpression GetPublicKeyMention() const
79         {
80             return pubKeyMention;
81         }
82         void onUserNameSet(const QString& username);
83         void setPublicKey(const QString& pk);
84 
85     private:
86         QRegularExpression nameMention;
87         QRegularExpression sanitizedNameMention;
88         QRegularExpression pubKeyMention;
89     };
90 
91     MessageProcessor(const SharedParams& sharedParams);
92 
93     std::vector<Message> processOutgoingMessage(bool isAction, QString const& content);
94 
95     Message processIncomingMessage(bool isAction, QString const& message);
96 
97     /**
98      * @brief Enables mention detection in the processor
99      */
enableMentions()100     inline void enableMentions()
101     {
102         detectingMentions = true;
103     }
104 
105     /**
106      * @brief Disables mention detection in the processor
107      */
disableMentions()108     inline void disableMentions()
109     {
110         detectingMentions = false;
111     };
112 
113 private:
114     bool detectingMentions = false;
115     const SharedParams& sharedParams;
116 };
117 
118 #endif /*MESSAGE_H*/
119