2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Functions necessary to access MUTT address book file.
33 #include "addrcache.h"
35 #define MUTT_HOME_FILE ".muttrc"
36 #define MUTTBUFSIZE 2048
37 #define MUTT_TAG_ALIAS "alias"
42 MuttFile *mutt_create() {
44 muttFile = g_new0( MuttFile, 1 );
45 muttFile->path = NULL;
46 muttFile->file = NULL;
47 muttFile->retVal = MGU_SUCCESS;
48 muttFile->uniqTable = g_hash_table_new( g_str_hash, g_str_equal );
49 muttFile->cbProgress = NULL;
56 void mutt_set_file( MuttFile* muttFile, const gchar *value ) {
57 g_return_if_fail( muttFile != NULL );
58 muttFile->path = mgu_replace_string( muttFile->path, value );
59 g_strstrip( muttFile->path );
63 * Register a callback function. When called, the function will be passed
64 * the following arguments:
67 * Current position (long)
68 * This can be used for a progress indicator.
70 void mutt_set_callback( MuttFile *muttFile, void *func ) {
71 muttFile->cbProgress = func;
77 static gint mutt_free_table_vis( gpointer key, gpointer value, gpointer data ) {
85 * Free up object by releasing internal memory.
87 void mutt_free( MuttFile *muttFile ) {
88 g_return_if_fail( muttFile != NULL );
91 if( muttFile->file ) fclose( muttFile->file );
93 /* Free internal stuff */
94 g_free( muttFile->path );
96 /* Free unique address table */
97 g_hash_table_foreach_remove( muttFile->uniqTable, mutt_free_table_vis, NULL );
98 g_hash_table_destroy( muttFile->uniqTable );
101 muttFile->file = NULL;
102 muttFile->path = NULL;
103 muttFile->retVal = MGU_SUCCESS;
104 muttFile->uniqTable = NULL;
105 muttFile->cbProgress = NULL;
107 /* Now release file object */
112 * Display object to specified stream.
114 void mutt_print_file( MuttFile *muttFile, FILE *stream ) {
115 g_return_if_fail( muttFile != NULL );
116 fprintf( stream, "MUTT File:\n" );
117 fprintf( stream, "file spec: '%s'\n", muttFile->path );
118 fprintf( stream, " ret val: %d\n", muttFile->retVal );
122 * Open file for read.
123 * return: TRUE if file opened successfully.
125 static gint mutt_open_file( MuttFile* muttFile ) {
126 if( muttFile->path ) {
127 muttFile->file = g_fopen( muttFile->path, "rb" );
128 if( ! muttFile->file ) {
129 muttFile->retVal = MGU_OPEN_FILE;
130 return muttFile->retVal;
134 /* printf( "file not specified\n" ); */
135 muttFile->retVal = MGU_NO_FILE;
136 return muttFile->retVal;
139 /* Setup a buffer area */
140 muttFile->retVal = MGU_SUCCESS;
141 return muttFile->retVal;
147 static void mutt_close_file( MuttFile *muttFile ) {
148 g_return_if_fail( muttFile != NULL );
149 if( muttFile->file ) fclose( muttFile->file );
150 muttFile->file = NULL;
154 * Read line of text from file.
155 * Enter: muttFile File object.
156 * flagCont Continuation flag, set if back-slash character at EOL.
157 * Return: ptr to buffer where line starts.
159 static gchar *mutt_get_line( MuttFile *muttFile, gboolean *flagCont ) {
160 gchar buf[ MUTTBUFSIZE ];
165 if( feof( muttFile->file ) ) return NULL;
172 ch = fgetc( muttFile->file );
173 if( ch == '\0' || ch == EOF ) {
174 if( *buf == '\0' ) return NULL;
179 /* Replace backslash with NULL */
180 if( lptr ) *lptr = '\0';
191 /* Copy into private buffer */
192 return g_strdup( buf );
196 * Parsed address data.
198 typedef struct _Mutt_ParsedRec_ Mutt_ParsedRec;
199 struct _Mutt_ParsedRec_ {
206 * Enter: rec Data record.
208 static void mutt_free_rec( Mutt_ParsedRec *rec ) {
210 g_free( rec->address );
220 * Enter: rec Data record.
222 */void mutt_print_rec( Mutt_ParsedRec *rec, FILE *stream ) {
223 fprintf( stream, "\taddr: %s\tname: %s\n", rec->address, rec->name );
227 * Parse recipient list for each address.
228 * Enter: rcpList Recipients extracted from file.
229 * addrCount Updated with recipient count.
230 * Return: Linked list of recipients.
232 static GSList *mutt_parse_rcplist( gchar *rcpList, gint *addrCount ) {
233 gchar *ptr, *pStart, *pEnd, *pAddr, *pName, *address, *name;
244 address = name = NULL;
245 pName = pAddr = NULL;
248 if( ! isspace( *ptr ) ) break;
264 if( isspace( ch ) ) {
271 /* Extract address */
273 address = g_strndup( pStart, pAddr - pStart );
276 address = g_strdup( pStart );
278 g_strstrip( address );
292 if( isspace( ch ) ) continue;
296 /* Extract name (if any) */
298 /* Look for closing parens */
310 name = g_strndup( pName, pEnd - pName );
315 name = g_strdup( pName );
321 name = g_strdup( "" );
325 rec = g_new0( Mutt_ParsedRec, 1 );
326 rec->address = address;
328 list = g_slist_append( list, rec );
331 /* mutt_print_rec( rec, stdout ); */
338 * Insert person and address into address cache.
339 * Enter: muttFile MUTT control data.
340 * cache Address cache.
341 * address E-Mail address.
343 * Return: E-Mail object, either inserted or found in hash table.
345 static ItemEMail *mutt_insert_table(
346 MuttFile *muttFile, AddressCache *cache, gchar *address,
353 /* Test whether address already in hash table */
354 key = g_strdup( address );
356 email = g_hash_table_lookup( muttFile->uniqTable, key );
358 if( email == NULL ) {
359 /* No - create person */
360 person = addritem_create_item_person();
361 addritem_person_set_common_name( person, name );
362 addrcache_id_person( cache, person );
363 addrcache_add_person( cache, person );
365 /* Add email for person */
366 email = addritem_create_item_email();
367 addritem_email_set_address( email, address );
368 addrcache_id_email( cache, email );
369 addrcache_person_add_email( cache, person, email );
372 g_hash_table_insert( muttFile->uniqTable, key, email );
375 /* Yes - update person with longest name */
376 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
377 if( strlen( name ) > strlen( ADDRITEM_NAME(person) ) ) {
378 addritem_person_set_common_name( person, name );
389 * Build address book entries.
390 * Enter: muttFile MUTT control data.
391 * cache Address cache.
393 * listAddr List of address items.
394 * addrCount Address list count.
396 static void mutt_build_address(
397 MuttFile *muttFile, AddressCache *cache,
398 gchar *aliasName, GSList *listAddr, gint addrCount )
406 if( listAddr != NULL && addrCount > 1 ) {
407 group = addritem_create_item_group();
408 addritem_group_set_name( group, aliasName );
409 addrcache_id_group( cache, group );
410 addrcache_add_group( cache, group );
418 /* Insert person/email */
419 email = mutt_insert_table(
420 muttFile, cache, rec->address, rec->name );
422 /* Add email to group */
424 addritem_group_add_email( group, email );
427 mutt_free_rec( rec );
428 node = g_slist_next( node );
433 * Parse address line adn build address items.
434 * Enter: muttFile MUTT control data.
435 * cache Address cache.
438 static void mutt_build_items( MuttFile *muttFile, AddressCache *cache, gchar *line ) {
441 gchar *aliasTag, *aliasName, *recipient;
444 /* printf( "\nBUILD >%s<\n", line ); */
445 list = mgu_parse_string( line, 3, &tCount );
448 mgu_free_dlist( list );
454 aliasTag = list->data;
455 node = g_list_next( list );
456 aliasName = node->data;
457 node = g_list_next( node );
458 recipient = node->data;
461 if( strcmp( aliasTag, MUTT_TAG_ALIAS ) == 0 ) {
463 /* printf( "aliasName :%s:\n", aliasName ); */
464 /* printf( "recipient :%s:\n", recipient ); */
465 addrList = mutt_parse_rcplist( recipient, &aCount );
466 /* printf( "---\n" ); */
467 mutt_build_address( muttFile, cache, aliasName, addrList, aCount );
470 mgu_free_dlist( list );
476 * Read file data into address cache.
477 * Enter: muttFile MUTT control data.
478 * cache Address cache.
480 static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
481 GSList *listValue = NULL;
482 gboolean flagEOF = FALSE, flagCont = FALSE, lastCont = FALSE;
483 gchar *line = NULL, *lineValue = NULL;
487 /* Find EOF for progress indicator */
488 fseek( muttFile->file, 0L, SEEK_END );
489 posEnd = ftell( muttFile->file );
490 fseek( muttFile->file, 0L, SEEK_SET );
494 line = mutt_get_line( muttFile, &flagCont );
496 posCur = ftell( muttFile->file );
497 if( muttFile->cbProgress ) {
498 /* Call progress indicator */
499 ( muttFile->cbProgress ) ( muttFile, & posEnd, & posCur );
502 if( line == NULL ) flagEOF = TRUE;
505 lineValue = mgu_list_coalesce( listValue );
507 mutt_build_items( muttFile, cache, lineValue );
511 mgu_free_list( listValue );
516 /* Add line to list */
517 listValue = g_slist_append( listValue, g_strdup( line ) );
524 mgu_free_list( listValue );
529 * ============================================================================================
530 * Read file into list. Main entry point
531 * Enter: muttFile MUTT control data.
532 * cache Address cache to load.
533 * Return: Status code.
534 * ============================================================================================
536 gint mutt_import_data( MuttFile *muttFile, AddressCache *cache ) {
537 g_return_val_if_fail( muttFile != NULL, MGU_BAD_ARGS );
538 g_return_val_if_fail( cache != NULL, MGU_BAD_ARGS );
539 muttFile->retVal = MGU_SUCCESS;
540 addrcache_clear( cache );
541 cache->dataRead = FALSE;
542 mutt_open_file( muttFile );
543 if( muttFile->retVal == MGU_SUCCESS ) {
544 /* Read data into the cache */
545 mutt_read_file( muttFile, cache );
546 mutt_close_file( muttFile );
549 cache->modified = FALSE;
550 cache->dataRead = TRUE;
552 return muttFile->retVal;
555 #define WORK_BUFLEN 1024
558 * Attempt to find a Mutt file.
559 * Return: Filename, or home directory if not found, or empty string if
560 * no home. Filename should be g_free() when done.
562 gchar *mutt_find_file( void ) {
563 const gchar *homedir;
564 gchar str[ WORK_BUFLEN ];
568 homedir = g_get_home_dir();
569 if( ! homedir ) return g_strdup( "" );
571 strcpy( str, homedir );
574 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
575 str[ len ] = G_DIR_SEPARATOR;
579 strcat( str, MUTT_HOME_FILE );
581 /* Attempt to open */
582 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
586 /* Truncate filename */
589 return g_strdup( str );