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