forked from InvictusInnovations/keyhotee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailprocessorimpl.cpp
127 lines (96 loc) · 3.49 KB
/
mailprocessorimpl.cpp
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
#include "mailprocessorimpl.hpp"
#include <bts/application.hpp>
#include <atomic>
class TMailProcessor::TOutboxQueue
{
public:
explicit TOutboxQueue(const bts::profile_ptr& profile)
{
Outbox = profile->get_pending_db();
Sent = profile->get_sent_db();
}
bool AnyOperationsPending() const;
/// Returns length of the queue.
unsigned int GetLength() const;
void Release()
{
assert(AnyOperationsPending() == false);
delete this;
}
private:
virtual ~TOutboxQueue() {}
private:
TMessageDB Outbox;
TMessageDB Sent;
};
bool TMailProcessor::TOutboxQueue::AnyOperationsPending() const
{
return false;
}
unsigned int TMailProcessor::TOutboxQueue::GetLength() const
{
return 0;
}
TMailProcessor::TMailProcessor(IUpdateSink& updateSink,
const bts::profile_ptr& loadedProfile) :
Sink(updateSink),
Profile(loadedProfile)
{
Drafts = Profile->get_draft_db();
OutboxQueue = new TOutboxQueue(Profile);
}
TMailProcessor::~TMailProcessor()
{
OutboxQueue->Release();
}
void TMailProcessor::Send(const TIdentity& senderId, const TPhysicalMailMessage& msg)
{
TPhysicalMailMessage msgToSend(msg);
TRecipientPublicKeys bccList(msg.bcc_list);
/// \warning Message to be sent must have cleared bcc list.
msgToSend.bcc_list.clear();
size_t totalRecipientCount = msgToSend.to_list.size() + msgToSend.cc_list.size() + bccList.size();
//Sink.OnMessageGroupPending(totalRecipientCount);
//Sink.OnMessagePending
auto my_priv_key = Profile->get_keychain().get_identity_key(senderId.dac_id_string);
auto app = bts::application::instance();
for(const auto& public_key : msgToSend.to_list)
app->send_email(msgToSend, public_key, my_priv_key);
for(const auto& public_key : msgToSend.cc_list)
app->send_email(msgToSend, public_key, my_priv_key);
for(const auto& public_key : bccList)
app->send_email(msgToSend, public_key, my_priv_key);
}
void TMailProcessor::Save(const TIdentity& senderId, const TPhysicalMailMessage& sourceMsg,
const TStoredMailMessage* msgToOverwrite, TStoredMailMessage* savedMsg)
{
assert(savedMsg != nullptr);
Sink.OnMessageSaving();
TStorableMessage storableMsg;
PrepareStorableMessage(senderId, sourceMsg, &storableMsg);
*savedMsg = Drafts->store(storableMsg);
/** FIXME - block for another bug in backend. It is impossible to uniquely identify message_header
object.
https://github.com/InvictusInnovations/keyhotee/issues/107
*/
msgToOverwrite = nullptr;
Sink.OnMessageSaved(*savedMsg, msgToOverwrite);
if(msgToOverwrite != nullptr)
Drafts->remove(*msgToOverwrite);
}
void TMailProcessor::PrepareStorableMessage(const TIdentity& senderId,
const TPhysicalMailMessage& sourceMsg, TStorableMessage* storableMsg)
{
/** It looks for me like another bug in backend. Even decrypted message object can be constructed
directly by using interface of this class it is not sufficient to successfully store it in
message_db. All operations performed while sending/receiving email must be performed (except
transmission of course). So it must be first encrypted and next decrypted to properly fill
actual decrypted message.
*/
bts::bitchat::decrypted_message msg(sourceMsg);
auto senderPrivKey = Profile->get_keychain().get_identity_key(senderId.dac_id_string);
msg.sign(senderPrivKey);
auto encMsg = msg.encrypt(senderId.public_key);
encMsg.timestamp = fc::time_point::now();
encMsg.decrypt(senderPrivKey, *storableMsg);
}