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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Functions necessary to access MUTT address book file.
30 #include "addrcache.h"
32 #define MUTT_HOME_FILE ".muttrc"
33 #define MUTTBUFSIZE 2048
34 #define MUTT_TAG_ALIAS "alias"
39 MuttFile *mutt_create() {
41 muttFile = g_new0( MuttFile, 1 );
42 muttFile->path = NULL;
43 muttFile->file = NULL;
44 muttFile->retVal = MGU_SUCCESS;
45 muttFile->uniqTable = g_hash_table_new( g_str_hash, g_str_equal );
46 muttFile->cbProgress = NULL;
53 void mutt_set_file( MuttFile* muttFile, const gchar *value ) {
54 g_return_if_fail( muttFile != NULL );
55 muttFile->path = mgu_replace_string( muttFile->path, value );
56 g_strstrip( muttFile->path );
60 * Register a callback function. When called, the function will be passed
61 * the following arguments:
64 * Current position (long)
65 * This can be used for a progress indicator.
67 void mutt_set_callback( MuttFile *muttFile, void *func ) {
68 muttFile->cbProgress = func;
74 static gint mutt_free_table_vis( gpointer key, gpointer value, gpointer data ) {
82 * Free up object by releasing internal memory.
84 void mutt_free( MuttFile *muttFile ) {
85 g_return_if_fail( muttFile != NULL );
88 if( muttFile->file ) fclose( muttFile->file );
90 /* Free internal stuff */
91 g_free( muttFile->path );
93 /* Free unique address table */
94 g_hash_table_freeze( muttFile->uniqTable );
95 g_hash_table_foreach_remove( muttFile->uniqTable, mutt_free_table_vis, NULL );
96 g_hash_table_thaw( muttFile->uniqTable );
97 g_hash_table_destroy( muttFile->uniqTable );
100 muttFile->file = NULL;
101 muttFile->path = NULL;
102 muttFile->retVal = MGU_SUCCESS;
103 muttFile->uniqTable = NULL;
104 muttFile->cbProgress = NULL;
106 /* Now release file object */
111 * Display object to specified stream.
113 void mutt_print_file( MuttFile *muttFile, FILE *stream ) {
114 g_return_if_fail( muttFile != NULL );
115 fprintf( stream, "MUTT File:\n" );
116 fprintf( stream, "file spec: '%s'\n", muttFile->path );
117 fprintf( stream, " ret val: %d\n", muttFile->retVal );
121 * Open file for read.
122 * return: TRUE if file opened successfully.
124 static gint mutt_open_file( MuttFile* muttFile ) {
125 if( muttFile->path ) {
126 muttFile->file = fopen( muttFile->path, "rb" );
127 if( ! muttFile->file ) {
128 muttFile->retVal = MGU_OPEN_FILE;
129 return muttFile->retVal;
133 /* printf( "file not specified\n" ); */
134 muttFile->retVal = MGU_NO_FILE;
135 return muttFile->retVal;
138 /* Setup a buffer area */
139 muttFile->retVal = MGU_SUCCESS;
140 return muttFile->retVal;
146 static void mutt_close_file( MuttFile *muttFile ) {
147 g_return_if_fail( muttFile != NULL );
148 if( muttFile->file ) fclose( muttFile->file );
149 muttFile->file = NULL;
153 * Read line of text from file.
154 * Enter: muttFile File object.
155 * flagCont Continuation flag, set if back-slash character at EOL.
156 * Return: ptr to buffer where line starts.
158 static gchar *mutt_get_line( MuttFile *muttFile, gboolean *flagCont ) {
159 gchar buf[ MUTTBUFSIZE ];
164 if( feof( muttFile->file ) ) return NULL;
171 ch = fgetc( muttFile->file );
172 if( ch == '\0' || ch == EOF ) {
173 if( *buf == '\0' ) return NULL;
178 /* Replace backslash with NULL */
179 if( lptr ) *lptr = '\0';
190 /* Copy into private buffer */
191 return g_strdup( buf );
195 * Parsed address data.
197 typedef struct _Mutt_ParsedRec_ Mutt_ParsedRec;
198 struct _Mutt_ParsedRec_ {
205 * Enter: rec Data record.
207 static mutt_free_rec( Mutt_ParsedRec *rec ) {
209 g_free( rec->address );
219 * Enter: rec Data record.
221 */void mutt_print_rec( Mutt_ParsedRec *rec, FILE *stream ) {
222 fprintf( stream, "\taddr: %s\tname: %s\n", rec->address, rec->name );
226 * Parse recipient list for each address.
227 * Enter: rcpList Recipients extracted from file.
228 * addrCount Updated with recipient count.
229 * Return: Linked list of recipients.
231 static GSList *mutt_parse_rcplist( gchar *rcpList, gint *addrCount ) {
232 gchar *ptr, *pStart, *pEnd, *pAddr, *pName, *address, *name;
243 address = name = NULL;
244 pName = pAddr = NULL;
247 if( ! isspace( *ptr ) ) break;
263 if( isspace( ch ) ) {
270 /* Extract address */
272 address = g_strndup( pStart, pAddr - pStart );
275 address = g_strdup( pStart );
277 g_strstrip( address );
291 if( isspace( ch ) ) continue;
295 /* Extract name (if any) */
297 /* Look for closing parens */
309 name = g_strndup( pName, pEnd - pName );
314 name = g_strdup( pName );
320 name = g_strdup( "" );
324 rec = g_new0( Mutt_ParsedRec, 1 );
325 rec->address = address;
327 list = g_slist_append( list, rec );
330 /* mutt_print_rec( rec, stdout ); */
337 * Insert person and address into address cache.
338 * Enter: muttFile MUTT control data.
339 * cache Address cache.
340 * address E-Mail address.
342 * Return: E-Mail object, either inserted or found in hash table.
344 static ItemEMail *mutt_insert_table(
345 MuttFile *muttFile, AddressCache *cache, gchar *address,
352 /* Test whether address already in hash table */
353 key = g_strdup( address );
355 email = g_hash_table_lookup( muttFile->uniqTable, key );
357 if( email == NULL ) {
358 /* No - create person */
359 person = addritem_create_item_person();
360 addritem_person_set_common_name( person, name );
361 addrcache_id_person( cache, person );
362 addrcache_add_person( cache, person );
364 /* Add email for person */
365 email = addritem_create_item_email();
366 addritem_email_set_address( email, address );
367 addrcache_id_email( cache, email );
368 addrcache_person_add_email( cache, person, email );
371 g_hash_table_insert( muttFile->uniqTable, key, email );
374 /* Yes - update person with longest name */
375 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
376 if( strlen( name ) > strlen( ADDRITEM_NAME(person) ) ) {
377 addritem_person_set_common_name( person, name );
388 * Build address book entries.
389 * Enter: muttFile MUTT control data.
390 * cache Address cache.
392 * listAddr List of address items.
393 * addrCount Address list count.
395 static void mutt_build_address(
396 MuttFile *muttFile, AddressCache *cache,
397 gchar *aliasName, GSList *listAddr, gint addrCount )
405 if( listAddr != NULL && addrCount > 1 ) {
406 group = addritem_create_item_group();
407 addritem_group_set_name( group, aliasName );
408 addrcache_id_group( cache, group );
409 addrcache_add_group( cache, group );
417 /* Insert person/email */
418 email = mutt_insert_table(
419 muttFile, cache, rec->address, rec->name );
421 /* Add email to group */
423 addritem_group_add_email( group, email );
426 mutt_free_rec( rec );
427 node = g_slist_next( node );
432 * Parse address line adn build address items.
433 * Enter: muttFile MUTT control data.
434 * cache Address cache.
437 static void mutt_build_items( MuttFile *muttFile, AddressCache *cache, gchar *line ) {
440 gchar *aliasTag, *aliasName, *recipient;
443 /* printf( "\nBUILD >%s<\n", line ); */
444 list = mgu_parse_string( line, 3, &tCount );
447 mgu_free_dlist( list );
453 aliasTag = list->data;
454 node = g_list_next( list );
455 aliasName = node->data;
456 node = g_list_next( node );
457 recipient = node->data;
460 if( strcmp( aliasTag, MUTT_TAG_ALIAS ) == 0 ) {
462 /* printf( "aliasName :%s:\n", aliasName ); */
463 /* printf( "recipient :%s:\n", recipient ); */
464 addrList = mutt_parse_rcplist( recipient, &aCount );
465 /* printf( "---\n" ); */
466 mutt_build_address( muttFile, cache, aliasName, addrList, aCount );
469 mgu_free_dlist( list );
475 * Read file data into address cache.
476 * Enter: muttFile MUTT control data.
477 * cache Address cache.
479 static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
480 GSList *listValue = NULL;
481 gboolean flagEOF = FALSE, flagCont = FALSE, lastCont = FALSE;
482 gchar *line = NULL, *lineValue = NULL;
486 /* Find EOF for progress indicator */
487 fseek( muttFile->file, 0L, SEEK_END );
488 posEnd = ftell( muttFile->file );
489 fseek( muttFile->file, 0L, SEEK_SET );
493 line = mutt_get_line( muttFile, &flagCont );
495 posCur = ftell( muttFile->file );
496 if( muttFile->cbProgress ) {
497 /* Call progress indicator */
498 ( muttFile->cbProgress ) ( muttFile, & posEnd, & posCur );
501 if( line == NULL ) flagEOF = TRUE;
504 lineValue = mgu_list_coalesce( listValue );
506 mutt_build_items( muttFile, cache, lineValue );
510 mgu_free_list( listValue );
515 /* Add line to list */
516 listValue = g_slist_append( listValue, g_strdup( line ) );
523 mgu_free_list( listValue );
528 * ============================================================================================
529 * Read file into list. Main entry point
530 * Enter: muttFile MUTT control data.
531 * cache Address cache to load.
532 * Return: Status code.
533 * ============================================================================================
535 gint mutt_import_data( MuttFile *muttFile, AddressCache *cache ) {
536 g_return_val_if_fail( muttFile != NULL, MGU_BAD_ARGS );
537 g_return_val_if_fail( cache != NULL, MGU_BAD_ARGS );
538 muttFile->retVal = MGU_SUCCESS;
539 addrcache_clear( cache );
540 cache->dataRead = FALSE;
541 mutt_open_file( muttFile );
542 if( muttFile->retVal == MGU_SUCCESS ) {
543 /* Read data into the cache */
544 mutt_read_file( muttFile, cache );
545 mutt_close_file( muttFile );
548 cache->modified = FALSE;
549 cache->dataRead = TRUE;
551 return muttFile->retVal;
554 #define WORK_BUFLEN 1024
557 * Attempt to find a Mutt file.
558 * Return: Filename, or home directory if not found, or empty string if
559 * no home. Filename should be g_free() when done.
561 gchar *mutt_find_file( void ) {
563 gchar str[ WORK_BUFLEN ];
567 homedir = g_get_home_dir();
568 if( ! homedir ) return g_strdup( "" );
570 strcpy( str, homedir );
573 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
574 str[ len ] = G_DIR_SEPARATOR;
578 strcat( str, MUTT_HOME_FILE );
580 /* Attempt to open */
581 if( ( fp = fopen( str, "rb" ) ) != NULL ) {
585 /* Truncate filename */
588 return g_strdup( str );