1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18 
19 /*
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.
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #include "claws-features.h"
29 #endif
30 
31 #include <glib.h>
32 #include <sys/stat.h>
33 #include <string.h>
34 
35 #include "mgutils.h"
36 #include "vcard.h"
37 #include "addritem.h"
38 #include "addrcache.h"
39 #include "adbookbase.h"
40 #include "utils.h"
41 #include "codeconv.h"
42 #include "quoted-printable.h"
43 #include "file-utils.h"
44 
45 #define GNOMECARD_DIR     ".gnome"
46 #define GNOMECARD_FILE    "GnomeCard"
47 #define GNOMECARD_SECTION "[file]"
48 #define GNOMECARD_PARAM   "open"
49 
50 #define VCARD_TEST_LINES  200
51 
52 /*
53 * Create new cardfile object.
54 */
vcard_create()55 VCardFile *vcard_create() {
56 	VCardFile *cardFile;
57 	cardFile = g_new0( VCardFile, 1 );
58 	cardFile->type = ADBOOKTYPE_VCARD;
59 	cardFile->addressCache = addrcache_create();
60 	cardFile->retVal = MGU_SUCCESS;
61 
62 	cardFile->file = NULL;
63 	cardFile->path = NULL;
64 	cardFile->bufptr = cardFile->buffer;
65 	return cardFile;
66 }
67 
68 /*
69 * Properties...
70 */
vcard_set_name(VCardFile * cardFile,const gchar * value)71 void vcard_set_name( VCardFile* cardFile, const gchar *value ) {
72 	cm_return_if_fail( cardFile != NULL );
73 	addrcache_set_name( cardFile->addressCache, value );
74 }
vcard_set_file(VCardFile * cardFile,const gchar * 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 );
80 }
vcard_set_accessed(VCardFile * cardFile,const gboolean value)81 void vcard_set_accessed( VCardFile *cardFile, const gboolean value ) {
82 	cm_return_if_fail( cardFile != NULL );
83 	cardFile->addressCache->accessFlag = value;
84 }
85 
86 /*
87 * Test whether file was modified since last access.
88 * Return: TRUE if file was modified.
89 */
vcard_get_modified(VCardFile * cardFile)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;
95 }
vcard_get_accessed(VCardFile * cardFile)96 gboolean vcard_get_accessed( VCardFile *cardFile ) {
97 	cm_return_val_if_fail( cardFile != NULL, FALSE );
98 	return cardFile->addressCache->accessFlag;
99 }
100 
101 /*
102 * Test whether file was read.
103 * Return: TRUE if file was read.
104 */
vcard_get_read_flag(VCardFile * cardFile)105 gboolean vcard_get_read_flag( VCardFile *cardFile ) {
106 	cm_return_val_if_fail( cardFile != NULL, FALSE );
107 	return cardFile->addressCache->dataRead;
108 }
109 
110 /*
111 * Return status code from last file operation.
112 * Return: Status code.
113 */
vcard_get_status(VCardFile * cardFile)114 gint vcard_get_status( VCardFile *cardFile ) {
115 	cm_return_val_if_fail( cardFile != NULL, -1 );
116 	return cardFile->retVal;
117 }
118 
vcard_get_root_folder(VCardFile * cardFile)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 );
122 }
vcard_get_name(VCardFile * cardFile)123 gchar *vcard_get_name( VCardFile *cardFile ) {
124 	cm_return_val_if_fail( cardFile != NULL, NULL );
125 	return addrcache_get_name( cardFile->addressCache );
126 }
127 
128 /*
129 * Create new cardfile object for specified file.
130 */
vcard_create_path(const gchar * path)131 static VCardFile *vcard_create_path( const gchar *path ) {
132 	VCardFile *cardFile;
133 	cardFile = vcard_create();
134 	vcard_set_file(cardFile, path);
135 	return cardFile;
136 }
137 
138 /*
139 * Free up cardfile object by releasing internal memory.
140 */
vcard_free(VCardFile * cardFile)141 void vcard_free( VCardFile *cardFile ) {
142 	cm_return_if_fail( cardFile != NULL );
143 
144 	/* Close file */
145 	if( cardFile->file ) claws_fclose( cardFile->file );
146 
147 	/* Clear cache */
148 	addrcache_clear( cardFile->addressCache );
149 	addrcache_free( cardFile->addressCache );
150 
151 	/* Free internal stuff */
152 	g_free( cardFile->path );
153 
154 	/* Clear pointers */
155 	cardFile->file = NULL;
156 	cardFile->path = NULL;
157 	cardFile->bufptr = NULL;
158 
159 	cardFile->type = ADBOOKTYPE_NONE;
160 	cardFile->addressCache = NULL;
161 	cardFile->retVal = MGU_SUCCESS;
162 
163 	/* Now release file object */
164 	g_free( cardFile );
165 }
166 
167 /*
168 * Open file for read.
169 * return: TRUE if file opened successfully.
170 */
vcard_open_file(VCardFile * cardFile)171 static gint vcard_open_file( VCardFile* cardFile ) {
172 	cm_return_val_if_fail( cardFile != NULL, -1 );
173 
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;
182 		}
183 	}
184 	else {
185 		/* g_printerr( "file not specified\n" ); */
186 		cardFile->retVal = MGU_NO_FILE;
187 		return cardFile->retVal;
188 	}
189 
190 	/* Setup a buffer area */
191 	cardFile->buffer[0] = '\0';
192 	cardFile->bufptr = cardFile->buffer;
193 	cardFile->retVal = MGU_SUCCESS;
194 	return cardFile->retVal;
195 }
196 
197 /*
198 * Close file.
199 */
vcard_close_file(VCardFile * cardFile)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;
204 }
205 
206 /*
207 * Read line of text from file.
208 * Return: ptr to buffer where line starts.
209 */
vcard_read_line(VCardFile * cardFile)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 )
213 			return NULL;
214 		g_strstrip( cardFile->buffer );
215 		cardFile->bufptr = cardFile->buffer;
216 	}
217 	return cardFile->bufptr;
218 }
219 
220 /*
221 * Read line of text from file.
222 * Return: ptr to buffer where line starts.
223 */
vcard_get_line(VCardFile * cardFile)224 static gchar *vcard_get_line( VCardFile *cardFile ) {
225 	gchar buf[ VCARDBUFSIZE ];
226 	gchar *start, *end;
227 	gint len;
228 
229 	if (vcard_read_line( cardFile ) == NULL ) {
230 		buf[0] = '\0';
231 		return NULL;
232 	}
233 
234 	/* Copy into private buffer */
235 	start = cardFile->bufptr;
236 	len = strlen( start );
237 	end = start + len;
238 	memcpy( buf, start, len );
239 	buf[ len ] = '\0';
240 	g_strstrip(buf);
241 	cardFile->bufptr = end + 1;
242 
243 	/* Return a copy of buffer */
244 	return g_strdup( buf );
245 }
246 
247 /*
248 * Free linked lists of character strings.
249 */
vcard_free_lists(GSList * listName,GSList * listAddr,GSList * listRem,GSList * listID)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 );
255 }
256 
257 /*
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.
261 */
vcard_read_qp(VCardFile * cardFile,char * tagvalue)262 static gchar *vcard_read_qp( VCardFile *cardFile, char *tagvalue ) {
263 	GSList *listQP = NULL;
264 	gint len = 0;
265 	gchar *line = tagvalue;
266 	while( line ) {
267 		listQP = g_slist_append( listQP, line );
268 		len = strlen( line ) - 1;
269 		if( line[ len ] != '=' ) break;
270 		line[ len ] = '\0';
271 		line = vcard_get_line( cardFile );
272 	}
273 
274 	/* Coalesce linked list into one long buffer. */
275 	line = mgu_list_coalesce( listQP );
276 
277 	/* Clean up */
278 	g_slist_free_full( listQP, g_free );
279 	listQP = NULL;
280 	return line;
281 }
282 
283 /*
284 * Parse tag name from line buffer.
285 * Return: Buffer containing the tag name, or NULL if no delimiter char found.
286 */
vcard_get_tagname(char * line,gchar dlm)287 static gchar *vcard_get_tagname( char* line, gchar dlm ) {
288 	gint len = 0;
289 	gchar *tag = NULL;
290 	gchar *lptr = line;
291 	gchar *down;
292 	while( *lptr++ ) {
293 		if( *lptr == dlm ) {
294 			len = lptr - line;
295 			tag = g_strndup( line, len+1 );
296 			tag[ len ] = '\0';
297 			down = g_utf8_strdown( tag, -1 );
298 			g_free(tag);
299 			return down;
300 		}
301 	}
302 	return tag;
303 }
304 
305 /*
306 * Parse tag value from line buffer.
307 * Return: Buffer containing the tag value. Empty string is returned if
308 * no delimiter char found.
309 */
vcard_get_tagvalue(gchar * line,gchar dlm)310 static gchar *vcard_get_tagvalue( gchar* line, gchar dlm ) {
311 	gchar *value = NULL;
312 	gchar *start = NULL;
313 	gchar *lptr;
314 	gint len = 0;
315 
316 	for( lptr = line; *lptr; lptr++ ) {
317 		if( *lptr == dlm ) {
318 			if( ! start )
319 				start = lptr + 1;
320 		}
321 	}
322 	if( start ) {
323 		len = lptr - start;
324 		value = g_strndup( start, len+1 );
325 	}
326 	else {
327 		/* Ensure that we get an empty string */
328 		value = g_strndup( "", 1 );
329 	}
330 	value[ len ] = '\0';
331 	return value;
332 }
333 
334 /*
335 * Build an address list entry and append to list of address items.
336 */
vcard_build_items(VCardFile * cardFile,GSList * listName,GSList * listAddr,GSList * listRem,GSList * listID)337 static void vcard_build_items(
338 	VCardFile *cardFile, GSList *listName, GSList *listAddr,
339 	GSList *listRem, GSList *listID )
340 {
341 	GSList *nodeName = listName;
342 	GSList *nodeID = listID;
343 	gchar *str;
344 	while( nodeName ) {
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;
351 			if( *str != '\0' ) {
352 				ItemEMail *email = addritem_create_item_email();
353 				addritem_email_set_address( email, str );
354 				if( nodeRemarks ) {
355 					str = nodeRemarks->data;
356 					if( str ) {
357 						if( g_utf8_collate( str, "internet" ) != 0 ) {
358 							if( *str != '\0' )
359 								addritem_email_set_remarks( email, str );
360 						}
361 					}
362 				}
363 				addrcache_id_email( cardFile->addressCache, email );
364 				addrcache_person_add_email( cardFile->addressCache, person, email );
365 			}
366 			nodeAddress = g_slist_next( nodeAddress );
367 			nodeRemarks = g_slist_next( nodeRemarks );
368 		}
369 		if( person->listEMail ) {
370 			addrcache_id_person( cardFile->addressCache, person );
371 			addrcache_add_person( cardFile->addressCache, person );
372 			if( nodeID ) {
373 				str = nodeID->data;
374 				addritem_person_set_external_id( person, str );
375 			}
376 		}
377 		else {
378 			addritem_free_item_person( person );
379 		}
380 		nodeName = g_slist_next( nodeName );
381 		nodeID = g_slist_next( nodeID );
382 	}
383 }
384 
385 /* Unescape characters in quoted-printable string. */
vcard_unescape_qp(gchar * value)386 static gchar *vcard_unescape_qp( gchar *value ) {
387 	gchar *res = NULL;
388 	gint len;
389 	if (value == NULL)
390 		return NULL;
391 
392 	len = strlen(value);
393 	res = g_malloc(len);
394 	qp_decode_const(res, len-1, 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);
398 		g_free(res);
399 		res = mybuf;
400 	}
401 	return res;
402 }
403 
404  /*
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.
413 */
vcard_read_file(VCardFile * cardFile)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; */
418 
419 	for( ;; ) {
420 		gchar *line =  vcard_get_line( cardFile );
421 		if( line == NULL ) break;
422 
423 		/* g_print( "%s\n", line ); */
424 
425 		/* Parse line */
426 		tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
427 		if( tagtemp == NULL ) {
428 			g_free( line );
429 			continue;
430 		}
431 
432 		/* g_print( "\ttemp:  %s\n", tagtemp ); */
433 		tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
434 		if( tagvalue == NULL ) {
435 			g_free( tagtemp );
436 			g_free( line );
437 			continue;
438 		}
439 
440 		tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
441 		tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
442 		if( tagname == NULL ) {
443 			tagname = tagtemp;
444 			tagtemp = NULL;
445 		}
446 
447 		/* g_print( "\tname:  %s\n", tagname ); */
448 		/* g_print( "\ttype:  %s\n", tagtype ); */
449 		/* g_print( "\tvalue: %s\n", tagvalue ); */
450 
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) {
454 			gchar *tmp;
455 			/* Quoted-Printable: could span multiple lines */
456 			tagvalue = vcard_read_qp( cardFile, tagvalue );
457 			tmp = vcard_unescape_qp( tagvalue );
458 			g_free(tagvalue);
459 			tagvalue=tmp;
460 			/* g_print( "QUOTED-PRINTABLE !!! final\n>%s<\n", tagvalue ); */
461 		}
462 
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;
468 		}
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 ) );
472 		}
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 ) );
477 		}
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 ) );
481 		}
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;
490 		}
491 
492 		g_free( tagname );
493 		g_free( tagtype );
494 		g_free( tagvalue );
495 		g_free( tagtemp );
496 		g_free( line );
497 		line = NULL;
498 	}
499 
500 	/* Free lists */
501 	vcard_free_lists( listName, listAddress, listRemarks, listID );
502 	listName = listAddress = listRemarks = listID = NULL;
503 }
504 
505 /* ============================================================================================ */
506 /*
507 * Read file into list. Main entry point
508 * Return: TRUE if file read successfully.
509 */
510 /* ============================================================================================ */
vcard_read_data(VCardFile * cardFile)511 gint vcard_read_data( VCardFile *cardFile ) {
512 	cm_return_val_if_fail( cardFile != NULL, -1 );
513 
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 );
523 
524 			/* Mark cache */
525 			addrcache_mark_file( cardFile->addressCache, cardFile->path );
526 			cardFile->addressCache->modified = FALSE;
527 			cardFile->addressCache->dataRead = TRUE;
528 		}
529 	}
530 	return cardFile->retVal;
531 }
532 
533 /*
534 * Return link list of persons.
535 */
vcard_get_list_person(VCardFile * cardFile)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 );
539 }
540 
541 /*
542 * Return link list of folders. This is always NULL since there are
543 * no folders in GnomeCard.
544 * Return: NULL.
545 */
vcard_get_list_folder(VCardFile * cardFile)546 GList *vcard_get_list_folder( VCardFile *cardFile ) {
547 	cm_return_val_if_fail( cardFile != NULL, NULL );
548 	return NULL;
549 }
550 
551 /*
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.
556 */
vcard_get_all_persons(VCardFile * cardFile)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 );
560 }
561 
562 #define WORK_BUFLEN 1024
563 
564 /*
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.
568 */
vcard_find_gnomecard(void)569 gchar *vcard_find_gnomecard( void ) {
570 	const gchar *homedir;
571 	gchar buf[ WORK_BUFLEN ];
572 	gchar str[ WORK_BUFLEN + 1 ];
573 	gchar *fileSpec;
574 	gint len, lenlbl, i;
575 	FILE *fp;
576 
577 	homedir = get_home_dir();
578 	if( ! homedir ) return NULL;
579 
580 	strncpy( str, homedir, WORK_BUFLEN );
581 	len = strlen( str );
582 	if( len > 0 ) {
583 		if( str[ len-1 ] != G_DIR_SEPARATOR ) {
584 			str[ len ] = G_DIR_SEPARATOR;
585 			str[ ++len ] = '\0';
586 		}
587 	}
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) );
591 
592 	fileSpec = NULL;
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 ) ) {
598 				break;
599 			}
600 		}
601 
602 		while( claws_fgets( buf, sizeof( buf ), fp ) != NULL ) {
603 			g_strchomp( buf );
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 );
610 					}
611 				}
612 			}
613 		}
614 		claws_fclose( fp );
615 	}
616 
617 	if( fileSpec == NULL ) {
618 		/* Use the home directory */
619 		str[ len ] = '\0';
620 		fileSpec = g_strdup( str );
621 	}
622 
623 	return fileSpec;
624 }
625 
626 /*
627 * Attempt to read file, testing for valid vCard format.
628 * Return: TRUE if file appears to be valid format.
629 */
vcard_test_read_file(const gchar * fileSpec)630 gint vcard_test_read_file( const gchar *fileSpec ) {
631 	gboolean haveStart;
632 	gchar *tagtemp = NULL, *tagname = NULL, *tagvalue = NULL, *tagtype = NULL, *line;
633 	VCardFile *cardFile;
634 	gint retVal, lines;
635 
636 	if( ! fileSpec ) return MGU_NO_FILE;
637 
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;
643 		haveStart = FALSE;
644 		lines = VCARD_TEST_LINES;
645 		while( lines > 0 ) {
646 			lines--;
647 			if( ( line =  vcard_get_line( cardFile ) ) == NULL ) break;
648 
649 			/* Parse line */
650 			tagtemp = vcard_get_tagname( line, VCARD_SEP_TAG );
651 			if( tagtemp == NULL ) {
652 				g_free( line );
653 				continue;
654 			}
655 
656 			tagvalue = vcard_get_tagvalue( line, VCARD_SEP_TAG );
657 			if( tagvalue == NULL ) {
658 				g_free( tagtemp );
659 				g_free( line );
660 				continue;
661 			}
662 
663 			tagname = vcard_get_tagname( tagtemp, VCARD_SEP_TYPE );
664 			tagtype = vcard_get_tagvalue( tagtemp, VCARD_SEP_TYPE );
665 			if( tagname == NULL ) {
666 				tagname = tagtemp;
667 				tagtemp = NULL;
668 			}
669 
670 			if( g_utf8_collate( tagtype, VCARD_TYPE_QP ) == 0 ) {
671 				gchar *tmp;
672 				/* Quoted-Printable: could span multiple lines */
673 				tagvalue = vcard_read_qp( cardFile, tagvalue );
674 				tmp = vcard_unescape_qp( tagvalue );
675 				g_free(tagvalue);
676 				tagvalue=tmp;
677 			}
678 			if( g_utf8_collate( tagname, VCARD_TAG_START ) == 0 &&
679 				g_ascii_strcasecmp( tagvalue, VCARD_NAME ) == 0 ) {
680 				haveStart = TRUE;
681 			}
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;
686 			}
687 
688 			g_free( tagname );
689 			g_free( tagtype );
690 			g_free( tagvalue );
691 			g_free( tagtemp );
692 			g_free( line );
693 		}
694 		vcard_close_file( cardFile );
695 	}
696 	retVal = cardFile->retVal;
697 	vcard_free( cardFile );
698 	cardFile = NULL;
699 	return retVal;
700 }
701 
702 /*
703 * End of Source.
704 */
705 
706