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"
37 MuttFile *mutt_create() {
39 muttFile = g_new0( MuttFile, 1 );
40 muttFile->path = NULL;
41 muttFile->file = NULL;
42 muttFile->bufptr = muttFile->buffer;
43 muttFile->retVal = MGU_SUCCESS;
44 muttFile->cbProgress = NULL;
51 void mutt_set_file( MuttFile* muttFile, const gchar *value ) {
52 g_return_if_fail( muttFile != NULL );
53 muttFile->path = mgu_replace_string( muttFile->path, value );
54 g_strstrip( muttFile->path );
58 * Register a callback function. When called, the function will be passed
59 * the following arguments:
62 * Current position (long)
63 * This can be used for a progress indicator.
65 void mutt_set_callback( MuttFile *muttFile, void *func ) {
66 muttFile->cbProgress = func;
70 * Free up object by releasing internal memory.
72 void mutt_free( MuttFile *muttFile ) {
73 g_return_if_fail( muttFile != NULL );
76 if( muttFile->file ) fclose( muttFile->file );
78 /* Free internal stuff */
79 g_free( muttFile->path );
82 muttFile->file = NULL;
83 muttFile->path = NULL;
84 muttFile->retVal = MGU_SUCCESS;
85 muttFile->cbProgress = NULL;
87 /* Now release file object */
92 * Display object to specified stream.
94 void mutt_print_file( MuttFile *muttFile, FILE *stream ) {
95 g_return_if_fail( muttFile != NULL );
96 fprintf( stream, "MUTT File:\n" );
97 fprintf( stream, "file spec: '%s'\n", muttFile->path );
98 fprintf( stream, " ret val: %d\n", muttFile->retVal );
102 * Open file for read.
103 * return: TRUE if file opened successfully.
105 static gint mutt_open_file( MuttFile* muttFile ) {
106 /* printf( "Opening file\n" ); */
107 if( muttFile->path ) {
108 muttFile->file = fopen( muttFile->path, "r" );
109 if( ! muttFile->file ) {
110 /* printf( "can't open %s\n", muttFile->path ); */
111 muttFile->retVal = MGU_OPEN_FILE;
112 return muttFile->retVal;
116 /* printf( "file not specified\n" ); */
117 muttFile->retVal = MGU_NO_FILE;
118 return muttFile->retVal;
121 /* Setup a buffer area */
122 muttFile->buffer[0] = '\0';
123 muttFile->bufptr = muttFile->buffer;
124 muttFile->retVal = MGU_SUCCESS;
125 return muttFile->retVal;
131 static void mutt_close_file( MuttFile *muttFile ) {
132 g_return_if_fail( muttFile != NULL );
133 if( muttFile->file ) fclose( muttFile->file );
134 muttFile->file = NULL;
138 * Read line of text from file.
139 * Enter: muttFile File object.
140 * flagCont Continuation flag, set if back-slash character at EOL.
141 * Return: ptr to buffer where line starts.
143 static gchar *mutt_get_line( MuttFile *muttFile, gboolean *flagCont ) {
144 gchar buf[ MUTTBUFSIZE ];
149 if( feof( muttFile->file ) ) return NULL;
156 ch = fgetc( muttFile->file );
157 if( ch == '\0' || ch == EOF ) {
158 if( *buf == '\0' ) return NULL;
163 /* Replace backslash with NULL */
164 if( lptr ) *lptr = '\0';
175 /* Copy into private buffer */
176 return g_strdup( buf );
180 * Parsed address data.
182 typedef struct _Mutt_ParsedRec_ Mutt_ParsedRec;
183 struct _Mutt_ParsedRec_ {
188 static mutt_free_rec( Mutt_ParsedRec *rec ) {
190 g_free( rec->address );
198 void mutt_print_rec( Mutt_ParsedRec *rec, FILE *stream ) {
199 fprintf( stream, "\taddr: %s\tname: %s\n", rec->address, rec->name );
203 * Parse recipient list for each address.
204 * Enter: rcpList Recipients extracted from file.
205 * addrCount Updated with recipient count.
206 * Return: Linked list of recipients.
208 static GSList *mutt_parse_rcplist( gchar *rcpList, gint *addrCount ) {
209 gchar *ptr, *pStart, *pEnd, *pAddr, *pName, *address, *name;
220 address = name = NULL;
221 pName = pAddr = NULL;
224 if( ! isspace( *ptr ) ) break;
240 if( isspace( ch ) ) {
247 /* Extract address */
249 address = g_strndup( pStart, pAddr - pStart );
252 address = g_strdup( pStart );
254 g_strstrip( address );
268 if( isspace( ch ) ) continue;
272 /* Extract name (if any) */
274 /* Look for closing parens */
286 name = g_strndup( pName, pEnd - pName );
291 name = g_strdup( pName );
297 name = g_strdup( "" );
301 rec = g_new0( Mutt_ParsedRec, 1 );
302 rec->address = address;
304 list = g_slist_append( list, rec );
308 /* mutt_print_rec( rec, stdout ); */
315 * Build address book entries.
316 * Enter: aliasName Alias,
317 * listAddr List of address items.
318 * addrCount Address list count.
319 * cache Cache to update.
321 static void mutt_build_address(
322 gchar *aliasName, GSList *listAddr, gint addrCount,
323 AddressCache *cache )
333 if( listAddr != NULL && addrCount > 1 ) {
334 group = addritem_create_item_group();
335 addritem_group_set_name( group, aliasName );
336 addrcache_id_group( cache, group );
337 addrcache_add_group( cache, group );
345 person = addritem_create_item_person();
346 addritem_person_set_common_name( person, rec->name );
347 addrcache_id_person( cache, person );
348 addrcache_add_person( cache, person );
349 if( addrCount < 2 ) {
350 addritem_person_set_nick_name( person, aliasName );
353 /* Add email for person */
354 email = addritem_create_item_email();
355 addritem_email_set_address( email, rec->address );
356 addrcache_id_email( cache, email );
357 addrcache_person_add_email( cache, person, email );
359 /* Add email to group */
361 addritem_group_add_email( group, email );
364 mutt_free_rec( rec );
367 node = g_slist_next( node );
372 * Parse address line adn build address items.
373 * Enter: line Data record.
374 * cache Address cache.
376 static void mutt_build_items( gchar *line, AddressCache *cache ) {
379 gchar *aliasTag, *aliasName, *recipient;
382 /* printf( "\nBUILD >%s<\n", line ); */
383 list = mgu_parse_string( line, 3, &tCount );
386 mgu_free_dlist( list );
392 aliasTag = list->data;
393 node = g_list_next( list );
394 aliasName = node->data;
395 node = g_list_next( node );
396 recipient = node->data;
399 if( strcmp( aliasTag, "alias" ) == 0 ) {
401 /* printf( "aliasName :%s:\n", aliasName ); */
402 /* printf( "recipient :%s:\n", recipient ); */
403 addrList = mutt_parse_rcplist( recipient, &aCount );
404 /* printf( "---\n" ); */
405 mutt_build_address( aliasName, addrList, aCount, cache );
408 mgu_free_dlist( list );
414 * Read file data into address cache.
416 static void mutt_read_file( MuttFile *muttFile, AddressCache *cache ) {
417 GSList *listValue = NULL;
418 gboolean flagEOF = FALSE, flagCont = FALSE, lastCont = FALSE;
419 gchar *line = NULL, *lineValue = NULL;
423 /* Find EOF for progress indicator */
424 fseek( muttFile->file, 0L, SEEK_END );
425 posEnd = ftell( muttFile->file );
426 fseek( muttFile->file, 0L, SEEK_SET );
430 line = mutt_get_line( muttFile, &flagCont );
432 posCur = ftell( muttFile->file );
433 if( muttFile->cbProgress ) {
434 /* Call progress indicator */
435 ( muttFile->cbProgress ) ( muttFile, & posEnd, & posCur );
438 if( line == NULL ) flagEOF = TRUE;
441 lineValue = mgu_list_coalesce( listValue );
443 mutt_build_items( lineValue, cache );
447 mgu_free_list( listValue );
452 /* Add line to list */
453 listValue = g_slist_append( listValue, g_strdup( line ) );
460 mgu_free_list( listValue );
465 * ============================================================================================
466 * Read file into list. Main entry point
467 * Enter: muttFile MUTT control data.
468 * cache Address cache to load.
469 * Return: Status code.
470 * ============================================================================================
472 gint mutt_import_data( MuttFile *muttFile, AddressCache *cache ) {
473 g_return_val_if_fail( muttFile != NULL, MGU_BAD_ARGS );
474 g_return_val_if_fail( cache != NULL, MGU_BAD_ARGS );
475 muttFile->retVal = MGU_SUCCESS;
476 addrcache_clear( cache );
477 cache->dataRead = FALSE;
478 mutt_open_file( muttFile );
479 if( muttFile->retVal == MGU_SUCCESS ) {
480 /* Read data into the cache */
481 mutt_read_file( muttFile, cache );
482 mutt_close_file( muttFile );
485 cache->modified = FALSE;
486 cache->dataRead = TRUE;
488 return muttFile->retVal;
491 #define WORK_BUFLEN 1024
494 * Attempt to find a Mutt file.
495 * Return: Filename, or home directory if not found, or empty string if
496 * no home. Filename should be g_free() when done.
498 gchar *mutt_find_file( void ) {
500 gchar str[ WORK_BUFLEN ];
504 homedir = g_get_home_dir();
505 if( ! homedir ) return g_strdup( "" );
507 strcpy( str, homedir );
510 if( str[ len-1 ] != G_DIR_SEPARATOR ) {
511 str[ len ] = G_DIR_SEPARATOR;
515 strcat( str, MUTT_HOME_FILE );
517 /* Attempt to open */
518 if( ( fp = fopen( str, "r" ) ) != NULL ) {
522 /* Truncate filename */
525 return g_strdup( str );