2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2012 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 cm_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
) {
73 * Free up object by releasing internal memory.
75 void pine_free( PineFile
*pineFile
) {
76 cm_return_if_fail( pineFile
!= NULL
);
79 if( pineFile
->file
) fclose( pineFile
->file
);
81 /* Free internal stuff */
82 g_free( pineFile
->path
);
84 /* Free unique address table */
85 g_hash_table_foreach_remove( pineFile
->uniqTable
, pine_free_table_vis
, NULL
);
86 g_hash_table_destroy( pineFile
->uniqTable
);
89 pineFile
->file
= NULL
;
90 pineFile
->path
= NULL
;
91 pineFile
->retVal
= MGU_SUCCESS
;
92 pineFile
->uniqTable
= NULL
;
93 pineFile
->cbProgress
= NULL
;
95 /* Now release file object */
100 * Open file for read.
101 * Enter: pineFile File object.
102 * return: TRUE if file opened successfully.
104 static gint
pine_open_file( PineFile
* pineFile
) {
105 if( pineFile
->path
) {
106 pineFile
->file
= g_fopen( pineFile
->path
, "rb" );
107 if( ! pineFile
->file
) {
108 pineFile
->retVal
= MGU_OPEN_FILE
;
109 return pineFile
->retVal
;
113 /* g_print( "file not specified\n" ); */
114 pineFile
->retVal
= MGU_NO_FILE
;
115 return pineFile
->retVal
;
118 /* Setup a buffer area */
119 pineFile
->retVal
= MGU_SUCCESS
;
120 return pineFile
->retVal
;
125 * Enter: pineFile File object.
127 static void pine_close_file( PineFile
*pineFile
) {
128 cm_return_if_fail( pineFile
!= NULL
);
129 if( pineFile
->file
) fclose( pineFile
->file
);
130 pineFile
->file
= NULL
;
134 * Read line of text from file.
135 * Enter: pineFile File object.
136 * Return: Copy of buffer. Should be g_free'd when done.
138 static gchar
*pine_read_line( PineFile
*pineFile
) {
139 gchar buf
[ PINEBUFSIZE
];
143 if( feof( pineFile
->file
) )
146 while( i
< PINEBUFSIZE
-1 ) {
147 c
= fgetc( pineFile
->file
);
167 /* Copy into private buffer */
168 return g_strdup( buf
);
172 * Parsed address data.
174 typedef struct _Pine_ParsedRec_ Pine_ParsedRec
;
175 struct _Pine_ParsedRec_
{
188 * Enter: rec Data record.
190 static void pine_free_rec( Pine_ParsedRec
*rec
) {
192 g_free( rec
->nickName
);
194 g_free( rec
->address
);
196 g_free( rec
->comments
);
197 mgu_clear_slist( rec
->listName
);
198 mgu_clear_slist( rec
->listAddr
);
199 g_slist_free( rec
->listName
);
200 g_slist_free( rec
->listAddr
);
201 rec
->nickName
= NULL
;
205 rec
->comments
= NULL
;
206 rec
->isGroup
= FALSE
;
214 static void pine_clean_name( Pine_ParsedRec
*rec
) {
218 if( p
== NULL
) return;
219 if( *p
== '\0' ) return;
221 g_strstrip( rec
->name
);
222 if( *p
== CHAR_APOS
|| *p
== CHAR_QUOTE
) {
226 /* If embedded comma present, surround match with quotes */
228 if( *p
== CHAR_COMMA
) {
229 p
= g_strdup_printf( "\"%s\"", rec
->name
);
239 * Parse pine address record.
240 * Enter: buf Address record buffer.
241 * Return: Data record.
243 static Pine_ParsedRec
*pine_parse_record( gchar
*buf
) {
249 for( i
= 0; i
< 5; i
++ )
252 /* Extract tab separated values */
260 tmp
[ pos
] = g_strndup( f
, len
);
269 /* Extract last value */
272 tmp
[ pos
++ ] = g_strndup( f
, len
);
275 /* Populate record */
277 rec
= g_new0( Pine_ParsedRec
, 1 );
278 rec
->isGroup
= FALSE
;
279 for( i
= 0; i
< pos
; i
++ ) {
284 if( i
== 0 ) rec
->nickName
= f
;
285 else if( i
== 1 ) rec
->name
= f
;
286 else if( i
== 2 ) rec
->address
= f
;
287 else if( i
== 3 ) rec
->fcc
= f
;
288 else if( i
== 4 ) rec
->comments
= f
;
292 if( rec
->address
!= NULL
) {
293 /* Strip leading/trailing parens */
296 len
= strlen( p
) - 1;
308 * Parse name from email address string.
309 * Enter: buf Start address of buffer to process (not modified).
310 * atp Pointer to email at (@) character.
311 * ap Pointer to start of email address returned.
312 * ep Pointer to end of email address returned.
313 * Return: Parsed name or NULL if not present. This should be g_free'd
316 static gchar
*pine_parse_name(
317 const gchar
*buf
, const gchar
*atp
, const gchar
**ap
,
330 /* Find first non-separator char */
333 if( strchr( ",; \n\r", *bp
) == NULL
) break;
337 /* Search back for start of name */
343 /* Found start of address/end of name part */
344 ilen
= -1 + ( size_t ) ( pos
- bp
);
345 name
= g_strndup( bp
, ilen
+ 1 );
346 *(name
+ ilen
+ 1) = '\0';
348 /* Remove leading trailing quotes and spaces */
349 mgu_str_ltc2space( name
, '\"', '\"' );
350 mgu_str_ltc2space( name
, '\'', '\'' );
351 mgu_str_ltc2space( name
, '\"', '\"' );
352 mgu_str_unescape( name
);
360 /* Search forward for end of address */
367 if( strchr( ",; \'\n\r", *pos
) ) break;
376 * Parse address list.
377 * Enter: pineFile Pine control data.
378 * cache Address cache.
381 static void pine_parse_address( PineFile
*pineFile
, AddressCache
*cache
, Pine_ParsedRec
*rec
) {
383 gchar addr
[ PINEBUFSIZE
];
390 cm_return_if_fail( rec
->address
!= NULL
);
393 while((atCh
= strchr( buf
, CHAR_AT
)) != NULL
) {
394 name
= pine_parse_name( buf
, atCh
, &bp
, &ep
);
395 len
= ( size_t ) ( ep
- bp
);
396 strncpy( addr
, bp
, len
);
398 extract_address( addr
);
400 if( name
== NULL
) name
= g_strdup( "" );
401 rec
->listName
= g_slist_append( rec
->listName
, name
);
402 rec
->listAddr
= g_slist_append( rec
->listAddr
, g_strdup( addr
) );
412 * Insert person and address into address cache.
413 * Enter: pineFile Pine control data.
414 * cache Address cache.
415 * address E-Mail address.
418 * Return: E-Mail object, either inserted or found in hash table.
420 static ItemEMail
*pine_insert_table(
421 PineFile
*pineFile
, AddressCache
*cache
, gchar
*address
,
422 gchar
*name
, gchar
*remarks
)
428 cm_return_val_if_fail( address
!= NULL
, NULL
);
430 /* create an entry with empty name if needed */
434 /* Test whether address already in hash table */
435 key
= g_utf8_strdown( address
, -1 );
436 email
= g_hash_table_lookup( pineFile
->uniqTable
, key
);
438 if( email
== NULL
) {
439 /* No - create person */
440 person
= addritem_create_item_person();
441 addritem_person_set_common_name( person
, name
);
442 addrcache_id_person( cache
, person
);
443 addrcache_add_person( cache
, person
);
445 /* Add email for person */
446 email
= addritem_create_item_email();
447 addritem_email_set_address( email
, address
);
448 addritem_email_set_remarks( email
, remarks
);
449 addrcache_id_email( cache
, email
);
450 addrcache_person_add_email( cache
, person
, email
);
453 g_hash_table_insert( pineFile
->uniqTable
, key
, email
);
456 /* Yes - update person with longest name */
457 person
= ( ItemPerson
* ) ADDRITEM_PARENT(email
);
458 if( strlen( name
) > strlen( ADDRITEM_NAME(person
) ) ) {
459 addritem_person_set_common_name( person
, name
);
470 * Parse address line adn build address items.
471 * Enter: pineFile Pine control data.
472 * cache Address cache to load.
473 * line Address record.
475 static void pine_build_items( PineFile
*pineFile
, AddressCache
*cache
, gchar
*line
) {
477 GSList
*nodeAddr
, *nodeName
;
481 rec
= pine_parse_record( line
);
483 pine_clean_name( rec
);
484 pine_parse_address( pineFile
, cache
, rec
);
485 /* pine_print_rec( rec, stdout ); */
486 /* g_print( "=========\n" ); */
490 group
= addritem_create_item_group();
491 addritem_group_set_name( group
, rec
->nickName
);
492 addrcache_id_group( cache
, group
);
493 addrcache_add_group( cache
, group
);
495 /* Add email to group */
496 nodeName
= rec
->listName
;
497 nodeAddr
= rec
->listAddr
;
499 email
= pine_insert_table(
500 pineFile
, cache
, nodeAddr
->data
,
501 nodeName
->data
, "" );
503 /* Add email to group */
504 addritem_group_add_email( group
, email
);
506 nodeAddr
= g_slist_next( nodeAddr
);
507 nodeName
= g_slist_next( nodeName
);
512 pineFile
, cache
, rec
->address
,
513 rec
->name
, rec
->comments
);
516 pine_free_rec( rec
);
521 * Read file data into address cache.
522 * Enter: pineFile Pine control data.
523 * cache Address cache to load.
525 static void pine_read_file( PineFile
*pineFile
, AddressCache
*cache
) {
526 GSList
*listValue
= NULL
;
527 gboolean flagEOF
= FALSE
, flagProc
= FALSE
, flagDone
= FALSE
;
528 gchar
*line
= NULL
, *lineValue
= NULL
;
532 /* Find EOF for progress indicator */
533 fseek( pineFile
->file
, 0L, SEEK_END
);
534 posEnd
= ftell( pineFile
->file
);
535 fseek( pineFile
->file
, 0L, SEEK_SET
);
538 while( ! flagDone
) {
544 line
= pine_read_line( pineFile
);
547 posCur
= ftell( pineFile
->file
);
548 if( pineFile
->cbProgress
) {
549 /* Call progress indicator */
550 ( pineFile
->cbProgress
) ( pineFile
, & posEnd
, & posCur
);
553 /* Add line to list */
558 /* Check for continuation line (1 space only) */
561 listValue
= g_slist_append(
562 listValue
, g_strdup( line
) );
571 if( listValue
!= NULL
) {
573 lineValue
= mgu_list_coalesce( listValue
);
576 pineFile
, cache
, lineValue
);
580 mgu_free_list( listValue
);
585 listValue
= g_slist_append(
586 listValue
, g_strdup( line
) );
595 mgu_free_list( listValue
);
600 * ============================================================================================
601 * Read file into list. Main entry point
602 * Enter: pineFile Pine control data.
603 * cache Address cache to load.
604 * Return: Status code.
605 * ============================================================================================
607 gint
pine_import_data( PineFile
*pineFile
, AddressCache
*cache
) {
608 cm_return_val_if_fail( pineFile
!= NULL
, MGU_BAD_ARGS
);
609 cm_return_val_if_fail( cache
!= NULL
, MGU_BAD_ARGS
);
611 pineFile
->retVal
= MGU_SUCCESS
;
612 addrcache_clear( cache
);
613 cache
->dataRead
= FALSE
;
614 pine_open_file( pineFile
);
615 if( pineFile
->retVal
== MGU_SUCCESS
) {
616 /* Read data into the cache */
617 pine_read_file( pineFile
, cache
);
618 pine_close_file( pineFile
);
621 cache
->modified
= FALSE
;
622 cache
->dataRead
= TRUE
;
624 return pineFile
->retVal
;
627 #define WORK_BUFLEN 1024
630 * Attempt to find a Pine addressbook file.
631 * Return: Filename, or home directory if not found, or empty string if
632 * no home. Filename should be g_free() when done.
634 gchar
*pine_find_file( void ) {
635 const gchar
*homedir
;
636 gchar str
[ WORK_BUFLEN
+ 1 ];
640 homedir
= get_home_dir();
641 if( ! homedir
) return g_strdup( "" );
643 strncpy( str
, homedir
, WORK_BUFLEN
);
646 if( str
[ len
-1 ] != G_DIR_SEPARATOR
) {
647 str
[ len
] = G_DIR_SEPARATOR
;
651 strncat( str
, PINE_HOME_FILE
, WORK_BUFLEN
- strlen(str
) );
653 /* Attempt to open */
654 if( ( fp
= g_fopen( str
, "rb" ) ) != NULL
) {
658 /* Truncate filename */
661 return g_strdup( str
);