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