2005-03-04 [paul] 1.0.1cvs21
[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_strcasecmp( entry->header, name ) == 0 ) {
205                         retVal = entry;
206                         break;
207                 }
208                 node = g_list_next( node );
209         }
210         return retVal;
211 }
212
213 /*
214  * Set selection for specified heaader.
215  * Enter: harvester Harvester.
216  *        name      Header name.
217  *        value     Value to set.
218  */
219 void addrharvest_set_header(
220         AddressHarvester* harvester, const gchar *name, const gboolean value )
221 {
222         HeaderEntry *entry;
223
224         g_return_if_fail( harvester != NULL );
225         entry = addrharvest_find( harvester, name );
226         if( entry != NULL ) {
227                 entry->selected = value;
228         }
229 }
230
231 /*
232  * Get address count
233  * Enter: harvester Harvester.
234  *        name      Header name.
235  * Return: Address count, or -1 if header not found.
236  */
237 gint addrharvest_get_count( AddressHarvester* harvester, const gchar *name ) {
238         HeaderEntry *entry;
239         gint count;
240
241         count = -1;
242         g_return_val_if_fail( harvester != NULL, count );
243         entry = addrharvest_find( harvester, name );
244         if( entry != NULL ) {
245                 count = entry->count;
246         }
247         return count;
248 }
249
250 /*
251 * Free up object by releasing internal memory.
252 * Enter: harvester Harvester.
253 */
254 void addrharvest_free( AddressHarvester *harvester ) {
255         g_return_if_fail( harvester != NULL );
256
257         /* Free internal stuff */
258         addrharvest_free_table( harvester );
259         g_free( harvester->path );
260
261         /* Clear pointers */
262         harvester->path = NULL;
263         harvester->retVal = MGU_SUCCESS;
264         harvester->headerTable = NULL;
265
266         harvester->folderSize = 0;
267
268         /* Now release object */
269         g_free( harvester );
270 }
271
272 /*
273 * Display object to specified stream.
274 * Enter: harvester Harvester.
275 *        stream    Output stream.
276 */
277 void addrharvest_print( AddressHarvester *harvester, FILE *stream ) {
278         GList *node;
279         HeaderEntry *entry;
280
281         g_return_if_fail( harvester != NULL );
282         fprintf( stream, "Address Harvester:\n" );
283         fprintf( stream, " file path: '%s'\n", harvester->path );
284         fprintf( stream, "max folder: %d'\n", harvester->folderSize );
285
286         node = harvester->headerTable;
287         while( node ) {
288                 entry = node->data;
289                 fprintf( stream, "   header: %s", entry->header );
290                 fprintf( stream, "\t: %s", entry->selected ? "yes" : "no" );
291                 fprintf( stream, "\t: %d\n", entry->count );
292                 node = g_list_next( node );
293         }
294         fprintf( stream, "  ret val: %d\n", harvester->retVal );
295 }
296
297 /*
298  * Insert address into cache.
299  * Enter: harvester Harvester object.
300  *        entry     Header object.
301  *        cache     Address cache to load.
302  *        name      Name.
303  *        address   eMail address.
304  */
305 static void addrharvest_insert_cache(
306                 AddressHarvester *harvester, HeaderEntry *entry,
307                 AddressCache *cache, const gchar *name,
308                 const gchar *address )
309 {
310         ItemPerson *person;
311         ItemFolder *folder;
312         gchar *folderName;
313         gboolean newFolder;
314         gint cnt;
315         gchar *key, *value;
316
317         newFolder = FALSE;
318         folder = entry->folder;
319         if( folder == NULL ) {
320                 newFolder = TRUE;       /* No folder yet */
321         }
322         if( entry->count % harvester->folderSize == 0 ) {
323                 newFolder = TRUE;       /* Folder is full */
324         }
325
326         /* Insert address */
327         key = g_strdup( address );
328         g_strdown( key );
329         person = g_hash_table_lookup( harvester->dupTable, key );
330         if( person ) {
331                 /* Update existing person to use longest name */
332                 value = ADDRITEM_NAME(person);
333                 if( strlen( name ) > strlen( value ) ) {
334                         addritem_person_set_common_name( person, name );
335                 }
336                 g_free( key );
337         }
338         else {
339                 /* Folder if required */
340                 if( newFolder ) {
341                         cnt = 1 + ( entry->count / harvester->folderSize );
342                         folderName =g_strdup_printf( "%s (%d)",
343                                         entry->header, cnt );
344                         folder = addritem_create_item_folder();
345                         addritem_folder_set_name( folder, folderName );
346                         addritem_folder_set_remarks( folder, "" );
347                         addrcache_id_folder( cache, folder );
348                         addrcache_add_folder( cache, folder );
349                         entry->folder = folder;
350                         g_free( folderName );
351                 }
352
353                 /* Insert entry */
354                 person = addrcache_add_contact(
355                                 cache, folder, name, address, "" );
356                 g_hash_table_insert( harvester->dupTable, key, person );
357                 entry->count++;
358         }
359         addritem_parse_first_last( person );
360 }
361
362 /*
363  * Remove specified string from name.
364  * Enter: name Name.
365  *        str  String to remove.
366  */
367 static void addrharvest_del_email( gchar *name, gchar *str ) {
368         gchar *p;
369         gint lenn, lenr;
370
371         lenr = strlen( str );
372         while((p = strcasestr( name, str )) != NULL) {
373                 lenn = strlen( p );
374                 memmove( p, p + lenr, lenn );
375         }
376 }
377
378 /*
379  * Find position of at (@) character in buffer.
380  * Enter:  buffer Start of buffer.
381  * Return: Position of at character, or NULL if not found.
382  * Note: This function searches for the last occurrence of an 'at' character
383  * prior to a valid delimiter character for the end of address. This enables
384  * an address to be found where it is also used as the name of the
385  * recipient. For example:
386  *     "axle.rose@netscape.com" <axle.rose@netscape.com>
387  * The last occurrence of the at character is detected.
388  */
389 static gchar *addrharvest_find_at( const gchar *buffer ) {
390         gchar *atCh;
391         gchar *p;
392
393         atCh = strchr( buffer, '@' );
394         if( atCh ) {
395                 /* Search forward for another one */
396                 p = atCh + 1;
397                 while( *p ) {
398                         if( *p == '>' ) {
399                                 break;
400                         }
401                         if( *p == ',' ) {
402                                 break;
403                         }
404                         if( *p == '\n' ) {
405                                 break;
406                         }
407                         if( *p == '@' ) {
408                                 atCh = p;
409                                 break;
410                         }
411                         p++;
412                 }
413         }
414         return atCh;
415 }
416
417 /*
418  * Find start and end of address string.
419  * Enter: buf Start address of buffer to process (not modified).
420  *        atp Pointer to email at (@) character.
421  *        bp  Pointer to start of email address (returned).
422  *        ep  Pointer to end of email address (returned).
423  */
424 static void addrharvest_find_address(
425                 const gchar *buf, const gchar *atp, const gchar **bp,
426                 const gchar **ep )
427 {
428         const gchar *p;
429
430         /* Find first non-separator char */
431         *bp = NULL;
432         p = buf;
433         while( TRUE ) {
434                 if( strchr( ",; \n\r", *p ) == NULL ) break;
435                 p++;
436         }
437         *bp = p;
438
439         /* Search forward for end of address */
440         *ep = NULL;
441         p = atp + 1;
442         while( TRUE ) {
443                 if( strchr( ",;", *p ) ) break;
444                 p++;
445         }
446         *ep = p;
447 }
448
449 /*
450  * Extract E-Mail address from buffer. If found, address is removed from
451  * buffer.
452  * Enter:  buffer Address buffer.
453  * Return: E-Mail address, or NULL if none found. Must g_free() when done.
454  */
455 static gchar *addrharvest_extract_address( gchar *buffer ) {
456         gchar *addr;
457         gchar *atCh, *p, *bp, *ep;
458         gint len;
459
460         addr = NULL;
461         atCh = addrharvest_find_at( buffer );
462         if( atCh ) {
463                 /* Search back for start of address */
464                 bp = NULL;
465                 p = atCh;
466                 while( p >= buffer ) {
467                         bp = p;
468                         if( *p == '<' ) {
469                                 *p = ' ';
470                                 bp++;
471                                 break;
472                         }
473                         p--;
474                 }
475
476                 /* Search fwd for end */
477                 ep = NULL;
478                 ep = p = atCh;
479                 while( *p ) {
480                         if( *p == '>' ) {
481                                 *p = ' ';
482                                 break;
483                         }
484                         else if( *p == ' ' ) {
485                                 break;
486                         }
487                         ep = p;
488                         p++;
489                 }
490
491                 /* Extract email */
492                 if( bp != NULL ) {
493                         len = ( ep - bp );
494                         if( len > 0 ) {
495                                 addr = g_strndup( bp, len + 1 );
496                                 memmove( bp, ep, len );
497                                 *bp = ' ';
498                         }
499                 }       
500         }
501         return addr;
502 }
503
504 /*
505  * Parse address from header buffer creating address in cache.
506  * Enter: harvester Harvester object.
507  *        entry     Header object.
508  *        cache     Address cache to load.
509  *        hdrBuf    Pointer to header buffer.
510  */
511 static void addrharvest_parse_address(
512                 AddressHarvester *harvester, HeaderEntry *entry,
513                 AddressCache *cache, const gchar *hdrBuf )
514 {
515         gchar buffer[ ADDR_BUFFSIZE + 2 ];
516         gchar buf[ADDR_BUFFSIZE];
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_strcasecmp( buffer, email ) == 0 ) {
555                                 name = "";
556                         }
557                         else {
558                                 name = buffer;
559                                 conv_unmime_header(buf, sizeof(buf), name,
560                                 NULL);
561                         }
562
563                         /* Insert into address book */
564                         addrharvest_insert_cache(
565                                 harvester, entry, cache, name, email );
566                         g_free( email );
567                 }
568                 hdrBuf = ep;
569         }
570 }
571
572 /*
573  * Test whether buffer contains a header that appears in header list.
574  * Enter: listHdr Header list.
575  *        buf     Header buffer.
576  * Return: TRUE if header in list.
577  */
578 static gboolean addrharvest_check_hdr( GList *listHdr, gchar *buf ) {
579         gboolean retVal;
580         GList *node;
581         gchar *p, *hdr, *nhdr;
582         gint len;
583
584         retVal = FALSE;
585         p = strchr( buf, ':' );
586         if( p ) {
587                 len = ( size_t ) ( p - buf );
588                 hdr = g_strndup( buf, len );
589                 node = listHdr;
590                 while( node ) {
591                         nhdr = node->data;
592                         if( g_strcasecmp( nhdr, hdr ) == 0 ) {
593                                 retVal = TRUE;
594                                 break;
595                         }
596                         node = g_list_next( node );
597                 }
598                 g_free( hdr );
599         }
600         return retVal;
601 }
602
603 /*
604  * Read header into a linked list of lines.
605  * Enter:  fp      File to read.
606  *         listHdr List of header lines of interest.
607  *         done    End of headers or end of file reached.
608  * Return: Linked list of lines.
609  */
610 static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done ) {
611         GSList *list;
612         gchar buf[ MSG_BUFFSIZE + 2 ];
613         gint ch;
614         gboolean foundHdr;
615
616         list = NULL;
617
618         /* Read line */
619         if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
620                 *done = TRUE;
621                 return list;
622         }
623
624         /* Test for end of headers */
625         if( buf[0] == '\r' || buf[0] == '\n' ) {
626                 *done = TRUE;
627                 return list;
628         }
629
630         /* Test whether required header */
631         foundHdr = addrharvest_check_hdr( listHdr, buf );
632
633         /* Read all header lines. Only add reqd ones to list */
634         while( TRUE ) {
635                 gchar *p;
636
637                 if( foundHdr ) {
638                         p = g_strdup( buf );
639                         list = g_slist_append( list, p );
640                 }
641
642                 /* Read first character */
643                 ch = fgetc( fp );
644                 if( ch == ' ' || ch == '\t' ) {
645                         /* Continuation character - read into buffer */
646                         if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
647                                 break;
648                         }
649                 }
650                 else {
651                         if( ch == EOF ) {
652                                 *done = TRUE;
653                         }
654                         else {
655                                 /* Push back character for next header */
656                                 ungetc( ch, fp );
657                         }
658                         break;
659                 }
660         }
661
662         return list;
663 }
664
665 /*
666  * Read specified file into address book.
667  * Enter:  harvester Harvester object.
668  *         fileName  File to read.
669  *         cache     Address cache to load.
670  * Return: Status.
671  */
672 static gint addrharvest_readfile(
673                 AddressHarvester *harvester, const gchar *fileName,
674                 AddressCache *cache, GList *listHdr )
675 {
676         gint retVal;
677         FILE *msgFile;
678         gchar *buf, *addr, *p;
679         HeaderEntry *entry;
680         GSList *list;
681         gboolean done;
682
683         msgFile = fopen( fileName, "rb" );
684         if( ! msgFile ) {
685                 /* Cannot open file */
686                 retVal = MGU_OPEN_FILE;
687                 return retVal;
688         }
689
690         done = FALSE;
691         while( TRUE ) {
692                 list = addrharvest_get_header( msgFile, listHdr, &done );
693                 if( done ) break;
694
695                 if( list == NULL ) {
696                         continue;
697                 }
698
699                 buf = mgu_list_coalesce( list );
700                 mgu_free_list( list );
701
702                 if(( p = strchr( buf, ':' ) ) != NULL ) {
703                         addr = p + 1;
704                         *p = '\0';
705
706                         entry = addrharvest_find( harvester, buf );
707                         if( entry && entry->selected ) {
708                                 /* Sanitize control characters */
709                                 p = addr;
710                                 while( *p ) {
711                                         if( *p == '\r' || *p == '\n' || *p == '\t' )
712                                                 *p = ' ';
713                                         p++;
714                                 }
715                                 addrharvest_parse_address(
716                                         harvester, entry, cache, addr );
717                         }
718                 }
719                 g_free( buf );
720         }
721
722         fclose( msgFile );
723         return MGU_SUCCESS;
724 }
725
726 /*
727  * Read all files in specified directory into address book. Directories are
728  * traversed recursively if necessary.
729  * Enter:  harvester Harvester object.
730  *         cache     Address cache to load.
731  *         msgList   List of message numbers, or NULL to process folder.
732  *         dir       Directory to process.
733  */
734 static void addrharvest_harvest_dir(
735         AddressHarvester *harvester, AddressCache *cache, GList *listHdr,
736         gchar *dir )
737 {
738         DIR *dp;
739         struct dirent *d;
740         struct stat s;
741         gint num;
742
743         if( ( dp = opendir( dir ) ) == NULL ) {
744                 return;
745         }
746
747         /* Process directory */
748         chdir( dir );
749         while( ( d = readdir( dp ) ) != NULL ) {
750                 stat( d->d_name, &s );
751                 if( S_ISDIR( s.st_mode ) ) {
752                         if( harvester->folderRecurse ) {
753                                 if( strstr( DIR_IGNORE, d->d_name ) != NULL )
754                                         continue;
755                                 addrharvest_harvest_dir(
756                                         harvester, cache, listHdr, d->d_name );
757                         }
758                 }
759                 if( S_ISREG( s.st_mode ) ) {
760                         if( ( num = to_number( d->d_name ) ) >= 0 ) {
761                                 addrharvest_readfile(
762                                         harvester, d->d_name, cache, listHdr );
763                         }
764                 }
765         }
766         chdir( ".." );
767         closedir( dp );
768 }
769
770 /*
771  * Read list of files in specified directory into address book.
772  * Enter:  harvester Harvester object.
773  *         cache     Address cache to load.
774  *         msgList   List of message numbers, or NULL to process folder.
775  */
776 static void addrharvest_harvest_list(
777         AddressHarvester *harvester, AddressCache *cache, GList *listHdr,
778         GList *msgList )
779 {
780         DIR *dp;
781         gint num;
782         GList *node;
783         gchar msgNum[ MSGNUM_BUFFSIZE ];
784
785         if( ( dp = opendir( harvester->path ) ) == NULL ) {
786                 return;
787         }
788
789         /* Process message list */
790         chdir( harvester->path );
791         node = msgList;
792         while( node ) {
793                 num = GPOINTER_TO_UINT( node->data );
794                 sprintf( msgNum, "%d", num );
795                 addrharvest_readfile( harvester, msgNum, cache, listHdr );
796                 node = g_list_next( node );
797         }
798         closedir( dp );
799 }
800
801 /*
802  * ============================================================================
803  * Read all files in specified directory into address book.
804  * Enter:  harvester Harvester object.
805  *         cache     Address cache to load.
806  *         msgList   List of message numbers, or NULL to process folder.
807  * Return: Status.
808  * ============================================================================
809  */
810 gint addrharvest_harvest(
811         AddressHarvester *harvester, AddressCache *cache, GList *msgList )
812 {
813         gint retVal;
814         GList *node;
815         GList *listHdr;
816
817         retVal = MGU_BAD_ARGS;
818         g_return_val_if_fail( harvester != NULL, retVal );
819         g_return_val_if_fail( cache != NULL, retVal );
820         g_return_val_if_fail( harvester->path != NULL, retVal );
821
822         /* Clear cache */
823         addrcache_clear( cache );
824         cache->dataRead = FALSE;
825
826         /* Build list of headers of interest */
827         listHdr = NULL;
828         node = harvester->headerTable;
829         while( node ) {
830                 HeaderEntry *entry;
831
832                 entry = node->data;
833                 if( entry->selected ) {
834                         gchar *p;
835
836                         p = g_strdup( entry->header );
837                         g_strdown( p );
838                         listHdr = g_list_append( listHdr, p );
839                 }
840                 node = g_list_next( node );
841         }
842
843         /* Process directory/files */
844         if( msgList == NULL ) {
845                 addrharvest_harvest_dir( harvester, cache, listHdr, harvester->path );
846         }
847         else {
848                 addrharvest_harvest_list( harvester, cache, listHdr, msgList );
849         }
850         mgu_free_dlist( listHdr );
851
852         /* Mark cache */
853         cache->modified = FALSE;
854         cache->dataRead = TRUE;
855
856         return retVal;
857 }
858
859 /*
860  * ============================================================================
861  * Test whether any headers have been selected for processing.
862  * Enter:  harvester Harvester object.
863  * Return: TRUE if a header was selected, FALSE if none were selected.
864  * ============================================================================
865  */
866 gboolean addrharvest_check_header( AddressHarvester *harvester ) {
867         gboolean retVal;
868         GList *node;
869
870         retVal = FALSE;
871         g_return_val_if_fail( harvester != NULL, retVal );
872
873         node = harvester->headerTable;
874         while( node ) {
875                 HeaderEntry *entry;
876
877                 entry = ( HeaderEntry * ) node->data;
878                 if( entry->selected ) return TRUE;
879                 node = g_list_next( node );
880         }
881         return retVal;
882 }
883
884 /*
885  * ============================================================================
886  * End of Source.
887  * ============================================================================
888  */
889
890