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())
94void AutocryptRecipient::updateFromMessage(
const HeaderMixupNodeHelper &mixup,
const KMime::Headers::Base *header)
96 Q_D(AutocryptRecipient);
97 QDateTime effectiveDate = mixup.dateHeader();
103 if (d->autocrypt_timestamp.isValid() && effectiveDate <= d->autocrypt_timestamp) {
107 d->autocrypt_timestamp = effectiveDate;
110 if (!d->counting_since.isValid()) {
111 d->counting_since = effectiveDate;
112 d->count_have_ach = 0;
117 const auto params = paramsFromAutocryptHeader(header);
118 if (d->addr.isEmpty()) {
119 d->addr = params.value(
"addr");
121 d->prefer_encrypt = params.contains(
"prefer-encrypt");
122 d->keydata = params.value(
"keydata");
125 d->last_seen = effectiveDate;
126 d->count_have_ach += 1;
128 d->count_no_ach += 1;
129 if (mixup.hasMailHeader(
"User-Agent")) {
130 d->bad_user_agent = mixup.mailHeaderAsBase(
"User-Agent")->
as7BitString(
false);
135void AutocryptRecipient::updateFromGossip(
const HeaderMixupNodeHelper &mixup,
const KMime::Headers::Base *header)
137 Q_D(AutocryptRecipient);
138 QDateTime effectiveDate = mixup.dateHeader();
144 if (d->gossip_timestamp.isValid() && effectiveDate <= d->gossip_timestamp) {
148 const auto params = paramsFromAutocryptHeader(header);
150 if (d->addr.isEmpty()) {
151 d->addr = params.value(
"addr");
152 }
else if (d->addr != params.value(
"addr")) {
157 d->gossip_timestamp = effectiveDate;
158 d->gossip_key = params.value(
"keydata");
164 const Q_D(AutocryptRecipient);
165 return d->toJson(format);
168void AutocryptRecipient::fromJson(
const QByteArray &json)
170 Q_D(AutocryptRecipient);
171 return d->fromJson(json);
174bool AutocryptRecipient::hasChanged()
const
176 const Q_D(AutocryptRecipient);
180void AutocryptRecipient::setChangedFlag(
bool changed)
182 Q_D(AutocryptRecipient);
183 d->changed = changed;
188 assert(QGpgME::openpgp());
189 auto context = GpgME::Context::create(GpgME::OpenPGP);
191 GpgME::Data data(&dp);
192 const auto keys = data.toKeys();
193 if (keys.size() == 0) {
199GpgME::Key MessageCore::AutocryptRecipient::gpgKey()
const
201 const Q_D(AutocryptRecipient);
202 return ::gpgKey(d->keydata);
205QByteArray MessageCore::AutocryptRecipient::gpgKeydata()
const
207 const Q_D(AutocryptRecipient);
211GpgME::Key AutocryptRecipient::gossipKey()
const
213 const Q_D(AutocryptRecipient);
214 return ::gpgKey(d->gossip_key);
217QByteArray MessageCore::AutocryptRecipient::gossipKeydata()
const
219 const Q_D(AutocryptRecipient);
225 const Q_D(AutocryptRecipient);
229QByteArray AutocryptRecipient::bad_user_agent()
const
231 const Q_D(AutocryptRecipient);
232 return d->bad_user_agent;
235QDateTime AutocryptRecipient::last_seen()
const
237 const Q_D(AutocryptRecipient);
241QDateTime AutocryptRecipient::autocrypt_timestamp()
const
243 const Q_D(AutocryptRecipient);
244 return d->autocrypt_timestamp;
247QDateTime AutocryptRecipient::counting_since()
const
249 const Q_D(AutocryptRecipient);
250 return d->counting_since;
253QDateTime AutocryptRecipient::gossip_timestamp()
const
255 const Q_D(AutocryptRecipient);
256 return d->gossip_timestamp;
259int AutocryptRecipient::count_have_ach()
const
261 const Q_D(AutocryptRecipient);
262 return d->count_have_ach;
265int AutocryptRecipient::count_no_ach()
const
267 const Q_D(AutocryptRecipient);
268 return d->count_no_ach;
271bool AutocryptRecipient::prefer_encrypt()
const
273 const Q_D(AutocryptRecipient);
274 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