-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathp3msgservice.h
More file actions
279 lines (212 loc) · 12.1 KB
/
p3msgservice.h
File metadata and controls
279 lines (212 loc) · 12.1 KB
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
/*******************************************************************************
* libretroshare/src/services: p3msgservice.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2004-2008 Robert Fernie <retroshare@lunamutt.com> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifndef MESSAGE_SERVICE_HEADER
#define MESSAGE_SERVICE_HEADER
#include <list>
#include <map>
#include <iostream>
#include "pqi/pqi.h"
#include "pqi/pqiindic.h"
#include "pqi/pqiservicemonitor.h"
#include "pqi/p3cfgmgr.h"
#include "services/p3service.h"
#include "rsitems/rsmsgitems.h"
#include "util/rsthreads.h"
#include "util/rsdebug.h"
#include "retroshare/rsgxsifacetypes.h"
#include "retroshare/rsmail.h"
#include "grouter/p3grouter.h"
#include "grouter/grouterclientservice.h"
#include "turtle/p3turtle.h"
#include "turtle/turtleclientservice.h"
#include "gxstrans/p3gxstrans.h"
class p3LinkMgr;
class p3IdService;
typedef uint32_t MessageIdentifier;
// Temp tweak to test grouter
class p3MsgService :
public RsMail,
public p3Service,
public p3Config,
public pqiServiceMonitor,
public GRouterClientService,
public GxsTransClient
{
public:
p3MsgService(p3ServiceControl *sc, p3IdService *id_service, p3GxsTrans& gxsMS);
virtual ~p3MsgService();
virtual RsServiceInfo getServiceInfo() override;
/// @see RsMsgs::sendMail
uint32_t sendMail(const RsGxsId from,
const std::string& subject,
const std::string& body,
const std::set<RsGxsId>& to = std::set<RsGxsId>(),
const std::set<RsGxsId>& cc = std::set<RsGxsId>(),
const std::set<RsGxsId>& bcc = std::set<RsGxsId>(),
const std::vector<FileInfo>& attachments = std::vector<FileInfo>(),
std::set<RsMailIdRecipientIdPair>& trackingIds =
RS_DEFAULT_STORAGE_PARAM(std::set<RsMailIdRecipientIdPair>),
std::string& errorMsg =
RS_DEFAULT_STORAGE_PARAM(std::string) ) override;
/* External Interface */
bool getMessageSummaries(Rs::Mail::BoxName box, std::list<Rs::Mail::MsgInfoSummary> &msgList) override;
bool getMessage(const std::string& mid, Rs::Mail::MessageInfo &msg) override;
void getMessageCount(uint32_t &nInbox, uint32_t &nInboxNew, uint32_t &nOutbox, uint32_t &nDraftbox, uint32_t &nSentbox, uint32_t &nTrashbox) override;
bool MessageDelete(const std::string &mid) override;
bool MessageRead(const std::string &mid, bool bUnreadByUser) override;
bool MessageJunk(const std::string &mid, bool bUnreadByUser) override;
bool MessageReplied(const std::string &mid, bool bUnreadByUser) override;
bool MessageForwarded(const std::string &mid, bool forwarded) override;
bool MessageStar(const std::string &mid, bool star) override;
bool MessageLoadEmbeddedImages(const std::string &mid, bool load) override;
bool getMsgParentId(const std::string &msgId, std::string &msgParentId) override;
// msgParentId == 0 --> remove
bool setMsgParentId(uint32_t msgId, uint32_t msgParentId);
RS_DEPRECATED_FOR(sendMail)
bool MessageSend(Rs::Mail::MessageInfo &info) override;
bool SystemMessage(const std::string &title, const std::string &message, uint32_t systemFlag) override;
bool MessageToDraft(Rs::Mail::MessageInfo &info, const std::string &msgParentId) override;
bool addMessage(const Rs::Mail::MessageInfo &info, Rs::Mail::BoxName box) override;
bool MessageToTrash(const std::string &mid, bool bTrash) override;
bool getMessageTag(const std::string &msgId, Rs::Mail::MsgTagInfo& info) override;
bool getMessageTagTypes(Rs::Mail::MsgTagType& tags) override;
bool setMessageTagType(uint32_t tagId, std::string& text, uint32_t rgb_color) override;
bool removeMessageTagType(uint32_t tagId) override;
/* set == false && tagId == 0 --> remove all */
bool setMessageTag(const std::string &msgId, uint32_t tagId, bool set) override;
bool resetMessageStandardTagTypes(Rs::Mail::MsgTagType& tags) override;
void loadWelcomeMsg(); /* startup message */
//std::list<RsMsgItem *> &getMsgList();
//std::list<RsMsgItem *> &getMsgOutList();
int tick() override;
/*** Overloaded from p3Config ****/
virtual RsSerialiser *setupSerialiser() override;
virtual bool saveList(bool& cleanup, std::list<RsItem*>&) override;
virtual bool loadList(std::list<RsItem*>& load) override;
virtual void saveDone() override;
/*** Overloaded from p3Config ****/
/*** Overloaded from pqiMonitor ***/
virtual void statusChange(const std::list<pqiServicePeer> &plist) override;
/// iterate through the outgoing queue if online, send
int checkOutgoingMessages();
/*** Overloaded from pqiMonitor ***/
/*** overloaded from p3turtle ***/
virtual void connectToGlobalRouter(p3GRouter *) override;
struct DistantMessengingInvite
{
rstime_t time_of_validity ;
};
struct DistantMessengingContact
{
rstime_t last_hit_time ;
RsPeerId virtual_peer_id ;
uint32_t status ;
bool pending_messages ;
};
void enableDistantMessaging(bool b) ;
bool distantMessagingEnabled() ;
void setDistantMessagingPermissionFlags(uint32_t flags) override;
uint32_t getDistantMessagingPermissionFlags() override;
/// @see GxsTransClient::receiveGxsTransMail(...)
virtual bool receiveGxsTransMail( const RsGxsId& authorId,
const RsGxsId& recipientId,
const uint8_t* data, uint32_t dataSize ) override;
/// @see GxsTransClient::notifyGxsTransSendStatus(...)
virtual bool notifyGxsTransSendStatus( RsGxsTransId mailId,
GxsTransSendStatus status ) override;
private:
bool setMsgFlag(const std::string &mid, uint32_t flag, uint32_t mask);
void locked_sendDistantMsgItem(RsMsgItem *msgitem, const RsGxsId &from, uint32_t msgId);
bool locked_getMessageTag(const std::string &msgId, Rs::Mail::MsgTagInfo& info);
void locked_checkForDuplicates();
RsMailStorageItem *locked_getMessageData(uint32_t mid) const;
/** This contains the ongoing tunnel handling contacts.
* The map is indexed by the hash */
std::map<GRouterMsgPropagationId, uint32_t> _grouter_ongoing_messages;
/// Contains ongoing messages handed to gxs mail
std::map<RsGxsTransId, uint32_t> gxsOngoingMessages;
RsMutex gxsOngoingMutex;
// Overloaded from GRouterClientService
virtual bool acceptDataFromPeer(const RsGxsId& gxs_id) override;
virtual void receiveGRouterData(const RsGxsId& destination_key,const RsGxsId& signing_key, GRouterServiceId &client_id, uint8_t *data, uint32_t data_size) override;
virtual void notifyDataStatus(const GRouterMsgPropagationId& msg_id,const RsGxsId& signer_id,uint32_t data_status) override;
// Utility functions
bool locked_findHashForVirtualPeerId(const RsPeerId& pid,Sha1CheckSum& hash) ;
void sendGRouterData(const RsGxsId &key_id,RsMsgItem *) ;
void manageDistantPeers() ;
void handleIncomingItem(RsMsgItem *, const Rs::Mail::MsgAddress &from, const Rs::Mail::MsgAddress &to) ;
uint32_t getNewUniqueMsgId();
MessageIdentifier internal_sendMessage(MessageIdentifier id, const Rs::Mail::MsgAddress &from, const Rs::Mail::MsgAddress &to, uint32_t flags);
uint32_t sendDistantMessage(RsMsgItem *item,const RsGxsId& signing_gxs_id);
void checkSizeAndSendMessage(RsMsgItem *msg, const RsPeerId &destination);
void cleanListOfReceivedMessageHashes();
int incomingMsgs();
void processIncomingMsg(RsMsgItem *mi,const Rs::Mail::MsgAddress& from,const Rs::Mail::MsgAddress& to) ;
bool checkAndRebuildPartialMessage(RsMsgItem*) ;
// These two functions generate MessageInfo and MessageInfoSummary structures for the UI to use
void initRsMI (const RsMailStorageItem& msi, const Rs::Mail::MsgAddress& from, const Rs::Mail::MsgAddress& to, uint32_t flags, Rs::Mail::MessageInfo& mi );
void initRsMIS(const RsMailStorageItem& msi, const Rs::Mail::MsgAddress& from, const Rs::Mail::MsgAddress& to,MessageIdentifier mid,Rs::Mail::MsgInfoSummary& mis);
// Creates a RsMsgItem from a RsMailStorageItem, and a 'to' fields.
RsMsgItem *createOutgoingMessageItem(const RsMailStorageItem& msi, const Rs::Mail::MsgAddress& to);
// Creates a RsMailStorageItem from a message info and a 'from' field.
RsMailStorageItem *initMIRsMsg(const Rs::Mail::MessageInfo &info);
// Creates a RsMailStorageItem from a MessageInfo.
bool initMIRsMsg(RsMailStorageItem *msi, const Rs::Mail::MessageInfo &info) ;
void initStandardTagTypes();
p3IdService *mIdService ;
p3ServiceControl *mServiceCtrl;
p3GRouter *mGRouter ;
/* Mutex Required for stuff below */
RsMutex mMsgMtx;
RsMsgSerialiser *_serialiser ;
// Extra method to convert previous data into new format.
bool parseList_backwardCompatibility(std::list<RsItem*>& load);
// Stored list of received/sent messages. Here we use a complete info containing the msg item itself, plus its
// origin.
std::map<uint32_t, RsMailStorageItem *> mReceivedMessages; // Inbox
std::map<uint32_t, RsMailStorageItem *> mSentMessages; // Sent box (msgOutgoing points to elements in this list). Also contains drafts and pending messages
std::map<uint32_t, RsMailStorageItem *> mTrashMessages; // Trash box
std::map<uint32_t, RsMailStorageItem *> mDraftMessages; // Draft box
// Messages that haven't made it out yet. These are stored as reference to the original message it->first.
// For each of them, a list of outgoing copies are stored (with their own identifier) along with the
// outgoing message information: flags, grouter status, etc.
std::map<MessageIdentifier, std::map<MessageIdentifier,RsOutgoingMessageInfo> > msgOutgoing;
// This map stores node-to-node incoming messages that need to be sent in multiple chunks. GRouter and p3GxsTrans already
// handle large messages internally.
std::map<RsPeerId, RsMsgItem *> _pendingPartialIncomingMessages ;
/* maps for tags types and msg tags */
std::map<uint32_t, RsMsgTagType*> mTags;
// Set of messages ids used. Any new msg id generated will be checked against this set and added to it.
std::set<uint32_t> mAllMessageIds;
std::map<Sha1CheckSum, uint32_t> mRecentlyReceivedMessageHashes;
RsMutex recentlyReceivedMutex;
// Saves the parent of the messages in draft for replied and forwarded
std::map<uint32_t, RsMsgParentId*> mParentId;
std::string config_dir;
bool mDistantMessagingEnabled ;
uint32_t mDistantMessagePermissions ;
bool mShouldEnableDistantMessaging ;
p3GxsTrans& mGxsTransServ;
void debug_dump();
RS_SET_CONTEXT_DEBUG_LEVEL(3)
};
#endif // MESSAGE_SERVICE_HEADER