7#include "autocryptrecipient.h"
8#include "autocryptrecipient_p.h"
9#include "autocryptutils.h"
12#include <QGpgME/DataProvider>
13#include <QGpgME/Protocol>
15#include <gpgme++/data.h>
17using namespace MessageCore;
19AutocryptRecipientPrivate::AutocryptRecipientPrivate() =
default;
25 entry.
insert(QStringLiteral(
"prefer_encrypt"), prefer_encrypt);
27 entry.
insert(QStringLiteral(
"autocrypt_timestamp"), autocrypt_timestamp.toString(
Qt::ISODate));
28 entry.
insert(QStringLiteral(
"count_have_ach"), count_have_ach);
29 entry.
insert(QStringLiteral(
"count_no_ach"), count_no_ach);
30 if (last_seen.isValid()) {
33 if (counting_since.isValid()) {
34 entry.
insert(QStringLiteral(
"counting_since"), counting_since.toString(
Qt::ISODate));
36 if (!bad_user_agent.isEmpty()) {
39 if (gossip_timestamp.isValid()) {
40 entry.
insert(QStringLiteral(
"gossip_timestamp"), gossip_timestamp.toString(
Qt::ISODate));
45 return document.
toJson(format);
48void AutocryptRecipientPrivate::fromJson(
const QByteArray &json)
52 const auto &obj = document.
object();
55 count_have_ach = obj.value(QStringLiteral(
"count_have_ach")).
toInt();
56 count_no_ach = obj.value(QStringLiteral(
"count_no_ach")).toInt();
57 prefer_encrypt = obj.value(QStringLiteral(
"prefer_encrypt")).toBool();
58 keydata = obj.value(QStringLiteral(
"keydata")).toString().toLatin1();
61 if (obj.contains(QStringLiteral(
"last_seen"))) {
67 if (obj.contains(QStringLiteral(
"counting_since"))) {
73 if (obj.contains(QStringLiteral(
"bad_user_agent"))) {
74 bad_user_agent = obj.value(QStringLiteral(
"bad_user_agent")).toString().toLatin1();
79 if (obj.contains(QStringLiteral(
"gossip_timestamp"))) {
81 gossip_key = obj.value(QStringLiteral(
"gossip_key")).toString().toLatin1();
89AutocryptRecipient::AutocryptRecipient()
90 : d_ptr(new AutocryptRecipientPrivate())
94AutocryptRecipient::~AutocryptRecipient() =
default;
96void AutocryptRecipient::updateFromMessage(
const HeaderMixupNodeHelper &mixup,
const KMime::Headers::Base *header)
98 Q_D(AutocryptRecipient);
99 QDateTime effectiveDate = mixup.dateHeader();
105 if (d->autocrypt_timestamp.isValid() && effectiveDate <= d->autocrypt_timestamp) {
109 d->autocrypt_timestamp = effectiveDate;
112 if (!d->counting_since.isValid()) {
113 d->counting_since = effectiveDate;
114 d->count_have_ach = 0;
119 const auto params = paramsFromAutocryptHeader(header);
120 if (d->addr.isEmpty()) {
121 d->addr = params.value(
"addr");
123 d->prefer_encrypt = params.contains(
"prefer-encrypt");
124 d->keydata = params.value(
"keydata");
127 d->last_seen = effectiveDate;
128 d->count_have_ach += 1;
130 d->count_no_ach += 1;
131 if (mixup.hasMailHeader(
"User-Agent")) {
132 d->bad_user_agent = mixup.mailHeaderAsBase(
"User-Agent")->
as7BitString(
false);
137void AutocryptRecipient::updateFromGossip(
const HeaderMixupNodeHelper &mixup,
const KMime::Headers::Base *header)
139 Q_D(AutocryptRecipient);
140 QDateTime effectiveDate = mixup.dateHeader();
146 if (d->gossip_timestamp.isValid() && effectiveDate <= d->gossip_timestamp) {
150 const auto params = paramsFromAutocryptHeader(header);
152 if (d->addr.isEmpty()) {
153 d->addr = params.value(
"addr");
154 }
else if (d->addr != params.value(
"addr")) {
159 d->gossip_timestamp = effectiveDate;
160 d->gossip_key = params.value(
"keydata");
166 const Q_D(AutocryptRecipient);
167 return d->toJson(format);
170void AutocryptRecipient::fromJson(
const QByteArray &json)
172 Q_D(AutocryptRecipient);
173 return d->fromJson(json);
176bool AutocryptRecipient::hasChanged()
const
178 const Q_D(AutocryptRecipient);
182void AutocryptRecipient::setChangedFlag(
bool changed)
184 Q_D(AutocryptRecipient);
185 d->changed = changed;
190 assert(QGpgME::openpgp());
191 auto context = GpgME::Context::create(GpgME::OpenPGP);
193 GpgME::Data data(&dp);
194 const auto keys = data.toKeys();
195 if (keys.size() == 0) {
201GpgME::Key MessageCore::AutocryptRecipient::gpgKey()
const
203 const Q_D(AutocryptRecipient);
204 return ::gpgKey(d->keydata);
207QByteArray MessageCore::AutocryptRecipient::gpgKeydata()
const
209 const Q_D(AutocryptRecipient);
213GpgME::Key AutocryptRecipient::gossipKey()
const
215 const Q_D(AutocryptRecipient);
216 return ::gpgKey(d->gossip_key);
219QByteArray MessageCore::AutocryptRecipient::gossipKeydata()
const
221 const Q_D(AutocryptRecipient);
227 const Q_D(AutocryptRecipient);
231QByteArray AutocryptRecipient::bad_user_agent()
const
233 const Q_D(AutocryptRecipient);
234 return d->bad_user_agent;
237QDateTime AutocryptRecipient::last_seen()
const
239 const Q_D(AutocryptRecipient);
243QDateTime AutocryptRecipient::autocrypt_timestamp()
const
245 const Q_D(AutocryptRecipient);
246 return d->autocrypt_timestamp;
249QDateTime AutocryptRecipient::counting_since()
const
251 const Q_D(AutocryptRecipient);
252 return d->counting_since;
255QDateTime AutocryptRecipient::gossip_timestamp()
const
257 const Q_D(AutocryptRecipient);
258 return d->gossip_timestamp;
261int AutocryptRecipient::count_have_ach()
const
263 const Q_D(AutocryptRecipient);
264 return d->count_have_ach;
267int AutocryptRecipient::count_no_ach()
const
269 const Q_D(AutocryptRecipient);
270 return d->count_no_ach;
273bool AutocryptRecipient::prefer_encrypt()
const
275 const Q_D(AutocryptRecipient);
276 return d->prefer_encrypt;
char * toString(const EngineQuery &query)
KCODECS_EXPORT QByteArray base64Decode(QByteArrayView in)
int toInt(bool *ok, int base) const const
QDateTime currentDateTime()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
bool isObject() const const
QJsonObject object() const const
void setObject(const QJsonObject &object)
QByteArray toJson(JsonFormat format) const const
iterator insert(QLatin1StringView key, const QJsonValue &value)
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
QString fromLatin1(QByteArrayView str)
QByteArray toLatin1() const const