2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Functions necessary to access vCard files. vCard files are used
21 * by GnomeCard for addressbook, and Netscape for sending business
22 * card information. Refer to http://www.imc.org/pdi/vcard-21.txt and
23 * RFC2426 for more information.
28 #include "claws-features.h"
38 #include "addrcache.h"
39 #include "adbookbase.h"
42 #include "quoted-printable.h"
43 #include "file-utils.h"
45 #define GNOMECARD_DIR ".gnome"
46 #define GNOMECARD_FILE "GnomeCard"
47 #define GNOMECARD_SECTION "[file]"
48 #define GNOMECARD_PARAM "open"
50 #define VCARD_TEST_LINES 200
53 * Create new cardfile object.
55 VCardFile
*vcard_create() {
57 cardFile
= g_new0( VCardFile
, 1 );
58 cardFile
->type
= ADBOOKTYPE_VCARD
;
59 cardFile
->addressCache
= addrcache_create();
60 cardFile
->retVal
= MGU_SUCCESS
;
62 cardFile
->file
= NULL
;
63 cardFile
->path
= NULL
;
64 cardFile
->bufptr
= cardFile
->buffer
;
71 void vcard_set_name( VCardFile
* cardFile
, const gchar
*value
) {
72 cm_return_if_fail( cardFile
!= NULL
);
73 addrcache_set_name( cardFile
->addressCache
, value
);
75 void vcard_set_file( VCardFile
* cardFile
, const gchar
*value
) {
76 cm_return_if_fail( cardFile
!= NULL
);
77 addrcache_refresh( cardFile
->addressCache
);
78 cardFile
->path
= mgu_replace_string( cardFile
->path
, value
);
79 g_strstrip( cardFile
->path
);
81 void vcard_set_accessed( VCardFile
*cardFile
, const gboolean value
) {
82 cm_return_if_fail( cardFile
!= NULL
);
83 cardFile
->addressCache
->accessFlag
= value
;
87 * Test whether file was modified since last access.
88 * Return: TRUE if file was modified.
90 gboolean
vcard_get_modified( VCardFile
*cardFile
) {
91 cm_return_val_if_fail( cardFile
!= NULL
, FALSE
);
92 cardFile
->addressCache
->modified
=
93 addrcache_check_file( cardFile
->addressCache
, cardFile
->path
);
94 return cardFile
->addressCache
->modified
;
96 gboolean
vcard_get_accessed( VCardFile
*cardFile
) {
97 cm_return_val_if_fail( cardFile
!= NULL
, FALSE
);
98 return cardFile
->addressCache
->accessFlag
;
102 * Test whether file was read.
103 * Return: TRUE if file was read.
105 gboolean
vcard_get_read_flag( VCardFile
*cardFile
) {
106 cm_return_val_if_fail( cardFile
!= NULL
, FALSE
);
107 return cardFile
->addressCache
->dataRead
;
111 * Return status code from last file operation.
112 * Return: Status code.
114 gint
vcard_get_status( VCardFile
*cardFile
) {
115 cm_return_val_if_fail( cardFile
!= NULL
, -1 );
116 return cardFile
->retVal
;
119 ItemFolder
*vcard_get_root_folder( VCardFile
*cardFile
) {
120 cm_return_val_if_fail( cardFile
!= NULL
, NULL
);
121 return addrcache_get_root_folder( cardFile
->addressCache
);
123 gchar
*vcard_get_name( VCardFile
*cardFile
) {
124 cm_return_val_if_fail( cardFile
!= NULL
, NULL
);
125 return addrcache_get_name( cardFile
->addressCache
);
129 * Create new cardfile object for specified file.
131 static VCardFile
*vcard_create_path( const gchar
*path
) {
133 cardFile
= vcard_create();
134 vcard_set_file(cardFile
, path
);
139 * Free up cardfile object by releasing internal memory.
141 void vcard_free( VCardFile
*cardFile
) {
142 cm_return_if_fail( cardFile
!= NULL
);
145 if( cardFile
->file
) claws_fclose( cardFile
->file
);
148 addrcache_clear( cardFile
->addressCache
);
149 addrcache_free( cardFile
->addressCache
);
151 /* Free internal stuff */
152 g_free( cardFile
->path
);
155 cardFile
->file
= NULL
;
156 cardFile
->path
= NULL
;
157 cardFile
->bufptr
= NULL
;
159 cardFile
->type
= ADBOOKTYPE_NONE
;
160 cardFile
->addressCache
= NULL
;
161 cardFile
->retVal
= MGU_SUCCESS
;
163 /* Now release file object */
168 * Open file for read.
169 * return: TRUE if file opened successfully.
171 static gint
vcard_open_file( VCardFile
* cardFile
) {
172 cm_return_val_if_fail( cardFile
!= NULL
, -1 );
174 /* g_print( "Opening file\n" ); */
175 cardFile
->addressCache
->dataRead
= FALSE
;
176 if( cardFile
->path
) {
177 cardFile
->file
= claws_fopen( cardFile
->path
, "rb" );
178 if( ! cardFile
->file
) {
179 /* g_printerr( "can't open %s\n", cardFile->path ); */
180 cardFile
->retVal
= MGU_OPEN_FILE
;
181 return cardFile
->retVal
;
185 /* g_printerr( "file not specified\n" ); */
186 cardFile
->retVal
= MGU_NO_FILE
;
187 return cardFile
->retVal
;
190 /* Setup a buffer area */
191 cardFile
->buffer
[0] = '\0';
192 cardFile
->bufptr
= cardFile
->buffer
;
193 cardFile
->retVal
= MGU_SUCCESS
;
194 return cardFile
->retVal
;
200 static void vcard_close_file( VCardFile
*cardFile
) {
201 cm_return_if_fail( cardFile
!= NULL
);
202 if( cardFile
->file
) claws_fclose( cardFile
->file
);
203 cardFile
->file
= NULL
;
207 * Read line of text from file.
208 * Return: ptr to buffer where line starts.
210 static gchar
*vcard_read_line( VCardFile
*cardFile
) {
211 while( *cardFile
->bufptr
== '\n' || *cardFile
->bufptr
== '\0' ) {
212 if( claws_fgets( cardFile
->buffer
, VCARDBUFSIZE
, cardFile
->file
) == NULL
)
214 g_strstrip( cardFile
->buffer
);
215 cardFile
->bufptr
= cardFile
->buffer
;
217 return cardFile
->bufptr
;
221 * Read line of text from file.
222 * Return: ptr to buffer where line starts.
224 static gchar
*vcard_get_line( VCardFile
*cardFile
) {
225 gchar buf
[ VCARDBUFSIZE
];
229 if (vcard_read_line( cardFile
) == NULL
) {
234 /* Copy into private buffer */
235 start
= cardFile
->bufptr
;
236 len
= strlen( start
);
238 memcpy( buf
, start
, len
);
241 cardFile
->bufptr
= end
+ 1;
243 /* Return a copy of buffer */
244 return g_strdup( buf
);
248 * Free linked lists of character strings.
250 static void vcard_free_lists( GSList
*listName
, GSList
*listAddr
, GSList
*listRem
, GSList
* listID
) {
251 g_slist_free_full( listName
, g_free
);
252 g_slist_free_full( listAddr
, g_free
);
253 g_slist_free_full( listRem
, g_free
);
254 g_slist_free_full( listID
, g_free
);
258 * Read quoted-printable text, which may span several lines into one long string.
259 * Param: cardFile - object.
260 * Param: tagvalue - will be placed into the linked list.
262 static gchar
*vcard_read_qp( VCardFile
*cardFile
, char *tagvalue
) {
263 GSList
*listQP
= NULL
;
265 gchar
*line
= tagvalue
;
267 listQP
= g_slist_append( listQP
, line
);
268 len
= strlen( line
) - 1;
269 if( line
[ len
] != '=' ) break;
271 line
= vcard_get_line( cardFile
);
274 /* Coalesce linked list into one long buffer. */
275 line
= mgu_list_coalesce( listQP
);
278 g_slist_free_full( listQP
, g_free
);
284 * Parse tag name from line buffer.
285 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
287 static gchar
*vcard_get_tagname( char* line
, gchar dlm
) {
295 tag
= g_strndup( line
, len
+1 );
297 down
= g_utf8_strdown( tag
, -1 );
306 * Parse tag value from line buffer.
307 * Return: Buffer containing the tag value. Empty string is returned if
308 * no delimiter char found.
310 static gchar
*vcard_get_tagvalue( gchar
* line
, gchar dlm
) {
316 for( lptr
= line
; *lptr
; lptr
++ ) {
324 value
= g_strndup( start
, len
+1 );
327 /* Ensure that we get an empty string */
328 value
= g_strndup( "", 1 );
335 * Build an address list entry and append to list of address items.
337 static void vcard_build_items(
338 VCardFile
*cardFile
, GSList
*listName
, GSList
*listAddr
,
339 GSList
*listRem
, GSList
*listID
)
341 GSList
*nodeName
= listName
;
342 GSList
*nodeID
= listID
;
345 GSList
*nodeAddress
= listAddr
;
346 GSList
*nodeRemarks
= listRem
;
347 ItemPerson
*person
= addritem_create_item_person();
348 addritem_person_set_common_name( person
, nodeName
->data
);
349 while( nodeAddress
) {
350 str
= nodeAddress
->data
;
352 ItemEMail
*email
= addritem_create_item_email();
353 addritem_email_set_address( email
, str
);
355 str
= nodeRemarks
->data
;
357 if( g_utf8_collate( str
, "internet" ) != 0 ) {
359 addritem_email_set_remarks( email
, str
);
363 addrcache_id_email( cardFile
->addressCache
, email
);
364 addrcache_person_add_email( cardFile
->addressCache
, person
, email
);
366 nodeAddress
= g_slist_next( nodeAddress
);
367 nodeRemarks
= g_slist_next( nodeRemarks
);
369 if( person
->listEMail
) {
370 addrcache_id_person( cardFile
->addressCache
, person
);
371 addrcache_add_person( cardFile
->addressCache
, person
);
374 addritem_person_set_external_id( person
, str
);
378 addritem_free_item_person( person
);
380 nodeName
= g_slist_next( nodeName
);
381 nodeID
= g_slist_next( nodeID
);
385 /* Unescape characters in quoted-printable string. */
386 static gchar
*vcard_unescape_qp( gchar
*value
) {
393 res
= g_malloc(len
+ 1);
394 qp_decode_const(res
, len
, value
);
395 if (!g_utf8_validate(res
, -1, NULL
)) {
396 gchar
*mybuf
= g_malloc(strlen(res
)*2 +1);
397 conv_localetodisp(mybuf
, strlen(res
)*2 +1, res
);
405 * Read file data into root folder.
406 * Note that one vCard can have multiple E-Mail addresses (MAIL tags);
407 * these are broken out into separate address items. An address item
408 * is generated for the person identified by FN tag and each EMAIL tag.
409 * If a sub-type is included in the EMAIL entry, this will be used as
410 * the Remarks member. Also note that it is possible for one vCard
411 * entry to have multiple FN tags; this might not make sense. However,
412 * it will generate duplicate address entries for each person listed.
414 static void vcard_read_file( VCardFile
*cardFile
) {
415 gchar
*tagtemp
= NULL
, *tagname
= NULL
, *tagvalue
= NULL
, *tagtype
= NULL
;
416 GSList
*listName
= NULL
, *listAddress
= NULL
, *listRemarks
= NULL
, *listID
= NULL
;
417 /* GSList *listQP = NULL; */
420 gchar
*line
= vcard_get_line( cardFile
);
421 if( line
== NULL
) break;
423 /* g_print( "%s\n", line ); */
426 tagtemp
= vcard_get_tagname( line
, VCARD_SEP_TAG
);
427 if( tagtemp
== NULL
) {
432 /* g_print( "\ttemp: %s\n", tagtemp ); */
433 tagvalue
= vcard_get_tagvalue( line
, VCARD_SEP_TAG
);
434 if( tagvalue
== NULL
) {
440 tagname
= vcard_get_tagname( tagtemp
, VCARD_SEP_TYPE
);
441 tagtype
= vcard_get_tagvalue( tagtemp
, VCARD_SEP_TYPE
);
442 if( tagname
== NULL
) {
447 /* g_print( "\tname: %s\n", tagname ); */
448 /* g_print( "\ttype: %s\n", tagtype ); */
449 /* g_print( "\tvalue: %s\n", tagvalue ); */
451 if( g_utf8_collate( tagtype
, VCARD_TYPE_QP
) == 0
452 || g_utf8_collate( tagtype
, VCARD_TYPE_E_QP
) == 0
453 || g_utf8_collate( tagtype
, VCARD_TYPE_CS_UTF8_E_QP
) == 0) {
455 /* Quoted-Printable: could span multiple lines */
456 tagvalue
= vcard_read_qp( cardFile
, tagvalue
);
457 tmp
= vcard_unescape_qp( tagvalue
);
460 /* g_print( "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
463 if( g_utf8_collate( tagname
, VCARD_TAG_START
) == 0 &&
464 g_ascii_strcasecmp( tagvalue
, VCARD_NAME
) == 0 ) {
465 /* g_print( "start card\n" ); */
466 vcard_free_lists( listName
, listAddress
, listRemarks
, listID
);
467 listName
= listAddress
= listRemarks
= listID
= NULL
;
469 if( g_utf8_collate( tagname
, VCARD_TAG_FULLNAME
) == 0 ) {
470 /* g_print( "- full name: %s\n", tagvalue ); */
471 listName
= g_slist_append( listName
, g_strdup( tagvalue
) );
473 if( g_utf8_collate( tagname
, VCARD_TAG_EMAIL
) == 0 ) {
474 /* g_print( "- address: %s\n", tagvalue ); */
475 listAddress
= g_slist_append( listAddress
, g_strdup( tagvalue
) );
476 listRemarks
= g_slist_append( listRemarks
, g_strdup( tagtype
) );
478 if( g_utf8_collate( tagname
, VCARD_TAG_UID
) == 0 ) {
479 /* g_print( "- id: %s\n", tagvalue ); */
480 listID
= g_slist_append( listID
, g_strdup( tagvalue
) );
482 if( g_utf8_collate( tagname
, VCARD_TAG_END
) == 0 &&
483 g_ascii_strcasecmp( tagvalue
, VCARD_NAME
) == 0 ) {
484 /* vCard is complete */
485 /* g_print( "end card\n--\n" ); */
486 /* vcard_dump_lists( listName, listAddress, listRemarks, listID, stdout ); */
487 vcard_build_items( cardFile
, listName
, listAddress
, listRemarks
, listID
);
488 vcard_free_lists( listName
, listAddress
, listRemarks
, listID
);
489 listName
= listAddress
= listRemarks
= listID
= NULL
;
501 vcard_free_lists( listName
, listAddress
, listRemarks
, listID
);
502 listName
= listAddress
= listRemarks
= listID
= NULL
;
505 /* ============================================================================================ */
507 * Read file into list. Main entry point
508 * Return: TRUE if file read successfully.
510 /* ============================================================================================ */
511 gint
vcard_read_data( VCardFile
*cardFile
) {
512 cm_return_val_if_fail( cardFile
!= NULL
, -1 );
514 cardFile
->retVal
= MGU_SUCCESS
;
515 cardFile
->addressCache
->accessFlag
= FALSE
;
516 if( addrcache_check_file( cardFile
->addressCache
, cardFile
->path
) ) {
517 addrcache_clear( cardFile
->addressCache
);
518 vcard_open_file( cardFile
);
519 if( cardFile
->retVal
== MGU_SUCCESS
) {
520 /* Read data into the list */
521 vcard_read_file( cardFile
);
522 vcard_close_file( cardFile
);
525 addrcache_mark_file( cardFile
->addressCache
, cardFile
->path
);
526 cardFile
->addressCache
->modified
= FALSE
;
527 cardFile
->addressCache
->dataRead
= TRUE
;
530 return cardFile
->retVal
;
534 * Return link list of persons.
536 GList
*vcard_get_list_person( VCardFile
*cardFile
) {
537 cm_return_val_if_fail( cardFile
!= NULL
, NULL
);
538 return addrcache_get_list_person( cardFile
->addressCache
);
542 * Return link list of folders. This is always NULL since there are
543 * no folders in GnomeCard.
546 GList
*vcard_get_list_folder( VCardFile
*cardFile
) {
547 cm_return_val_if_fail( cardFile
!= NULL
, NULL
);
552 * Return link list of all persons. Note that the list contains references
553 * to items. Do *NOT* attempt to use the addrbook_free_xxx() functions...
554 * this will destroy the addressbook data!
555 * Return: List of items, or NULL if none.
557 GList
*vcard_get_all_persons( VCardFile
*cardFile
) {
558 cm_return_val_if_fail( cardFile
!= NULL
, NULL
);
559 return addrcache_get_all_persons( cardFile
->addressCache
);
562 #define WORK_BUFLEN 1024
565 * Attempt to find a valid GnomeCard file.
566 * Return: Filename, or home directory if not found. Filename should
567 * be g_free() when done.
569 gchar
*vcard_find_gnomecard( void ) {
570 const gchar
*homedir
;
571 gchar buf
[ WORK_BUFLEN
];
572 gchar str
[ WORK_BUFLEN
+ 1 ];
577 homedir
= get_home_dir();
578 if( ! homedir
) return NULL
;
580 strncpy( str
, homedir
, WORK_BUFLEN
);
583 if( str
[ len
-1 ] != G_DIR_SEPARATOR
) {
584 str
[ len
] = G_DIR_SEPARATOR
;
588 strncat( str
, GNOMECARD_DIR
, WORK_BUFLEN
- strlen(str
) );
589 strncat( str
, G_DIR_SEPARATOR_S
, WORK_BUFLEN
- strlen(str
) );
590 strncat( str
, GNOMECARD_FILE
, WORK_BUFLEN
- strlen(str
) );
593 if( ( fp
= claws_fopen( str
, "rb" ) ) != NULL
) {
594 /* Read configuration file */
595 lenlbl
= strlen( GNOMECARD_SECTION
);
596 while( claws_fgets( buf
, sizeof( buf
), fp
) != NULL
) {
597 if( 0 == g_ascii_strncasecmp( buf
, GNOMECARD_SECTION
, lenlbl
) ) {
602 while( claws_fgets( buf
, sizeof( buf
), fp
) != NULL
) {
604 if( buf
[0] == '[' ) break;
605 for( i
= 0; i
< lenlbl
; i
++ ) {
606 if( buf
[i
] == '=' ) {
607 if( 0 == g_ascii_strncasecmp( buf
, GNOMECARD_PARAM
, i
) ) {
608 fileSpec
= g_strdup( buf
+ i
+ 1 );
609 g_strstrip( fileSpec
);
617 if( fileSpec
== NULL
) {
618 /* Use the home directory */
620 fileSpec
= g_strdup( str
);
627 * Attempt to read file, testing for valid vCard format.
628 * Return: TRUE if file appears to be valid format.
630 gint
vcard_test_read_file( const gchar
*fileSpec
) {
632 gchar
*tagtemp
= NULL
, *tagname
= NULL
, *tagvalue
= NULL
, *tagtype
= NULL
, *line
;
636 if( ! fileSpec
) return MGU_NO_FILE
;
638 cardFile
= vcard_create_path( fileSpec
);
639 cardFile
->retVal
= MGU_SUCCESS
;
640 vcard_open_file( cardFile
);
641 if( cardFile
->retVal
== MGU_SUCCESS
) {
642 cardFile
->retVal
= MGU_BAD_FORMAT
;
644 lines
= VCARD_TEST_LINES
;
647 if( ( line
= vcard_get_line( cardFile
) ) == NULL
) break;
650 tagtemp
= vcard_get_tagname( line
, VCARD_SEP_TAG
);
651 if( tagtemp
== NULL
) {
656 tagvalue
= vcard_get_tagvalue( line
, VCARD_SEP_TAG
);
657 if( tagvalue
== NULL
) {
663 tagname
= vcard_get_tagname( tagtemp
, VCARD_SEP_TYPE
);
664 tagtype
= vcard_get_tagvalue( tagtemp
, VCARD_SEP_TYPE
);
665 if( tagname
== NULL
) {
670 if( g_utf8_collate( tagtype
, VCARD_TYPE_QP
) == 0 ) {
672 /* Quoted-Printable: could span multiple lines */
673 tagvalue
= vcard_read_qp( cardFile
, tagvalue
);
674 tmp
= vcard_unescape_qp( tagvalue
);
678 if( g_utf8_collate( tagname
, VCARD_TAG_START
) == 0 &&
679 g_ascii_strcasecmp( tagvalue
, VCARD_NAME
) == 0 ) {
682 if( g_utf8_collate( tagname
, VCARD_TAG_END
) == 0 &&
683 g_ascii_strcasecmp( tagvalue
, VCARD_NAME
) == 0 ) {
684 /* vCard is complete */
685 if( haveStart
) cardFile
->retVal
= MGU_SUCCESS
;
694 vcard_close_file( cardFile
);
696 retVal
= cardFile
->retVal
;
697 vcard_free( cardFile
);