From c33ca4d81ed99f670f87ee9d9794fbddc592e173 Mon Sep 17 00:00:00 2001 From: Michael Rasmussen Date: Sat, 29 Mar 2025 03:41:51 +0100 Subject: [PATCH] Begin adding support for carddav server Signed-off-by: Michael Rasmussen --- autogen.sh | 2 +- configure.ac | 46 +-- example/Makefile.am | 10 +- example/vcard-example.c | 107 ++++++- src/Makefile.am | 28 +- src/curl/Makefile.am | 17 + src/curl/curllib.c | 537 ++++++++++++++++++++++++++++++++ src/curl/curllib.h | 46 +++ src/parser/Makefile.am | 18 ++ src/{ => parser}/globals.h | 18 ++ src/{ => parser}/vcard-parser.c | 14 +- src/{ => parser}/vcard-parser.h | 0 src/{ => parser}/vcard.c | 10 +- src/{ => parser}/vcard.h | 0 14 files changed, 790 insertions(+), 63 deletions(-) create mode 100644 src/curl/Makefile.am create mode 100644 src/curl/curllib.c create mode 100644 src/curl/curllib.h create mode 100644 src/parser/Makefile.am rename src/{ => parser}/globals.h (81%) rename src/{ => parser}/vcard-parser.c (95%) rename src/{ => parser}/vcard-parser.h (100%) rename src/{ => parser}/vcard.c (97%) rename src/{ => parser}/vcard.h (100%) diff --git a/autogen.sh b/autogen.sh index c9f6e38..f685fef 100755 --- a/autogen.sh +++ b/autogen.sh @@ -22,7 +22,7 @@ if [ ! -z ${AUTORECONF} ]; then else echo "No autoreconf found. Using plain old tools to rebuild build system" libtoolize --automake --force || error libtoolize - aclocal -I m4|| error aclocal + aclocal -I m4 || error aclocal autoheader --force || error autoheader automake --add-missing --force-missing --gnu || error automake autoconf --force || error autoconf diff --git a/configure.ac b/configure.ac index 9b9f85c..21d744a 100644 --- a/configure.ac +++ b/configure.ac @@ -1,30 +1,28 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. -AC_PREREQ(2.59) -AC_INIT(libvcard, 0.1, mir@datanom.net) +AC_PREREQ([2.72]) +AC_INIT([libvcard],[0.1],[mir@datanom.net]) +AC_COPYRIGHT([Copyright (c) 2024,2025 Michael Rasmussen.]) -LIB_VERSION="0:0:0" -AC_SUBST(LIB_VERSION) +AC_CANONICAL_TARGET -AC_CONFIG_SRCDIR(src/vcard.c) AM_INIT_AUTOMAKE AM_MAINTAINER_MODE -AC_CONFIG_HEADER([config.h]) +AC_CONFIG_HEADERS([config.h]) CFLAGS="-Wall" AC_CONFIG_MACRO_DIR([m4]) +AC_CONFIG_SRCDIR(src/Makefile.am) + # Checks for programs. AC_PROG_CC -AM_PROG_CC_STDC -AC_LANG_C -AC_ISC_POSIX +AC_PROG_CPP AC_PROG_INSTALL AC_PROG_LN_S AC_PROG_MAKE_SET AC_PROG_CPP -AC_PROG_INSTALL -AC_PROG_LIBTOOL +LT_INIT # Find pkg-config # @@ -37,28 +35,33 @@ PKG_CHECK_MODULES(GLIB, [[glib-2.0 >= 2.30 gmodule-2.0 >= 2.30 gobject-2.0 >= 2. AC_SUBST(GLIB_CFLAGS) AC_SUBST(GLIB_LIBS) -# Checks for header files. -AC_HEADER_STDC - # Checks for typedefs, structures, and compiler characteristics. AC_C_CONST AC_TYPE_SIZE_T AC_TYPE_SSIZE_T # Checks for library functions. +if test -n "$lt_prog_compiler_pic"; then + CFLAGS="$CFLAGS $lt_prog_compiler_pic" +fi + +# Checks for libraries. +PKG_CHECK_MODULES(CURL, [libcurl >= 7.15.5 gnutls]) +AC_SUBST(CURL_CFLAGS) +AC_SUBST(CURL_LIBS) # Optional building instructions if test $USE_MAINTAINER_MODE = yes; then - CFLAGS="$CFLAGS -g -Werror" + CFLAGS="$CFLAGS -g -Werror" LIBS="$GLIB_LIBS" else - CFLAGS="$CFLAGS -O -O2" + CFLAGS="$CFLAGS -O -O2" LIBS="$GLIB_LIBS" fi ac_enable_example="no" AC_ARG_ENABLE([example], - [AC_HELP_STRING([--enable-example],[Build example [default=no]])], + [AS_HELP_STRING([--enable-example],[Build example [default=no]])], [ac_enable_example="${enableval}"]) AC_MSG_CHECKING([whether to compile the example application]) @@ -85,14 +88,17 @@ AC_CONFIG_COMMANDS( echo -e "Email addresses can be found in the AUTHORS file." echo ""]], [ - CFLAGS="$CFLAGS" - LIBS="$LIBS" + CFLAGS="$CFLAGS $CURL_CFLAGS" + LIBS="$LIBS $CURL_LIBS" EXAMPLE="$ac_enable_example" ] ) -AC_OUTPUT([ +AC_CONFIG_FILES([ Makefile example/Makefile src/Makefile + src/parser/Makefile + src/curl/Makefile ]) +AC_OUTPUT diff --git a/example/Makefile.am b/example/Makefile.am index d6a6b11..ff3bfd8 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -3,7 +3,8 @@ AUTOMAKE_OPTIONS = gnu AM_CPPFLAGS = \ @GLIB_CFLAGS@ \ -I$(top_srcdir) -I$(top_builddir) \ - -I$(top_srcdir)/src -I$(top_builddir)/src \ + -I$(top_srcdir)/src/parser -I$(top_builddir)/src/parser \ + -I$(top_srcdir)/src/curl -I$(top_builddir)/src/curl \ -I$(top_srcdir)/example -I$(top_builddir)/example bin_PROGRAMS = vcard-example @@ -12,6 +13,7 @@ vcard_example_SOURCES = \ vcard-example.c vcard_example_LDADD = \ - -L${top_srcdir}/src \ - -lvCard \ - $(GLIB_LIBS) + $(top_srcdir)/src/parser/libvcard.a \ + $(top_srcdir)/src/curl/libcurl.a \ + @GLIB_LIBS@ \ + @CURL_LIBS@ diff --git a/example/vcard-example.c b/example/vcard-example.c index 43e5b2d..f8f1770 100644 --- a/example/vcard-example.c +++ b/example/vcard-example.c @@ -4,14 +4,83 @@ #include #include -#include +#include "globals.h" #include "vcard.h" +#include "curllib.h" + +typedef struct { + gchar* uid; + gchar* pwd; + gchar* url; + gboolean debug; +} Arguments; + +static void arguments_free(Arguments* a) { + if (a) { + g_free(a->pwd); + g_free(a->uid); + g_free(a->url); + g_free(a); + } +} + +static Arguments* parse_commandline(GOptionContext* opt_context, gint argc, gchar** argv) { + Arguments* args = g_new0(Arguments, 1); + static gchar* uid = NULL; + static gchar* pwd = NULL; + static gchar* url = NULL; + static gboolean debug = FALSE; + GError *error = NULL; + + static GOptionEntry opt_entries[] = { + { "username", 'u', 0, G_OPTION_ARG_STRING, &uid, "Username for the account", "U"}, + { "password", 'p', 0, G_OPTION_ARG_STRING, &pwd, "Password for the account", "P"}, + { "url", 'l', 0, G_OPTION_ARG_STRING, &url, "URL to the server", "://foo.bar[/collection]"}, + { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Enable debug mode", NULL }, + { NULL } + }; + + g_option_context_add_main_entries (opt_context, opt_entries, NULL); + if (!g_option_context_parse(opt_context, &argc, &argv, &error)) { + g_printerr ("Error parsing options: %s\n", error->message); + g_error_free(error); + g_free(args); + args = NULL; + goto CLEAN; + } + + if (!uid || !url) { + g_printerr("Missing username and/or url\n"); + g_free(args); + args = NULL; + goto CLEAN; + } else { + args->pwd = pwd; + args->uid = uid; + args->url = url; + args->debug = debug; + goto OK; + } + + CLEAN: + if (uid) + g_free(uid); + if (pwd) + g_free(pwd); + if (url) + g_free(url); + + OK: + return args; +} int main(int argc, char *argv[]) { - GError* err = NULL; - gchar* buffer = NULL; - int args; + GOptionContext* opt_context = g_option_context_new(NULL); + + Arguments* args = parse_commandline(opt_context, argc, argv); + if (args) { +/* if (argc > 1) { for (args = 1; args < argc; args++) { g_file_get_contents(argv[args], &buffer, NULL, &err); @@ -47,10 +116,36 @@ int main(int argc, char *argv[]) { g_print("\n"); VCard_list_free(list); } +*/ + + CarddavData* data = g_new0(CarddavData, 1); + data->debug = args->debug; + data->url = g_strdup(args->url); + data->user = g_strdup(args->uid); + data->pwd = g_strdup(args->pwd); + data->mkcol = FALSE; + arguments_free(args); + + GString* buf = g_string_new(NULL); + if (curl_get_options(data, buf)) { + g_print("ERROR\n%s\n", buf->str); + } else { + g_print("OK\nDisplayname: %s\nMKCOL support: %s\n", data->displayname, data->mkcol ? "True" : "False"); + } + g_string_free(buf, TRUE); + gboolean res = curl_get_resource_type(data); + if (res) { + g_printerr("Unautorized\n"); + } else { + g_print("CollectionType: %s\n", get_resource_type_name(data->type)); + } + curl_free_CarddavData(data); } else { - g_print("Mising file argument(s)\n"); - g_print("Usage: %s file [file ..]\n", argv[0]); + gchar* help = g_option_context_get_help(opt_context, TRUE, NULL); + g_print("%s\n", help); + g_free(help); } + g_option_context_free(opt_context); return 0; } diff --git a/src/Makefile.am b/src/Makefile.am index 157805a..63d2085 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,29 +1,3 @@ AUTOMAKE_OPTIONS = gnu -libvCard_la_CPPFLAGS = \ - -I$(top_builddir) \ - -I $(top_srcdir) \ - @GLIB_CFLAGS@ \ - $(AM_CPPFLAGS) - -libvCard_la_CFLAGS = \ - @GLIB_CFLAGS@ \ - $(AM_CFLAGS) - -lib_LTLIBRARIES = libvCard.la - -libvCard_la_SOURCES = \ - vcard.c \ - vcard-parser.h \ - vcard-parser.c - -libvCard_la_LDFLAGS = \ - -version-info @LIB_VERSION@ - -include_HEADERS = \ - vcard.h \ - globals.h - -libvCard_la_LIBADD = \ - @GLIB_LIBS@ - +SUBDIRS = parser curl diff --git a/src/curl/Makefile.am b/src/curl/Makefile.am new file mode 100644 index 0000000..97bb88c --- /dev/null +++ b/src/curl/Makefile.am @@ -0,0 +1,17 @@ +AUTOMAKE_OPTIONS = gnu + +AM_CPPFLAGS = \ + -I$(top_builddir)/src/curl \ + -I$(top_srcdir)/src/curl \ + -I$(top_builddir)/src/parser \ + -I$(top_srcdir)/src/parser \ + @GLIB_CFLAGS@ \ + @CURL_CFLAGS@ + + +noinst_LIBRARIES = libcurl.a + +libcurl_a_SOURCES = \ + curllib.h \ + curllib.c + diff --git a/src/curl/curllib.c b/src/curl/curllib.c new file mode 100644 index 0000000..6b18dd0 --- /dev/null +++ b/src/curl/curllib.c @@ -0,0 +1,537 @@ +/* + * curllib.c + * + * Copyright 2016 Michael Rasmussen + * + * 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., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include +#include +#include "globals.h" +#include "curllib.h" + +const char* const CollectionType_string[] = {"unknown", "principal","calendar","addressbook"}; + +#define prop_request "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" \ + "" + +typedef enum { + PROPFIND, + OPTIONS +} Request; + +typedef struct { + char * memory; + size_t size; +} MemoryStruct; + +typedef struct { + char trace_ascii; /* 1 or 0 */ +} Data; + +typedef struct { + MemoryStruct* headers; + MemoryStruct* data; + char error_buf[CURL_ERROR_SIZE]; +} Buffers; + +static Buffers* buffers_new() { + Buffers* buffers = g_new0(Buffers, 1); + buffers->data = g_new0(MemoryStruct, 1); + buffers->data->size = 0; + buffers->headers = g_new0(MemoryStruct, 1); + buffers->headers->size = 0; + + return buffers; +} + +static void buffers_free(Buffers* buffers) { + if (buffers) { + if (buffers->data->memory) + g_free(buffers->data->memory); + g_free(buffers->data); + if (buffers->headers->memory) + g_free(buffers->headers->memory); + g_free(buffers->headers); + g_free(buffers); + } +} + +/** + * This function is burrowed from the libcurl documentation + * @param text + * @param stream + * @param ptr + * @param size + * @param nohex + */ +static void dump(const char* text, FILE* stream, char* ptr, size_t size, char nohex) { + size_t i; + size_t c; + + unsigned int width=0x10; + + if(nohex) + /* without the hex output, we can fit more on screen */ + width = 0x40; + fprintf(stream, "%s, %zd bytes (0x%zx)\n", text, size, size); + for(i=0; i=0x20) && (ptr[i+c]<0x80)?ptr[i+c]:'.'); + /* check again for 0D0A, to avoid an extra \n if it's at width */ + if (nohex && (i+c+2 < size) && ptr[i+c+1]==0x0D && ptr[i+c+2]==0x0A) { + i+=(c+3-width); + break; + } + } + fputc('\n', stream); /* newline */ + } + fflush(stream); +} + +/** + * This function is burrowed from the libcurl documentation + * @param handle + * @param type + * @param data + * @param size + * @param userp + * @return + */ +static int my_trace(CURL* handle, curl_infotype type, char* data, size_t size, void* userp) { + Data* config = (Data *)userp; + const char* text; + (void)handle; /* prevent compiler warning */ + + switch (type) { + case CURLINFO_TEXT: + fprintf(stderr, "== Info: %s", data); + default: /* in case a new one is introduced to shock us */ + return 0; + case CURLINFO_HEADER_OUT: + text = "=> Send header"; + break; + case CURLINFO_DATA_OUT: + text = "=> Send data"; + break; + case CURLINFO_SSL_DATA_OUT: + text = "=> Send SSL data"; + break; + case CURLINFO_HEADER_IN: + text = "<= Recv header"; + break; + case CURLINFO_DATA_IN: + text = "<= Recv data"; + break; + case CURLINFO_SSL_DATA_IN: + text = "<= Recv SSL data"; + break; + } + dump(text, stderr, data, size, config->trace_ascii); + return 0; +} + +/** + * This function is burrowed from the libcurl documentation + * @param ptr + * @param size + * @return void* to memory region + */ +static void* myrealloc(void* ptr, size_t size) { +/* There might be a realloc() out there that doesn't like reallocing + * NULL pointers, so we take care of it here + * */ + if(ptr) + return realloc(ptr, size); + else + return malloc(size); +} + +/** + * This function is burrowed from the libcurl documentation + * @param ptr + * @param size + * @param nmemb + * @param data + * @return number of written bytes + */ +static size_t WriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data) { + size_t realsize = size * nmemb; + MemoryStruct* mem = (MemoryStruct *)data; + mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); + + if (mem->memory) { + memcpy(&(mem->memory[mem->size]), ptr, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + } + return realsize; +} + +static CURL* get_curl(CarddavData* data) { + CURL* curl; + gchar* userpwd; + Data config; + + curl = curl_easy_init(); + if (curl) { + if (data->pwd) { + userpwd = g_strconcat(data->user, ":", data->pwd, NULL); + curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd); + } else { + userpwd = g_strconcat(data->user, NULL); + curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd); + } + g_free(userpwd); + curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "carddavParser"); + curl_easy_setopt(curl, CURLOPT_URL, data->url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); + curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, WriteMemoryCallback); + if (data->debug) { + config.trace_ascii = 1; + curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace); + curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + } + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt(curl, CURLOPT_UNRESTRICTED_AUTH, 1); + curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL); + } + + return curl; +} + +static CURL* prepare_curl(CarddavData* dav, Buffers* buffers, Request type) { + CURL* curl = get_curl(dav); + if (curl) { + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)buffers->data); + curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)buffers->headers); + curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, (char *)buffers->error_buf); + switch (type) { + case OPTIONS: + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS"); + break; + case PROPFIND: + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND"); + break; + } + } + + return curl; +} + +static gboolean has_support(CarddavData* dav, const gchar* headers) { + gboolean support = FALSE; + + g_print("%s\n", headers); + gchar* start = g_strstr_len(headers, -1, "DAV"); + if (start) { + gchar** values = g_strsplit(start, ":", 0); + if (values && g_strv_length(values) > 1) { + gchar** options = g_strsplit(values[1], ",", 0); + if (options && g_strv_length(options) > 0) { + gchar** tmp = options; + while (*tmp) { + gchar *opt, *option; + gchar* end = g_strstr_len(*tmp, -1, "\r"); + if (end) { + gchar* str = g_strndup(*tmp, end - *tmp); + opt = g_ascii_strup(str, -1); + g_free(str); + option = g_strstrip(opt); + } else { + opt = g_ascii_strup(*tmp, -1); + option = g_strstrip(opt); + } + if (g_strcmp0(option, "ADDRESSBOOK") == 0) + support = TRUE; + if (g_strcmp0(option, "EXTENDED-MKCOL") == 0) + dav->mkcol = TRUE; + g_free(opt); + tmp++; + } + } + g_strfreev(options); + } + g_strfreev(values); + } + + return support; +} + +static gchar* find_text(const gchar* string, const gchar* start, const gchar* end) { + gchar* text = NULL; + + if (start && end) { + gchar* s_start = g_strstr_len(string, -1, start); + if (s_start) { + gchar* s_end = g_strstr_len(s_start, -1, end); + if (s_start && s_end) { + text = g_strndup(s_start + strlen(start), s_end - (s_start + strlen(start))); + } + } + } + + return text; +} + +static gchar* remove_ns(gchar* s) { + gchar* s1 = NULL; + //gchar* tmp = NULL; + + if (s) { + gchar* s_start = g_strstr_len(s, -1, ":"); + if (s_start) { + s1 = g_strndup(s_start + 1, strlen(s_start + 1)); + } else { + s1 = g_strdup(s); + } + g_strstrip(s1); + //g_free(tmp); + } + + return s1; +} + +static CollectionType get_collection_type(const gchar* s) { + CollectionType type = UNKNOWN; + int len = sizeof(CollectionType_string)/sizeof(CollectionType_string[0]); + + if (s) { + for (int i = 0; i < len; ++i) { + if (g_strcmp0(CollectionType_string[i], s) == 0) { + type = i; + break; + } + } + } + + return type; +} + +/* +gboolean curl_get_objects(VCalData* vcal, GString* buf) { +} + +gboolean curl_set_objects(VCalData* vcal, GString* buf) { +} + +gboolean curl_update_objects(VCalData* vcal, GString* buf) { +} + +gboolean curl_delete_objects(VCalData* vcal, GString* buf) { +} + +gboolean curl_get_freebusy(VCalData* vcal, GString* buf) { +} +*/ +gboolean curl_get_options(CarddavData* dav, GString* buf) { + gboolean res; + CURLcode curlres = 0; + Buffers* buffers = buffers_new(); + + CURL* curl = prepare_curl(dav, buffers, OPTIONS); + if (curl) { + curlres = curl_easy_perform(curl); + if (curlres != 0) { + if (buf) { + buf = g_string_assign(buf, buffers->error_buf); + } + res = TRUE; + } + else { + long code; + curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); + if (code != 200) { + res = TRUE; + } + else { + res = !has_support(dav, buffers->headers->memory); + GString* displayname = g_string_new(NULL); + if (!curl_get_displayname(dav, displayname)) + dav->displayname = g_string_free_and_steal(displayname); + else + g_string_free(displayname, TRUE); + } + } + curl_easy_cleanup(curl); + } else { + res = TRUE; + } + buffers_free(buffers); + + return res; +} + +gboolean curl_get_displayname(CarddavData* dav, GString* buf) { + gboolean res; + CURLcode curlres = 0; + Buffers* buffers = buffers_new(); + + if (!buf) return TRUE; + + CURL* curl = prepare_curl(dav, buffers, PROPFIND); + if (curl) { + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, prop_request); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L); + curlres = curl_easy_perform(curl); + if (curlres != 0) { + buf = g_string_assign(buf, buffers->error_buf); + res = TRUE; + } + else { + long code; + curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); + if (code != 207) { + res = TRUE; + } + else { + gchar* displayname = find_text(buffers->data->memory, "", ""); + if (displayname) { + buf = g_string_assign(buf, displayname); + g_free(displayname); + } + res = FALSE; + } + } + curl_easy_cleanup(curl); + } else { + res = TRUE; + } + + buffers_free(buffers); + + return res; +} + +gboolean curl_get_resource_type(CarddavData* dav) { + Buffers* buffers = buffers_new(); + gboolean res = TRUE; + CURLcode curlres = 0; + GPtrArray* types = g_ptr_array_new_null_terminated(1, (GDestroyNotify)g_free, TRUE); + + CURL* curl = prepare_curl(dav, buffers, PROPFIND); + if (curl) { + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, prop_request); + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, -1L); + curlres = curl_easy_perform(curl); + if (curlres != 0) { + g_ptr_array_add(types, g_strdup(buffers->error_buf)); + res = TRUE; + } + else { + long code; + curlres = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); + if (code != 207) { + res = TRUE; + } + else { + gchar* resourcetype = find_text(buffers->data->memory, "", ""); + if (resourcetype) { + gchar* tmp = g_strdup(resourcetype); + g_free(resourcetype); + resourcetype = find_text(tmp, "<", "/>"); + gchar* s = remove_ns(resourcetype); + g_free(resourcetype); + if (s) { + g_ptr_array_add(types, g_strdup(s)); + g_free(s); + resourcetype = find_text(tmp, "><", "/>"); + gchar* s = remove_ns(resourcetype); + g_ptr_array_add(types, g_strdup(s)); + g_free(resourcetype); + g_free(s); + } + g_free(tmp); + } + res = FALSE; + } + } + curl_easy_cleanup(curl); + } + + if (types->len > 0) { + gboolean authorized = FALSE; + gpointer* types_str = g_ptr_array_free(types, FALSE); + gchar** start = (gchar**)types_str; + while (*types_str) { + CollectionType type = get_collection_type(*types_str); + switch (type) { + case PRINCIPAL: + case CALENDAR: + case ADDRESSBOOK: + dav->type = type; + authorized = TRUE; + break; + default: + break; + } + types_str++; + } + if (authorized == FALSE) + dav->type = UNKNOWN; + g_strfreev(start); + } + + buffers_free(buffers); + + return res; +} + +void curl_free_CarddavData(CarddavData* dav) { + if (dav) { + g_free(dav->displayname); + g_free(dav->pwd); + g_free(dav->url); + g_free(dav->user); + g_free(dav); + } +} + +const gchar* get_resource_type_name(CollectionType type) { + if (type < TYPE_SIZE) + return CollectionType_string[type]; + + return NULL; +}; diff --git a/src/curl/curllib.h b/src/curl/curllib.h new file mode 100644 index 0000000..f44f2ce --- /dev/null +++ b/src/curl/curllib.h @@ -0,0 +1,46 @@ +/* + * curllib.h + * + * Copyright 2016 Michael Rasmussen + * + * 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., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#ifndef __CURLLIB_H__ +#define __CURLLIB_H__ + +#include + +G_BEGIN_DECLS + +#include "globals.h" + +/* +gboolean curl_get_objects(VCalData* vcal, GString* buf); +gboolean curl_set_objects(VCalData* vcal, GString* buf); +gboolean curl_update_objects(VCalData* vcal, GString* buf); +gboolean curl_delete_objects(VCalData* vcal, GString* buf); +gboolean curl_get_freebusy(VCalData* vcal, GString* buf); +*/ +gboolean curl_get_options(CarddavData* dav, GString* buf); +gboolean curl_get_displayname(CarddavData* dav, GString* buf); +gboolean curl_get_resource_type(CarddavData* dav); +void curl_free_CarddavData(CarddavData* dav); +const gchar* get_resource_type_name(CollectionType type); + +G_END_DECLS + +#endif diff --git a/src/parser/Makefile.am b/src/parser/Makefile.am new file mode 100644 index 0000000..0da718a --- /dev/null +++ b/src/parser/Makefile.am @@ -0,0 +1,18 @@ +AUTOMAKE_OPTIONS = gnu + +AM_CPPFLAGS = \ + -I$(top_builddir)/src/parser \ + -I $(top_srcdir)/src/parser \ + @GLIB_CFLAGS@ + +noinst_LIBRARIES = libvcard.a + +libvcard_a_SOURCES = \ + vcard.c \ + vcard-parser.h \ + vcard-parser.c + +include_HEADERS = \ + vcard.h \ + globals.h + diff --git a/src/globals.h b/src/parser/globals.h similarity index 81% rename from src/globals.h rename to src/parser/globals.h index b500d8c..eefb5ac 100644 --- a/src/globals.h +++ b/src/parser/globals.h @@ -36,6 +36,24 @@ typedef enum { typedef struct _VCard VCard; +typedef enum { + UNKNOWN, + PRINCIPAL, + CALENDAR, + ADDRESSBOOK, + TYPE_SIZE +} CollectionType; + +typedef struct { + gchar* user; + gchar* pwd; + gchar* url; + gchar* displayname; + gboolean debug; + gboolean mkcol; + CollectionType type; +} CarddavData; + G_END_DECLS #endif diff --git a/src/vcard-parser.c b/src/parser/vcard-parser.c similarity index 95% rename from src/vcard-parser.c rename to src/parser/vcard-parser.c index a62d8d3..380c19e 100644 --- a/src/vcard-parser.c +++ b/src/parser/vcard-parser.c @@ -110,6 +110,16 @@ static gboolean check_support(Property property, VCardVersion version) { return support; } +static void remove_cr(gchar **str) { + gchar *src, *dst; + + for (src = dst = *str; *src != '\0'; src++) { + *dst = *src; + if (*dst != '\r') dst++; + } + *dst = '\0'; +} + static VCardParserResponse has_required_version(const gchar** buf, VCardVersion* version) { const gchar** tmp = buf; gboolean found = FALSE; @@ -159,6 +169,7 @@ static gchar** vcard_text_normalize(const gchar** text) { str = g_strdup(*tmp+1); } else { if (str) { + remove_cr(&str); new[count++] = g_strdup(str); g_free(str); str = NULL; @@ -168,6 +179,7 @@ static gchar** vcard_text_normalize(const gchar** text) { tmp++; } if (str) { + remove_cr(&str); new[count++] = g_strdup(str); g_free(str); } @@ -306,7 +318,7 @@ VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, G //begin, end and version line is required r = VCARD_PARSER_BAD_FORMAT; if (buf && l > 3) { - if (g_strcmp0(buf[0], "BEGIN:VCARD") == 0 && g_strcmp0(buf[l-1], "END:VCARD") == 0) { + if (g_strcmp0(g_strchomp(buf[0]), "BEGIN:VCARD") == 0 && g_strcmp0(g_strchomp(buf[l-1]), "END:VCARD") == 0) { r = has_required_version((const gchar **)buf, version); if (r == VCARD_PARSER_OK) { init_g_hash_table(vcard, *version); diff --git a/src/vcard-parser.h b/src/parser/vcard-parser.h similarity index 100% rename from src/vcard-parser.h rename to src/parser/vcard-parser.h diff --git a/src/vcard.c b/src/parser/vcard.c similarity index 97% rename from src/vcard.c rename to src/parser/vcard.c index cba62ad..d7fd301 100644 --- a/src/vcard.c +++ b/src/parser/vcard.c @@ -150,8 +150,9 @@ GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean sk gchar** chunks = make_chunks(text); for (int i = 0; i < g_strv_length(chunks); i++) { - vc = g_new0(VCard, 1); - vc->version = version; + //vc = g_new0(VCard, 1); + //vc->version = version; + vc = VCard_new(version); VCardParserResponse r = vcard_parse_text(chunks[i], &vc->version, &vc->vcard); if (r != VCARD_PARSER_OK && skip_broken) { @@ -168,9 +169,10 @@ GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean sk GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken) { GSList* list = NULL; - VCard* vc = g_new0(VCard, 1); + //VCard* vc = g_new0(VCard, 1); - vc->version = version; + //vc->version = version; + VCard* vc = VCard_new(version); list = g_slist_prepend(list, vc); return list; diff --git a/src/vcard.h b/src/parser/vcard.h similarity index 100% rename from src/vcard.h rename to src/parser/vcard.h -- 2.47.3