This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cardcurler.cpp
586 lines (505 loc) · 22.2 KB
/
cardcurler.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/***************************************************************************
* Copyright (C) 2013 by Torsten Flammiger *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "cardcurler.h"
#include "option.h"
#include "cache.h"
#include "vCard/strutils.h"
/*
* CTOR
*/
CardCurler::CardCurler(const std::string &username, const std::string &password, const std::string &url, const string &rawQuery)
{
_url = url;
_username = username;
_password = password;
_rawQuery = rawQuery;
exportMode = false;
isSOGO = false;
}
/*
* Private method: used to detect the URL's a carddav server has to offer
* Returns a QStringList of all url's found by the XML query given
*
* @query : the xml snippet the carddav server expects to receive
* @return: a QStringList of raw vcard strings (BEGIN:VCARD ... END:VCARD)
*/
std::vector<std::string> CardCurler::getvCardURLs(const std::string &query) {
std::string s = get("PROPFIND", query);
if(Option::isVerbose()) {
std::cout << "CardCurler::getvCardURLs got PROPFIND result: " << s << std::endl;
}
// check xml case D:href (SOGo) or d:href (owncloud)
std::string href_begin = "<d:href>";
std::string href_end = "</d:href>";
if( StringUtils::contains(s, "D:href") ) {
href_begin = "<D:href>";
href_end = "</D:href>";
isSOGO = true;
if(Option::isVerbose()) {
std::cout << "Found SoGO namespace" << std::endl;
}
} else if( StringUtils::contains(s, "<href>") ) {
// radicale returns no namespaces here
href_begin = "<href>";
href_end = "</href>";
if(Option::isVerbose()) {
std::cout << "Assume Radicale response as there are no namespaces" << std::endl;
}
}
std::vector<std::string> result;
std::vector<std::string> tokens = StringUtils::split(s, href_begin);
if(Option::isVerbose()) {
std::cout << "Found " << tokens.size() << " tokens" << std::endl;
}
if(tokens.size() >= 1) {
for(unsigned int i=1; i<tokens.size(); i++) {
std::vector<std::string> inner = StringUtils::split(tokens.at(i), href_end);;
if(inner.size() >= 1) {
std::string url = inner.at(0);
if(Option::isVerbose()) {
std::cout << "CardCurler::getvCardURLs: parsed url: " << url << std::endl;
}
// RADICALE has URL's ending with vcf/
// disabled at 20140809
// if(StringUtils::endsWith(url, "vcf") || StringUtils::endsWith(url, "vcf/")) {
// result.push_back(url);
//}
result.push_back(url);
}
}
}
return result;
}
/*
* This public method used to fetch all cards for a given account.
*
* First it will ask the carddav server for the url's and then
* walk through each of the url's and download the vcard. If
* possible it will not end the connection after each download.
* It will cancel the action on any given failure given by libcurl
* and return whatever vcards where read at the given moment.
*
* @server: a full qualified hostname with protocol spec, i.e. http(s)://www.johndoe.com
* @query : the xml snippet the carddav server expects to receive
*
* @return: returns a Qlist of Person objects
*/
std::vector<Person> CardCurler::getAllCards(const std::string &server, const std::string &query) {
if(Option::isVerbose()) {
std::cout << "CardCurler::getAllCards called. Server: " << server << std::endl;
std::cout << "CardCurler::getAllCards called. Query: " << query << std::endl;
}
std::vector<Person> persons;
exportMode = true;
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
std::vector< std::string > cardUrls = getvCardURLs(query);
if(Option::isVerbose()) {
std::cout << "Number of card urls: " << cardUrls.size() << std::endl;
}
if(curl && cardUrls.size() > 0) {
if(Option::isVerbose()) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
}
std::string card;
std::string auth(_username + ":" + _password);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
curl_easy_setopt(curl, CURLOPT_HEADER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CardCurler::writeFunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &card);
//int j = 0;
for(unsigned int i=0; i < cardUrls.size(); i++) {
std::stringstream ss;
std::string url(cardUrls.at(i));
ss << server << url;
if(Option::isVerbose()) {
std::cout << "Curling url " << ss.str() << std::endl;
}
curl_easy_setopt(curl, CURLOPT_URL, ss.str().c_str());
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "CardCurler::getVCard() failed on URL: "
<< url
<< ", Code: "
<< curl_easy_strerror(res) << endl;
break;
}
if(Option::isVerbose()) {
std::cerr << "TRACE: " << "Card size: " << card.size() << " chars" << std::endl;
}
if(card.size() > 0) {
Person p;
std::vector<vCard> cards = vCard::fromString(card);
if(cards.size() == 1) {
createPerson(&cards[0], &p);
if(p.isValid()) {
std::cout << "fetched valid vcard from: " << server << url << endl;
p.rawCardData = card;
persons.push_back(p);
//j++;
}
}
card = "";
}
//if(j>12) break;
}
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return persons;
}
/*
* Private method: parses a pointer to a vCard object and pushes
* it's information to the Persons pointer.
* Only Firstname, Lastname and a List of Emails are of interrest.
*
* @vcdata: a pointer to a vCard object
* @p : a pointer to a Person object
*
* @return: void()
*/
void CardCurler::createPerson(const vCard *vcdata, Person *p) {
vCardPropertyList vcPropertyList = vcdata->properties();
for(vCardPropertyList::const_iterator it = vcPropertyList.begin(); it != vcPropertyList.end(); ++it ) {
vCardProperty vcProperty(*it);
std::string vcName(vcProperty.name());
if(vcName == VC_EMAIL) {
std::vector<std::string> emails = vcProperty.values();
for(std::vector<std::string>::const_iterator it = emails.begin(); it != emails.end(); ++it) {
std::string email(*it);
p->Emails.push_back(email);
}
} else if(vcName == VC_NAME) {
std::vector<std::string> values = vcProperty.values();
if (values.size() >= 2) { // at least firstname and lastname should be there
std::string fName = values.at(vCardProperty::Firstname);
std::string lName = values.at(vCardProperty::Lastname);
if(!fName.size() == 0 && !fName.size() == 0) {
p->FirstName = fName;
p->LastName = lName;
}
}
} else if(vcName == VC_FORMATTED_NAME) {
// not nice but there are entries in my carddav server
// who dont have a N (i.e. VC_NAME property containing firstname and lastname separately)
// but instead have only the formatted name property. So i will split this string
// on each whitespace and combine it somewhat friendly... But what about a title?
if(p->FirstName.size() == 0 || p->LastName.size() == 0) {
if(vcProperty.values().size() > 0) {
std::string formattedName = vcProperty.values().at(0);
if(formattedName.size() == 0) {
std::cerr << "There is no vcard property at index 0. Therefore I can't read the value of formatted name (FN)." << endl;
} else {
std::vector<std::string> tokens;
formattedName = StrUtils::simplify(formattedName); // remove doubled whitespace
formattedName = StrUtils::trim(formattedName); // remove leading and trailing whitespace
StrUtils::split(&tokens, formattedName, ' ');
if( tokens.size() == 1 ) {
p->LastName = tokens.at(0);
}
else if( tokens.size() >= 2 ) {
p->FirstName = tokens.at(0);
p->LastName = tokens.at(1);
} else {
std::cerr << "VCard property 'FN' contains invalid value(s): more then 2 tokens or none at all!: " << formattedName << std::endl;
}
}
} else {
std::cerr << "VCard has no values for property 'N'' and there are also no values for property 'FN'" << std::endl;
}
}
} else if (vcName == VC_REVISION) {
// append updated_at to the person
std::vector<std::string> revs = vcProperty.values();
if(!revs.size() == 0) {
p->lastUpdatedAt = revs.at(0);
}
}
}
}
/*
* Helper method to check if a given list of strings contains a string
*
* @list..: pointer to a QStringList to check
* @query.: the QString to check for
*
* @return: TRUE if the list contains the query, FALSE otherwise
*/
bool CardCurler::listContainsQuery(const std::vector<string> *list, const string &query) {
if(list->size() == 0) return false;
for(unsigned int i=0; i<list->size(); i++) {
if(StringUtils::contains(list->at(i), query)) {
return true;
}
}
return false;
}
// get server resource using libcurl
std::string CardCurler::get(const string &requestType, const std::string& query) {
std::string result;
// prepare the data structure from which curl reads the query which is then send to the peer
char *data = (char *)(query.c_str());
postdata pdata;
pdata.body_pos = 0;
pdata.body_size = strlen(data);
pdata.data = data;
CURL *curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Depth: 1");
headers = curl_slist_append(headers, "Content-Type: text/xml; charset=utf-8");
std::string auth(_username + ":" + _password);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, _url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, requestType.c_str());
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)pdata.body_size);
curl_easy_setopt(curl, CURLOPT_READDATA, &pdata);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &CardCurler::readFunc);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CardCurler::writeFunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
if(Option::isVerbose()) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
} else {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
}
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "CURL Error. Code: " << res << std::endl;
}
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return result;
}
std::vector<Person> CardCurler::curlCache(const std::string &query) {
if(Option::isVerbose()) {
std::cout << "Curling cache using query '" << query << "'";
}
Cache cache;
return cache.findInCache(query);
}
// curl a card online
std::vector<Person> CardCurler::curlCard(const std::string &query) {
// Result
std::vector<Person> people;
// execute the query!
std::string http_result = get("REPORT", query);
if(Option::isVerbose()) {
cout << "TRACE: " << "querying via func curlCard. Query is:" << query << endl;
}
std::string vcardAddressBeginToken = "<card:address-data>"; // defaults to Owncloud
std::string vcardAddressEndToken = "</card:address-data>";
if(http_result.find("<C:address-data>") != std::string::npos) {
vcardAddressBeginToken = "<C:address-data>";
vcardAddressEndToken = "</C:address-data>";
isSOGO = true;
}
if(http_result.find("<CR:address-data>") != std::string::npos) { // RADICALE
vcardAddressBeginToken = "<CR:address-data>";
vcardAddressEndToken = "</CR:address-data>";
}
std::vector<std::string> list = StringUtils::split(http_result, vcardAddressBeginToken);
if(list.size() > 0) {
for(unsigned int i=1; i<list.size(); i++) {
if(Option::isVerbose()) {
cout << list.at(i) << endl;
}
std::vector<std::string> _list = StringUtils::split(list.at(i), vcardAddressEndToken);
// _list contains 2 elements where the first element is a single vcard
if(_list.size() == 2) {
std::string s = _list.at(0);
StringUtils::replace(&s, " ", "");
if(isSOGO)
fixHtml(&s);
std::vector<vCard> vcards = vCard::fromString(s);
if(vcards.size() > 0) {
for(unsigned int j = 0; j < vcards.size(); j++) {
// there is only one vcard in the list - every time ;)
Person p;
vCard c = vcards.at(j);
createPerson(&c, &p);
if(p.isValid()) {
p.rawCardData = s;
people.push_back(p);
}
}
}
}
}
}
else
{
cout << "failed to get vcard data. code: " << res << endl;
}
return people;
}
/*
* This nice method tries to replace all html entities by its real char
* It will fail sometime! - or from time to time ;)
*/
void CardCurler::fixHtml(string* data) {
StringUtils::replace(data, " ", "");
StringUtils::replace(data, """, "\"");
StringUtils::replace(data, "&", "&");
StringUtils::replace(data, "<", "<");
StringUtils::replace(data, ">", ">");
StringUtils::replace(data, " ", " ");
StringUtils::replace(data, "¡", "¡");
StringUtils::replace(data, "¢", "¢");
StringUtils::replace(data, "£", "£");
StringUtils::replace(data, "¤", "¤");
StringUtils::replace(data, "¥", "¥");
StringUtils::replace(data, "¦", "¦");
StringUtils::replace(data, "§", "§");
StringUtils::replace(data, "¨", "¨");
StringUtils::replace(data, "©", "©");
StringUtils::replace(data, "ª", "ª");
StringUtils::replace(data, "«", "«");
StringUtils::replace(data, "¬", "¬");
StringUtils::replace(data, "­", "");
StringUtils::replace(data, "®", "®");
StringUtils::replace(data, "¯", "¯");
StringUtils::replace(data, "°", "°");
StringUtils::replace(data, "±", "±");
StringUtils::replace(data, "²", "²");
StringUtils::replace(data, "³", "³");
StringUtils::replace(data, "´", "´");
StringUtils::replace(data, "µ", "µ");
StringUtils::replace(data, "¶", "¶");
StringUtils::replace(data, "·", "·");
StringUtils::replace(data, "¸", "¸");
StringUtils::replace(data, "¹", "¹");
StringUtils::replace(data, "º", "º");
StringUtils::replace(data, "»", "»");
StringUtils::replace(data, "¼", "¼");
StringUtils::replace(data, "½", "½");
StringUtils::replace(data, "¾", "¾");
StringUtils::replace(data, "¿", "¿");
StringUtils::replace(data, "À", "À");
StringUtils::replace(data, "Á", "Á");
StringUtils::replace(data, "Â", "Â");
StringUtils::replace(data, "Ã", "Ã");
StringUtils::replace(data, "Ä", "Ä");
StringUtils::replace(data, "Å", "Å");
StringUtils::replace(data, "Æ", "Æ");
StringUtils::replace(data, "Ç", "Ç");
StringUtils::replace(data, "È", "È");
StringUtils::replace(data, "É", "É");
StringUtils::replace(data, "Ê", "Ê");
StringUtils::replace(data, "Ë", "Ë");
StringUtils::replace(data, "Ì", "Ì");
StringUtils::replace(data, "Í", "Í");
StringUtils::replace(data, "Î", "Î");
StringUtils::replace(data, "Ï", "Ï");
StringUtils::replace(data, "Ð", "Ð");
StringUtils::replace(data, "Ñ", "Ñ");
StringUtils::replace(data, "Ò", "Ò");
StringUtils::replace(data, "Ó", "Ó");
StringUtils::replace(data, "Ô", "Ô");
StringUtils::replace(data, "Õ", "Õ");
StringUtils::replace(data, "Ö", "Ö");
StringUtils::replace(data, "×", "×");
StringUtils::replace(data, "Ø", "Ø");
StringUtils::replace(data, "Ù", "Ù");
StringUtils::replace(data, "Ú", "Ú");
StringUtils::replace(data, "Û", "Û");
StringUtils::replace(data, "Ü", "Ü");
StringUtils::replace(data, "Ý", "Ý");
StringUtils::replace(data, "Þ", "Þ");
StringUtils::replace(data, "ß", "ß");
StringUtils::replace(data, "à", "à");
StringUtils::replace(data, "á", "á");
StringUtils::replace(data, "â", "â");
StringUtils::replace(data, "ã", "ã");
StringUtils::replace(data, "ä", "ä");
StringUtils::replace(data, "å", "å");
StringUtils::replace(data, "æ", "æ");
StringUtils::replace(data, "ç", "ç");
StringUtils::replace(data, "è", "è");
StringUtils::replace(data, "é", "é");
StringUtils::replace(data, "ê", "ê");
StringUtils::replace(data, "ë", "ë");
StringUtils::replace(data, "ì", "ì");
StringUtils::replace(data, "í", "í");
StringUtils::replace(data, "î", "î");
StringUtils::replace(data, "ï", "ï");
StringUtils::replace(data, "ð", "ð");
StringUtils::replace(data, "ñ", "ñ");
StringUtils::replace(data, "ò", "ò");
StringUtils::replace(data, "ó", "ó");
StringUtils::replace(data, "ô", "ô");
StringUtils::replace(data, "õ", "õ");
StringUtils::replace(data, "ö", "ö");
StringUtils::replace(data, "÷", "÷");
StringUtils::replace(data, "ø", "ø");
StringUtils::replace(data, "ù", "ù");
StringUtils::replace(data, "ú", "ú");
StringUtils::replace(data, "û", "û");
StringUtils::replace(data, "ü", "ü");
StringUtils::replace(data, "ý", "ý");
StringUtils::replace(data, "þ", "þ");
StringUtils::replace(data, "ÿ", "ÿ");;
}
// a really nice way to implement a write-data-function
// taken from https://github.com/akrennmair/newsbeuter/blob/master/src/google_api.cpp
// (and now I understand what is happening under the hood here...)
size_t CardCurler::writeFunc(void *buffer, size_t size, size_t nmemb, void *userp) {
// we know, that *userp points to a std::string because that is what we
// applied to CURLOPT_WRITEDATA. Therefore we can cast the void pointer into a
// pointer to std::string and simply use STL method append() to append the data
// that we receive in *buffer
std::string * pbuf = static_cast<std::string *>(userp);
pbuf->append(static_cast<const char *>(buffer), size * nmemb);
return size * nmemb;
}
// making the same as in our writeFunc is not possible here. AFAIK.
// I don't know a method making a pointer to std::string from const char*
// without copying it - and thus we would work on a copy of *buffer instead on *buffer directly
size_t CardCurler::readFunc(void *buffer, size_t size, size_t nmemb, void *userp)
{
if (userp)
{
postdata *ud = (postdata*) userp;
int curlSize = size * nmemb;
int available = (ud->body_size - ud->body_pos);
if (available > 0)
{
int written = std::min( curlSize, available );
memcpy(buffer, ((char*)(ud->data)) + ud->body_pos, written);
ud->body_pos += written;
return written;
}
}
return 0;
}