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