2004-12-01 [paul] 0.9.12cvs177.2
[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                                         sizeof(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                                 name = buffer;
559                                 conv_unmime_header_overwrite(name);
560                         }
561
562                         /* Insert into address book */
563                         addrharvest_insert_cache(
564                                 harvester, entry, cache, name, email );
565                         g_free( email );
566                 }
567                 hdrBuf = ep;
568         }
569 }
570
571 /*
572  * Test whether buffer contains a header that appears in header list.
573  * Enter: listHdr Header list.
574  *        buf     Header buffer.
575  * Return: TRUE if header in list.
576  */
577 static gboolean addrharvest_check_hdr( GList *listHdr, gchar *buf ) {
578         gboolean retVal;
579         GList *node;
580         gchar *p, *hdr, *nhdr;
581         gint len;
582
583         retVal = FALSE;
584         p = strchr( buf, ':' );
585         if( p ) {
586                 len = ( size_t ) ( p - buf );
587                 hdr = g_strndup( buf, len );
588                 node = listHdr;
589                 while( node ) {
590                         nhdr = node->data;
591                         if (g_ascii_strncasecmp(nhdr, hdr, sizeof(nhdr)) == 0 ) {
592                                 retVal = TRUE;
593                                 break;
594                         }
595                         node = g_list_next( node );
596                 }
597                 g_free( hdr );
598         }
599         return retVal;
600 }
601
602 /*
603  * Read header into a linked list of lines.
604  * Enter:  fp      File to read.
605  *         listHdr List of header lines of interest.
606  *         done    End of headers or end of file reached.
607  * Return: Linked list of lines.
608  */
609 static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done ) {
610         GSList *list;
611         gchar buf[ MSG_BUFFSIZE + 2 ];
612         gint ch;
613         gboolean foundHdr;
614
615         list = NULL;
616
617         /* Read line */
618         if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
619                 *done = TRUE;
620                 return list;
621         }
622
623         /* Test for end of headers */
624         if( buf[0] == '\r' || buf[0] == '\n' ) {
625                 *done = TRUE;
626                 return list;
627         }
628
629         /* Test whether required header */
630         foundHdr = addrharvest_check_hdr( listHdr, buf );
631
632         /* Read all header lines. Only add reqd ones to list */
633         while( TRUE ) {
634                 gchar *p;
635
636                 if( foundHdr ) {
637                         p = g_strdup( buf );
638                         list = g_slist_append( list, p );
639                 }
640
641                 /* Read first character */
642                 ch = fgetc( fp );
643                 if( ch == ' ' || ch == '\t' ) {
644                         /* Continuation character - read into buffer */
645                         if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
646                                 break;
647                         }
648                 }
649                 else {
650                         if( ch == EOF ) {
651                                 *done = TRUE;
652                         }
653                         else {
654                                 /* Push back character for next header */
655                                 ungetc( ch, fp );
656                         }
657                         break;
658                 }
659         }
660
661         return list;
662 }
663
664 /*
665  * Read specified file into address book.
666  * Enter:  harvester Harvester object.
667  *         fileName  File to read.
668  *         cache     Address cache to load.
669  * Return: Status.
670  */
671 static gint addrharvest_readfile(
672                 AddressHarvester *harvester, const gchar *fileName,
673                 AddressCache *cache, GList *listHdr )
674 {
675         gint retVal;
676         FILE *msgFile;
677         gchar *buf, *addr, *p;
678         HeaderEntry *entry;
679         GSList *list;
680         gboolean done;
681
682         msgFile = fopen( fileName, "rb" );
683         if( ! msgFile ) {
684                 /* Cannot open file */
685                 retVal = MGU_OPEN_FILE;
686                 return retVal;
687         }
688
689         done = FALSE;
690         while( TRUE ) {
691                 list = addrharvest_get_header( msgFile, listHdr, &done );
692                 if( done ) break;
693
694                 if( list == NULL ) {
695                         continue;
696                 }
697
698                 buf = mgu_list_coalesce( list );
699                 mgu_free_list( list );
700
701                 if(( p = strchr( buf, ':' ) ) != NULL ) {
702                         addr = p + 1;
703                         *p = '\0';
704
705                         entry = addrharvest_find( harvester, buf );
706                         if( entry && entry->selected ) {
707                                 /* Sanitize control characters */
708                                 p = addr;
709                                 while( *p ) {
710                                         if( *p == '\r' || *p == '\n' || *p == '\t' )
711                                                 *p = ' ';
712                                         p++;
713                                 }
714                                 addrharvest_parse_address(
715                                         harvester, entry, cache, addr );
716                         }
717                 }
718                 g_free( buf );
719         }
720
721         fclose( msgFile );
722         return MGU_SUCCESS;
723 }
724
725 /*
726  * Read all files in specified directory into address book. Directories are
727  * traversed recursively if necessary.
728  * Enter:  harvester Harvester object.
729  *         cache     Address cache to load.
730  *         msgList   List of message numbers, or NULL to process folder.
731  *         dir       Directory to process.
732  */
733 static void addrharvest_harvest_dir(
734         AddressHarvester *harvester, AddressCache *cache, GList *listHdr,
735         gchar *dir )
736 {
737         DIR *dp;
738         struct dirent *d;
739         struct stat s;
740         gint num;
741
742         if( ( dp = opendir( dir ) ) == NULL ) {
743                 return;
744         }
745
746         /* Process directory */
747         chdir( dir );
748         while( ( d = readdir( dp ) ) != NULL ) {
749                 stat( d->d_name, &s );
750                 if( S_ISDIR( s.st_mode ) ) {
751                         if( harvester->folderRecurse ) {
752                                 if( strstr( DIR_IGNORE, d->d_name ) != NULL )
753                                         continue;
754                                 addrharvest_harvest_dir(
755                                         harvester, cache, listHdr, d->d_name );
756                         }
757                 }
758                 if( S_ISREG( s.st_mode ) ) {
759                         if( ( num = to_number( d->d_name ) ) >= 0 ) {
760                                 addrharvest_readfile(
761                                         harvester, d->d_name, cache, listHdr );
762                         }
763                 }
764         }
765         chdir( ".." );
766         closedir( dp );
767 }
768
769 /*
770  * Read list of files in specified directory into address book.
771  * Enter:  harvester Harvester object.
772  *         cache     Address cache to load.
773  *         msgList   List of message numbers, or NULL to process folder.
774  */
775 static void addrharvest_harvest_list(
776         AddressHarvester *harvester, AddressCache *cache, GList *listHdr,
777         GList *msgList )
778 {
779         DIR *dp;
780         gint num;
781         GList *node;
782         gchar msgNum[ MSGNUM_BUFFSIZE ];
783
784         if( ( dp = opendir( harvester->path ) ) == NULL ) {
785                 return;
786         }
787
788         /* Process message list */
789         chdir( harvester->path );
790         node = msgList;
791         while( node ) {
792                 num = GPOINTER_TO_UINT( node->data );
793                 sprintf( msgNum, "%d", num );
794                 addrharvest_readfile( harvester, msgNum, cache, listHdr );
795                 node = g_list_next( node );
796         }
797         closedir( dp );
798 }
799
800 /*
801  * ============================================================================
802  * Read all files in specified directory into address book.
803  * Enter:  harvester Harvester object.
804  *         cache     Address cache to load.
805  *         msgList   List of message numbers, or NULL to process folder.
806  * Return: Status.
807  * ============================================================================
808  */
809 gint addrharvest_harvest(
810         AddressHarvester *harvester, AddressCache *cache, GList *msgList )
811 {
812         gint retVal;
813         GList *node;
814         GList *listHdr;
815
816         retVal = MGU_BAD_ARGS;
817         g_return_val_if_fail( harvester != NULL, retVal );
818         g_return_val_if_fail( cache != NULL, retVal );
819         g_return_val_if_fail( harvester->path != NULL, retVal );
820
821         /* Clear cache */
822         addrcache_clear( cache );
823         cache->dataRead = FALSE;
824
825         /* Build list of headers of interest */
826         listHdr = NULL;
827         node = harvester->headerTable;
828         while( node ) {
829                 HeaderEntry *entry;
830
831                 entry = node->data;
832                 if( entry->selected ) {
833                         gchar *p;
834
835                         p = g_strdup( entry->header );
836                         g_strdown( p );
837                         listHdr = g_list_append( listHdr, p );
838                 }
839                 node = g_list_next( node );
840         }
841
842         /* Process directory/files */
843         if( msgList == NULL ) {
844                 addrharvest_harvest_dir( harvester, cache, listHdr, harvester->path );
845         }
846         else {
847                 addrharvest_harvest_list( harvester, cache, listHdr, msgList );
848         }
849         mgu_free_dlist( listHdr );
850
851         /* Mark cache */
852         cache->modified = FALSE;
853         cache->dataRead = TRUE;
854
855         return retVal;
856 }
857
858 /*
859  * ============================================================================
860  * Test whether any headers have been selected for processing.
861  * Enter:  harvester Harvester object.
862  * Return: TRUE if a header was selected, FALSE if none were selected.
863  * ============================================================================
864  */
865 gboolean addrharvest_check_header( AddressHarvester *harvester ) {
866         gboolean retVal;
867         GList *node;
868
869         retVal = FALSE;
870         g_return_val_if_fail( harvester != NULL, retVal );
871
872         node = harvester->headerTable;
873         while( node ) {
874                 HeaderEntry *entry;
875
876                 entry = ( HeaderEntry * ) node->data;
877                 if( entry->selected ) return TRUE;
878                 node = g_list_next( node );
879         }
880         return retVal;
881 }
882
883 /*
884  * ============================================================================
885  * End of Source.
886  * ============================================================================
887  */
888
889