Don't call strncpy with mismatching length
[claws.git] / src / vcard.c
index d6c63a7cd8cfa476c9e73b154dee8d0c6f316508..2e8fce511c1845509628b5159e570912196cdd8f 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001 Match Grun
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
  *
  * 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
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
- * Functions necessary to access VCard files. VCard files are used
+ * Functions necessary to access vCard files. vCard files are used
  * by GnomeCard for addressbook, and Netscape for sending business
- * card information. Refer to RFC2426 for more information.
+ * card information. Refer to http://www.imc.org/pdi/vcard-21.txt and
+ * RFC2426 for more information.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <sys/stat.h>
 #include <string.h>
 #include "vcard.h"
 #include "addritem.h"
 #include "addrcache.h"
+#include "adbookbase.h"
+#include "utils.h"
+#include "codeconv.h"
+#include "quoted-printable.h"
+#include "file-utils.h"
 
 #define GNOMECARD_DIR     ".gnome"
 #define GNOMECARD_FILE    "GnomeCard"
 VCardFile *vcard_create() {
        VCardFile *cardFile;
        cardFile = g_new0( VCardFile, 1 );
-       cardFile->name = NULL;
-       cardFile->path = NULL;
-       cardFile->file = NULL;
-       cardFile->bufptr = cardFile->buffer;
+       cardFile->type = ADBOOKTYPE_VCARD;
        cardFile->addressCache = addrcache_create();
        cardFile->retVal = MGU_SUCCESS;
-       cardFile->accessFlag = FALSE;
+
+       cardFile->file = NULL;
+       cardFile->path = NULL;
+       cardFile->bufptr = cardFile->buffer;
        return cardFile;
 }
 
@@ -59,41 +69,42 @@ VCardFile *vcard_create() {
 * Properties...
 */
 void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
-       g_return_if_fail( cardFile != NULL );
-       cardFile->name = mgu_replace_string( cardFile->name, value );
-       g_strstrip( cardFile->name );
+       cm_return_if_fail( cardFile != NULL );
+       addrcache_set_name( cardFile->addressCache, value );
 }
 void vcard_set_file( VCardFile* cardFile, const gchar *value ) {
-       g_return_if_fail( cardFile != NULL );
+       cm_return_if_fail( cardFile != NULL );
        addrcache_refresh( cardFile->addressCache );
        cardFile->path = mgu_replace_string( cardFile->path, value );
        g_strstrip( cardFile->path );
 }
 void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) {
-       g_return_if_fail( cardFile != NULL );
-       cardFile->accessFlag = value;
+       cm_return_if_fail( cardFile != NULL );
+       cardFile->addressCache->accessFlag = value;
 }
 
 /*
 * Test whether file was modified since last access.
 * Return: TRUE if file was modified.
 */
-gboolean vcard_get_modified( VCardFile *vcardFile ) {
-       g_return_val_if_fail( vcardFile != NULL, FALSE );
-       return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
+gboolean vcard_get_modified( VCardFile *cardFile ) {
+       cm_return_val_if_fail( cardFile != NULL, FALSE );
+       cardFile->addressCache->modified =
+               addrcache_check_file( cardFile->addressCache, cardFile->path );
+       return cardFile->addressCache->modified;
 }
-gboolean vcard_get_accessed( VCardFile *vcardFile ) {
-       g_return_val_if_fail( vcardFile != NULL, FALSE );
-       return addrcache_check_file( vcardFile->addressCache, vcardFile->path );
+gboolean vcard_get_accessed( VCardFile *cardFile ) {
+       cm_return_val_if_fail( cardFile != NULL, FALSE );
+       return cardFile->addressCache->accessFlag;
 }
 
 /*
 * Test whether file was read.
 * Return: TRUE if file was read.
 */
-gboolean vcard_get_read_flag( VCardFile *vcardFile ) {
-       g_return_val_if_fail( vcardFile != NULL, FALSE );
-       return vcardFile->addressCache->dataRead;
+gboolean vcard_get_read_flag( VCardFile *cardFile ) {
+       cm_return_val_if_fail( cardFile != NULL, FALSE );
+       return cardFile->addressCache->dataRead;
 }
 
 /*
@@ -101,30 +112,23 @@ gboolean vcard_get_read_flag( VCardFile *vcardFile ) {
 * Return: Status code.
 */
 gint vcard_get_status( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, -1 );
+       cm_return_val_if_fail( cardFile != NULL, -1 );
        return cardFile->retVal;
 }
 
 ItemFolder *vcard_get_root_folder( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, NULL );
+       cm_return_val_if_fail( cardFile != NULL, NULL );
        return addrcache_get_root_folder( cardFile->addressCache );
 }
 gchar *vcard_get_name( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, NULL );
-       return cardFile->name;
-}
-
-/*
-* Refresh internal variables to force a file read.
-*/
-void vcard_force_refresh( VCardFile *cardFile ) {
-       addrcache_refresh( cardFile->addressCache );
+       cm_return_val_if_fail( cardFile != NULL, NULL );
+       return addrcache_get_name( cardFile->addressCache );
 }
 
 /*
 * Create new cardfile object for specified file.
 */
