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