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