-VCardFile *vcard_create_path( const gchar *path ) {
+static VCardFile *vcard_create_path( const gchar *path ) {
        VCardFile *cardFile;
        cardFile = vcard_create();
        vcard_set_file(cardFile, path);
@@ -135,44 +139,29 @@ VCardFile *vcard_create_path( const gchar *path ) {
 * Free up cardfile object by releasing internal memory.
 */
 void vcard_free( VCardFile *cardFile ) {
-       g_return_if_fail( cardFile != NULL );
+       cm_return_if_fail( cardFile != NULL );
 
        /* Close file */
-       if( cardFile->file ) fclose( cardFile->file );
-
-       /* Free internal stuff */
-       g_free( cardFile->name );
-       g_free( cardFile->path );
+       if( cardFile->file ) claws_fclose( cardFile->file );
 
        /* Clear cache */
        addrcache_clear( cardFile->addressCache );
        addrcache_free( cardFile->addressCache );
 
-       // Clear pointers
+       /* Free internal stuff */
+       g_free( cardFile->path );
+
+       /* Clear pointers */
        cardFile->file = NULL;
-       cardFile->name = NULL;
        cardFile->path = NULL;
+       cardFile->bufptr = NULL;
+
+       cardFile->type = ADBOOKTYPE_NONE;
        cardFile->addressCache = NULL;
        cardFile->retVal = MGU_SUCCESS;
-       cardFile->accessFlag = FALSE;
 
        /* Now release file object */
        g_free( cardFile );
-
-}
-
-/*
-* Display object to specified stream.
-*/
-void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
-       g_return_if_fail( cardFile != NULL );
-
-       fprintf( stream, "VCardFile:\n" );
-       fprintf( stream, "     name: '%s'\n", cardFile->name );
-       fprintf( stream, "file spec: '%s'\n", cardFile->path );
-       fprintf( stream, "  ret val: %d\n",   cardFile->retVal );
-       addrcache_print( cardFile->addressCache, stream );
-       addritem_print_item_folder( cardFile->addressCache->rootFolder, stream );
 }
 
 /*
@@ -180,20 +169,20 @@ void vcard_print_file( VCardFile *cardFile, FILE *stream ) {
 * return: TRUE if file opened successfully.
 */
 static gint vcard_open_file( VCardFile* cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, -1 );
+       cm_return_val_if_fail( cardFile != NULL, -1 );
 
-       // fprintf( stdout, "Opening file\n" );
+       /* g_print( "Opening file\n" ); */
        cardFile->addressCache->dataRead = FALSE;
        if( cardFile->path ) {
-               cardFile->file = fopen( cardFile->path, "r" );
+               cardFile->file = claws_fopen( cardFile->path, "rb" );
                if( ! cardFile->file ) {
-                       // fprintf( stderr, "can't open %s\n", cardFile->path );
+                       /* g_printerr( "can't open %s\n", cardFile->path ); */
                        cardFile->retVal = MGU_OPEN_FILE;
                        return cardFile->retVal;
                }
        }
        else {
-               // fprintf( stderr, "file not specified\n" );
+               /* g_printerr( "file not specified\n" ); */
                cardFile->retVal = MGU_NO_FILE;
                return cardFile->retVal;
        }
@@ -209,8 +198,8 @@ static gint vcard_open_file( VCardFile* cardFile ) {
 * Close file.
 */
 static void vcard_close_file( VCardFile *cardFile ) {
-       g_return_if_fail( cardFile != NULL );
-       if( cardFile->file ) fclose( cardFile->file );
+       cm_return_if_fail( cardFile != NULL );
+       if( cardFile->file ) claws_fclose( cardFile->file );
        cardFile->file = NULL;
 }
 
@@ -220,7 +209,7 @@ static void vcard_close_file( VCardFile *cardFile ) {
 */
 static gchar *vcard_read_line( VCardFile *cardFile ) {
        while( *cardFile->bufptr == '\n' || *cardFile->bufptr == '\0' ) {
-               if( fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
+               if( claws_fgets( cardFile->buffer, VCARDBUFSIZE, cardFile->file ) == NULL )
                        return NULL;
                g_strstrip( cardFile->buffer );
                cardFile->bufptr = cardFile->buffer;
@@ -246,7 +235,7 @@ static gchar *vcard_get_line( VCardFile *cardFile ) {
        start = cardFile->bufptr;
        len = strlen( start );
        end = start + len;
-       strncpy( buf, start, len );
+       memcpy( buf, start, len );
        buf[ len ] = '\0';
        g_strstrip(buf);
        cardFile->bufptr = end + 1;
@@ -259,10 +248,10 @@ static gchar *vcard_get_line( VCardFile *cardFile ) {
 * Free linked lists of character strings.
 */
 static void vcard_free_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList* listID ) {
-       mgu_free_list( listName );
-       mgu_free_list( listAddr );
-       mgu_free_list( listRem );
-       mgu_free_list( listID );
+       g_slist_free_full( listName, g_free );
+       g_slist_free_full( listAddr, g_free );
+       g_slist_free_full( listRem, g_free );
+       g_slist_free_full( listID, g_free );
 }
 
 /*
@@ -277,18 +266,16 @@ static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
        while( line ) {
                listQP = g_slist_append( listQP, line );
                len = strlen( line ) - 1;
-               if( len > 0 ) {
-                       if( line[ len ] != '=' ) break;
-                       line[ len ] = '\0';
-               }
+               if( line[ len ] != '=' ) break;
+               line[ len ] = '\0';
                line = vcard_get_line( cardFile );
        }
 
-       // Coalesce linked list into one long buffer.
+       /* Coalesce linked list into one long buffer. */
        line = mgu_list_coalesce( listQP );
 
-       // Clean up
-       mgu_free_list( listQP );
+       /* Clean up */
+       g_slist_free_full( listQP, g_free );
        listQP = NULL;
        return line;
 }
@@ -301,13 +288,15 @@ static gchar *vcard_get_tagname( char* line, gchar dlm ) {
        gint len = 0;
        gchar *tag = NULL;
        gchar *lptr = line;
+       gchar *down;
        while( *lptr++ ) {
                if( *lptr == dlm ) {
                        len = lptr - line;
                        tag = g_strndup( line, len+1 );
                        tag[ len ] = '\0';
-                       g_strdown( tag );
-                       return tag;
+                       down = g_utf8_strdown( tag, -1 );
+                       g_free(tag);
+                       return down;
                }
        }
        return tag;
@@ -335,36 +324,19 @@ static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
                value = g_strndup( start, len+1 );
        }
        else {
-               // Ensure that we get an empty string
+               /* Ensure that we get an empty string */
                value = g_strndup( "", 1 );
        }
        value[ len ] = '\0';
        return value;
 }
 
-/*
-* Dump linked lists of character strings (for debug).
-*/
-static void vcard_dump_lists( GSList *listName, GSList *listAddr, GSList *listRem, GSList *listID, FILE *stream ) {
-       fprintf( stream, "dump name\n" );
-       fprintf( stream, "------------\n" );
-       mgu_print_list( listName, stdout );
-       fprintf( stream, "dump address\n" );
-       fprintf( stream, "------------\n" );
-       mgu_print_list( listAddr, stdout );
-       fprintf( stream, "dump remarks\n" );
-       fprintf( stdout, "------------\n" );
-       mgu_print_list( listRem, stdout );
-       fprintf( stream, "dump id\n" );
-       fprintf( stdout, "------------\n" );
-       mgu_print_list( listID, stdout );
-}
-
 /*
 * Build an address list entry and append to list of address items.
 */
-static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *listAddr, GSList *listRem,
-                               GSList *listID )
+static void vcard_build_items(
+       VCardFile *cardFile, GSList *listName, GSList *listAddr,
+       GSList *listRem, GSList *listID )
 {
        GSList *nodeName = listName;
        GSList *nodeID = listID;
@@ -379,11 +351,12 @@ static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *li
                        if( *str != '\0' ) {
                                ItemEMail *email = addritem_create_item_email();
                                addritem_email_set_address( email, str );
-                               str = nodeRemarks->data;
                                if( nodeRemarks ) {
+                                       str = nodeRemarks->data;
                                        if( str ) {
-                                               if( g_strcasecmp( str, "internet" ) != 0 ) {
-                                                       if( *str != '\0' ) addritem_email_set_remarks( email, str );
+                                               if( g_utf8_collate( str, "internet" ) != 0 ) {
+                                                       if( *str != '\0' )
+                                                               addritem_email_set_remarks( email, str );
                                                }
                                        }
                                }
@@ -396,166 +369,159 @@ static void vcard_build_items( VCardFile *cardFile, GSList *listName, GSList *li
                if( person->listEMail ) {
                        addrcache_id_person( cardFile->addressCache, person );
                        addrcache_add_person( cardFile->addressCache, person );
+                       if( nodeID ) {
+                               str = nodeID->data;
+                               addritem_person_set_external_id( person, str );
+                       }
                }
                else {
                        addritem_free_item_person( person );
                }
-               if( nodeID ) {
-                       str = nodeID->data;
-                       addritem_person_set_external_id( person, str );
-               }
                nodeName = g_slist_next( nodeName );
                nodeID = g_slist_next( nodeID );
        }
 }
 
-// Unescape characters in quoted-printable string.
-static void vcard_unescape_qp( gchar *value ) {
-       gchar *ptr, *src, *dest;
-       gint d, v = 0;
-       gchar ch;
-       gboolean gotch;
-       ptr = value;
-       while( *ptr ) {
-               gotch = FALSE;
-               if( *ptr == '=' ) {
-                       v = 0;
-                       ch = *(ptr + 1);
-                       if( ch ) {
-                               if( ch > '0' && ch < '8' ) v = ch - '0';
-                       }
-                       d = -1;
-                       ch = *(ptr + 2);
-                       if( ch ) {
-                               if( ch > '\x60' ) ch -= '\x20';
-                               if( ch > '0' && ch < ' ' ) d = ch - '0';
-                               d = ch - '0';
-                               if( d > 9 ) d -= 7;
-                               if( d > -1 && d < 16 ) {
-                                       v = ( 16 * v ) + d;
-                                       gotch = TRUE;
-                               }
-                       }
-               }
-               if( gotch ) {
-                       // Replace = with char and move down in buffer
-                       *ptr = v;
-                       src = ptr + 3;
-                       dest = ptr + 1;
-                       while( *src ) {
-                               *dest++ = *src++;
-                       }
-                       *dest = '\0';
-               }
-               ptr++;
+/* Unescape characters in quoted-printable string. */
+static gchar *vcard_unescape_qp( gchar *value ) {
+       gchar *res = NULL;
+       gint len;
+       if (value == NULL)
+               return NULL;
+               
+       len = strlen(value);
+       res = g_malloc(len);
+       qp_decode_const(res, len-1, value);
+       if (!g_utf8_validate(res, -1, NULL)) {
+               gchar *mybuf = g_malloc(strlen(res)*2 +1);
+               conv_localetodisp(mybuf, strlen(res)*2 +1, res);
+               g_free(res);
+               res = mybuf;
        }
+       return res;
 }
 
-/*
+ /*
 * Read file data into root folder.
-* Note that one VCard can have multiple E-Mail addresses (MAIL tags);
+* Note that one vCard can have multiple E-Mail addresses (MAIL tags);
 * these are broken out into separate address items. An address item
 * is generated for the person identified by FN tag and each EMAIL tag.
 * If a sub-type is included in the EMAIL entry, this will be used as
-* the Remarks member. Also note that it is possible for one VCard
+* the Remarks member. Also note that it is possible for one vCard
 * entry to have multiple FN tags; this might not make sense. However,
 * it will generate duplicate address entries for each person listed.
 */
 static void vcard_read_file( VCardFile *cardFile ) {
        gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL;
        GSList *listName = NULL, *listAddress = NULL, *listRemarks = NULL, *listID = NULL;
-       //GSList *listQP = NULL;
+       /* GSList *listQP = NULL; */
 
        for( ;; ) {
                gchar *line =  vcard_get_line( cardFile );
                if( line == NULL ) break;
 
-               // fprintf( stdout, "%s\n", line );
+               /* g_print( "%s\n", line ); */
 
                /* Parse line */
                tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
-               if( tagtemp ) {
-                       // fprintf( stdout, "\ttemp:  %s\n", tagtemp );
-                       tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
-                       tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
-                       tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
-                       if( tagname == NULL ) {
-                               tagname = tagtemp;
-                               tagtemp = NULL;
-                       }
+               if( tagtemp == NULL ) {
+                       g_free( line );
+                       continue;
+               }
 
-                       // fprintf( stdout, "\tname:  %s\n", tagname );
-                       // fprintf( stdout, "\ttype:  %s\n", tagtype );
-                       // fprintf( stdout, "\tvalue: %s\n", tagvalue );
+               /* g_print( "\ttemp:  %s\n", tagtemp ); */
+               tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
+               if( tagvalue == NULL ) {
+                       g_free( tagtemp );
+                       g_free( line );
+                       continue;
+               }
 
-                       if( tagvalue ) {
-                               if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
-                                       // Quoted-Printable: could span multiple lines
-                                       tagvalue = vcard_read_qp( cardFile, tagvalue );
-                                       vcard_unescape_qp( tagvalue );
-                                       // fprintf( stdout, "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue );
-                               }
+               tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
+               tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
+               if( tagname == NULL ) {
+                       tagname = tagtemp;
+                       tagtemp = NULL;
+               }
 
-                               if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
-                                       g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                       // fprintf( stdout, "start card\n" );
-                                       vcard_free_lists( listName, listAddress, listRemarks, listID );
-                                       listName = listAddress = listRemarks = listID = NULL;
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
-                                       // fprintf( stdout, "- full name: %s\n", tagvalue );
-                                       listName = g_slist_append( listName, g_strdup( tagvalue ) );
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_EMAIL ) == 0 ) {
-                                       // fprintf( stdout, "- address: %s\n", tagvalue );
-                                       listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
-                                       listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_UID ) == 0 ) {
-                                       // fprintf( stdout, "- id: %s\n", tagvalue );
-                                       listID = g_slist_append( listID, g_strdup( tagvalue ) );
-                               }
-                               if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
-                                       g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                       // VCard is complete
-                                       // fprintf( stdout, "end card\n--\n" );
-                                       // vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout );
-                                       vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
-                                       vcard_free_lists( listName, listAddress, listRemarks, listID );
-                                       listName = listAddress = listRemarks = listID = NULL;
-                               }
-                               g_free( tagvalue );
-                       }
-                       g_free( tagname );
-                       g_free( tagtype );
+               /* g_print( "\tname:  %s\n", tagname ); */
+               /* g_print( "\ttype:  %s\n", tagtype ); */
+               /* g_print( "\tvalue: %s\n", tagvalue ); */
+
+               if( g_utf8_collate( tagtype, VCARD_TYPE_QP ) == 0
+                   || g_utf8_collate( tagtype, VCARD_TYPE_E_QP ) == 0
+                   || g_utf8_collate( tagtype, VCARD_TYPE_CS_UTF8_E_QP ) == 0) {
+                       gchar *tmp;
+                       /* Quoted-Printable: could span multiple lines */
+                       tagvalue = vcard_read_qp( cardFile, tagvalue );
+                       tmp = vcard_unescape_qp( tagvalue );
+                       g_free(tagvalue);
+                       tagvalue=tmp;
+                       /* g_print( "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
+               }
+
+               if( g_utf8_collate( tagname, VCARD_TAG_START ) == 0 &&
+                       g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                       /* g_print( "start card\n" ); */
+                       vcard_free_lists( listName, listAddress, listRemarks, listID );
+                       listName = listAddress = listRemarks = listID = NULL;
+               }
+               if( g_utf8_collate( tagname, VCARD_TAG_FULLNAME ) == 0 ) {
+                       /* g_print( "- full name: %s\n", tagvalue ); */
+                       listName = g_slist_append( listName, g_strdup( tagvalue ) );
+               }
+               if( g_utf8_collate( tagname, VCARD_TAG_EMAIL ) == 0 ) {
+                       /* g_print( "- address: %s\n", tagvalue ); */
+                       listAddress = g_slist_append( listAddress, g_strdup( tagvalue ) );
+                       listRemarks = g_slist_append( listRemarks, g_strdup( tagtype ) );
                }
+               if( g_utf8_collate( tagname, VCARD_TAG_UID ) == 0 ) {
+                       /* g_print( "- id: %s\n", tagvalue ); */
+                       listID = g_slist_append( listID, g_strdup( tagvalue ) );
+               }
+               if( g_utf8_collate( tagname, VCARD_TAG_END ) == 0 &&
+                       g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                       /* vCard is complete */
+                       /* g_print( "end card\n--\n" ); */
+                       /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
+                       vcard_build_items( cardFile, listName, listAddress, listRemarks, listID );
+                       vcard_free_lists( listName, listAddress, listRemarks, listID );
+                       listName = listAddress = listRemarks = listID = NULL;
+               }
+
+               g_free( tagname );
+               g_free( tagtype );
+               g_free( tagvalue );
+               g_free( tagtemp );
+               g_free( line );
+               line = NULL;
        }
 
-       // Free lists
+       /* Free lists */
        vcard_free_lists( listName, listAddress, listRemarks, listID );
        listName = listAddress = listRemarks = listID = NULL;
 }
 
-// ============================================================================================
+/* ============================================================================================ */
 /*
 * Read file into list. Main entry point
 * Return: TRUE if file read successfully.
 */
-// ============================================================================================
+/* ============================================================================================ */
 gint vcard_read_data( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, -1 );
+       cm_return_val_if_fail( cardFile != NULL, -1 );
 
        cardFile->retVal = MGU_SUCCESS;
-       cardFile->accessFlag = FALSE;
+       cardFile->addressCache->accessFlag = FALSE;
        if( addrcache_check_file( cardFile->addressCache, cardFile->path ) ) {
                addrcache_clear( cardFile->addressCache );
                vcard_open_file( cardFile );
                if( cardFile->retVal == MGU_SUCCESS ) {
-                       // Read data into the list
+                       /* Read data into the list */
                        vcard_read_file( cardFile );
                        vcard_close_file( cardFile );
 
-                       // Mark cache
+                       /* Mark cache */
                        addrcache_mark_file( cardFile->addressCache, cardFile->path );
                        cardFile->addressCache->modified = FALSE;
                        cardFile->addressCache->dataRead = TRUE;
@@ -568,7 +534,7 @@ gint vcard_read_data( VCardFile *cardFile ) {
 * Return link list of persons.
 */
 GList *vcard_get_list_person( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, NULL );
+       cm_return_val_if_fail( cardFile != NULL, NULL );
        return addrcache_get_list_person( cardFile->addressCache );
 }
 
@@ -578,7 +544,7 @@ GList *vcard_get_list_person( VCardFile *cardFile ) {
 * Return: NULL.
 */
 GList *vcard_get_list_folder( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, NULL );
+       cm_return_val_if_fail( cardFile != NULL, NULL );
        return NULL;
 }
 
@@ -589,35 +555,10 @@ GList *vcard_get_list_folder( VCardFile *cardFile ) {
 * Return: List of items, or NULL if none.
 */
 GList *vcard_get_all_persons( VCardFile *cardFile ) {
-       g_return_val_if_fail( cardFile != NULL, NULL );
+       cm_return_val_if_fail( cardFile != NULL, NULL );
        return addrcache_get_all_persons( cardFile->addressCache );
 }
 
-/*
-* Validate that all parameters specified.
-* Return: TRUE if data is good.
-*/
-gboolean vcard_validate( const VCardFile *cardFile ) {
-       gboolean retVal;
-
-       g_return_val_if_fail( cardFile != NULL, FALSE );
-
-       retVal = TRUE;
-       if( cardFile->path ) {
-               if( strlen( cardFile->path ) < 1 ) retVal = FALSE;
-       }
-       else {
-               retVal = FALSE;
-       }
-       if( cardFile->name ) {
-               if( strlen( cardFile->name ) < 1 ) retVal = FALSE;
-       }
-       else {
-               retVal = FALSE;
-       }
-       return retVal;
-}
-
 #define WORK_BUFLEN 1024
 
 /*
@@ -626,17 +567,17 @@ gboolean vcard_validate( const VCardFile *cardFile ) {
 *      be g_free() when done.
 */
 gchar *vcard_find_gnomecard( void ) {
-       gchar *homedir;
+       const gchar *homedir;
        gchar buf[ WORK_BUFLEN ];
-       gchar str[ WORK_BUFLEN ];
+       gchar str[ WORK_BUFLEN + 1 ];
        gchar *fileSpec;
        gint len, lenlbl, i;
        FILE *fp;
 
-       homedir = g_get_home_dir();
+       homedir = get_home_dir();
        if( ! homedir ) return NULL;
 
-       strcpy( str, homedir );
+       strncpy( str, homedir, WORK_BUFLEN );
        len = strlen( str );
        if( len > 0 ) {
                if( str[ len-1 ] != G_DIR_SEPARATOR ) {
@@ -644,37 +585,37 @@ gchar *vcard_find_gnomecard( void ) {
                        str[ ++len ] = '\0';
                }
        }
-       strcat( str, GNOMECARD_DIR );
-       strcat( str, G_DIR_SEPARATOR_S );
-       strcat( str, GNOMECARD_FILE );
+       strncat( str, GNOMECARD_DIR, WORK_BUFLEN - strlen(str) );
+       strncat( str, G_DIR_SEPARATOR_S, WORK_BUFLEN - strlen(str) );
+       strncat( str, GNOMECARD_FILE, WORK_BUFLEN - strlen(str) );
 
        fileSpec = NULL;
-       if( ( fp = fopen( str, "r" ) ) != NULL ) {
-               // Read configuration file
+       if( ( fp = claws_fopen( str, "rb" ) ) != NULL ) {
+               /* Read configuration file */
                lenlbl = strlen( GNOMECARD_SECTION );
-               while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
-                       if( 0 == g_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
+               while( claws_fgets( buf, sizeof( buf ), fp ) != NULL ) {
+                       if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_SECTION, lenlbl ) ) {
                                break;
                        }
                }
 
-               while( fgets( buf, sizeof( buf ), fp ) != NULL ) {
+               while( claws_fgets( buf, sizeof( buf ), fp ) != NULL ) {
                        g_strchomp( buf );
                        if( buf[0] == '[' ) break;
                        for( i = 0; i < lenlbl; i++ ) {
                                if( buf[i] == '=' ) {
-                                       if( 0 == g_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
+                                       if( 0 == g_ascii_strncasecmp( buf, GNOMECARD_PARAM, i ) ) {
                                                fileSpec = g_strdup( buf + i + 1 );
                                                g_strstrip( fileSpec );
                                        }
                                }
                        }
                }
-               fclose( fp );
+               claws_fclose( fp );
        }
 
        if( fileSpec == NULL ) {
-               // Use the home directory
+               /* Use the home directory */
                str[ len ] = '\0';
                fileSpec = g_strdup( str );
        }
@@ -683,7 +624,7 @@ gchar *vcard_find_gnomecard( void ) {
 }
 
 /*
-* Attempt to read file, testing for valid VCard format.
+* Attempt to read file, testing for valid vCard format.
 * Return: TRUE if file appears to be valid format.
 */
 gint vcard_test_read_file( const gchar *fileSpec ) {
@@ -707,35 +648,48 @@ gint vcard_test_read_file( const gchar *fileSpec ) {
 
                        /* Parse line */
                        tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
-                       if( tagtemp ) {
-                               tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
-                               tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
-                               tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
-                               if( tagname == NULL ) {
-                                       tagname = tagtemp;
-                                       tagtemp = NULL;
-                               }
+                       if( tagtemp == NULL ) {
+                               g_free( line );
+                               continue;
+                       }
 
-                               if( tagvalue ) {
-                                       if( g_strcasecmp( tagtype, VCARD_TYPE_QP ) == 0 ) {
-                                               // Quoted-Printable: could span multiple lines
-                                               tagvalue = vcard_read_qp( cardFile, tagvalue );
-                                               vcard_unescape_qp( tagvalue );
-                                       }
-                                       if( g_strcasecmp( tagname, VCARD_TAG_START ) == 0 &&
-                                               g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                               haveStart = TRUE;
-                                       }
-                                       if( g_strcasecmp( tagname, VCARD_TAG_END ) == 0 &&
-                                               g_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
-                                               // VCard is complete
-                                               if( haveStart ) cardFile->retVal = MGU_SUCCESS;
-                                       }
-                                       g_free( tagvalue );
-                               }
-                               g_free( tagname );
-                               g_free( tagtype );
+                       tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
+                       if( tagvalue == NULL ) {
+                               g_free( tagtemp );
+                               g_free( line );
+                               continue;
                        }
+
+                       tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
+                       tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
+                       if( tagname == NULL ) {
+                               tagname = tagtemp;
+                               tagtemp = NULL;
+                       }
+
+                       if( g_utf8_collate( tagtype, VCARD_TYPE_QP ) == 0 ) {
+                               gchar *tmp;
+                               /* Quoted-Printable: could span multiple lines */
+                               tagvalue = vcard_read_qp( cardFile, tagvalue );
+                               tmp = vcard_unescape_qp( tagvalue );
+                               g_free(tagvalue);
+                               tagvalue=tmp;
+                       }
+                       if( g_utf8_collate( tagname, VCARD_TAG_START ) == 0 &&
+                               g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                               haveStart = TRUE;
+                       }
+                       if( g_utf8_collate( tagname, VCARD_TAG_END ) == 0 &&
+                               g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
+                               /* vCard is complete */
+                               if( haveStart ) cardFile->retVal = MGU_SUCCESS;
+                       }
+
+                       g_free( tagname );
+                       g_free( tagtype );
+                       g_free( tagvalue );
+                       g_free( tagtemp );
+                       g_free( line );
                }
                vcard_close_file( cardFile );
        }