4 * Copyright 2016 Michael Rasmussen <mir@datanom.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 #include <curl/curl.h>
25 #include <curl/easy.h>
29 const char* const CollectionType_string
[] = {"unknown", "principal","calendar","addressbook"};
31 #define prop_request "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
32 "<A:propfind xmlns:A=\"DAV:\">" \
35 "<A:current-user-privilege-set/>" \
38 "<A:getlastmodified/>" \
39 "<A:getcontenttype/>" \
54 char trace_ascii
; /* 1 or 0 */
58 MemoryStruct
* headers
;
60 char error_buf
[CURL_ERROR_SIZE
];
63 static Buffers
* buffers_new() {
64 Buffers
* buffers
= g_new0(Buffers
, 1);
65 buffers
->data
= g_new0(MemoryStruct
, 1);
66 buffers
->data
->size
= 0;
67 buffers
->headers
= g_new0(MemoryStruct
, 1);
68 buffers
->headers
->size
= 0;
73 static void buffers_free(Buffers
* buffers
) {
75 if (buffers
->data
->memory
)
76 g_free(buffers
->data
->memory
);
77 g_free(buffers
->data
);
78 if (buffers
->headers
->memory
)
79 g_free(buffers
->headers
->memory
);
80 g_free(buffers
->headers
);
86 * This function is burrowed from the libcurl documentation
93 static void dump(const char* text
, FILE* stream
, char* ptr
, size_t size
, char nohex
) {
97 unsigned int width
=0x10;
100 /* without the hex output, we can fit more on screen */
102 fprintf(stream
, "%s, %zd bytes (0x%zx)\n", text
, size
, size
);
103 for(i
=0; i
<size
; i
+= width
) {
104 fprintf(stream
, "%04zx: ", i
);
106 /* hex not disabled, show it */
107 for(c
= 0; c
< width
; c
++) {
109 fprintf(stream
, "%02x ", ptr
[i
+c
]);
114 for(c
= 0; (c
< width
) && (i
+c
< size
); c
++) {
115 /* check for 0D0A; if found, skip past and start a new line of output */
116 if (nohex
&& (i
+c
+1 < size
) && ptr
[i
+c
]==0x0D && ptr
[i
+c
+1]==0x0A) {
120 fprintf(stream
, "%c",(ptr
[i
+c
]>=0x20) && (ptr
[i
+c
]<0x80)?ptr
[i
+c
]:'.');
121 /* check again for 0D0A, to avoid an extra \n if it's at width */
122 if (nohex
&& (i
+c
+2 < size
) && ptr
[i
+c
+1]==0x0D && ptr
[i
+c
+2]==0x0A) {
127 fputc('\n', stream
); /* newline */
133 * This function is burrowed from the libcurl documentation
141 static int my_trace(CURL
* handle
, curl_infotype type
, char* data
, size_t size
, void* userp
) {
142 Data
* config
= (Data
*)userp
;
144 (void)handle
; /* prevent compiler warning */
148 fprintf(stderr
, "== Info: %s", data
);
149 default: /* in case a new one is introduced to shock us */
151 case CURLINFO_HEADER_OUT
:
152 text
= "=> Send header";
154 case CURLINFO_DATA_OUT
:
155 text
= "=> Send data";
157 case CURLINFO_SSL_DATA_OUT
:
158 text
= "=> Send SSL data";
160 case CURLINFO_HEADER_IN
:
161 text
= "<= Recv header";
163 case CURLINFO_DATA_IN
:
164 text
= "<= Recv data";
166 case CURLINFO_SSL_DATA_IN
:
167 text
= "<= Recv SSL data";
170 dump(text
, stderr
, data
, size
, config
->trace_ascii
);
175 * This function is burrowed from the libcurl documentation
178 * @return void* to memory region
180 static void* myrealloc(void* ptr
, size_t size
) {
181 /* There might be a realloc() out there that doesn't like reallocing
182 * NULL pointers, so we take care of it here
185 return realloc(ptr
, size
);
191 * This function is burrowed from the libcurl documentation
196 * @return number of written bytes
198 static size_t WriteMemoryCallback(void* ptr
, size_t size
, size_t nmemb
, void* data
) {
199 size_t realsize
= size
* nmemb
;
200 MemoryStruct
* mem
= (MemoryStruct
*)data
;
201 mem
->memory
= (char *)myrealloc(mem
->memory
, mem
->size
+ realsize
+ 1);
204 memcpy(&(mem
->memory
[mem
->size
]), ptr
, realsize
);
205 mem
->size
+= realsize
;
206 mem
->memory
[mem
->size
] = 0;
211 static CURL
* get_curl(CarddavData
* data
) {
216 curl
= curl_easy_init();
219 userpwd
= g_strconcat(data
->user
, ":", data
->pwd
, NULL
);
220 curl_easy_setopt(curl
, CURLOPT_USERPWD
, userpwd
);
222 userpwd
= g_strconcat(data
->user
, NULL
);
223 curl_easy_setopt(curl
, CURLOPT_USERPWD
, userpwd
);
226 curl_easy_setopt(curl
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
227 curl_easy_setopt(curl
, CURLOPT_USERAGENT
, "carddavParser");
228 curl_easy_setopt(curl
, CURLOPT_URL
, data
->url
);
229 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, WriteMemoryCallback
);
230 curl_easy_setopt(curl
, CURLOPT_HEADERFUNCTION
, WriteMemoryCallback
);
232 config
.trace_ascii
= 1;
233 curl_easy_setopt(curl
, CURLOPT_DEBUGFUNCTION
, my_trace
);
234 curl_easy_setopt(curl
, CURLOPT_DEBUGDATA
, &config
);
235 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, 1);
237 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, 1);
238 curl_easy_setopt(curl
, CURLOPT_UNRESTRICTED_AUTH
, 1);
239 curl_easy_setopt(curl
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
245 static CURL
* prepare_curl(CarddavData
* dav
, Buffers
* buffers
, Request type
) {
246 CURL
* curl
= get_curl(dav
);
248 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, (void *)buffers
->data
);
249 curl_easy_setopt(curl
, CURLOPT_WRITEHEADER
, (void *)buffers
->headers
);
250 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, (char *)buffers
->error_buf
);
253 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, "OPTIONS");
256 curl_easy_setopt(curl
, CURLOPT_CUSTOMREQUEST
, "PROPFIND");
264 static gboolean
has_support(CarddavData
* dav
, const gchar
* headers
) {
265 gboolean support
= FALSE
;
267 g_print("%s\n", headers
);
268 gchar
* start
= g_strstr_len(headers
, -1, "DAV");
270 gchar
** values
= g_strsplit(start
, ":", 0);
271 if (values
&& g_strv_length(values
) > 1) {
272 gchar
** options
= g_strsplit(values
[1], ",", 0);
273 if (options
&& g_strv_length(options
) > 0) {
274 gchar
** tmp
= options
;
277 gchar
* end
= g_strstr_len(*tmp
, -1, "\r");
279 gchar
* str
= g_strndup(*tmp
, end
- *tmp
);
280 opt
= g_ascii_strup(str
, -1);
282 option
= g_strstrip(opt
);
284 opt
= g_ascii_strup(*tmp
, -1);
285 option
= g_strstrip(opt
);
287 if (g_strcmp0(option
, "ADDRESSBOOK") == 0)
289 if (g_strcmp0(option
, "EXTENDED-MKCOL") == 0)
303 static gchar
* find_text(const gchar
* string
, const gchar
* start
, const gchar
* end
) {
307 gchar
* s_start
= g_strstr_len(string
, -1, start
);
309 gchar
* s_end
= g_strstr_len(s_start
, -1, end
);
310 if (s_start
&& s_end
) {
311 text
= g_strndup(s_start
+ strlen(start
), s_end
- (s_start
+ strlen(start
)));
319 static gchar
* remove_ns(gchar
* s
) {
324 gchar
* s_start
= g_strstr_len(s
, -1, ":");
326 s1
= g_strndup(s_start
+ 1, strlen(s_start
+ 1));
337 static CollectionType
get_collection_type(const gchar
* s
) {
338 CollectionType type
= UNKNOWN
;
339 int len
= sizeof(CollectionType_string
)/sizeof(CollectionType_string
[0]);
342 for (int i
= 0; i
< len
; ++i
) {
343 if (g_strcmp0(CollectionType_string
[i
], s
) == 0) {
354 gboolean curl_get_objects(VCalData* vcal, GString* buf) {
357 gboolean curl_set_objects(VCalData* vcal, GString* buf) {
360 gboolean curl_update_objects(VCalData* vcal, GString* buf) {
363 gboolean curl_delete_objects(VCalData* vcal, GString* buf) {
366 gboolean curl_get_freebusy(VCalData* vcal, GString* buf) {
369 gboolean
curl_get_options(CarddavData
* dav
, GString
* buf
) {
371 CURLcode curlres
= 0;
372 Buffers
* buffers
= buffers_new();
374 CURL
* curl
= prepare_curl(dav
, buffers
, OPTIONS
);
376 curlres
= curl_easy_perform(curl
);
379 buf
= g_string_assign(buf
, buffers
->error_buf
);
385 curlres
= curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &code
);
390 res
= !has_support(dav
, buffers
->headers
->memory
);
391 GString
* displayname
= g_string_new(NULL
);
392 if (!curl_get_displayname(dav
, displayname
))
393 dav
->displayname
= g_string_free_and_steal(displayname
);
395 g_string_free(displayname
, TRUE
);
398 curl_easy_cleanup(curl
);
402 buffers_free(buffers
);
407 gboolean
curl_get_displayname(CarddavData
* dav
, GString
* buf
) {
409 CURLcode curlres
= 0;
410 Buffers
* buffers
= buffers_new();
412 if (!buf
) return TRUE
;
414 CURL
* curl
= prepare_curl(dav
, buffers
, PROPFIND
);
416 curl_easy_setopt(curl
, CURLOPT_POSTFIELDS
, prop_request
);
417 curl_easy_setopt(curl
, CURLOPT_POSTFIELDSIZE
, -1L);
418 curlres
= curl_easy_perform(curl
);
420 buf
= g_string_assign(buf
, buffers
->error_buf
);
425 curlres
= curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &code
);
430 gchar
* displayname
= find_text(buffers
->data
->memory
, "<displayname>", "</displayname>");
432 buf
= g_string_assign(buf
, displayname
);
438 curl_easy_cleanup(curl
);
443 buffers_free(buffers
);
448 gboolean
curl_get_resource_type(CarddavData
* dav
) {
449 Buffers
* buffers
= buffers_new();
451 CURLcode curlres
= 0;
452 GPtrArray
* types
= g_ptr_array_new_null_terminated(1, (GDestroyNotify
)g_free
, TRUE
);
454 CURL
* curl
= prepare_curl(dav
, buffers
, PROPFIND
);
456 curl_easy_setopt(curl
, CURLOPT_POSTFIELDS
, prop_request
);
457 curl_easy_setopt(curl
, CURLOPT_POSTFIELDSIZE
, -1L);
458 curlres
= curl_easy_perform(curl
);
460 g_ptr_array_add(types
, g_strdup(buffers
->error_buf
));
465 curlres
= curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &code
);
470 gchar
* resourcetype
= find_text(buffers
->data
->memory
, "<resourcetype>", "</resourcetype>");
472 gchar
* tmp
= g_strdup(resourcetype
);
473 g_free(resourcetype
);
474 resourcetype
= find_text(tmp
, "<", "/>");
475 gchar
* s
= remove_ns(resourcetype
);
476 g_free(resourcetype
);
478 g_ptr_array_add(types
, g_strdup(s
));
480 resourcetype
= find_text(tmp
, "><", "/>");
481 gchar
* s
= remove_ns(resourcetype
);
482 g_ptr_array_add(types
, g_strdup(s
));
483 g_free(resourcetype
);
491 curl_easy_cleanup(curl
);
494 if (types
->len
> 0) {
495 gboolean authorized
= FALSE
;
496 gpointer
* types_str
= g_ptr_array_free(types
, FALSE
);
497 gchar
** start
= (gchar
**)types_str
;
499 CollectionType type
= get_collection_type(*types_str
);
512 if (authorized
== FALSE
)
517 buffers_free(buffers
);
522 void curl_free_CarddavData(CarddavData
* dav
) {
524 g_free(dav
->displayname
);
532 const gchar
* get_resource_type_name(CollectionType type
) {
533 if (type
< TYPE_SIZE
)
534 return CollectionType_string
[type
];