2005-03-18 [paul] 1.0.3cvs2.4
[claws.git] / src / addrharvest.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 Match Grun
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * Functions for an E-Mail address harvester.
22  */
23
24 #include <sys/stat.h>
25 #include <dirent.h>
26 #include <glib.h>
27 #include <string.h>
28
29 #include "utils.h"
30 #include "mgutils.h"
31 #include "addrharvest.h"
32 #include "addritem.h"
33
34 /* Mail header names of interest */
35 static gchar *_headerFrom_     = HEADER_FROM;
36 static gchar *_headerReplyTo_  = HEADER_REPLY_TO;
37 static gchar *_headerSender_   = HEADER_SENDER;
38 static gchar *_headerErrorsTo_ = HEADER_ERRORS_TO;
39 static gchar *_headerCC_       = HEADER_CC;
40 static gchar *_headerTo_       = HEADER_TO;
41
42 #define ADDR_BUFFSIZE    1024
43 #define MSG_BUFFSIZE     2048
44 #define MSGNUM_BUFFSIZE  32
45 #define DFL_FOLDER_SIZE  20
46
47 /* Noise strings included by some other E-Mail clients */
48 #define REM_NAME_STRING  "(E-mail)"
49 #define REM_NAME_STRING2 "(E-mail 2)"
50
51 /* Directories to ignore */
52 #define DIR_IGNORE ".\t..\t.sylpheed_mark\t.sylpheed_cache"
53
54 /*
55  * Header entry.
56  */
57 typedef struct _HeaderEntry HeaderEntry;
58 struct _HeaderEntry {
59         gchar      *header;
60         gboolean   selected;
61         ItemFolder *folder;
62         gint       count;
63 };
64
65 /*
66  * Build header table entry.
67  * Enter: harvester Harvester object.
68  *        name      Header name.
69  */
70 static void addrharvest_build_entry(
71                 AddressHarvester* harvester, gchar *name )
72 {
73         HeaderEntry *entry;
74
75         entry = g_new0( HeaderEntry, 1 );
76         entry->header = name;
77         entry->selected = FALSE;
78         entry->folder = NULL;
79         entry->count = 0;
80         harvester->headerTable = g_list_append( harvester->headerTable, entry );
81 }
82
83 /*
84  * Free key in table.
85  */
86 static gint addrharvest_free_table_vis( gpointer key, gpointer value, gpointer data ) {
87         g_free( key );
88         key = NULL;
89         value = NULL;
90         return TRUE;
91 }
92
93 /*
94  * Free lookup table.
95  */
96 static void addrharvest_free_table( AddressHarvester* harvester ) {
97         GList *node;
98         HeaderEntry *entry;
99
100         /* Free header list */
101         node = harvester->headerTable;
102         while( node ) {
103                 entry = ( HeaderEntry * ) node->data;
104                 entry->header = NULL;
105                 entry->selected = FALSE;
106                 entry->folder = NULL;
107                 entry->count = 0;
108                 g_free( entry );
109                 node = g_list_next( node );
110         }
111         g_list_free( harvester->headerTable );
112         harvester->headerTable = NULL;
113
114         /* Free duplicate table */
115         g_hash_table_freeze( harvester->dupTable );
116         g_hash_table_foreach_remove( harvester->dupTable, addrharvest_free_table_vis, NULL );
117         g_hash_table_thaw( harvester->dupTable );
118         g_hash_table_destroy( harvester->dupTable );
119         harvester->dupTable = NULL;
120 }
121
122 /*
123 * Create new object.
124 * Return: Harvester.
125 */
126 AddressHarvester *addrharvest_create( void ) {
127         AddressHarvester *harvester;
128
129         harvester = g_new0( AddressHarvester, 1 );
130         harvester->path = NULL;
131         harvester->dupTable = g_hash_table_new( g_str_hash, g_str_equal );
132         harvester->folderSize = DFL_FOLDER_SIZE;
133         harvester->retVal = MGU_SUCCESS;
134
135         /* Build header table */
136         harvester->headerTable = NULL;
137         addrharvest_build_entry( harvester, _headerFrom_ );
138         addrharvest_build_entry( harvester, _headerReplyTo_ );
139         addrharvest_build_entry( harvester, _headerSender_ );
140         addrharvest_build_entry( harvester, _headerErrorsTo_ );
141         addrharvest_build_entry( harvester, _headerCC_ );
142         addrharvest_build_entry( harvester, _headerTo_ );
143
144         return harvester;
145 }
146
147 /*
148 * Properties...
149 */
150 /*
151  * Specify path to folder that will be harvested.
152  * Entry: harvester Harvester object.
153  *        value     Full directory path.
154  */
155 void addrharvest_set_path( AddressHarvester* harvester, const gchar *value ) {
156         g_return_if_fail( harvester != NULL );
157         harvester->path = mgu_replace_string( harvester->path, value );
158         g_strstrip( harvester->path );
159 }
160
161 /*
162  * Specify maximum folder size.
163  * Entry: harvester Harvester object.
164  *        value     Folder size.
165  */
166 void addrharvest_set_folder_size(
167         AddressHarvester* harvester, const gint value )
168 {
169         g_return_if_fail( harvester != NULL );
170         if( value > 0 ) {
171                 harvester->folderSize = value;
172         }
173 }
174
175 /*
176  * Specify folder recursion.
177  * Entry: harvester Harvester object.
178  *        value     TRUE to process sub-folders, FALSE to process folder only.
179  */
180 void addrharvest_set_recurse(
181         AddressHarvester* harvester, const gboolean value )
182 {
183         g_return_if_fail( harvester != NULL );
184         harvester->folderRecurse = value;
185 }
186
187 /*
188  * Search (case insensitive) for header entry with specified name.
189  * Enter: harvester Harvester.
190  *        name      Header name.
191  * Return: Header, or NULL if not found.
192  */
193 static HeaderEntry *addrharvest_find( 
194         AddressHarvester* harvester, const gchar *name ) {
195         HeaderEntry *retVal;
196         GList *node;
197
198         retVal = NULL;
199         node = harvester->headerTable;
200         while( node ) {
201                 HeaderEntry *entry;
202
203                 entry = node->data;
204                 if (g_ascii_strncasecmp(entry->header, name,
205                                         strlen(entry->header)) == 0 ) {
206                         retVal = entry;
207                         break;
208                 }
209                 node = g_list_next( node );
210         }
211         return retVal;
212 }
213
214 /*
215  * Set selection for specified heaader.
216  * Enter: harvester Harvester.
217  *        name      Header name.
218  *        value     Value to set.
219  */
220 void addrharvest_set_header(
221         AddressHarvester* harvester, const gchar *name, const gboolean value )
222 {
223         HeaderEntry *entry;
224
225         g_return_if_fail( harvester != NULL );
226         entry = addrharvest_find( harvester, name );
227         if( entry != NULL ) {
228                 entry->selected = value;
229         }
230 }
231
232 /*
233  * Get address count
234  * Enter: harvester Harvester.
235  *        name      Header name.
236  * Return: Address count, or -1 if header not found.
237  */
238 gint addrharvest_get_count( AddressHarvester* harvester, const gchar *name ) {
239         HeaderEntry *entry;
240         gint count;
241
242         count = -1;
243         g_return_val_if_fail( harvester != NULL, count );
244         entry = addrharvest_find( harvester, name );
245         if( entry != NULL ) {
246                 count = entry->count;
247         }
248         return count;
249 }
250
251 /*
252 * Free up object by releasing internal memory.
253 * Enter: harvester Harvester.
254 */
255 void addrharvest_free( AddressHarvester *harvester ) {
256         g_return_if_fail( harvester != NULL );
257
258         /* Free internal stuff */
259         addrharvest_free_table( harvester );
260         g_free( harvester->path );
261
262         /* Clear pointers */
263         harvester->path = NULL;
264         harvester->retVal = MGU_SUCCESS;
265         harvester->headerTable = NULL;
266
267         harvester->folderSize = 0;
268
269         /* Now release object */
270         g_free( harvester );
271 }
272
273 /*
274 * Display object to specified stream.
275 * Enter: harvester Harvester.
276 *        stream    Output stream.
277 */
278 void addrharvest_print( AddressHarvester *harvester, FILE *stream ) {
279         GList *node;
280         HeaderEntry *entry;
281
282         g_return_if_fail( harvester != NULL );
283         fprintf( stream, "Address Harvester:\n" );
284         fprintf( stream, " file path: '%s'\n", harvester->path );
285         fprintf( stream, "max folder: %d'\n", harvester->folderSize );
286
287         node = harvester->headerTable;
288         while( node ) {
289                 entry = node->data;
290                 fprintf( stream, "   header: %s", entry->header );
291                 fprintf( stream, "\t: %s", entry->selected ? "yes" : "no" );
292                 fprintf( stream, "\t: %d\n", entry->count );
293                 node = g_list_next( node );
294         }
295         fprintf( stream, "  ret val: %d\n", harvester->retVal );
296 }
297
298 /*
299  * Insert address into cache.
300  * Enter: harvester Harvester object.
301  *        entry     Header object.
302  *        cache     Address cache to load.
303  *        name      Name.
304  *        address   eMail address.
305  */
306 static void addrharvest_insert_cache(
307                 AddressHarvester *harvester, HeaderEntry *entry,
308                 AddressCache *cache, const gchar *name,
309                 const gchar *address )
310 {
311         ItemPerson *person;
312         ItemFolder *folder;
313         gchar *folderName;
314         gboolean newFolder;
315         gint cnt;
316         gchar *key, *value;
317
318         newFolder = FALSE;
319         folder = entry->folder;
320         if( folder == NULL ) {
321                 newFolder = TRUE;       /* No folder yet */
322         }
323         if( entry->count % harvester->folderSize == 0 ) {
324                 newFolder = TRUE;       /* Folder is full */
325         }
326
327         /* Insert address */
328         key = g_strdup( address );
329         g_strdown( key );
330         person = g_hash_table_lookup( harvester->dupTable, key );
331         if( person ) {
332                 /* Update existing person to use longest name */
333                 value = ADDRITEM_NAME(person);
334                 if( strlen( name ) > strlen( value ) ) {
335                         addritem_person_set_common_name( person, name );
336                 }
337                 g_free( key );
338         }
339         else {
340                 /* Folder if required */
341                 if( newFolder ) {
342                         cnt = 1 + ( entry->count / harvester->folderSize );
343                         folderName =g_strdup_printf( "%s (%d)",
344                                         entry->header, cnt );
345                         folder = addritem_create_item_folder();
346                         addritem_folder_set_name( folder, folderName );
347                         addritem_folder_set_remarks( folder, "" );
348                         addrcache_id_folder( cache, folder );
349                         addrcache_add_folder( cache, folder );
350                         entry->folder = folder;
351                         g_free( folderName );
352                 }
353
354                 /* Insert entry */
355                 person = addrcache_add_contact(
356                                 cache, folder, name, address, "" );
357                 g_hash_table_insert( harvester->dupTable, key, person );
358                 entry->count++;
359         }
360         addritem_parse_first_last( person );
361 }
362
363 /*
364  * Remove specified string from name.
365  * Enter: name Name.
366  *        str  String to remove.
367  */
368 static void addrharvest_del_email( gchar *name, gchar *str ) {
369         gchar *p;
370         gint lenn, lenr;
371
372         lenr = strlen( str );
373         while((p = strcasestr( name, str )) != NULL) {
374                 lenn = strlen( p );
375                 memmove( p, p + lenr, lenn );
376         }
377 }
378
379 /*
380  * Find position of at (@) character in buffer.
381  * Enter:  buffer Start of buffer.
382  * Return: Position of at character, or NULL if not found.
383  * Note: This function searches for the last occurrence of an 'at' character
384  * prior to a valid delimiter character for the end of address. This enables
385  * an address to be found where it is also used as the name of the
386  * recipient. For example:
387  *     "axle.rose@netscape.com" <axle.rose@netscape.com>
388  * The last occurrence of the at character is detected.
389  */
390 static gchar *addrharvest_find_at( const gchar *buffer ) {
391         gchar *atCh;
392         gchar *p;
393
394         atCh = strchr( buffer, '@' );
395         if( atCh ) {
396                 /* Search forward for another one */
397                 p = atCh + 1;
398                 while( *p ) {
399                         if( *p == '>' ) {
400                                 break;
401                         }
402                         if( *p == ',' ) {
403                                 break;
404                         }
405                         if( *p == '\n' ) {
406                                 break;
407                         }
408                         if( *p == '@' ) {
409                                 atCh = p;
410                                 break;
411                         }
412                         p++;
413                 }
414         }
415         return atCh;
416 }
417
418 /*
419  * Find start and end of address string.
420  * Enter: buf Start address of buffer to process (not modified).
421  *        atp Pointer to email at (@) character.
422  *        bp  Pointer to start of email address (returned).
423  *        ep  Pointer to end of email address (returned).
424  */
425 static void addrharvest_find_address(
426                 const gchar *buf, const gchar *atp, const gchar **bp,
427                 const gchar **ep )
428 {
429         const gchar *p;
430
431         /* Find first non-separator char */
432         *bp = NULL;
433         p = buf;
434         while( TRUE ) {
435                 if( strchr( ",; \n\r", *p ) == NULL ) break;
436                 p++;
437         }
438         *bp = p;
439
440         /* Search forward for end of address */
441         *ep = NULL;
442         p = atp + 1;
443         while( TRUE ) {
444                 if( strchr( ",;", *p ) ) break;
445                 p++;
446         }
447         *ep = p;
448 }
449
450 /*
451  * Extract E-Mail address from buffer. If found, address is removed from
452  * buffer.
453  * Enter:  buffer Address buffer.
454  * Return: E-Mail address, or NULL if none found. Must g_free() when done.
455  */
456 static gchar *addrharvest_extract_address( gchar *buffer ) {
457         gchar *addr;
458         gchar *atCh, *p, *bp, *ep;
459         gint len;
460
461         addr = NULL;
462         atCh = addrharvest_find_at( buffer );
463         if( atCh ) {
464                 /* Search back for start of address */
465                 bp = NULL;
466                 p = atCh;
467                 while( p >= buffer ) {
468                         bp = p;
469                         if( *p == '<' ) {
470                                 *p = ' ';
471                                 bp++;
472                                 break;
473                         }
474                         p--;
475                 }
476
477                 /* Search fwd for end */
478                 ep = NULL;
479                 ep = p = atCh;
480                 while( *p ) {
481                         if( *p == '>' ) {
482                                 *p = ' ';
483                                 break;
484                         }
485                         else if( *p == ' ' ) {
486                                 break;
487                         }
488                         ep = p;
489                         p++;
490                 }
491
492                 /* Extract email */
493                 if( bp != NULL ) {
494                         len = ( ep - bp );
495                         if( len > 0 ) {
496                                 addr = g_strndup( bp, len + 1 );
497                                 memmove( bp, ep, len );
498                                 *bp = ' ';
499                         }
500                 }       
501         }
502         return addr;
503 }
504
505 /*
506  * Parse address from header buffer creating address in cache.
507  * Enter: harvester Harvester object.
508  *        entry     Header object.
509  *        cache     Address cache to load.
510  *        hdrBuf    Pointer to header buffer.
511  */
512 static void addrharvest_parse_address(
513                 AddressHarvester *harvester, HeaderEntry *entry,
514                 AddressCache *cache, const gchar *hdrBuf )
515 {
516         gchar buffer[ ADDR_BUFFSIZE + 2 ];
517         const gchar *bp;
518         const gchar *ep;
519         gchar *atCh, *email, *name;
520         gint bufLen;
521
522         /* Search for an address */
523         while((atCh = addrharvest_find_at( hdrBuf )) != NULL) {
524                 /* Find addres string */
525                 addrharvest_find_address( hdrBuf, atCh, &bp, &ep );
526
527                 /* Copy into buffer */
528                 bufLen = ( size_t ) ( ep - bp );
529                 if( bufLen > ADDR_BUFFSIZE ) {
530                         bufLen = ADDR_BUFFSIZE;
531                 }
532                 strncpy( buffer, bp, bufLen );
533                 buffer[ bufLen ] = '\0';
534                 buffer[ bufLen + 1 ] = '\0';
535                 buffer[ bufLen + 2 ] = '\0';
536
537                 /* Extract address from buffer */
538                 email = addrharvest_extract_address( buffer );
539                 if( email ) {
540                         /* Unescape characters */
541                         mgu_str_unescape( buffer );
542
543                         /* Remove noise characaters */
544                         addrharvest_del_email( buffer, REM_NAME_STRING );
545                         addrharvest_del_email( buffer, REM_NAME_STRING2 );
546
547                         /* Remove leading trailing quotes and spaces */
548                         mgu_str_ltc2space( buffer, '\"', '\"' );
549                         mgu_str_ltc2space( buffer, '\'', '\'' );
550                         mgu_str_ltc2space( buffer, '\"', '\"' );
551                         mgu_str_ltc2space( buffer, '(', ')' );
552                         g_strstrip( buffer );
553
554                         if( g_ascii_strcasecmp( buffer, email ) == 0 ) {
555                                 name = "";
556                         }
557                         else {
558                                 gchar *tmp = conv_unmime_header(buffer, NULL);
559                                 strncpy2(buffer, tmp, sizeof buffer);
560                                 name = buffer;
561                                 g_free(tmp);
562                         }
563
564                         /* Insert into address book */
565                         addrharvest_insert_cache(
566                                 harvester, entry, cache, name, email );
567                         g_free( email );
568                 }
569                 hdrBuf = ep;
570         }
571 }
572
573 /*
574  * Test whether buffer contains a header that appears in header list.
575  * Enter: listHdr Header list.
576  *        buf     Header buffer.
577  * Return: TRUE if header in list.
578  */
579 static gboolean addrharvest_check_hdr( GList *listHdr, gchar *buf ) {
580         gboolean retVal;
581         GList *node;
582         gchar *p, *hdr, *nhdr;
583         gint len;
584
585         retVal = FALSE;
586         p = strchr( buf, ':' );
587         if( p ) {
588                 len = ( size_t ) ( p - buf );
589                 hdr = g_strndup( buf, len );
590                 node = listHdr;
591                 while( node ) {
592                         nhdr = node->data;
593                         if (g_ascii_strncasecmp(nhdr, hdr, strlen(nhdr)) == 0 ) {
594                                 retVal = TRUE;
595                                 break;
596                         }
597                         node = g_list_next( node );
598                 }
599                 g_free( hdr );
600         }
601         return retVal;
602 }
603
604 /*
605  * Read header into a linked list of lines.
606  * Enter:  fp      File to read.
607  *         listHdr List of header lines of interest.
608  *         done    End of headers or end of file reached.
609  * Return: Linked list of lines.
610  */
611 static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done ) {
612         GSList *list;
613         gchar buf[ MSG_BUFFSIZE + 2 ];
614         gint ch;
615         gboolean foundHdr;
616
617         list = NULL;
618
619         /* Read line */
620         if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
621                 *done = TRUE;
622                 return list;
623         }
624
625         /* Test for end of headers */
626         if( buf[0] == '\r' || buf[0] == '\n' ) {
627                 *done = TRUE;
628                 return list;
629         }
630
631         /* Test whether required header */
632         foundHdr = addrharvest_check_hdr( listHdr, buf );
633
634         /* Read all header lines. Only add reqd ones to list */
635         while( TRUE ) {
636                 gchar *p;
637
638                 if( foundHdr ) {
639                         p = g_strdup( buf );
640                         list = g_slist_append( list, p );
641                 }
642
643                 /* Read first character */
644                 ch = fgetc( fp );
645                 if( ch == ' ' || ch == '\t' ) {
646                         /* Continuation character - read into buffer */
647                         if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
648                                 break;
649                         }
650                 }
651                 else {
652                         if( ch == EOF ) {
653                                 *done = TRUE;
654                         }
655                         else {
656                                 /* Push back character for next header */
657                                 ungetc( ch, fp );
658                         }
659                         break;
660                 }
661         }
662
663         return list;
664 }
665
666 /*
667  * Read specified file into address book.
668  * Enter:  harvester Harvester object.
669  *         fileName  File to read.
670  *         cache     Address cache to load.
671  * Return: Status.
672  */
673 static gint addrharvest_readfile(
674                 AddressHarvester *harvester, const gchar *fileName,
675                 AddressCache *cache, GList *listHdr )
676 {
677         gint retVal;
678         FILE *msgFile;
679         gchar *buf, *addr, *p;
680         HeaderEntry *entry;
681         GSList *list;
682         gboolean done;
683
684         msgFile = fopen( fileName, "rb" );
685         if( ! msgFile ) {
686                 /* Cannot open file */
687                 retVal = MGU_OPEN_FILE;
688                 return retVal;
689         }
690
691         done = FALSE;
692         while( TRUE ) {
693                 list = addrharvest_get_header( msgFile, listHdr, &done );
694                 if( done ) break;
695
696                 if( list == NULL ) {
697                         continue;
698                 }
699
700                 buf = mgu_list_coalesce( list );
701                 mgu_free_list( list );
702
703                 if(( p = strchr( buf, ':' ) ) != NULL ) {
704                         addr = p + 1;
705                         *p = '\0';
706
707                         entry = addrharvest_find( harvester, buf );
708                         if( entry && entry->selected ) {
709                                 /* Sanitize control characters */
710                                 p = addr;
711                                 while( *p ) {
712                                         if( *p == '\r' || *p == '\n' || *p == '\t' )
713                                                 *p = ' ';
714                                         p++;
715                                 }
716                                 addrharvest_parse_address(
717                                         harvester, entry, cache, addr );
718                         }
719                 }
720                 g_free( buf );
721         }
722
723         fclose( msgFile );
724         return MGU_SUCCESS;
725 }
726
727 /*
728  * Read all files in specified directory into address book. Directories are
729  * traversed recursively if necessary.
730  * Enter:  harvester Harvester object.
731  *         cache     Address cache to load.
732  *         msgList   List of message numbers, or NULL to process folder.
733  *         dir       Directory to process.
734  */
735 static void addrharvest_harvest_dir(
736         AddressHarvester *harvester, AddressCache *cache, GList *listHdr,
737         gchar *dir )
738 {
739         DIR *dp;
740         struct dirent *d;
741         struct stat s;
742         gint num;
743
744         if( ( dp = opendir( dir ) ) == NULL ) {
745                 return;
746         }
747
748         /* Process directory */
749         chdir( dir );
750         while( ( d = readdir( dp ) ) != NULL ) {
751                 stat( d->d_name, &s );
752                 if( S_ISDIR( s.st_mode ) ) {
753                         if( harvester->folderRecurse ) {
754                                 if( strstr( DIR_IGNORE, d->d_name ) != NULL )
755                                         continue;
756                                 addrharvest_harvest_dir(
757                                         harvester, cache, listHdr, d->d_name );
758                         }
759                 }
760                 if( S_ISREG( s.st_mode ) ) {
761                         if( ( num = to_number( d->d_name ) ) >= 0 ) {
762                                 addrharvest_readfile(
763                                         harvester, d->d_name, cache, listHdr );
764                         }
765                 }
766         }
767         chdir( ".." );
768         closedir( dp );
769 }
770
771 /*
772  * Read list of files in specified directory into address book.
773  * Enter:  harvester Harvester object.
774  *         cache     Address cache to load.
775  *         msgList   List of message numbers, or NULL to process folder.
776  */
777 static void addrharvest_harvest_list(
778         AddressHarvester *harvester, AddressCache *cache, GList *listHdr,
779         GList *msgList )
780 {
781         DIR *dp;
782         gint num;
783         GList *node;
784         gchar msgNum[ MSGNUM_BUFFSIZE ];
785
786         if( ( dp = opendir( harvester->path ) ) == NULL ) {
787                 return;
788         }
789
790         /* Process message list */
791         chdir( harvester->path );
792         node = msgList;
793         while( node ) {
794                 num = GPOINTER_TO_UINT( node->data );
795                 sprintf( msgNum, "%d", num );
796                 addrharvest_readfile( harvester, msgNum, cache, listHdr );
797                 node = g_list_next( node );
798         }
799         closedir( dp );
800 }
801
802 /*
803  * ============================================================================
804  * Read all files in specified directory into address book.
805  * Enter:  harvester Harvester object.
806  *         cache     Address cache to load.
807  *         msgList   List of message numbers, or NULL to process folder.
808  * Return: Status.
809  * ============================================================================
810  */
811 gint addrharvest_harvest(
812         AddressHarvester *harvester, AddressCache *cache, GList *msgList )
813 {
814         gint retVal;
815         GList *node;
816         GList *listHdr;
817
818         retVal = MGU_BAD_ARGS;
819         g_return_val_if_fail( harvester != NULL, retVal );
820         g_return_val_if_fail( cache != NULL, retVal );
821         g_return_val_if_fail( harvester->path != NULL, retVal );
822
823         /* Clear cache */
824         addrcache_clear( cache );
825         cache->dataRead = FALSE;
826
827         /* Build list of headers of interest */
828         listHdr = NULL;
829         node = harvester->headerTable;
830         while( node ) {
831                 HeaderEntry *entry;
832
833                 entry = node->data;
834                 if( entry->selected ) {
835                         gchar *p;
836
837                         p = g_strdup( entry->header );
838                         g_strdown( p );
839                         listHdr = g_list_append( listHdr, p );
840                 }
841                 node = g_list_next( node );
842         }
843
844         /* Process directory/files */
845         if( msgList == NULL ) {
846                 addrharvest_harvest_dir( harvester, cache, listHdr, harvester->path );
847         }
848         else {
849                 addrharvest_harvest_list( harvester, cache, listHdr, msgList );
850         }
851         mgu_free_dlist( listHdr );
852
853         /* Mark cache */
854         cache->modified = FALSE;
855         cache->dataRead = TRUE;
856
857         return retVal;
858 }
859
860 /*
861  * ============================================================================
862  * Test whether any headers have been selected for processing.
863  * Enter:  harvester Harvester object.
864  * Return: TRUE if a header was selected, FALSE if none were selected.
865  * ============================================================================
866  */
867 gboolean addrharvest_check_header( AddressHarvester *harvester ) {
868         gboolean retVal;
869         GList *node;
870
871         retVal = FALSE;
872         g_return_val_if_fail( harvester != NULL, retVal );
873
874         node = harvester->headerTable;
875         while( node ) {
876                 HeaderEntry *entry;
877
878                 entry = ( HeaderEntry * ) node->data;
879                 if( entry->selected ) return TRUE;
880                 node = g_list_next( node );
881         }
882         return retVal;
883 }
884
885 /*
886  * ============================================================================
887  * End of Source.
888  * ============================================================================
889  */
890
891