2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2007 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/>.
21 * Functions necessary to access Pine address book file.
32 #include "addrcache.h"
34 #define PINE_HOME_FILE ".addressbook"
35 #define PINEBUFSIZE 2048
36 #define CHAR_QUOTE '\"'
37 #define CHAR_APOS '\''
38 #define CHAR_COMMA ','
44 PineFile *pine_create() {
46 pineFile = g_new0( PineFile, 1 );
47 pineFile->path = NULL;
48 pineFile->file = NULL;
49 pineFile->retVal = MGU_SUCCESS;
50 pineFile->uniqTable = g_hash_table_new( g_str_hash, g_str_equal );
51 pineFile->cbProgress = NULL;
58 void pine_set_file( PineFile* pineFile, const gchar *value ) {
59 g_return_if_fail( pineFile != NULL );
60 pineFile->path = mgu_replace_string( pineFile->path, value );
61 g_strstrip( pineFile->path );
67 static gint pine_free_table_vis( gpointer key, gpointer value, gpointer data ) {
75 * Free up object by releasing internal memory.
77 void pine_free( PineFile *pineFile ) {
78 g_return_if_fail( pineFile != NULL );
81 if( pineFile->file ) fclose( pineFile->file );
83 /* Free internal stuff */
84 g_free( pineFile->path );
86 /* Free unique address table */
87 g_hash_table_foreach_remove( pineFile->uniqTable, pine_free_table_vis, NULL );
88 g_hash_table_destroy( pineFile->uniqTable );
91 pineFile->file = NULL;
92 pineFile->path = NULL;
93 pineFile->retVal = MGU_SUCCESS;
94 pineFile->uniqTable = NULL;
95 pineFile->cbProgress = NULL;
97 /* Now release file object */
102 * Open file for read.
103 * Enter: pineFile File object.
104 * return: TRUE if file opened successfully.
106 static gint pine_open_file( PineFile* pineFile ) {
107 if( pineFile->path ) {
108 pineFile->file = g_fopen( pineFile->path, "rb" );
109 if( ! pineFile->file ) {
110 pineFile->retVal = MGU_OPEN_FILE;
111 return pineFile->retVal;
115 /* g_print( "file not specified\n" ); */
116 pineFile->retVal = MGU_NO_FILE;
117 return pineFile->retVal;
120 /* Setup a buffer area */
121 pineFile->retVal = MGU_SUCCESS;
122 return pineFile->retVal;
127 * Enter: pineFile File object.
129 static void pine_close_file( PineFile *pineFile ) {
130 g_return_if_fail( pineFile != NULL );
131 if( pineFile->file ) fclose( pineFile->file );
132 pineFile->file = NULL;
136 * Read line of text from file.
137 * Enter: pineFile File object.
138 * Return: Copy of buffer. Should be g_free'd when done.
140 static gchar *pine_read_line( PineFile *pineFile ) {
141 gchar buf[ PINEBUFSIZE ];
145 if( feof( pineFile->file ) )
148 while( i < PINEBUFSIZE-1 ) {
149 c = fgetc( pineFile->file );
169 /* Copy into private buffer */
170 return g_strdup( buf );
174 * Parsed address data.
176 typedef struct _Pine_ParsedRec_ Pine_ParsedRec;
177 struct _Pine_ParsedRec_ {
190 * Enter: rec Data record.
192 static void pine_free_rec( Pine_ParsedRec *rec ) {
194 g_free( rec->nickName );
196 g_free( rec->address );
198 g_free( rec->comments );
199 mgu_clear_slist( rec->listName );
200 mgu_clear_slist( rec->listAddr );
201 g_slist_free( rec->listName );
202 g_slist_free( rec->listAddr );
203 rec->nickName = NULL;
207 rec->comments = NULL;
208 rec->isGroup = FALSE;
216 static void pine_clean_name( Pine_ParsedRec *rec ) {
220 if( p == NULL ) return;
221 if( *p == '\0' ) return;
223 g_strstrip( rec->name );
224 if( *p == CHAR_APOS || *p == CHAR_QUOTE ) {
228 /* If embedded comma present, surround match with quotes */
230 if( *p == CHAR_COMMA ) {
231 p = g_strdup_printf( "\"%s\"", rec->name );
241 * Parse pine address record.
242 * Enter: buf Address record buffer.
243 * Return: Data record.
245 static Pine_ParsedRec *pine_parse_record( gchar *buf ) {
251 for( i = 0; i < 5; i++ )
254 /* Extract tab separated values */
262 tmp[ pos ] = g_strndup( f, len );
271 /* Extract last value */
274 tmp[ pos++ ] = g_strndup( f, len );
277 /* Populate record */
279 rec = g_new0( Pine_ParsedRec, 1 );
280 rec->isGroup = FALSE;
281 for( i = 0; i < pos; i++ ) {
286 if( i == 0 ) rec->nickName = f;
287 else if( i == 1 ) rec->name = f;
288 else if( i == 2 ) rec->address = f;
289 else if( i == 3 ) rec->fcc = f;
290 else if( i == 4 ) rec->comments = f;
294 if( rec->address != NULL ) {
295 /* Strip leading/trailing parens */
298 len = strlen( p ) - 1;
310 * Parse name from email address string.
311 * Enter: buf Start address of buffer to process (not modified).
312 * atp Pointer to email at (@) character.
313 * ap Pointer to start of email address returned.
314 * ep Pointer to end of email address returned.
315 * Return: Parsed name or NULL if not present. This should be g_free'd
318 static gchar *pine_parse_name(
319 const gchar *buf, const gchar *atp, const gchar **ap,
332 /* Find first non-separator char */
335 if( strchr( ",; \n\r", *bp ) == NULL ) break;
339 /* Search back for start of name */
345 /* Found start of address/end of name part */
346 ilen = -1 + ( size_t ) ( pos - bp );
347 name = g_strndup( bp, ilen + 1 );
348 *(name + ilen + 1) = '\0';
350 /* Remove leading trailing quotes and spaces */
351 mgu_str_ltc2space( name, '\"', '\"' );
352 mgu_str_ltc2space( name, '\'', '\'' );
353 mgu_str_ltc2space( name, '\"', '\"' );
354 mgu_str_unescape( name );
362 /* Search forward for end of address */
369 if( strchr( ",; \'\n\r", *pos ) ) break;
378 * Parse address list.
379 * Enter: pineFile Pine control data.
380 * cache Address cache.
383 static void pine_parse_address( PineFile *pineFile, AddressCache *cache, Pine_ParsedRec *rec ) {
385 gchar addr[ PINEBUFSIZE ];
392 g_return_if_fail( rec->address != NULL );
395 while((atCh = strchr( buf, CHAR_AT )) != NULL) {
396 name = pine_parse_name( buf, atCh, &bp, &ep );
397 len = ( size_t ) ( ep - bp );
398 strncpy( addr, bp, len );
400 extract_address( addr );
402 if( name == NULL ) name = g_strdup( "" );
403 rec->listName = g_slist_append( rec->listName, name );
404 rec->listAddr = g_slist_append( rec->listAddr, g_strdup( addr ) );
414 * Insert person and address into address cache.
415 * Enter: pineFile Pine control data.
416 * cache Address cache.
417 * address E-Mail address.
420 * Return: E-Mail object, either inserted or found in hash table.
422 static ItemEMail *pine_insert_table(
423 PineFile *pineFile, AddressCache *cache, gchar *address,
424 gchar *name, gchar *remarks )
430 g_return_val_if_fail( address != NULL, NULL );
432 /* create an entry with empty name if needed */
436 /* Test whether address already in hash table */
437 key = g_strdup( address );
439 email = g_hash_table_lookup( pineFile->uniqTable, key );
441 if( email == NULL ) {
442 /* No - create person */
443 person = addritem_create_item_person();
444 addritem_person_set_common_name( person, name );
445 addrcache_id_person( cache, person );
446 addrcache_add_person( cache, person );
448 /* Add email for person */
449 email = addritem_create_item_email();
450 addritem_email_set_address( email, address );
451 addritem_email_set_remarks( email, remarks );
452 addrcache_id_email( cache, email );
453 addrcache_person_add_email( cache, person, email );
456 g_hash_table_insert( pineFile->uniqTable, key, email );
459 /* Yes - update person with longest name */
460 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
461 if( strlen( name ) > strlen( ADDRITEM_NAME(person) ) ) {
462 addritem_person_set_common_name( person, name );
473 * Parse address line adn build address items.
474 * Enter: pineFile Pine control data.
475 * cache Address cache to load.
476 * line Address record.
478 static void pine_build_items( PineFile *pineFile, AddressCache *cache, gchar *line ) {
480 GSList *nodeAddr, *nodeName;
484 rec = pine_parse_record( line );
486 pine_clean_name( rec );
487 pine_parse_address( pineFile, cache, rec );
488 /* pine_print_rec( rec, stdout ); */
489 /* g_print( "=========\n" ); */
493 group = addritem_create_item_group();
494 addritem_group_set_name( group, rec->nickName );
495 addrcache_id_group( cache, group );
496 addrcache_add_group( cache, group );
498 /* Add email to group */
499 nodeName = rec->listName;
500 nodeAddr = rec->listAddr;
502 email = pine_insert_table(
503 pineFile, cache, nodeAddr->data,
504 nodeName->data, "" );
506 /* Add email to group */
507 addritem_group_add_email( group, email );
509 nodeAddr = g_slist_next( nodeAddr );
510 nodeName = g_slist_next( nodeName );
514 email = pine_insert_table(
515 pineFile, cache, rec->address,
516 rec->name, rec->comments );
519 pine_free_rec( rec );
524 * Read file data into address cache.
525 * Enter: pineFile Pine control data.
526 * cache Address cache to load.
528 static void pine_read_file( PineFile *pineFile, AddressCache *cache ) {
529 GSList *listValue = NULL;
530 gboolean flagEOF = FALSE, flagProc = FALSE, flagDone = FALSE;
531 gchar *line = NULL, *lineValue = NULL;
535 /* Find EOF for progress indicator */
536 fseek( pineFile->file, 0L, SEEK_END );
537 posEnd = ftell( pineFile->file );
538 fseek( pineFile->file, 0L, SEEK_SET );
541 while( ! flagDone ) {
547 line = pine_read_line( pineFile );
550 posCur = ftell( pineFile->file );
551 if( pineFile->cbProgress ) {
552 /* Call progress indicator */
553 ( pineFile->cbProgress ) ( pineFile, & posEnd, & posCur );
556 /* Add line to list */
561 /* Check for continuation line (1 space only) */
564 listValue = g_slist_append(
565 listValue, g_strdup( line ) );
574 if( listValue != NULL ) {
576 lineValue = mgu_list_coalesce( listValue );
579 pineFile, cache, lineValue );
583 mgu_free_list( listValue );
588 listValue = g_slist_append(
589 listValue, g_strdup( line ) );
598 mgu_free_list( listValue );
603 * ============================================================================================
604 * Read file into list. Main entry point
605 * Enter: pineFile Pine control data.
606 * cache Address cache to load.
607 * Return: Status code.
608 * ============================================================================================
610 gint pine_import_data( PineFile *pineFile, AddressCache *cache ) {
611 g_return_val_if_fail( pineFile != NULL, MGU_BAD_ARGS );
612 g_return_val_if_fail( cache != NULL, MGU_BAD_ARGS );
614 pineFile->retVal = MGU_SUCCESS;
615 addrcache_clear( cache );
616 cache->dataRead = FALSE;
617 pine_open_file( pineFile );
618 if( pineFile->retVal == MGU_SUCCESS ) {
619 /* Read data into the cache */
620 pine_read_file( pineFile, cache );
621 pine_close_file( pineFile );
624 cache->modified = FALSE;
625 cache->dataRead = TRUE;
627 return pineFile->retVal;
630 #define WORK_BUFLEN 1024
633 * Attempt to find a Pine addressbook file.
634 * Return: Filename, or home directory if not found, or empty string if
635 * no home. Filename should be g_free() when done.
637 gchar *pine_find_file( void ) {
638 const gchar *homedir;
639 gchar str[ WORK_BUFLEN ];
643 homedir = get_home_dir();
644 if( ! homedir ) return g_strdup( "" );
646 strcpy( str, homedir );
649 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
650 str[ len ] = G_DIR_SEPARATOR;
654 strcat( str, PINE_HOME_FILE );
656 /* Attempt to open */
657 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
661 /* Truncate filename */
664 return g_strdup( str );