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