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
# -*- 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
#
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])
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
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
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@
#include <stdio.h>
#include <glib.h>
-#include <globals.h>
+#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", "<http|https>://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);
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;
}
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
--- /dev/null
+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
+
--- /dev/null
+/*
+ * curllib.c
+ *
+ * Copyright 2016 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 <string.h>
+#include <stdlib.h>
+#include <curl/curl.h>
+#include <curl/easy.h>
+#include "globals.h"
+#include "curllib.h"
+
+const char* const CollectionType_string[] = {"unknown", "principal","calendar","addressbook"};
+
+#define prop_request "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
+ "<A:propfind xmlns:A=\"DAV:\">" \
+ "<A:prop>" \
+ "<A:displayname/>" \
+ "<A:current-user-privilege-set/>" \
+ "<A:resourcetype/>" \
+ "<A:getetag/>" \
+ "<A:getlastmodified/>" \
+ "<A:getcontenttype/>" \
+ "</A:prop>" \
+ "</A:propfind>"
+
+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<size; i+= width) {
+ fprintf(stream, "%04zx: ", i);
+ if(!nohex) {
+ /* hex not disabled, show it */
+ for(c = 0; c < width; c++) {
+ if(i+c < size)
+ fprintf(stream, "%02x ", ptr[i+c]);
+ else
+ fputs(" ", stream);
+ }
+ }
+ for(c = 0; (c < width) && (i+c < size); c++) {
+ /* check for 0D0A; if found, skip past and start a new line of output */
+ if (nohex && (i+c+1 < size) && ptr[i+c]==0x0D && ptr[i+c+1]==0x0A) {
+ i+=(c+2-width);
+ break;
+ }
+ fprintf(stream, "%c",(ptr[i+c]>=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, "<displayname>", "</displayname>");
+ 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, "<resourcetype>", "</resourcetype>");
+ 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;
+};
--- /dev/null
+/*
+ * curllib.h
+ *
+ * Copyright 2016 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 <glib.h>
+
+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
+++ /dev/null
-/*
- * globals.h
- *
- * Copyright 2019 Michael Rasmussen <mir@datanom.net>
- *
- * 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 __GLOBALS_H__
-#define __GLOBALS_H__
-
-#include <glib.h>
-
-G_BEGIN_DECLS
-
-typedef enum {
- VCARD_VERSION_UNSUPPORTED,
- VCARD_VERSION_DETECT,
- VCARD_VERSION_2_1,
- VCARD_VERSION_3_0,
- VCARD_VERSION_4_0,
-} VCardVersion;
-
-typedef struct _VCard VCard;
-
-G_END_DECLS
-
-#endif
--- /dev/null
+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
+
--- /dev/null
+/*
+ * globals.h
+ *
+ * Copyright 2019 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 __GLOBALS_H__
+#define __GLOBALS_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef enum {
+ VCARD_VERSION_UNSUPPORTED,
+ VCARD_VERSION_DETECT,
+ VCARD_VERSION_2_1,
+ VCARD_VERSION_3_0,
+ VCARD_VERSION_4_0,
+} VCardVersion;
+
+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
--- /dev/null
+/*
+ * vcard-parser.c
+ *
+ * Copyright 2019 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 <vcard-parser.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+static gchar* Properties[VCARD_PROPERTIES+1] = {
+ "N",
+ "FN",
+ "PHOTO",
+ "BDAY",
+ "ADR",
+ "LABEL",
+ "TEL",
+ "EMAIL",
+ "MAILER",
+ "GEO",
+ "TITLE",
+ "ROLE",
+ "LOGO",
+ "ORG",
+ "NOTE",
+ "REV",
+ "SOUND",
+ "URL",
+ "UID",
+ "VERSION",
+ "KEY",
+ "TZ",
+ "SOURCE",
+ "AGENT",
+ "PROFILE",
+ "CATEGORIES",
+ "SORT-STRING",
+ "PRODID",
+ "NICKNAME",
+ "NAME",
+ "CLASS",
+ "FBURL",
+ "CAPURI",
+ "CALURI",
+ "CALADRURI",
+ "IMPP",
+ "XML",
+ "ANNIVERSARY",
+ "CLIENTPIDMAP",
+ "LANG",
+ "GENDER",
+ "KIND",
+ "MEMBER",
+ "RELATED",
+ "BIRTHPLACE",
+ "DEATHPLACE",
+ "DEATHDATE",
+ "EXPERTISE",
+ "HOBBY",
+ "INTEREST",
+ "ORG-DIRECTORY",
+ NULL
+};
+
+static void vcard_property_free(gpointer data) {
+ if (!data) return;
+ VCardProperty* vp = (VCardProperty*) data;
+ g_free(vp->name);
+ g_free(vp->value);
+ g_free(vp);
+}
+
+static Property get_property(const gchar* property) {
+ for (Property i = N; i < VCARD_PROPERTIES; i++) {
+ if (g_strcmp0(Properties[i], property) == 0) return i;
+ }
+
+ return VCARD_PROPERTIES;
+}
+
+static gboolean check_support(Property property, VCardVersion version) {
+ gboolean support = FALSE;
+
+ if (version == VCARD_VERSION_2_1) {
+ support = property <= TZ ? TRUE : FALSE;
+ } else if (version == VCARD_VERSION_3_0) {
+ support = property <= IMPP ? TRUE : FALSE;
+ } else if (version == VCARD_VERSION_4_0) {
+ support = property <= VCARD_PROPERTIES ? TRUE : FALSE;
+ }
+
+ 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;
+ VCardParserResponse result = VCARD_PARSER_VERSION_MISSING;
+
+ while (*tmp && !found) {
+ if (!g_str_has_prefix(*tmp, "VERSION")) {
+ tmp++;
+ } else {
+ found = TRUE;
+ gchar** b = g_strsplit(*tmp, ":", 0);
+ if (g_strv_length(b) == 2) {
+ if (*version == VCARD_VERSION_DETECT) {
+ *version = str_2_vcard_version(b[1]);
+ result = VCARD_PARSER_OK;
+ } else {
+ if (*version == str_2_vcard_version(b[1])) {
+ result = VCARD_PARSER_OK;
+ } else
+ result = VCARD_PARSER_VERSION_MISMATCH;
+ }
+ }
+ g_strfreev(b);
+ }
+ }
+
+ return result;
+}
+
+static gchar** vcard_text_normalize(const gchar** text) {
+ gchar** new = NULL;
+ const gchar** tmp = text;
+ int count = 0;
+ gchar* str = NULL;
+
+ if (text == NULL) return NULL;
+
+ new = g_new0(gchar*, g_strv_length((gchar **) text) + 1);
+ while (*tmp && g_strcmp0(*tmp, "") != 0) {
+ if (*tmp[0] == ' ') {
+ // Multi line value
+ if (str) {
+ gchar* s = g_strdup(str);
+ g_free(str);
+ str = g_strconcat(s, "\n", *tmp+1, NULL);
+ } else
+ str = g_strdup(*tmp+1);
+ } else {
+ if (str) {
+ remove_cr(&str);
+ new[count++] = g_strdup(str);
+ g_free(str);
+ str = NULL;
+ }
+ str = g_strdup(*tmp);
+ }
+ tmp++;
+ }
+ if (str) {
+ remove_cr(&str);
+ new[count++] = g_strdup(str);
+ g_free(str);
+ }
+
+ return new;
+}
+
+static VCardParserResponse vcard_parse(gchar** text, VCardVersion version, GHashTable** vcard) {
+ VCardParserResponse r = VCARD_PARSER_ERROR;
+ guint l = g_strv_length(text);
+ gboolean support;
+
+ for (guint i = 0; i < l; i++) {
+ gchar** b = g_strsplit(text[i], ":", 2);
+ if (g_strv_length(b) != 2 || !b[0] || !b[1]) {
+ destroy_hash_table(*vcard);
+ *vcard = NULL;
+ g_strfreev(b);
+ return VCARD_PARSER_ERROR;
+ }
+ gchar** c = g_strsplit(b[0], ";", 0);
+ if (g_strcmp0(c[0], "BEGIN") == 0 ||
+ g_strcmp0(c[0], "VERSION") == 0 ||
+ g_strcmp0(c[0], "END") == 0) {
+ g_strfreev(c);
+ g_strfreev(b);
+ continue;
+ }
+ if (g_str_has_prefix(c[0], "X-")) {
+ support = TRUE;
+ } else {
+ Property property = get_property(c[0]);
+ support = check_support(property, version);
+ }
+ if (support) {
+ GSList* value = g_hash_table_lookup(*vcard, c[0]);
+ VCardProperty* vp = g_new(VCardProperty, 1);
+ vp->name = g_strdup(b[0]);
+ vp->value = g_strdup(b[1]);
+ value = g_slist_append(value, vp);
+ g_hash_table_insert(*vcard, g_strdup(c[0]), value);
+ } else {
+ destroy_hash_table(*vcard);
+ *vcard = NULL;
+ g_strfreev(c);
+ g_strfreev(b);
+ return VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH;
+ }
+ g_strfreev(c);
+ g_strfreev(b);
+ }
+ r = VCARD_PARSER_OK;
+
+ return r;
+}
+
+Property vcard_max_property(VCardVersion version) {
+ Property p = VCARD_PROPERTIES;
+
+ if (version == VCARD_VERSION_2_1) {
+ p = TZ;
+ } else if (version == VCARD_VERSION_3_0) {
+ p = IMPP;
+ } else if (version == VCARD_VERSION_4_0) {
+ p = VCARD_PROPERTIES - 1;
+ }
+
+ return p;
+}
+
+void destroy_hash_table(GHashTable* ht) {
+ GHashTableIter iter;
+ gpointer key, value;
+
+ g_hash_table_iter_init (&iter, ht);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ if (value)
+ g_slist_free_full((GSList *) value, vcard_property_free);
+ }
+
+ g_hash_table_destroy(ht);
+}
+
+void init_g_hash_table(GHashTable** ht, VCardVersion version) {
+ if (!ht) return;
+
+ Property property = vcard_max_property(version);
+ if (property < VCARD_PROPERTIES) {
+ *ht = g_hash_table_new_full(
+ g_str_hash, g_str_equal, (GDestroyNotify) g_free, NULL);
+ for (Property i = N; i <= property; i++) {
+ g_hash_table_insert(*ht, g_strdup(Properties[i]), NULL);
+ }
+ }
+}
+
+VCardVersion str_2_vcard_version(const gchar* version) {
+ VCardVersion vv = VCARD_VERSION_DETECT;
+
+ if (version) {
+ if (g_strcmp0("2.1", version) == 0)
+ vv = VCARD_VERSION_2_1;
+ else if (g_strcmp0("3.0", version) == 0)
+ vv = VCARD_VERSION_3_0;
+ else if (g_strcmp0("4.0", version) == 0)
+ vv = VCARD_VERSION_4_0;
+ else
+ vv = VCARD_VERSION_UNSUPPORTED;
+ }
+
+ return vv;
+}
+
+gchar* vcard_version_2_str(VCardVersion version) {
+ gchar* vv = NULL;
+
+ if (VCARD_VERSION_2_1 == version)
+ vv = g_strdup("2.1");
+ else if (VCARD_VERSION_3_0 == version)
+ vv = g_strdup("3.0");
+ else if (VCARD_VERSION_4_0 == version)
+ vv = g_strdup("4.0");
+
+ return vv;
+}
+
+VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, GHashTable** vcard) {
+ VCardParserResponse r = VCARD_PARSER_ERROR;
+
+ if (text == NULL || strlen(text) < 1) return VCARD_PARSER_OK;
+
+ gchar** b = g_strsplit(text, "\n", 0);
+ gchar** buf = vcard_text_normalize((const gchar**)b);
+ g_strfreev(b);
+ guint l = g_strv_length(buf);
+ //begin, end and version line is required
+ r = VCARD_PARSER_BAD_FORMAT;
+ if (buf && l > 3) {
+ 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);
+ if (vcard && *vcard) {
+ r = vcard_parse(buf, *version, vcard);
+ } else {
+ r = VCARD_PARSER_ERROR;
+ }
+ }
+ }
+ }
+ g_strfreev(buf);
+
+ return r;
+}
--- /dev/null
+/*
+ * vcard-parser.h
+ *
+ * Copyright 2019 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 __VCARD_PARSER_H__
+#define __VCARD_PARSER_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#include <globals.h>
+
+typedef enum {
+ // vCard 2.1 properties and up
+ // https://github.com/emacsmirror/addressbook/blob/master/vcard-21.txt
+ N = 0,
+ FN,
+ PHOTO,
+ BDAY,
+ ADR,
+ LABEL, // Removed in vCard 4.0
+ TEL,
+ EMAIL,
+ MAILER, // Removed in vCard 4.0
+ GEO,
+ TITLE,
+ ROLE,
+ LOGO,
+ ORG,
+ NOTE,
+ REV,
+ SOUND,
+ URL,
+ UID,
+ VERSION,
+ KEY,
+ TZ,
+ SOURCE,
+ AGENT, // Removed in vCard 4.0
+ PROFILE, // Removed in vCard 4.0
+ // vCard 3.0 properties
+ // https://tools.ietf.org/html/rfc2426
+ CATEGORIES,
+ SORT_STRING,
+ PRODID,
+ NICKNAME,
+ NAME, // Removed in vCard 4.0
+ CLASS, // Removed in vCard 4.0
+ // rfc2739 properties
+ FBURL,
+ CAPURI,
+ CALURI,
+ CALADRURI,
+ // rfc4770 properties
+ IMPP,
+ // vCard 4.0 properties
+ // https://tools.ietf.org/html/rfc6350
+ XML,
+ ANNIVERSARY,
+ CLIENTPIDMAP,
+ LANG,
+ GENDER,
+ KIND,
+ MEMBER,
+ RELATED,
+ // rfc6474 properties
+ BIRTHPLACE,
+ DEATHPLACE,
+ DEATHDATE,
+ // rfc6715 properties
+ EXPERTISE,
+ HOBBY,
+ INTEREST,
+ ORG_DIRECTORY,
+ VCARD_PROPERTIES,
+} Property;
+
+typedef enum {
+ VCARD_PARSER_OK,
+ VCARD_PARSER_VERSION_MISSING,
+ VCARD_PARSER_VERSION_MISMATCH,
+ VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH,
+ VCARD_PARSER_BAD_FORMAT,
+ VCARD_PARSER_ERROR,
+} VCardParserResponse;
+
+typedef struct {
+ gchar* name;
+ gchar* value;
+} VCardProperty;
+
+VCardVersion str_2_vcard_version(const gchar* version);
+gchar* vcard_version_2_str(VCardVersion version);
+Property vcard_max_property(VCardVersion version);
+VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, GHashTable** vcard);
+void init_g_hash_table(GHashTable** ht, VCardVersion version);
+void destroy_hash_table(GHashTable* ht);
+
+G_END_DECLS
+
+#endif
--- /dev/null
+/*
+ * vcard.c
+ *
+ * Copyright 2019 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 <unistd.h>
+#include <errno.h>
+#include <glib/gprintf.h>
+#include <vcard.h>
+#include <vcard-parser.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+struct _VCard {
+ VCardParserResponse state;
+ VCardVersion version;
+ GHashTable* vcard;
+};
+
+static char *replaceWord(const char *s, const char *oldW, const char *newW) {
+ char *result;
+ int i, cnt = 0;
+ int newWlen = strlen(newW);
+ int oldWlen = strlen(oldW);
+
+ for (i = 0; s[i] != '\0'; i++) {
+ if (strstr(&s[i], oldW) == &s[i]) {
+ cnt++;
+ i += oldWlen - 1;
+ }
+ }
+
+ result = (char *) malloc(i + cnt * (newWlen - oldWlen) + 1);
+
+ i = 0;
+ while (*s) {
+ if (strstr(s, oldW) == s) {
+ strcpy(&result[i], newW);
+ i += newWlen;
+ s += oldWlen;
+ }
+ else
+ result[i++] = *s++;
+ }
+
+ result[i] = '\0';
+ return result;
+}
+
+static void hash_table_cb(gpointer key, gpointer value, gpointer data) {
+ GSList* list = (GSList*) value;
+ gchar** str = (gchar**) data;
+ gchar* s = NULL;
+
+ if (!list) return;
+
+ while (list) {
+ VCardProperty* vp = (VCardProperty*) list->data;
+ gchar* s0 = replaceWord(vp->value, "\n", "\n ");
+ if (!*str)
+ s = g_strconcat(vp->name, ":", s0, NULL);
+ else {
+ s = g_strconcat(*str, "\n", vp->name, ":", s0, NULL);
+ g_free(*str);
+ }
+ g_free(s0);
+ *str = g_strdup(s);
+ g_free(s);
+ list = list->next;
+ }
+}
+
+static gchar** make_chunks(const gchar* text) {
+ gchar** chunks = NULL;
+ gchar* v_text = g_strdup(text);
+ g_strstrip(v_text);
+
+ gchar** tmp = g_strsplit(v_text, "END:VCARD", 0);
+ g_free(v_text);
+ guint lines = g_strv_length(tmp);
+ if (lines < 2)
+ return tmp;
+
+ if (strlen(tmp[lines-1]) < 1) {
+ chunks = g_new0(gchar*, lines);
+ lines--;
+ } else
+ chunks = g_new0(gchar*, lines + 1);
+
+ for(int i = 0; i < lines; i++) {
+ g_strchug(tmp[i]);
+ chunks[i] = g_strconcat(tmp[i], "END:VCARD", NULL);
+ }
+ g_strfreev(tmp);
+
+ return chunks;
+}
+
+void VCard_free(VCard* object) {
+ if (object == NULL) return;
+
+ if (object->vcard)
+ destroy_hash_table(object->vcard);
+ g_free(object);
+}
+
+void VCard_list_free(GSList* list) {
+ GSList* tmp = list;
+
+ while(tmp) {
+ VCard* object = (VCard*) tmp->data;
+ VCard_free(object);
+ tmp = tmp->next;
+ }
+
+ if(list)
+ g_slist_free(list);
+}
+
+VCard* VCard_new(VCardVersion version) {
+ VCard* vc = g_new0(VCard, 1);
+ vc->version = version;
+
+ return vc;
+}
+
+GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean skip_broken) {
+ GSList* list = NULL;
+ VCard* vc = NULL;
+
+ if (!text || strlen(text) < 1) return NULL;
+
+ gchar** chunks = make_chunks(text);
+ for (int i = 0; i < g_strv_length(chunks); i++) {
+ //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) {
+ VCard_free(vc);
+ continue;
+ }
+ vc->state = r;
+ list = g_slist_prepend(list, vc);
+ }
+ g_strfreev(chunks);
+
+ return list;
+}
+
+GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken) {
+ GSList* list = NULL;
+ //VCard* vc = g_new0(VCard, 1);
+
+ //vc->version = version;
+ VCard* vc = VCard_new(version);
+ list = g_slist_prepend(list, vc);
+
+ return list;
+}
+
+void VCard_print_list(GSList* list) {
+ VCard_print_list_fd(list, STDOUT_FILENO);
+}
+
+void VCard_print_list_fd(GSList* list, int fd) {
+ GSList* tmp = list;
+ if (fd > 0) {
+ while(tmp) {
+ VCard* object = (VCard*) tmp->data;
+ VCard_print_fd(object, fd);
+ if (tmp->next) {
+ ssize_t len = strlen("\n");
+ if (write(fd, "\n", len) < len)
+ g_warning(strerror(errno));
+ }
+ tmp = tmp->next;
+ }
+ }
+}
+
+void VCard_print(VCard* object){
+ VCard_print_fd(object, STDOUT_FILENO);
+}
+
+void VCard_print_fd(VCard* object, int fd) {
+ gchar *body = NULL, *s = NULL;
+
+ if (object && object->vcard && fd > 0) {
+ gchar* version = vcard_version_2_str(object->version);
+ s = g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version);
+ g_free(version);
+ g_hash_table_foreach(object->vcard, hash_table_cb, &s);
+ body = g_strdup_printf("%s\nEND:VCARD", s);
+ g_free(s);
+ ssize_t len = strlen(body);
+ if (write(fd, body, len) < len)
+ g_warning(strerror(errno));
+ g_free(body);
+ }
+}
+
+gchar* VCard_get_state(VCard* object) {
+ gchar* message = NULL;
+
+ if (!object) return NULL;
+
+ switch (object->state) {
+ case VCARD_PARSER_OK:
+ message = g_strdup("OK");
+ break;
+ case VCARD_PARSER_VERSION_MISMATCH:
+ message = g_strdup("Version mismatch");
+ break;
+ case VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH:
+ message = g_strdup("Attribute not supported in this version");
+ break;
+ case VCARD_PARSER_BAD_FORMAT:
+ message = g_strdup("Bad input format");
+ break;
+ case VCARD_PARSER_ERROR:
+ message = g_strdup("Unknown error");
+ break;
+ default:
+ message = g_strdup("Unhandled error");
+ break;
+ }
+
+ return message;
+}
+
+GSList* VCard_get_property(VCard* object, const gchar* property) {
+ GSList *values = NULL, *tmp;
+ GSList *iter;
+ VCardProperty* vp = NULL;
+
+ if (object && object->vcard && property) {
+ tmp = g_hash_table_lookup(object->vcard, property);
+ for (iter = tmp; iter; iter = g_slist_next(iter)) {
+ vp = (VCardProperty*) iter->data;
+ values = g_slist_prepend(values, g_strdup_printf("%s:%s", vp->name, vp->value));
+ }
+ }
+
+ return values;
+}
--- /dev/null
+/*
+ * vcard.h
+ *
+ * Copyright 2019 Michael Rasmussen <mir@datanom.net>
+ *
+ * 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 __VCARD_H__
+#define __VCARD_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#include <stdio.h>
+#include <globals.h>
+
+//VCard* VCard_new(VCardVersion version);
+GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean skip_broken);
+GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken);
+void VCard_free(VCard* object);
+void VCard_list_free(GSList* list);
+
+void VCard_print_list(GSList* list);
+void VCard_print_list_fd(GSList* list, int fd);
+void VCard_print(VCard* object);
+void VCard_print_fd(VCard* object, int fd);
+gchar* VCard_get_state(VCard* object);
+GSList* VCard_get_property(VCard* object, const gchar* property);
+
+G_END_DECLS
+
+#endif
+++ /dev/null
-/*
- * vcard-parser.c
- *
- * Copyright 2019 Michael Rasmussen <mir@datanom.net>
- *
- * 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 <vcard-parser.h>
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-static gchar* Properties[VCARD_PROPERTIES+1] = {
- "N",
- "FN",
- "PHOTO",
- "BDAY",
- "ADR",
- "LABEL",
- "TEL",
- "EMAIL",
- "MAILER",
- "GEO",
- "TITLE",
- "ROLE",
- "LOGO",
- "ORG",
- "NOTE",
- "REV",
- "SOUND",
- "URL",
- "UID",
- "VERSION",
- "KEY",
- "TZ",
- "SOURCE",
- "AGENT",
- "PROFILE",
- "CATEGORIES",
- "SORT-STRING",
- "PRODID",
- "NICKNAME",
- "NAME",
- "CLASS",
- "FBURL",
- "CAPURI",
- "CALURI",
- "CALADRURI",
- "IMPP",
- "XML",
- "ANNIVERSARY",
- "CLIENTPIDMAP",
- "LANG",
- "GENDER",
- "KIND",
- "MEMBER",
- "RELATED",
- "BIRTHPLACE",
- "DEATHPLACE",
- "DEATHDATE",
- "EXPERTISE",
- "HOBBY",
- "INTEREST",
- "ORG-DIRECTORY",
- NULL
-};
-
-static void vcard_property_free(gpointer data) {
- if (!data) return;
- VCardProperty* vp = (VCardProperty*) data;
- g_free(vp->name);
- g_free(vp->value);
- g_free(vp);
-}
-
-static Property get_property(const gchar* property) {
- for (Property i = N; i < VCARD_PROPERTIES; i++) {
- if (g_strcmp0(Properties[i], property) == 0) return i;
- }
-
- return VCARD_PROPERTIES;
-}
-
-static gboolean check_support(Property property, VCardVersion version) {
- gboolean support = FALSE;
-
- if (version == VCARD_VERSION_2_1) {
- support = property <= TZ ? TRUE : FALSE;
- } else if (version == VCARD_VERSION_3_0) {
- support = property <= IMPP ? TRUE : FALSE;
- } else if (version == VCARD_VERSION_4_0) {
- support = property <= VCARD_PROPERTIES ? TRUE : FALSE;
- }
-
- return support;
-}
-
-static VCardParserResponse has_required_version(const gchar** buf, VCardVersion* version) {
- const gchar** tmp = buf;
- gboolean found = FALSE;
- VCardParserResponse result = VCARD_PARSER_VERSION_MISSING;
-
- while (*tmp && !found) {
- if (!g_str_has_prefix(*tmp, "VERSION")) {
- tmp++;
- } else {
- found = TRUE;
- gchar** b = g_strsplit(*tmp, ":", 0);
- if (g_strv_length(b) == 2) {
- if (*version == VCARD_VERSION_DETECT) {
- *version = str_2_vcard_version(b[1]);
- result = VCARD_PARSER_OK;
- } else {
- if (*version == str_2_vcard_version(b[1])) {
- result = VCARD_PARSER_OK;
- } else
- result = VCARD_PARSER_VERSION_MISMATCH;
- }
- }
- g_strfreev(b);
- }
- }
-
- return result;
-}
-
-static gchar** vcard_text_normalize(const gchar** text) {
- gchar** new = NULL;
- const gchar** tmp = text;
- int count = 0;
- gchar* str = NULL;
-
- if (text == NULL) return NULL;
-
- new = g_new0(gchar*, g_strv_length((gchar **) text) + 1);
- while (*tmp && g_strcmp0(*tmp, "") != 0) {
- if (*tmp[0] == ' ') {
- // Multi line value
- if (str) {
- gchar* s = g_strdup(str);
- g_free(str);
- str = g_strconcat(s, "\n", *tmp+1, NULL);
- } else
- str = g_strdup(*tmp+1);
- } else {
- if (str) {
- new[count++] = g_strdup(str);
- g_free(str);
- str = NULL;
- }
- str = g_strdup(*tmp);
- }
- tmp++;
- }
- if (str) {
- new[count++] = g_strdup(str);
- g_free(str);
- }
-
- return new;
-}
-
-static VCardParserResponse vcard_parse(gchar** text, VCardVersion version, GHashTable** vcard) {
- VCardParserResponse r = VCARD_PARSER_ERROR;
- guint l = g_strv_length(text);
- gboolean support;
-
- for (guint i = 0; i < l; i++) {
- gchar** b = g_strsplit(text[i], ":", 2);
- if (g_strv_length(b) != 2 || !b[0] || !b[1]) {
- destroy_hash_table(*vcard);
- *vcard = NULL;
- g_strfreev(b);
- return VCARD_PARSER_ERROR;
- }
- gchar** c = g_strsplit(b[0], ";", 0);
- if (g_strcmp0(c[0], "BEGIN") == 0 ||
- g_strcmp0(c[0], "VERSION") == 0 ||
- g_strcmp0(c[0], "END") == 0) {
- g_strfreev(c);
- g_strfreev(b);
- continue;
- }
- if (g_str_has_prefix(c[0], "X-")) {
- support = TRUE;
- } else {
- Property property = get_property(c[0]);
- support = check_support(property, version);
- }
- if (support) {
- GSList* value = g_hash_table_lookup(*vcard, c[0]);
- VCardProperty* vp = g_new(VCardProperty, 1);
- vp->name = g_strdup(b[0]);
- vp->value = g_strdup(b[1]);
- value = g_slist_append(value, vp);
- g_hash_table_insert(*vcard, g_strdup(c[0]), value);
- } else {
- destroy_hash_table(*vcard);
- *vcard = NULL;
- g_strfreev(c);
- g_strfreev(b);
- return VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH;
- }
- g_strfreev(c);
- g_strfreev(b);
- }
- r = VCARD_PARSER_OK;
-
- return r;
-}
-
-Property vcard_max_property(VCardVersion version) {
- Property p = VCARD_PROPERTIES;
-
- if (version == VCARD_VERSION_2_1) {
- p = TZ;
- } else if (version == VCARD_VERSION_3_0) {
- p = IMPP;
- } else if (version == VCARD_VERSION_4_0) {
- p = VCARD_PROPERTIES - 1;
- }
-
- return p;
-}
-
-void destroy_hash_table(GHashTable* ht) {
- GHashTableIter iter;
- gpointer key, value;
-
- g_hash_table_iter_init (&iter, ht);
- while (g_hash_table_iter_next(&iter, &key, &value)) {
- if (value)
- g_slist_free_full((GSList *) value, vcard_property_free);
- }
-
- g_hash_table_destroy(ht);
-}
-
-void init_g_hash_table(GHashTable** ht, VCardVersion version) {
- if (!ht) return;
-
- Property property = vcard_max_property(version);
- if (property < VCARD_PROPERTIES) {
- *ht = g_hash_table_new_full(
- g_str_hash, g_str_equal, (GDestroyNotify) g_free, NULL);
- for (Property i = N; i <= property; i++) {
- g_hash_table_insert(*ht, g_strdup(Properties[i]), NULL);
- }
- }
-}
-
-VCardVersion str_2_vcard_version(const gchar* version) {
- VCardVersion vv = VCARD_VERSION_DETECT;
-
- if (version) {
- if (g_strcmp0("2.1", version) == 0)
- vv = VCARD_VERSION_2_1;
- else if (g_strcmp0("3.0", version) == 0)
- vv = VCARD_VERSION_3_0;
- else if (g_strcmp0("4.0", version) == 0)
- vv = VCARD_VERSION_4_0;
- else
- vv = VCARD_VERSION_UNSUPPORTED;
- }
-
- return vv;
-}
-
-gchar* vcard_version_2_str(VCardVersion version) {
- gchar* vv = NULL;
-
- if (VCARD_VERSION_2_1 == version)
- vv = g_strdup("2.1");
- else if (VCARD_VERSION_3_0 == version)
- vv = g_strdup("3.0");
- else if (VCARD_VERSION_4_0 == version)
- vv = g_strdup("4.0");
-
- return vv;
-}
-
-VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, GHashTable** vcard) {
- VCardParserResponse r = VCARD_PARSER_ERROR;
-
- if (text == NULL || strlen(text) < 1) return VCARD_PARSER_OK;
-
- gchar** b = g_strsplit(text, "\n", 0);
- gchar** buf = vcard_text_normalize((const gchar**)b);
- g_strfreev(b);
- guint l = g_strv_length(buf);
- //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) {
- r = has_required_version((const gchar **)buf, version);
- if (r == VCARD_PARSER_OK) {
- init_g_hash_table(vcard, *version);
- if (vcard && *vcard) {
- r = vcard_parse(buf, *version, vcard);
- } else {
- r = VCARD_PARSER_ERROR;
- }
- }
- }
- }
- g_strfreev(buf);
-
- return r;
-}
+++ /dev/null
-/*
- * vcard-parser.h
- *
- * Copyright 2019 Michael Rasmussen <mir@datanom.net>
- *
- * 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 __VCARD_PARSER_H__
-#define __VCARD_PARSER_H__
-
-#include <glib.h>
-
-G_BEGIN_DECLS
-
-#include <globals.h>
-
-typedef enum {
- // vCard 2.1 properties and up
- // https://github.com/emacsmirror/addressbook/blob/master/vcard-21.txt
- N = 0,
- FN,
- PHOTO,
- BDAY,
- ADR,
- LABEL, // Removed in vCard 4.0
- TEL,
- EMAIL,
- MAILER, // Removed in vCard 4.0
- GEO,
- TITLE,
- ROLE,
- LOGO,
- ORG,
- NOTE,
- REV,
- SOUND,
- URL,
- UID,
- VERSION,
- KEY,
- TZ,
- SOURCE,
- AGENT, // Removed in vCard 4.0
- PROFILE, // Removed in vCard 4.0
- // vCard 3.0 properties
- // https://tools.ietf.org/html/rfc2426
- CATEGORIES,
- SORT_STRING,
- PRODID,
- NICKNAME,
- NAME, // Removed in vCard 4.0
- CLASS, // Removed in vCard 4.0
- // rfc2739 properties
- FBURL,
- CAPURI,
- CALURI,
- CALADRURI,
- // rfc4770 properties
- IMPP,
- // vCard 4.0 properties
- // https://tools.ietf.org/html/rfc6350
- XML,
- ANNIVERSARY,
- CLIENTPIDMAP,
- LANG,
- GENDER,
- KIND,
- MEMBER,
- RELATED,
- // rfc6474 properties
- BIRTHPLACE,
- DEATHPLACE,
- DEATHDATE,
- // rfc6715 properties
- EXPERTISE,
- HOBBY,
- INTEREST,
- ORG_DIRECTORY,
- VCARD_PROPERTIES,
-} Property;
-
-typedef enum {
- VCARD_PARSER_OK,
- VCARD_PARSER_VERSION_MISSING,
- VCARD_PARSER_VERSION_MISMATCH,
- VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH,
- VCARD_PARSER_BAD_FORMAT,
- VCARD_PARSER_ERROR,
-} VCardParserResponse;
-
-typedef struct {
- gchar* name;
- gchar* value;
-} VCardProperty;
-
-VCardVersion str_2_vcard_version(const gchar* version);
-gchar* vcard_version_2_str(VCardVersion version);
-Property vcard_max_property(VCardVersion version);
-VCardParserResponse vcard_parse_text(const gchar* text, VCardVersion* version, GHashTable** vcard);
-void init_g_hash_table(GHashTable** ht, VCardVersion version);
-void destroy_hash_table(GHashTable* ht);
-
-G_END_DECLS
-
-#endif
+++ /dev/null
-/*
- * vcard.c
- *
- * Copyright 2019 Michael Rasmussen <mir@datanom.net>
- *
- * 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 <unistd.h>
-#include <errno.h>
-#include <glib/gprintf.h>
-#include <vcard.h>
-#include <vcard-parser.h>
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-struct _VCard {
- VCardParserResponse state;
- VCardVersion version;
- GHashTable* vcard;
-};
-
-static char *replaceWord(const char *s, const char *oldW, const char *newW) {
- char *result;
- int i, cnt = 0;
- int newWlen = strlen(newW);
- int oldWlen = strlen(oldW);
-
- for (i = 0; s[i] != '\0'; i++) {
- if (strstr(&s[i], oldW) == &s[i]) {
- cnt++;
- i += oldWlen - 1;
- }
- }
-
- result = (char *) malloc(i + cnt * (newWlen - oldWlen) + 1);
-
- i = 0;
- while (*s) {
- if (strstr(s, oldW) == s) {
- strcpy(&result[i], newW);
- i += newWlen;
- s += oldWlen;
- }
- else
- result[i++] = *s++;
- }
-
- result[i] = '\0';
- return result;
-}
-
-static void hash_table_cb(gpointer key, gpointer value, gpointer data) {
- GSList* list = (GSList*) value;
- gchar** str = (gchar**) data;
- gchar* s = NULL;
-
- if (!list) return;
-
- while (list) {
- VCardProperty* vp = (VCardProperty*) list->data;
- gchar* s0 = replaceWord(vp->value, "\n", "\n ");
- if (!*str)
- s = g_strconcat(vp->name, ":", s0, NULL);
- else {
- s = g_strconcat(*str, "\n", vp->name, ":", s0, NULL);
- g_free(*str);
- }
- g_free(s0);
- *str = g_strdup(s);
- g_free(s);
- list = list->next;
- }
-}
-
-static gchar** make_chunks(const gchar* text) {
- gchar** chunks = NULL;
- gchar* v_text = g_strdup(text);
- g_strstrip(v_text);
-
- gchar** tmp = g_strsplit(v_text, "END:VCARD", 0);
- g_free(v_text);
- guint lines = g_strv_length(tmp);
- if (lines < 2)
- return tmp;
-
- if (strlen(tmp[lines-1]) < 1) {
- chunks = g_new0(gchar*, lines);
- lines--;
- } else
- chunks = g_new0(gchar*, lines + 1);
-
- for(int i = 0; i < lines; i++) {
- g_strchug(tmp[i]);
- chunks[i] = g_strconcat(tmp[i], "END:VCARD", NULL);
- }
- g_strfreev(tmp);
-
- return chunks;
-}
-
-void VCard_free(VCard* object) {
- if (object == NULL) return;
-
- if (object->vcard)
- destroy_hash_table(object->vcard);
- g_free(object);
-}
-
-void VCard_list_free(GSList* list) {
- GSList* tmp = list;
-
- while(tmp) {
- VCard* object = (VCard*) tmp->data;
- VCard_free(object);
- tmp = tmp->next;
- }
-
- if(list)
- g_slist_free(list);
-}
-
-VCard* VCard_new(VCardVersion version) {
- VCard* vc = g_new0(VCard, 1);
- vc->version = version;
-
- return vc;
-}
-
-GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean skip_broken) {
- GSList* list = NULL;
- VCard* vc = NULL;
-
- if (!text || strlen(text) < 1) return NULL;
-
- gchar** chunks = make_chunks(text);
- for (int i = 0; i < g_strv_length(chunks); i++) {
- vc = g_new0(VCard, 1);
- vc->version = version;
-
- VCardParserResponse r = vcard_parse_text(chunks[i], &vc->version, &vc->vcard);
- if (r != VCARD_PARSER_OK && skip_broken) {
- VCard_free(vc);
- continue;
- }
- vc->state = r;
- list = g_slist_prepend(list, vc);
- }
- g_strfreev(chunks);
-
- return list;
-}
-
-GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken) {
- GSList* list = NULL;
- VCard* vc = g_new0(VCard, 1);
-
- vc->version = version;
- list = g_slist_prepend(list, vc);
-
- return list;
-}
-
-void VCard_print_list(GSList* list) {
- VCard_print_list_fd(list, STDOUT_FILENO);
-}
-
-void VCard_print_list_fd(GSList* list, int fd) {
- GSList* tmp = list;
- if (fd > 0) {
- while(tmp) {
- VCard* object = (VCard*) tmp->data;
- VCard_print_fd(object, fd);
- if (tmp->next) {
- ssize_t len = strlen("\n");
- if (write(fd, "\n", len) < len)
- g_warning(strerror(errno));
- }
- tmp = tmp->next;
- }
- }
-}
-
-void VCard_print(VCard* object){
- VCard_print_fd(object, STDOUT_FILENO);
-}
-
-void VCard_print_fd(VCard* object, int fd) {
- gchar *body = NULL, *s = NULL;
-
- if (object && object->vcard && fd > 0) {
- gchar* version = vcard_version_2_str(object->version);
- s = g_strdup_printf("BEGIN:VCARD\nVERSION:%s", version);
- g_free(version);
- g_hash_table_foreach(object->vcard, hash_table_cb, &s);
- body = g_strdup_printf("%s\nEND:VCARD", s);
- g_free(s);
- ssize_t len = strlen(body);
- if (write(fd, body, len) < len)
- g_warning(strerror(errno));
- g_free(body);
- }
-}
-
-gchar* VCard_get_state(VCard* object) {
- gchar* message = NULL;
-
- if (!object) return NULL;
-
- switch (object->state) {
- case VCARD_PARSER_OK:
- message = g_strdup("OK");
- break;
- case VCARD_PARSER_VERSION_MISMATCH:
- message = g_strdup("Version mismatch");
- break;
- case VCARD_PARSER_ATTRIBUTE_VERSION_MISMATCH:
- message = g_strdup("Attribute not supported in this version");
- break;
- case VCARD_PARSER_BAD_FORMAT:
- message = g_strdup("Bad input format");
- break;
- case VCARD_PARSER_ERROR:
- message = g_strdup("Unknown error");
- break;
- default:
- message = g_strdup("Unhandled error");
- break;
- }
-
- return message;
-}
-
-GSList* VCard_get_property(VCard* object, const gchar* property) {
- GSList *values = NULL, *tmp;
- GSList *iter;
- VCardProperty* vp = NULL;
-
- if (object && object->vcard && property) {
- tmp = g_hash_table_lookup(object->vcard, property);
- for (iter = tmp; iter; iter = g_slist_next(iter)) {
- vp = (VCardProperty*) iter->data;
- values = g_slist_prepend(values, g_strdup_printf("%s:%s", vp->name, vp->value));
- }
- }
-
- return values;
-}
+++ /dev/null
-/*
- * vcard.h
- *
- * Copyright 2019 Michael Rasmussen <mir@datanom.net>
- *
- * 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 __VCARD_H__
-#define __VCARD_H__
-
-#include <glib.h>
-
-G_BEGIN_DECLS
-
-#include <stdio.h>
-#include <globals.h>
-
-//VCard* VCard_new(VCardVersion version);
-GSList* VCard_new_from_text(const gchar* text, VCardVersion version, gboolean skip_broken);
-GSList* VCard_new_from_file(FILE* file, VCardVersion version, gboolean skip_broken);
-void VCard_free(VCard* object);
-void VCard_list_free(GSList* list);
-
-void VCard_print_list(GSList* list);
-void VCard_print_list_fd(GSList* list, int fd);
-void VCard_print(VCard* object);
-void VCard_print_fd(VCard* object, int fd);
-gchar* VCard_get_state(VCard* object);
-GSList* VCard_get_property(VCard* object, const gchar* property);
-
-G_END_DECLS
-
-#endif