qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
message.h
Go to the documentation of this file.
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 #pragma once
21 
22 #include "src/core/coreext.h"
23 #include "src/core/extension.h"
24 
25 #include <QDateTime>
26 #include <QRegularExpression>
27 #include <QString>
28 
29 #include <vector>
30 
31 class Friend;
32 class CoreExt;
33 
34 // NOTE: This could be extended in the future to handle all text processing (see
35 // ChatMessage::createChatMessage)
37 {
39 };
40 
41 // May need to be extended in the future to have a more varianty type (imagine
42 // if we wanted to add message replies and shoved a reply id in here)
44 {
46  // Indicates start position within a Message::content
47  size_t start;
48  // Indicates end position within a Message::content
49  size_t end;
50 };
51 
52 struct Message
53 {
54  bool isAction;
55  QString content;
56  QDateTime timestamp;
58  std::vector<MessageMetadata> metadata;
59 };
60 
61 
63 {
64 
65 public:
71  {
72 
73  public:
74  SharedParams(uint64_t maxCoreMessageSize_, uint64_t maxExtendedMessageSize_)
75  : maxCoreMessageSize(maxCoreMessageSize_)
76  , maxExtendedMessageSize(maxExtendedMessageSize_)
77  {}
78 
79  QRegularExpression getNameMention() const
80  {
81  return nameMention;
82  }
83  QRegularExpression getSanitizedNameMention() const
84  {
85  return sanitizedNameMention;
86  }
87  QRegularExpression getPublicKeyMention() const
88  {
89  return pubKeyMention;
90  }
91  void onUserNameSet(const QString& username);
92  void setPublicKey(const QString& pk);
93 
94  uint64_t getMaxCoreMessageSize() const
95  {
96  return maxCoreMessageSize;
97  }
98 
99  uint64_t getMaxExtendedMessageSize() const
100  {
101  return maxExtendedMessageSize;
102  }
103 
104  private:
107  QRegularExpression nameMention;
108  QRegularExpression sanitizedNameMention;
109  QRegularExpression pubKeyMention;
110  };
111 
113 
114  std::vector<Message> processOutgoingMessage(bool isAction, const QString& content, ExtensionSet extensions);
115  Message processIncomingCoreMessage(bool isAction, const QString& content);
116  Message processIncomingExtMessage(const QString& content);
117 
121  inline void enableMentions()
122  {
123  detectingMentions = true;
124  }
125 
129  inline void disableMentions()
130  {
131  detectingMentions = false;
132  }
133 
134 private:
135  bool detectingMentions = false;
137 };
MessageMetadata::start
size_t start
Definition: message.h:47
MessageMetadata::end
size_t end
Definition: message.h:49
MessageProcessor::SharedParams::getNameMention
QRegularExpression getNameMention() const
Definition: message.h:79
MessageProcessor::SharedParams::pubKeyMention
QRegularExpression pubKeyMention
Definition: message.h:109
MessageProcessor::SharedParams::getMaxCoreMessageSize
uint64_t getMaxCoreMessageSize() const
Definition: message.h:94
Message::isAction
bool isAction
Definition: message.h:54
MessageMetadata
Definition: message.h:43
MessageProcessor::SharedParams::nameMention
QRegularExpression nameMention
Definition: message.h:107
coreext.h
Message::metadata
std::vector< MessageMetadata > metadata
Definition: message.h:58
MessageProcessor::processIncomingExtMessage
Message processIncomingExtMessage(const QString &content)
Definition: message.cpp:155
MessageMetadataType
MessageMetadataType
Definition: message.h:36
Message::extensionSet
ExtensionSet extensionSet
Definition: message.h:57
MessageMetadataType::selfMention
@ selfMention
MessageProcessor::SharedParams::sanitizedNameMention
QRegularExpression sanitizedNameMention
Definition: message.h:108
CoreExt
Definition: coreext.h:41
extension.h
MessageProcessor::SharedParams
Definition: message.h:70
Friend
Definition: friend.h:31
MessageProcessor::MessageProcessor
MessageProcessor(const SharedParams &sharedParams)
Definition: message.cpp:79
MessageProcessor::SharedParams::SharedParams
SharedParams(uint64_t maxCoreMessageSize_, uint64_t maxExtendedMessageSize_)
Definition: message.h:74
MessageProcessor::detectingMentions
bool detectingMentions
Definition: message.h:135
MessageProcessor::processOutgoingMessage
std::vector< Message > processOutgoingMessage(bool isAction, const QString &content, ExtensionSet extensions)
Converts an outgoing message into one (or many) sanitized Message(s)
Definition: message.cpp:86
MessageProcessor::sharedParams
const SharedParams & sharedParams
Definition: message.h:136
MessageMetadata::type
MessageMetadataType type
Definition: message.h:45
ExtensionSet
std::bitset< ExtensionType::max > ExtensionSet
Definition: extension.h:32
MessageProcessor::processIncomingCoreMessage
Message processIncomingCoreMessage(bool isAction, const QString &content)
Converts an incoming message into a sanitized Message.
Definition: message.cpp:118
MessageProcessor::SharedParams::getSanitizedNameMention
QRegularExpression getSanitizedNameMention() const
Definition: message.h:83
Message::timestamp
QDateTime timestamp
Definition: message.h:56
MessageProcessor::enableMentions
void enableMentions()
Enables mention detection in the processor.
Definition: message.h:121
MessageProcessor
Definition: message.h:62
Message::content
QString content
Definition: message.h:55
MessageProcessor::SharedParams::getPublicKeyMention
QRegularExpression getPublicKeyMention() const
Definition: message.h:87
MessageProcessor::SharedParams::setPublicKey
void setPublicKey(const QString &pk)
Set the public key on which a message should be highlighted.
Definition: message.cpp:72
MessageProcessor::SharedParams::maxCoreMessageSize
uint64_t maxCoreMessageSize
Definition: message.h:105
MessageProcessor::SharedParams::maxExtendedMessageSize
uint64_t maxExtendedMessageSize
Definition: message.h:106
MessageProcessor::SharedParams::getMaxExtendedMessageSize
uint64_t getMaxExtendedMessageSize() const
Definition: message.h:99
MessageProcessor::disableMentions
void disableMentions()
Disables mention detection in the processor.
Definition: message.h:129
Message
Definition: message.h:52
MessageProcessor::SharedParams::onUserNameSet
void onUserNameSet(const QString &username)
Definition: message.cpp:58