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 ) )
168 memset(buf, 0, MUTTBUFSIZE);
171 while( i < MUTTBUFSIZE-1 ) {
172 ch = fgetc( muttFile->file );
173 if( ch == '\0' || ch == EOF ) {
180 /* Replace backslash with NULL */
194 /* Copy into private buffer */
195 return g_strdup( buf );
199 * Parsed address data.
201 typedef struct _Mutt_ParsedRec_ Mutt_ParsedRec;
202 struct _Mutt_ParsedRec_ {
209 * Enter: rec Data record.
211 static void mutt_free_rec( Mutt_ParsedRec *rec ) {
213 g_free( rec->address );
223 * Enter: rec Data record.
225 */void mutt_print_rec( Mutt_ParsedRec *rec, FILE *stream ) {
226 fprintf( stream, "\taddr: %s\tname: %s\n", rec->address, rec->name );
230 * Parse recipient list for each address.
231 * Enter: rcpList Recipients extracted from file.
232 * addrCount Updated with recipient count.
233 * Return: Linked list of recipients.
235 static GSList *mutt_parse_rcplist( gchar *rcpList, gint *addrCount ) {
236 gchar *ptr, *pStart, *pEnd, *pAddr, *pName, *address, *name;
247 address = name = NULL;
248 pName = pAddr = NULL;
251 if( ! isspace( *ptr ) ) break;
267 if( isspace( ch ) ) {
274 /* Extract address */
276 address = g_strndup( pStart, pAddr - pStart );
279 address = g_strdup( pStart );
281 g_strstrip( address );
295 if( isspace( ch ) ) continue;
299 /* Extract name (if any) */
301 /* Look for closing parens */
313 name = g_strndup( pName, pEnd - pName );
318 name = g_strdup( pName );
324 name = g_strdup( "" );
328 rec = g_new0( Mutt_ParsedRec, 1 );
329 rec->address = address;
331 list = g_slist_append( list, rec );
334 /* mutt_print_rec( rec, stdout ); */
341 * Insert person and address into address cache.
342 * Enter: muttFile MUTT control data.
343 * cache Address cache.
344 * address E-Mail address.
346 * Return: E-Mail object, either inserted or found in hash table.
348 static ItemEMail *mutt_insert_table(
349 MuttFile *muttFile, AddressCache *cache, gchar *address,
356 /* Test whether address already in hash table */
357 key = g_strdup( address );
359 email = g_hash_table_lookup( muttFile->uniqTable, key );
361 if( email == NULL ) {
362 /* No - create person */
363 person = addritem_create_item_person();
364 addritem_person_set_common_name( person, name );
365 addrcache_id_person( cache, person );
366 addrcache_add_person( cache, person );
368 /* Add email for person */
369 email = addritem_create_item_email();
370 addritem_email_set_address( email, address );
371 addrcache_id_email( cache, email );
372 addrcache_person_add_email( cache, person, email );
375 g_hash_table_insert( muttFile->uniqTable, key, email );
378 /* Yes - update person with longest name */
379 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
380 if( strlen( name ) > strlen( ADDRITEM_NAME(person) ) ) {
381 addritem_person_set_common_name( person, name );
392 * Build address book entries.
393 * Enter: muttFile MUTT control data.
394 * cache Address cache.
396 * listAddr List of address items.
397 * addrCount Address list count.
399 static void mutt_build_address(
400 MuttFile *muttFile, AddressCache *cache,
401 gchar *aliasName, GSList *listAddr, gint addrCount )
409 if( listAddr != NULL && addrCount > 1 ) {
410 group = addritem_create_item_group();
411 addritem_group_set_name( group, aliasName );
412 addrcache_id_group( cache, group );
413 addrcache_add_group( cache, group );
421 /* Insert person/email */
422 email = mutt_insert_table(
423 muttFile, cache, rec->address, rec->name );
425 /* Add email to group */
427 addritem_group_add_email( group, email );
430 mutt_free_rec( rec );
431 node = g_slist_next( node );
436 * Parse address line adn build address items.
437 * Enter: muttFile MUTT control data.
438 * cache Address cache.
441 static void mutt_build_items( MuttFile *muttFile, AddressCache *cache, gchar *line ) {
444 gchar *aliasTag, *aliasName, *recipient;
447 /* printf( "\nBUILD >%s<\n", line ); */
448 list = mgu_parse_string( line, 3, &tCount );
451 mgu_free_dlist( list );
457 aliasTag = list->data;
458 node = g_list_next( list );
459 aliasName = node->data;
460 node = g_list_next( node );
461 recipient = node->data;
464 if( strcmp( aliasTag, MUTT_TAG_ALIAS ) == 0 ) {
466 /* printf( "aliasName :%s:\n", aliasName ); */
467 /* printf( "recipient :%s:\n", recipient ); */
468 addrList = mutt_parse_rcplist( recipient, &aCount );
469 /* printf( "---\n" ); */
470 mutt_build_address( muttFile, cache, aliasName, addrList, aCount );
473 mgu_free_dlist( list );
479 * Read file data into address cache.
480 * Enter: muttFile MUTT control data.
481 * cache Address cache.
483 static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
484 GSList *listValue = NULL;
485 gboolean flagEOF = FALSE, flagCont = FALSE, lastCont = FALSE;
486 gchar *line = NULL, *lineValue = NULL;
490 /* Find EOF for progress indicator */
491 fseek( muttFile->file, 0L, SEEK_END );
492 posEnd = ftell( muttFile->file );
493 fseek( muttFile->file, 0L, SEEK_SET );
497 line = mutt_get_line( muttFile, &flagCont );
499 posCur = ftell( muttFile->file );
500 if( muttFile->cbProgress ) {
501 /* Call progress indicator */
502 ( muttFile->cbProgress ) ( muttFile, & posEnd, & posCur );
505 if( line == NULL ) flagEOF = TRUE;
508 lineValue = mgu_list_coalesce( listValue );
510 mutt_build_items( muttFile, cache, lineValue );
514 mgu_free_list( listValue );
519 /* Add line to list */
520 listValue = g_slist_append( listValue, g_strdup( line ) );
527 mgu_free_list( listValue );
532 * ============================================================================================
533 * Read file into list. Main entry point
534 * Enter: muttFile MUTT control data.
535 * cache Address cache to load.
536 * Return: Status code.
537 * ============================================================================================
539 gint mutt_import_data( MuttFile *muttFile, AddressCache *cache ) {
540 g_return_val_if_fail( muttFile != NULL, MGU_BAD_ARGS );
541 g_return_val_if_fail( cache != NULL, MGU_BAD_ARGS );
542 muttFile->retVal = MGU_SUCCESS;
543 addrcache_clear( cache );
544 cache->dataRead = FALSE;
545 mutt_open_file( muttFile );
546 if( muttFile->retVal == MGU_SUCCESS ) {
547 /* Read data into the cache */
548 mutt_read_file( muttFile, cache );
549 mutt_close_file( muttFile );
552 cache->modified = FALSE;
553 cache->dataRead = TRUE;
555 return muttFile->retVal;
558 #define WORK_BUFLEN 1024
561 * Attempt to find a Mutt file.
562 * Return: Filename, or home directory if not found, or empty string if
563 * no home. Filename should be g_free() when done.
565 gchar *mutt_find_file( void ) {
566 const gchar *homedir;
567 gchar str[ WORK_BUFLEN ];
571 homedir = g_get_home_dir();
572 if( ! homedir ) return g_strdup( "" );
574 strcpy( str, homedir );
577 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
578 str[ len ] = G_DIR_SEPARATOR;
582 strcat( str, MUTT_HOME_FILE );
584 /* Attempt to open */
585 if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
589 /* Truncate filename */
592 return g_strdup( str );