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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* This file is part of buteo-sync-plugin-caldav package
*
* Copyright (C) 2025 Damien Caliste <dcaliste@free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "davclient.h"
#include <QNetworkAccessManager>
#include <QDnsLookup>
#include <QTimer>
#include "settings_p.h"
#include "request_p.h"
#include "propfind_p.h"
#include "report_p.h"
#include "head_p.h"
#include "put_p.h"
#include "delete_p.h"
#include "logging_p.h"
namespace {
Buteo::Dav::Client::Reply reply(const Request &request, const QString &path)
{
return Buteo::Dav::Client::Reply(path, request.networkError(),
request.errorMessage(),
request.errorData());
}
QString ensureRoot(const QString &path)
{
if (path.startsWith(QChar('/')))
return path;
else
return QString::fromLatin1("/%1").arg(path);
}
}
class Buteo::Dav::ClientPrivate
{
public:
ClientPrivate()
{
}
ClientPrivate(const QString &serverAddress)
{
// Todo: use .chopped(1) here from Qt5.10.
m_settings.setServerAddress(serverAddress.endsWith(QChar('/'))
? serverAddress.left(serverAddress.length() - 1)
: serverAddress);
}
~ClientPrivate()
{
}
void setCalendarList(const QList<Buteo::Dav::CalendarInfo> &serverList)
{
if (m_settings.serverAddress().endsWith(QStringLiteral(".memotoo.com"))) {
// Memotoo has a single calendar, and is using categories
// to sort events. It's possible to list events on categories
// using a specific path for the request. But it's not possible
// to send events in a specific category without mentioning
// this category in the ICS data. While the server is exposing the
// categories as a list of calendars in a PROPFIND request, we
// should report a single calendar, valid for all categories,
// so the user can view all the events.
m_calendars.clear();
for (const Buteo::Dav::CalendarInfo &info : serverList) {
if (info.remotePath.endsWith(QStringLiteral("/category0/"))) {
m_calendars << info;
m_calendars[0].remotePath.chop(10);
break;
}
}
} else {
m_calendars = serverList;
}
}
Settings m_settings;
QNetworkAccessManager *m_networkManager;
bool m_wellKnowRetryInProgress = false;
QString m_userPrincipal;
QMap<QString, PropFind::UserAddressSet> m_serviceData;
QList<Buteo::Dav::CalendarInfo> m_calendars;
};
/*!
\class Client
\preliminary
\brief A client implementation for DAV operations.
Instances of this class can be used to perform DAV operations.
*/
/*!
Create a new client to perform DAV operations on \param serverAddress.
The server address should have the form as http[s]://dav.example.org.
*/
Buteo::Dav::Client::Client(const QString &serverAddress, QObject *parent)
: QObject(parent), d(new ClientPrivate(serverAddress))
{
d->m_networkManager = new QNetworkAccessManager(this);
}
/*!
Create a new client for \param service on the server at \param domain.
The domain should be of the form example.org. A DNS lookup is done on
the domain name to find if \param service is used and at which address.
Connect to the &Client::dnsLookupFinished signal to know when the
DNS lookup finished.
*/
Buteo::Dav::Client::Client(const QString &domain, const QString &service, QObject *parent)
: QObject(parent), d(new ClientPrivate)
{
d->m_networkManager = new QNetworkAccessManager(this);
const QString dnsService = QString::fromLatin1("_%1s._tcp.%2").arg(service).arg(domain);
QDnsLookup *dnsLookup = new QDnsLookup(QDnsLookup::SRV, dnsService, this);
connect(dnsLookup, &QDnsLookup::finished, this,
[this, dnsLookup, dnsService] () {
dnsLookup->deleteLater();
qCDebug(lcDav) << "Got DNS response" << dnsLookup->error();
if (dnsLookup->error() == QDnsLookup::NoError) {
for (const QDnsServiceRecord &record : dnsLookup->serviceRecords()) {
if (record.name() == dnsService) {
d->m_settings.setServerAddress(QString::fromLatin1("https://%1").arg(record.target()));
qCDebug(lcDav) << "Server address is" << d->m_settings.serverAddress();
break;
}
}
} else {
qCWarning(lcDav) << "DNS lookup failure:" << dnsLookup->errorString();
}
Reply reply(dnsService,
d->m_settings.serverAddress().isEmpty() ? QNetworkReply::ContentNotFoundError : QNetworkReply::NoError,
dnsLookup->errorString(),
QByteArray());
emit dnsLookupFinished(reply);
});
// Give time for the caller to connect to the serverAddressChanged signal.
qCDebug(lcDav) << "Will starting a DNS look-up on" << dnsService;
QTimer::singleShot(0, dnsLookup, [dnsLookup] () {dnsLookup->lookup();});
}
Buteo::Dav::Client::~Client()
{
}
/*!
Returns the server address as defined on construction or obtained by
DNS lookup.
*/
QString Buteo::Dav::Client::serverAddress() const
{
return d->m_settings.serverAddress();
}
/*!
Returns true if the client should ignore SSL errors, like self-signed certificates.
*/
bool Buteo::Dav::Client::ignoreSSLErrors() const
{
return d->m_settings.ignoreSSLErrors();
}
/*!
Set if the client should ignore SSL errors.
*/
void Buteo::Dav::Client::setIgnoreSSLErrors(bool ignore)
{
d->m_settings.setIgnoreSSLErrors(ignore);
}
/*!
Provide the login:password couple for a basic authentication on the server.
*/
void Buteo::Dav::Client::setAuthLogin(const QString &username, const QString &password)
{
d->m_settings.setUsername(username);
d->m_settings.setPassword(password);
}
/*!
Provide the token for a bearer authentication on the server.
*/
void Buteo::Dav::Client::setAuthToken(const QString &token)
{
d->m_settings.setAuthToken(token);
}
/*!
Inquire the server about the logged-in user and the main information
about the various DAV services the server provide. When \param service
is not empty, it restrict the inquiry to the specified service.
The list of inquired services can be obtained with services().
In case the information from the logged-in user is not available,
a fallback strategy is to only get \param service path, using the
.well-known/service mechanism. This is only possible when \param
service is not empty.
If the DAV services are not available on the root '/' of the server,
it is possible to give the \param davPath where the services are
available at. If the DAV path is not known, it can be guessed using
the .well-known/service mechanism, but only if \param service is
provided.
\sa userPrincipal(), services(), servicePath() and serviceMailto().
*/
void Buteo::Dav::Client::requestUserPrincipalAndServiceData(const QString &service,
const QString &davPath)
{
d->m_userPrincipal.clear();
d->m_serviceData.clear();
PropFind *userRequest = new PropFind(d->m_networkManager, &d->m_settings, this);
connect(userRequest, &Request::finished, this,
[this, userRequest, service] (const QString &uri) {
userRequest->deleteLater();
const QString userPrincipal = userRequest->userPrincipal();
if (!userRequest->hasError() && !userPrincipal.isEmpty()) {
d->m_userPrincipal = userPrincipal;
// determine the mailto href for this user.
PropFind *hrefsRequest = new PropFind(d->m_networkManager, &d->m_settings, this);
connect(hrefsRequest, &Request::finished, this,
[this, hrefsRequest] (const QString &uri) {
hrefsRequest->deleteLater();
if (!hrefsRequest->hasError()) {
d->m_serviceData = hrefsRequest->userAddressSets();
}
d->m_wellKnowRetryInProgress = false;
emit userPrincipalDataFinished(reply(*hrefsRequest, uri));
});
hrefsRequest->listUserAddressSet(userPrincipal, service);
} else if (!service.isEmpty() && !d->m_wellKnowRetryInProgress) {
// Can't find a user principal, try with a .well-known redirection.
Head *serviceRequest = new Head(d->m_networkManager, &d->m_settings, this);
connect(serviceRequest, &Request::finished, this,
[this, serviceRequest, service] (const QString &uri) {
serviceRequest->deleteLater();
if (!serviceRequest->hasError()) {
const QUrl url = serviceRequest->serviceUrl(service);
// Redirection may point to a different [sub]domain.
d->m_settings.setServerAddress(QString::fromLatin1("%1://%2").arg(url.scheme()).arg(url.host()));
// Retry to get a user principal using the provided redirect.
d->m_wellKnowRetryInProgress = true;
requestUserPrincipalAndServiceData(service, url.path());
} else {
emit userPrincipalDataFinished(reply(*serviceRequest, uri));
}
});
serviceRequest->getServiceUrl(service);
} else {
d->m_wellKnowRetryInProgress = false;
emit userPrincipalDataFinished(reply(*userRequest, uri));
}
});
userRequest->listCurrentUserPrincipal(ensureRoot(davPath));
}
/*!
Returns the path used to identify the logged-in user. It is available
after userPrincipalDataFinished() signal has been triggered.
\sa requestUserPrincipalAndServiceData()
*/
QString Buteo::Dav::Client::userPrincipal() const
{
return d->m_userPrincipal;
}
/*!
Returns the list of discovered (and supported) services on the DAV server.
It is available after userPrincipalDataFinished() signal has been triggered
and only if the logged-in user has been identified.
\sa requestUserPrincipalAndServiceData()
*/
QStringList Buteo::Dav::Client::services() const
{
return d->m_serviceData.keys();
}
/*!
Returns the email address declared by the user for \param service.
It is available after userPrincipalDataFinished() signal has been triggered
and only if the logged-in user has been identified.
\sa requestUserPrincipalAndServiceData()
*/
QString Buteo::Dav::Client::serviceMailto(const QString &service) const
{
return d->m_serviceData[service].mailto;
}
/*!
Returns the path at which \param service is running on the server.
\sa requestUserPrincipalAndServiceData()
*/
QString Buteo::Dav::Client::servicePath(const QString &service) const
{
return d->m_serviceData[service].path;
}
/*!
Request the list of VCALENDAR available at \param path on the server.
If \param path is empty, the path registered for "caldav" service is
used (when properly discovered).
\sa services() and calendars()
*/
void Buteo::Dav::Client::requestCalendarList(const QString &path)
{
d->m_calendars.clear();
PropFind *calendarRequest = new PropFind(d->m_networkManager, &d->m_settings, this);
connect(calendarRequest, &Request::finished, this,
[this, calendarRequest] (const QString &uri) {
calendarRequest->deleteLater();
if (!calendarRequest->hasError()) {
d->setCalendarList(calendarRequest->calendars());
}
emit calendarListFinished(reply(*calendarRequest, uri));
});
calendarRequest->listCalendars(path.isEmpty() ? servicePath(QStringLiteral("caldav")) : path);
}
/*!
Returns the list of available on the server calendars. This list is available
only after calendarListFinished() signal has been triggered.
\sa requestCalendarList()
*/
QList<Buteo::Dav::CalendarInfo> Buteo::Dav::Client::calendars() const
{
return d->m_calendars;
}
/*!
Request the list of etags for any calendar resources available at \param path
which occur within \param from and \param to.
The list of etags for every found resource will be exposed in the
calendarEtagsFinished() signal, as a map between resource path and etag.
*/
void Buteo::Dav::Client::getCalendarEtags(const QString &path,
const QDateTime &from, const QDateTime &to)
{
Report *report = new Report(d->m_networkManager, &d->m_settings);
connect(report, &Report::finished, this,
[this, report] (const QString &uri) {
report->deleteLater();
QHash<QString, QString> etags;
for (const Buteo::Dav::Resource &resource : report->response()) {
if (!resource.href.contains(uri)) {
qCWarning(lcDav) << "href does not contain server path:" << resource.href << ":" << uri;
} else {
etags.insert(resource.href, resource.etag);
}
}
emit calendarEtagsFinished(reply(*report, uri), etags);
});
report->getAllETags(path, from, to);
}
/*!
Request the list of any calendar resources available at \param path
which occur within \param from and \param to.
The list of found resources will be exposed in the calendarResourcesFinished()
signal.
*/
void Buteo::Dav::Client::getCalendarResources(const QString &path,
const QDateTime &from, const QDateTime &to)
{
Report *report = new Report(d->m_networkManager, &d->m_settings);
connect(report, &Report::finished, this,
[this, report] (const QString &uri) {
report->deleteLater();
emit calendarResourcesFinished(reply(*report, uri), report->response());
});
report->getAllEvents(path, from, to);
}
/*!
Request the list of any calendar resources available at \param path
matching the provided \param uids.
The list of found resources will be exposed in the calendarResourcesFinished()
signal.
*/
void Buteo::Dav::Client::getCalendarResources(const QString &path, const QStringList &uids)
{
Report *report = new Report(d->m_networkManager, &d->m_settings);
connect(report, &Report::finished, this,
[this, report] (const QString &uri) {
report->deleteLater();
emit calendarResourcesFinished(reply(*report, uri), report->response());
});
report->multiGetEvents(path, uids);
}
/*!
Send the given calendar \param data to the server at \param path location.
When \param etag is empty, the resource must not already exist on the server.
When \param etag is not empty, the resource on the server must match the
provided \param etag.
When the operation is complete, the sendCalendarFinished() signal will be
emitted. The \param etag of this signal is the new etag of the resource as
saved on the server. It may be empty is the server configuration don't reply
with the new etag.
*/
void Buteo::Dav::Client::sendCalendarResource(const QString &path, const QString &data, const QString &etag)
{
Put *put = new Put(d->m_networkManager, &d->m_settings);
connect(put, &Put::finished, this,
[this, put] (const QString &uri) {
put->deleteLater();
emit sendCalendarFinished(reply(*put, uri), put->updatedETag(uri));
});
put->sendIcalData(path, data, etag);
}
/*!
Delete the resource from the server at \param path.
*/
void Buteo::Dav::Client::deleteResource(const QString &path)
{
Delete *del = new Delete(d->m_networkManager, &d->m_settings, this);
connect(del, &Delete::finished, this,
[this, del] (const QString &uri) {
del->deleteLater();
emit deleteFinished(reply(*del, uri));
});
del->deleteEvent(path);
}
|