2007-01-21 [colin] 2.7.1cvs44
[claws.git] / src / ldif.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2007 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 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /*
21  * Functions necessary to access LDIF files (LDAP Data Interchange Format
22  * files).
23  */
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
28 #include <sys/stat.h>
29
30 #include "mgutils.h"
31 #include "ldif.h"
32 #include "addritem.h"
33 #include "addrcache.h"
34
35 #include "base64.h"
36 #include "utils.h"
37
38 #define LDIF_SEP_TAG    ':'
39 #define LDIF_LANG_TAG   ';'
40
41 /**
42  * Create new object.
43  * \return Initialized LDIF file object.
44  */
45 LdifFile *ldif_create() {
46         LdifFile *ldifFile;
47         ldifFile = g_new0( LdifFile, 1 );
48         ldifFile->path = NULL;
49         ldifFile->file = NULL;
50         ldifFile->bufptr = ldifFile->buffer;
51         ldifFile->hashFields = g_hash_table_new( g_str_hash, g_str_equal );
52         ldifFile->tempList = NULL;
53         ldifFile->dirtyFlag = TRUE;
54         ldifFile->accessFlag = FALSE;
55         ldifFile->retVal = MGU_SUCCESS;
56         ldifFile->cbProgress = NULL;
57         ldifFile->importCount = 0;
58         return ldifFile;
59 }
60
61 /**
62  * Specify full file specification of LDIF file.
63  * \param ldifFile LDIF import control object.
64  * \param value    Value of access flag.
65  */
66 void ldif_set_file( LdifFile *ldifFile, const gchar *value ) {
67         g_return_if_fail( ldifFile != NULL );
68
69         if( ldifFile->path ) {
70                 if( strcmp( ldifFile->path, value ) != 0 )
71                         ldifFile->dirtyFlag = TRUE;
72         }
73         else {
74                 ldifFile->dirtyFlag = TRUE;
75         }
76         ldifFile->path = mgu_replace_string( ldifFile->path, value );
77         g_strstrip( ldifFile->path );
78         ldifFile->importCount = 0;
79 }
80
81 /**
82  * Set the file access indicator.
83  * \param ldifFile LDIF import control object.
84  * \param value    File specification.
85  */
86 void ldif_set_accessed( LdifFile *ldifFile, const gboolean value ) {
87         g_return_if_fail( ldifFile != NULL );
88         ldifFile->accessFlag = value;
89 }
90
91 /**
92  * Register a progress indicator callback function.
93  *
94  * \param ldifFile LDIF import control object.
95  * \param func     Function to be called. When called, the function will be
96  *                 passed the following arguments:
97  *
98  * <ul>
99  * <li>LdifFile object,</li>
100  * <li>File size (long),</li>
101  * <li>Current position (long)</li>
102  * </ul>
103  */
104 void ldif_set_callback( LdifFile *ldifFile, void *func ) {
105         ldifFile->cbProgress = func;
106 }
107
108 /**
109  * Create field record object.
110  * \return Initialized LDIF field object.
111  */
112 static Ldif_FieldRec *ldif_create_fieldrec( const gchar *field ) {
113         Ldif_FieldRec *rec = g_new0( Ldif_FieldRec, 1 );
114         rec->tagName = g_strdup( field );
115         rec->userName = NULL;
116         rec->reserved = FALSE;
117         rec->selected = FALSE;
118         return rec;
119 }
120
121 /**
122  * Free field record object.
123  * \param rec LDIF field object.
124  */
125 static void ldif_free_fieldrec( Ldif_FieldRec *rec ) {
126         if( rec ) {
127                 g_free( rec->tagName );
128                 g_free( rec->userName );
129                 rec->tagName = NULL;
130                 rec->userName = NULL;
131                 rec->reserved = FALSE;
132                 rec->selected = FALSE;
133                 g_free( rec );
134         }
135 }
136
137 /**
138  * Set user name for field record.
139  * \param rec   LDIF field object.
140  * \param value User name to set. Note that reserved fields cannot be
141  *              named.
142  */
143 void ldif_field_set_name( Ldif_FieldRec *rec, const gchar *value ) {
144         g_return_if_fail( rec != NULL );
145
146         if( ! rec->reserved ) {
147                 rec->userName = mgu_replace_string( rec->userName, value );
148                 g_strstrip( rec->userName );
149         }
150 }
151
152 /**
153  * Specify selection for field record.
154  * \param rec   LDIF field object.
155  * \param value Set to <i>TRUE</i> to select field. Note that reserved
156  *              fields cannot be unselected.
157  */
158 void ldif_field_set_selected( Ldif_FieldRec *rec, const gboolean value ) {
159         g_return_if_fail( rec != NULL );
160
161         if( ! rec->reserved ) {
162                 rec->selected = value;
163         }
164 }
165
166 /**
167  * Toggle selection for field record. Note that reserved fields cannot be
168  * toggled.
169  * \param rec   LDIF field object.
170  */
171 void ldif_field_toggle( Ldif_FieldRec *rec ) {
172         g_return_if_fail( rec != NULL );
173
174         if( ! rec->reserved ) {
175                 rec->selected = !rec->selected;
176         }
177 }
178
179 /**
180  * Free hash table entry visitor function.
181  * \param  key   Key.
182  * \param  value Value (the LDIF field record).
183  * \param  data  User data.
184  * \return <code>-1</code>.
185 */
186 static gint ldif_hash_free_vis( gpointer key, gpointer value, gpointer data ) {
187         ldif_free_fieldrec( ( Ldif_FieldRec * ) value );
188         value = NULL;
189         key = NULL;
190         return -1;
191 }
192
193 /**
194  * Free up object by releasing internal memory.
195  * \param ldifFile LDIF import control object.
196  */
197 void ldif_free( LdifFile *ldifFile ) {
198         g_return_if_fail( ldifFile != NULL );
199
200         /* Close file */
201         if( ldifFile->file ) fclose( ldifFile->file );
202
203         /* Free internal stuff */
204         g_free( ldifFile->path );
205
206         /* Free field list */
207         g_hash_table_foreach_remove( ldifFile->hashFields, ldif_hash_free_vis, NULL );
208         g_hash_table_destroy( ldifFile->hashFields );
209         ldifFile->hashFields = NULL;
210
211         /* Clear pointers */
212         ldifFile->file = NULL;
213         ldifFile->path = NULL;
214         ldifFile->retVal = MGU_SUCCESS;
215         ldifFile->tempList = NULL;
216         ldifFile->dirtyFlag = FALSE;
217         ldifFile->accessFlag = FALSE;
218         ldifFile->cbProgress = NULL;
219
220         /* Now release file object */
221         g_free( ldifFile );
222 }
223
224 /**
225  * Display field record.
226  * \param rec    LDIF field object.
227  * \param stream File output stream.
228  */
229 void ldif_print_fieldrec( Ldif_FieldRec *rec, FILE *stream ) {
230         fprintf( stream, "\ttag:\t%s", rec->reserved ? "yes" : "no" );
231         fprintf( stream, "\t%s", rec->selected ? "yes" : "no" );
232         fprintf( stream, "\t:%s:\t:%s:\n", rec->userName, rec->tagName );
233 }
234
235 /**
236  * Display field record.
237  * \param key   Key.
238  * \param value Value (the LDIF field record).
239  * \param data  User data (file output stream).
240  * 
241  */
242 static void ldif_print_file_vis( gpointer key, gpointer value, gpointer data ) {
243         Ldif_FieldRec *rec = value;
244         FILE *stream = data;
245         ldif_print_fieldrec( rec, stream );
246 }
247
248 /**
249  * Display object to specified stream.
250  * \param ldifFile LDIF import control object.
251  * \param stream   File output stream.
252  */
253 void ldif_print_file( LdifFile *ldifFile, FILE *stream ) {
254         g_return_if_fail( ldifFile != NULL );
255         fprintf( stream, "LDIF File:\n" );
256         fprintf( stream, "file spec: '%s'\n", ldifFile->path );
257         fprintf( stream, "  ret val: %d\n",   ldifFile->retVal );
258         fprintf( stream, "   fields: {\n" );
259         g_hash_table_foreach( ldifFile->hashFields, ldif_print_file_vis, stream );
260         fprintf( stream, "} ---\n" );
261 }
262
263 /**
264  * Open file for read.
265  * \param  ldifFile LDIF import control object.
266  * \return <i>TRUE</i> if file opened successfully.
267  */
268 static gint ldif_open_file( LdifFile* ldifFile ) {
269         /* printf( "Opening file\n" ); */
270         if( ldifFile->path ) {
271                 ldifFile->file = g_fopen( ldifFile->path, "rb" );
272                 if( ! ldifFile->file ) {
273                         /* printf( "can't open %s\n", ldifFile->path ); */
274                         ldifFile->retVal = MGU_OPEN_FILE;
275                         return ldifFile->retVal;
276                 }
277         }
278         else {
279                 /* printf( "file not specified\n" ); */
280                 ldifFile->retVal = MGU_NO_FILE;
281                 return ldifFile->retVal;
282         }
283
284         /* Setup a buffer area */
285         ldifFile->buffer[0] = '\0';
286         ldifFile->bufptr = ldifFile->buffer;
287         ldifFile->retVal = MGU_SUCCESS;
288         return ldifFile->retVal;
289 }
290
291 /**
292  * Close file.
293  * \param  ldifFile LDIF import control object.
294  */
295 static void ldif_close_file( LdifFile *ldifFile ) {
296         g_return_if_fail( ldifFile != NULL );
297         if( ldifFile->file ) fclose( ldifFile->file );
298         ldifFile->file = NULL;
299 }
300
301 /**
302  * Read line of text from file.
303  * \param  ldifFile LDIF import control object.
304  * \return ptr to buffer where line starts.
305  */
306 static gchar *ldif_get_line( LdifFile *ldifFile ) {
307         gchar buf[ LDIFBUFSIZE ];
308         gint ch;
309         int i = 0;
310
311         if( feof( ldifFile->file ) ) 
312                 return NULL;
313
314         while( i < LDIFBUFSIZE-1 ) {
315                 ch = fgetc( ldifFile->file );
316                 if( ch == '\0' || ch == EOF ) {
317                         if( i == 0 ) return NULL;
318                         break;
319                 }
320 #if HAVE_DOSISH_SYSTEM
321 #else
322                 if( ch == '\r' ) 
323                         continue;
324 #endif
325                 if( ch == '\n' ) 
326                         break;
327                 buf[i] = ch;
328                 i++;
329         }
330         buf[i] = '\0';
331
332         /* Return a copy of buffer */
333         return g_strdup( buf );
334 }
335
336 /**
337  * Parse tag name from line buffer.
338  * \param  line Buffer.
339  * \param  flag64 Base-64 encoder flag.
340  * \return Buffer containing the tag name, or NULL if no delimiter char found.
341  *         If a double delimiter (::) is found, flag64 is set.
342  */
343 static gchar *ldif_get_tagname( char* line, gboolean *flag64 ) {
344         gint len = 0;
345         gchar *tag = NULL;
346         gchar *lptr = line;
347         gchar *sptr = NULL;
348
349         while( *lptr++ ) {
350                 /* Check for language tag */
351                 if( *lptr == LDIF_LANG_TAG ) {
352                         if( sptr == NULL ) sptr = lptr;
353                 }
354
355                 /* Check for delimiter */
356                 if( *lptr == LDIF_SEP_TAG ) {
357                         if( sptr ) {
358                                 len = sptr - line;
359                         }
360                         else {
361                                 len = lptr - line;
362                         }
363
364                         /* Base-64 encoding? */
365                         if( * ++lptr == LDIF_SEP_TAG ) *flag64 = TRUE;
366
367                         tag = g_strndup( line, len+1 );
368                         tag[ len ] = '\0';
369                         g_strdown( tag );
370                         return tag;
371                 }
372         }
373         return tag;
374 }
375
376 /**
377  * Parse tag value from line buffer.
378  * \param  line Buffer.
379  * \return Buffer containing the tag value. Empty string is returned if
380  *         no delimiter char found.
381  */
382 static gchar *ldif_get_tagvalue( gchar* line ) {
383         gchar *value = NULL;
384         gchar *start = NULL;
385         gchar *lptr;
386         gint len = 0;
387
388         for( lptr = line; *lptr; lptr++ ) {
389                 if( *lptr == LDIF_SEP_TAG ) {
390                         if( ! start )
391                                 start = lptr + 1;
392                 }
393         }
394         if( start ) {
395                 if( *start == LDIF_SEP_TAG ) start++;
396                 len = lptr - start;
397                 value = g_strndup( start, len+1 );
398                 g_strstrip( value );
399         }
400         else {
401                 /* Ensure that we get an empty string */
402                 value = g_strndup( "", 1 );
403         }
404         value[ len ] = '\0';
405         return value;
406 }
407
408 /**
409  * Parsed address data record.
410  */
411 typedef struct _Ldif_ParsedRec_ Ldif_ParsedRec;
412 struct _Ldif_ParsedRec_ {
413         GSList *listCName;
414         GSList *listFName;
415         GSList *listLName;
416         GSList *listNName;
417         GSList *listAddress;
418         GSList *listID;
419         GSList *userAttr;
420 };
421
422 /**
423  * User attribute data record.
424  */
425 typedef struct _Ldif_UserAttr_ Ldif_UserAttr;
426 struct _Ldif_UserAttr_ {
427         gchar *name;
428         gchar *value;
429 };
430
431 /**
432  * Build an address list entry and append to list of address items in the
433  * address cache. Name is formatted as "<first-name> <last-name>".
434  * \param ldifFile LDIF import control object.
435  * \param rec      LDIF field object.
436  * \param cache    Address cache to be populated with data.
437  */
438 static void ldif_build_items(
439                 LdifFile *ldifFile, Ldif_ParsedRec *rec, AddressCache *cache )
440 {
441         GSList *nodeFirst;
442         GSList *nodeAddress;
443         GSList *nodeAttr;
444         gchar *firstName = NULL, *lastName = NULL, *fullName = NULL;
445         gchar *nickName = NULL;
446         gint iLen = 0, iLenT = 0;
447         ItemPerson *person;
448         ItemEMail *email;
449
450         nodeAddress = rec->listAddress;
451         if( nodeAddress == NULL ) return;
452
453         /* Find longest first name in list */
454         nodeFirst = rec->listFName;
455         while( nodeFirst ) {
456                 if( firstName == NULL ) {
457                         firstName = nodeFirst->data;
458                         iLen = strlen( firstName );
459                 }
460                 else {
461                         if( ( iLenT = strlen( nodeFirst->data ) ) > iLen ) {
462                                 firstName = nodeFirst->data;
463                                 iLen = iLenT;
464                         }
465                 }
466                 nodeFirst = g_slist_next( nodeFirst );
467         }
468
469         /* Format name */
470         if( rec->listLName ) {
471                 lastName = rec->listLName->data;
472         }
473
474         if( firstName ) {
475                 if( lastName ) {
476                         fullName = g_strdup_printf(
477                                 "%s %s", firstName, lastName );
478                 }
479                 else {
480                         fullName = g_strdup_printf( "%s", firstName );
481                 }
482         }
483         else {
484                 if( lastName ) {
485                         fullName = g_strdup_printf( "%s", lastName );
486                 }
487         }
488         
489         if (!fullName || strlen(fullName) == 0) {
490                 g_free(fullName);
491                 fullName = NULL;
492                 if (rec->listCName)
493                         fullName = g_strdup(rec->listCName->data);
494         }
495         
496         if( fullName ) {
497                 g_strchug( fullName ); g_strchomp( fullName );
498         }
499
500         if( rec->listNName ) {
501                 nickName = rec->listNName->data;
502         }
503
504         person = addritem_create_item_person();
505         addritem_person_set_common_name( person, fullName );
506         addritem_person_set_first_name( person, firstName );
507         addritem_person_set_last_name( person, lastName );
508         addritem_person_set_nick_name( person, nickName );
509         addrcache_id_person( cache, person );
510         addrcache_add_person( cache, person );
511         ++ldifFile->importCount;
512
513         /* Add address item */
514         while( nodeAddress ) {
515                 email = addritem_create_item_email();
516                 addritem_email_set_address( email, nodeAddress->data );
517                 addrcache_id_email( cache, email );
518                 addrcache_person_add_email( cache, person, email );
519                 nodeAddress = g_slist_next( nodeAddress );
520         }
521         g_free( fullName );
522         fullName = firstName = lastName = NULL;
523
524         /* Add user attributes */
525         nodeAttr = rec->userAttr;
526         while( nodeAttr ) {
527                 Ldif_UserAttr *attr = nodeAttr->data;
528                 UserAttribute *attrib = addritem_create_attribute();
529                 addritem_attrib_set_name( attrib, attr->name );
530                 addritem_attrib_set_value( attrib, attr->value );
531                 addritem_person_add_attribute( person, attrib );
532                 nodeAttr = g_slist_next( nodeAttr );
533         }
534         nodeAttr = NULL;
535 }
536
537 /**
538  * Add selected field as user attribute.
539  * \param rec       LDIF field object.
540  * \param tagName   LDIF tag name.
541  * \param tagValue  Data value.
542  * \param hashField Hash table to populate.
543  */
544 static void ldif_add_user_attr(
545                 Ldif_ParsedRec *rec, gchar *tagName, gchar *tagValue,
546                 GHashTable *hashField )
547 {
548         Ldif_FieldRec *fld = NULL;
549         Ldif_UserAttr *attr = NULL;
550         gchar *name;
551
552         fld = g_hash_table_lookup( hashField, tagName );
553         if( fld ) {
554                 if( ! fld->selected ) return;
555
556                 name = fld->tagName;
557                 if( fld->userName ) {
558                         name = fld->userName;
559                 }
560                 attr = g_new0( Ldif_UserAttr, 1 );
561                 attr->name = g_strdup( name );
562                 attr->value = g_strdup( tagValue );
563                 rec->userAttr = g_slist_append( rec->userAttr, attr );
564         }
565 }
566
567 /**
568  * Add value to parsed data.
569  * \param rec       LDIF field object.
570  * \param tagName   LDIF tag name.
571  * \param tagValue  Data value.
572  * \param hashField Hash table to populate.
573  */
574 static void ldif_add_value(
575                Ldif_ParsedRec *rec, gchar *tagName, gchar *tagValue,
576                GHashTable *hashField )
577 {
578         gchar *nm, *val;
579
580         nm = g_strdup( tagName );
581         g_strdown( nm );
582         if( tagValue ) {
583                 val = g_strdup( tagValue );
584         }
585         else {
586                 val = g_strdup( "" );
587         }
588         g_strstrip( val );
589
590         if( g_utf8_collate( nm, LDIF_TAG_COMMONNAME ) == 0 ) {
591                 rec->listCName = g_slist_append( rec->listCName, val );
592         }
593         else if( g_utf8_collate( nm, LDIF_TAG_FIRSTNAME ) == 0 ) {
594                 rec->listFName = g_slist_append( rec->listFName, val );
595         }
596         else if( g_utf8_collate( nm, LDIF_TAG_LASTNAME ) == 0 ) {
597                 rec->listLName = g_slist_append( rec->listLName, val );
598         }
599         else if( g_utf8_collate( nm, LDIF_TAG_NICKNAME ) == 0 ) {
600                 rec->listNName = g_slist_append( rec->listNName, val );
601         }
602         else if( g_utf8_collate( nm, LDIF_TAG_EMAIL ) == 0 ) {
603                 rec->listAddress = g_slist_append( rec->listAddress, val );
604         }
605         else {
606                 /* Add field as user attribute */
607                 ldif_add_user_attr( rec, tagName, tagValue, hashField );
608         }
609         g_free( nm );
610 }
611
612 /**
613  * Clear parsed data record.
614  * \param rec LDIF field object.
615  */
616 static void ldif_clear_rec( Ldif_ParsedRec *rec ) {
617         GSList *list;
618
619         /* Free up user attributes */
620         list = rec->userAttr;
621         while( list ) {
622                 Ldif_UserAttr *attr = list->data;
623                 g_free( attr->name );
624                 g_free( attr->value );
625                 g_free( attr );
626                 list = g_slist_next( list );
627         }
628         g_slist_free( rec->userAttr );
629
630         g_slist_free( rec->listCName );
631         g_slist_free( rec->listFName );
632         g_slist_free( rec->listLName );
633         g_slist_free( rec->listNName );
634         g_slist_free( rec->listAddress );
635         g_slist_free( rec->listID );
636
637         rec->userAttr = NULL;
638         rec->listCName = NULL;
639         rec->listFName = NULL;
640         rec->listLName = NULL;
641         rec->listNName = NULL;
642         rec->listAddress = NULL;
643         rec->listID = NULL;
644 }
645
646 /**
647  * Read file data into address cache.
648  * Note that one LDIF record identifies one entity uniquely with the
649  * distinguished name (dn) tag. Each person can have multiple E-Mail
650  * addresses. Also, each person can have many common name (cn) tags.
651  *
652  * \param  ldifFile LDIF import control object.
653  * \param  cache    Address cache to be populated with data.
654  */
655 static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
656         gchar *tagName = NULL, *tagValue = NULL;
657         gchar *lastTag = NULL, *fullValue = NULL;
658         GSList *listValue = NULL;
659         gboolean flagEOF = FALSE, flagEOR = FALSE;
660         gboolean flag64 = FALSE, last64 = FALSE;
661         Ldif_ParsedRec *rec;
662         long posEnd = 0L;
663         long posCur = 0L;
664         GHashTable *hashField;
665
666         hashField = ldifFile->hashFields;
667         rec = g_new0( Ldif_ParsedRec, 1 );
668         ldif_clear_rec( rec );
669
670         /* Find EOF for progress indicator */
671         fseek( ldifFile->file, 0L, SEEK_END );
672         posEnd = ftell( ldifFile->file );
673         fseek( ldifFile->file, 0L, SEEK_SET );
674
675         while( ! flagEOF ) {
676                 gchar *line =  ldif_get_line( ldifFile );
677
678                 posCur = ftell( ldifFile->file );
679                 if( ldifFile->cbProgress ) {
680                         /* Call progress indicator */
681                         ( ldifFile->cbProgress ) ( ldifFile, & posEnd, & posCur );
682                 }
683
684                 flag64 = FALSE;
685                 if( line == NULL ) {
686                         flagEOF = flagEOR = TRUE;
687                 }
688                 else if( *line == '\0' ) {
689                         flagEOR = TRUE;
690                 }
691
692                 if( flagEOR ) {
693                         /* EOR, Output address data */
694                         if( lastTag ) {
695                                 /* Save record */
696                                 fullValue = mgu_list_coalesce( listValue );
697                                 if (fullValue && last64) {
698                                         gchar *out = g_malloc(strlen(fullValue));
699                                         int len = 0;
700                                         if ((len = base64_decode(out, fullValue,
701                                                         strlen(fullValue))) >= 0) {
702                                                 g_free(fullValue);
703                                                 fullValue = out;
704                                                 fullValue[len] = '\0';
705                                         } else
706                                                 g_free(out);
707                                 }
708                                 /* Base-64 encoded data */
709                                 /*
710                                 if( last64 ) {
711                                         ldif_dump_b64( fullValue );
712                                 }
713                                 */
714
715                                 ldif_add_value( rec, lastTag, fullValue, hashField );
716                                 /* ldif_print_record( rec, stdout ); */
717                                 ldif_build_items( ldifFile, rec, cache );
718                                 ldif_clear_rec( rec );
719                                 g_free( lastTag );
720                                 mgu_free_list( listValue );
721                                 lastTag = NULL;
722                                 listValue = NULL;
723                                 last64 = FALSE;
724                         }
725                 }
726                 if( line ) {
727                         flagEOR = FALSE;
728                         if( *line == ' ' ) {
729                                 /* Continuation line */
730                                 listValue = g_slist_append(
731                                         listValue, g_strdup( line+1 ) );
732                         }
733                         else if( *line == '=' ) {
734                                 /* Base-64 encoded continuation field */
735                                 listValue = g_slist_append(
736                                         listValue, g_strdup( line ) );
737                         }
738                         else {
739                                 /* Parse line */
740                                 tagName = ldif_get_tagname( line, &flag64 );
741                                 if( tagName ) {
742                                         tagValue = ldif_get_tagvalue( line );
743                                         if( tagValue ) {
744                                                 if( lastTag ) {
745                                                         /* Save data */
746                                                         fullValue =
747                                                                 mgu_list_coalesce( listValue );
748                                                         if (fullValue && last64) {
749                                                                 gchar *out = g_malloc(strlen(fullValue));
750                                                                 int len = 0;
751                                                                 if ((len = base64_decode(out, fullValue,
752                                                                                 strlen(fullValue))) >= 0) {
753                                                                         g_free(fullValue);
754                                                                         fullValue = out;
755                                                                         fullValue[len] = '\0';
756                                                                 } else
757                                                                         g_free(out);
758                                                         }
759                                                         /* Base-64 encoded data */
760                                                         /*
761                                                         if( last64 ) {
762                                                                 ldif_dump_b64( fullValue );
763                                                         }
764                                                         */
765
766                                                         ldif_add_value(
767                                                                 rec, lastTag, fullValue,
768                                                                 hashField );
769                                                         g_free( lastTag );
770                                                         mgu_free_list( listValue );
771                                                         lastTag = NULL;
772                                                         listValue = NULL;
773                                                         last64 = FALSE;
774                                                 }
775
776                                                 lastTag = g_strdup( tagName );
777                                                 listValue = g_slist_append(
778                                                         listValue,
779                                                         g_strdup( tagValue ) );
780                                                 g_free( tagValue );
781                                                 last64 = flag64;
782                                         }
783                                         g_free( tagName );
784                                 }
785                         }
786                 }
787                 g_free( line );
788         }
789
790         /* Release data */
791         ldif_clear_rec( rec );
792         g_free( rec );
793         g_free( lastTag );
794         mgu_free_list( listValue );
795 }
796
797 /**
798  * Add list of field names to hash table.
799  * \param table Hashtable.
800  * \param list  List of fields.
801  */
802 static void ldif_hash_add_list( GHashTable *table, GSList *list ) {
803         GSList *node = list;
804
805         /* mgu_print_list( list, stdout ); */
806         while( node ) {
807                 gchar *tag = node->data;
808                 if( ! g_hash_table_lookup( table, tag ) ) {
809                         Ldif_FieldRec *rec = NULL;
810                         gchar *key = g_strdup( tag );
811
812                         rec = ldif_create_fieldrec( tag );
813                         if( g_utf8_collate( tag, LDIF_TAG_DN ) == 0 ) {
814                                 rec->reserved = rec->selected = TRUE;
815                                 rec->userName = g_strdup( "dn" );
816                         }
817                         else if( g_utf8_collate( tag, LDIF_TAG_COMMONNAME ) == 0 ) {
818                                 rec->reserved = rec->selected = TRUE;
819                                 rec->userName = g_strdup( _( "Display Name" ) );
820                         }
821                         else if( g_utf8_collate( tag, LDIF_TAG_FIRSTNAME ) == 0 ) {
822                                 rec->reserved = rec->selected = TRUE;
823                                 rec->userName = g_strdup( _( "First Name" ) );
824                         }
825                         else if( g_utf8_collate( tag, LDIF_TAG_LASTNAME ) == 0 ) {
826                                 rec->reserved = rec->selected = TRUE;
827                                 rec->userName = g_strdup( _( "Last Name" ) );
828                         }
829                         else if( g_utf8_collate( tag, LDIF_TAG_NICKNAME ) == 0 ) {
830                                 rec->reserved = rec->selected = TRUE;
831                                 rec->userName = g_strdup( _( "Nick Name" ) );
832                         }
833                         else if( g_utf8_collate( tag, LDIF_TAG_EMAIL ) == 0 ) {
834                                 rec->reserved = rec->selected = TRUE;
835                                 rec->userName = g_strdup( _( "Email Address" ) );
836                         }
837                         g_hash_table_insert( table, key, rec );
838                 }
839                 node = g_slist_next( node );
840         }
841 }
842
843 /**
844  * Sorted list comparison function.
845  * \param  ptr1 First field.
846  * \param  ptr2 Second field.
847  * \return <code>-1, 0, +1</code> if first record less than, equal,
848  *         greater than second.
849  */
850 static gint ldif_field_compare( gconstpointer ptr1, gconstpointer ptr2 ) {
851         const Ldif_FieldRec *rec1 = ptr1;
852         const Ldif_FieldRec *rec2 = ptr2;
853
854         if( rec1->reserved ) {
855                 if( ! rec2->reserved ) {
856                         return +1;
857                 }
858         }
859         else {
860                 if( rec2->reserved ) {
861                         return -1;
862                 }
863         }
864         return g_utf8_collate( rec1->tagName, rec2->tagName );
865 }
866
867 /*
868  * Append hash table entry to list - visitor function.
869  * \param key   Key.
870  * \param value Data value.
871  * \param data  User data (the LDIF import control object).
872  */
873 static void ldif_hash2list_vis( gpointer key, gpointer value, gpointer data ) {
874         LdifFile *ldf = data;
875         ldf->tempList =
876                 g_list_insert_sorted( ldf->tempList, value, ldif_field_compare );
877 }
878
879 /**
880  * Read tag names for file data.
881  * \param  ldifFile LDIF import control object.
882  */
883 static void ldif_read_tag_list( LdifFile *ldifFile ) {
884         gchar *tagName = NULL;
885         GSList *listTags = NULL;
886         gboolean flagEOF = FALSE, flagEOR = FALSE, flagMail = FALSE;
887         gboolean flag64 = FALSE;
888         long posEnd = 0L;
889         long posCur = 0L;
890
891         /* Clear hash table */
892         g_hash_table_foreach_remove(
893                 ldifFile->hashFields, ldif_hash_free_vis, NULL );
894
895         /* Find EOF for progress indicator */
896         fseek( ldifFile->file, 0L, SEEK_END );
897         posEnd = ftell( ldifFile->file );
898         fseek( ldifFile->file, 0L, SEEK_SET );
899
900         /* Process file */
901         while( ! flagEOF ) {
902                 gchar *line = ldif_get_line( ldifFile );
903                 posCur = ftell( ldifFile->file );
904                 if( ldifFile->cbProgress ) {
905                         /* Call progress indicator */
906                         ( ldifFile->cbProgress ) ( ldifFile, & posEnd, & posCur );
907                 }
908
909                 flag64 = FALSE;
910                 if( line == NULL ) {
911                         flagEOF = flagEOR = TRUE;
912                 }
913                 else if( *line == '\0' ) {
914                         flagEOR = TRUE;
915                 }
916
917                 if( flagEOR ) {
918                         /* EOR, Output address data */
919                         /* Save field list to hash table */
920                         if( flagMail ) {
921                                 ldif_hash_add_list(
922                                         ldifFile->hashFields, listTags );
923                         }
924                         mgu_free_list( listTags );
925                         listTags = NULL;
926                         flagMail = FALSE;
927                 }
928                 if( line ) {
929                         flagEOR = FALSE;
930                         if( *line == ' ' ) {
931                                 /* Continuation line */
932                         }
933                         else if( *line == '=' ) {
934                                 /* Base-64 encoded continuation field */
935                         }
936                         else {
937                                 /* Parse line */
938                                 tagName = ldif_get_tagname( line, &flag64 );
939                                 if( tagName ) {
940                                         /* Add tag to list */
941                                         listTags = g_slist_append( listTags, tagName );
942
943                                         if( g_utf8_collate(
944                                                 tagName, LDIF_TAG_EMAIL ) == 0 )
945                                         {
946                                                 flagMail = TRUE;
947                                         }
948                                 }
949                         }
950                 }
951                 g_free( line );
952         }
953
954         /* Release data */
955         mgu_free_list( listTags );
956         listTags = NULL;
957 }
958
959 /**
960  * Read file into list. Main entry point
961  * \param  ldifFile LDIF import control object.
962  * \param  cache    Address cache to load.
963  * \return Status code.
964  */
965 gint ldif_import_data( LdifFile *ldifFile, AddressCache *cache ) {
966         g_return_val_if_fail( ldifFile != NULL, MGU_BAD_ARGS );
967         ldifFile->retVal = MGU_SUCCESS;
968         addrcache_clear( cache );
969         cache->dataRead = FALSE;
970         ldif_open_file( ldifFile );
971         if( ldifFile->retVal == MGU_SUCCESS ) {
972                 /* Read data into the cache */
973                 ldif_read_file( ldifFile, cache );
974                 ldif_close_file( ldifFile );
975
976                 /* Mark cache */
977                 cache->modified = FALSE;
978                 cache->dataRead = TRUE;
979         }
980         return ldifFile->retVal;
981 }
982
983 /**
984  * Process entire file reading list of unique fields. List of fields may be
985  * accessed with the <code>ldif_get_fieldlist()</code> function.
986  * \param  ldifFile LDIF import control object.
987  * \return Status code.
988  */
989 gint ldif_read_tags( LdifFile *ldifFile ) {
990         g_return_val_if_fail( ldifFile != NULL, MGU_BAD_ARGS );
991         ldifFile->retVal = MGU_SUCCESS;
992         if( ldifFile->dirtyFlag ) {
993                 ldif_open_file( ldifFile );
994                 if( ldifFile->retVal == MGU_SUCCESS ) {
995                         /* Read data into the cache */
996                         ldif_read_tag_list( ldifFile );
997                         ldif_close_file( ldifFile );
998                         ldifFile->dirtyFlag = FALSE;
999                         ldifFile->accessFlag = TRUE;
1000                 }
1001         }
1002         return ldifFile->retVal;
1003 }
1004
1005 /**
1006  * Return list of fields for LDIF file.
1007  * \param  ldifFile LDIF import control object.
1008  * \return Linked list of <code>Ldif_FieldRec</code> objects. This list may be
1009  *         <code>g_free()</code>. Note that the objects in the list should not
1010  *         be freed since they refer to objects inside the internal cache.
1011  *         These objects will be freed when LDIF file object is freed.
1012  */
1013 GList *ldif_get_fieldlist( LdifFile *ldifFile ) {
1014         GList *list = NULL;
1015
1016         g_return_val_if_fail( ldifFile != NULL, NULL );
1017         if( ldifFile->hashFields ) {
1018                 ldifFile->tempList = NULL;
1019                 g_hash_table_foreach( ldifFile->hashFields, ldif_hash2list_vis, ldifFile );
1020                 list = ldifFile->tempList;
1021                 ldifFile->tempList = NULL;
1022         }
1023         return list;
1024 }
1025
1026 /**
1027  * Output LDIF name-value pair to stream. Only non-empty names and values will
1028  * be output to file.
1029  * \param stream File output stream.
1030  * \param name   Name.
1031  * \param value  Data value.
1032  * \return <i>TRUE</i> if data output.
1033  */
1034 gboolean ldif_write_value( FILE *stream, const gchar *name, const gchar *value ) {
1035         if( name == NULL ) return FALSE;
1036         if( value == NULL ) return FALSE;
1037         if( strlen( name ) < 1 ) return FALSE;
1038         if( strlen( value ) < 1 ) return FALSE;
1039         fprintf( stream, "%s: ", name );
1040         fprintf( stream, "%s\n", value );
1041         return TRUE;
1042 }
1043
1044 /**
1045  * Output LDIF End of Record to stream.
1046  * \param stream File output stream.
1047  * \return <i>TRUE</i> if data output.
1048  */
1049 void ldif_write_eor( FILE *stream ) {
1050         /* Simple but caller should not need to know how to end record. */
1051         fprintf( stream, "\n" );
1052 }
1053
1054 /*
1055  * ============================================================================
1056  * End of Source.
1057  * ============================================================================
1058  */
1059