2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003 Match Grun
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Export address book to LDIF file.
34 #include "exportldif.h"
38 #define DFL_DIR_SYLPHEED_OUT "sylpheed-out"
39 #define DFL_FILE_SYLPHEED_OUT "addressbook.ldif"
41 #define FMT_BUFSIZE 2048
42 #define XML_BUFSIZE 2048
44 /* Settings - properties */
45 #define EXML_PROPFILE_NAME "exportldif.xml"
46 #define EXMLPROP_DIRECTORY "directory"
47 #define EXMLPROP_FILE "file"
48 #define EXMLPROP_SUFFIX "suffix"
49 #define EXMLPROP_RDN_INDEX "rdn"
50 #define EXMLPROP_USE_DN "use-dn"
51 #define EXMLPROP_EXCL_EMAIL "exclude-mail"
53 static gchar *_attrName_UID_ = "uid";
54 static gchar *_attrName_DName_ = "cn";
55 static gchar *_attrName_EMail_ = "mail";
58 * Create initialized LDIF export control object.
59 * \return Initialized export control data.
61 ExportLdifCtl *exportldif_create( void ) {
62 ExportLdifCtl *ctl = g_new0( ExportLdifCtl, 1 );
65 ctl->dirOutput = NULL;
68 ctl->rdnIndex = EXPORT_LDIF_ID_UID;
70 ctl->excludeEMail = TRUE;
71 ctl->retVal = MGU_SUCCESS;
73 ctl->settingsFile = g_strconcat(
74 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
80 * Free up object by releasing internal memory.
81 * \return ctl Export control data.
83 void exportldif_free( ExportLdifCtl *ctl ) {
86 g_return_if_fail( ctl != NULL );
89 g_free( ctl->fileLdif );
90 g_free( ctl->dirOutput );
91 g_free( ctl->suffix );
92 g_free( ctl->settingsFile );
96 ctl->dirOutput = NULL;
99 ctl->rdnIndex = EXPORT_LDIF_ID_UID;
101 ctl->excludeEMail = FALSE;
102 ctl->retVal = MGU_SUCCESS;
105 /* Now release object */
110 * Print control object.
111 * \param ctl Export control data.
112 * \param stream Output stream.
114 void exportldif_print( ExportLdifCtl *ctl, FILE *stream ) {
115 fprintf( stream, "ExportLdifCtl:\n" );
116 fprintf( stream, " path: %s\n", ctl->path );
117 fprintf( stream, "directory: %s\n", ctl->dirOutput );
118 fprintf( stream, " file: %s\n", ctl->fileLdif );
119 fprintf( stream, " suffix: %s\n", ctl->suffix );
120 fprintf( stream, " rdn: %d\n", ctl->rdnIndex );
121 fprintf( stream, " use DN: %s\n", ctl->useDN ? "y" : "n" );
122 fprintf( stream, " ex EMail: %s\n", ctl->excludeEMail ? "y" : "n" );
123 fprintf( stream, " settings: %s\n", ctl->settingsFile );
127 * Specify directory where LDIF files are created.
128 * \param ctl Export control data.
129 * \param value Full directory path.
131 void exportldif_set_output_dir( ExportLdifCtl *ctl, const gchar *value ) {
132 g_return_if_fail( ctl != NULL );
133 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, value );
134 g_strstrip( ctl->dirOutput );
138 * Specify full file specification of LDIF file.
139 * \param ctl Export control data.
140 * \param value Full file specification.
142 void exportldif_set_path( ExportLdifCtl *ctl, const gchar *value ) {
143 g_return_if_fail( ctl != NULL );
144 ctl->path = mgu_replace_string( ctl->path, value );
145 g_strstrip( ctl->path );
149 * Specify file name of LDIF file.
150 * \param ctl Export control data.
151 * \param value File name.
153 void exportldif_set_file_ldif( ExportLdifCtl *ctl, const gchar *value ) {
154 g_return_if_fail( ctl != NULL );
155 ctl->fileLdif = mgu_replace_string( ctl->fileLdif, value );
156 g_strstrip( ctl->fileLdif );
160 * Specify suffix to be used for creating DN entries.
161 * \param ctl Export control data.
162 * \param value Suffix.
164 void exportldif_set_suffix( ExportLdifCtl *ctl, const char *value ) {
165 g_return_if_fail( ctl != NULL );
166 ctl->suffix = mgu_replace_string( ctl->suffix, value );
167 g_strstrip( ctl->suffix );
171 * Specify index of variable to be used for creating RDN entries.
172 * \param ctl Export control data.
173 * \param value Index to variable, as follows:
175 * <li><code>EXPORT_LDIF_ID_UID</code> - Use Sylpheed UID.</li>
176 * <li><code>EXPORT_LDIF_ID_DNAME</code> - Use Sylpheed display name.</li>
177 * <li><code>EXPORT_LDIF_ID_EMAIL</code> - Use first E-Mail address.</li>
180 void exportldif_set_rdn( ExportLdifCtl *ctl, const gint value ) {
181 g_return_if_fail( ctl != NULL );
182 ctl->rdnIndex = value;
186 * Specify that <code>DN</code> attribute, if present, should be used as the
188 * \param ctl Export control data.
189 * \param value <i>TRUE</i> if DN should be used.
191 void exportldif_set_use_dn( ExportLdifCtl *ctl, const gboolean value ) {
192 g_return_if_fail( ctl != NULL );
197 * Specify that records without E-Mail addresses should be excluded.
198 * \param ctl Export control data.
199 * \param value <i>TRUE</i> if records without E-Mail should be excluded.
201 void exportldif_set_exclude_email( ExportLdifCtl *ctl, const gboolean value ) {
202 g_return_if_fail( ctl != NULL );
203 ctl->excludeEMail = value;
207 * Format LDAP value name with no embedded commas.
208 * \param value Data value to format.
209 * \return Formatted string, should be freed after use.
211 static gchar *exportldif_fmt_value( gchar *value ) {
217 /* Duplicate incoming value */
218 dest = dupval = g_strdup( value );
220 /* Copy characters, ignoring commas */
235 * Build DN for entry.
236 * \param ctl Export control data.
237 * \param person Person to format.
238 * \return Formatted DN entry.
240 static gchar *exportldif_fmt_dn(
241 ExportLdifCtl *ctl, const ItemPerson *person )
243 gchar buf[ FMT_BUFSIZE ];
244 gchar *retVal = NULL;
247 gchar *dupval = NULL;
251 if( ctl->rdnIndex == EXPORT_LDIF_ID_UID ) {
252 attr = _attrName_UID_;
253 value = ADDRITEM_ID( person );
255 else if( ctl->rdnIndex == EXPORT_LDIF_ID_DNAME ) {
256 attr = _attrName_DName_;
257 value = ADDRITEM_NAME( person );
258 dupval = exportldif_fmt_value( value );
260 else if( ctl->rdnIndex == EXPORT_LDIF_ID_EMAIL ) {
263 node = person->listEMail;
265 ItemEMail *email = node->data;
267 attr = _attrName_EMail_;
268 value = email->address;
269 dupval = exportldif_fmt_value( value );
276 if( strlen( value ) > 0 ) {
280 /* Format and free duplicated value */
281 strcat( buf, dupval );
285 /* Use original value */
286 strcat( buf, value );
291 if( strlen( ctl->suffix ) > 0 ) {
293 strcat( buf, ctl->suffix );
297 retVal = g_strdup( buf );
305 * Find DN by searching attribute list.
306 * \param ctl Export control data.
307 * \param person Person to format.
308 * \return Formatted DN entry, should be freed after use.
310 static gchar *exportldif_find_dn(
311 ExportLdifCtl *ctl, const ItemPerson *person )
313 gchar *retVal = NULL;
316 node = person->listAttrib;
318 UserAttribute *attrib = node->data;
320 node = g_list_next( node );
321 if( g_strcasecmp( attrib->name, LDIF_TAG_DN ) == 0 ) {
322 retVal = g_strdup( attrib->value );
330 * Format E-Mail entries for person.
331 * \param person Person to format.
332 * \param stream Output stream.
333 * \return <i>TRUE</i> if entry formatted.
335 static gboolean exportldif_fmt_email( const ItemPerson *person, FILE *stream ) {
336 gboolean retVal = FALSE;
339 node = person->listEMail;
341 ItemEMail *email = node->data;
343 node = g_list_next( node );
344 ldif_write_value( stream, LDIF_TAG_EMAIL, email->address );
351 * Test for E-Mail entries for person.
352 * \param person Person to test.
353 * \return <i>TRUE</i> if person has E-Mail address.
355 static gboolean exportldif_test_email( const ItemPerson *person )
357 gboolean retVal = FALSE;
360 node = person->listEMail;
362 ItemEMail *email = node->data;
364 node = g_list_next( node );
365 if( email->address ) {
366 if( strlen( email->address ) > 0 ) {
377 * Format persons in an address book folder.
378 * \param ctl Export control data.
379 * \param stream Output stream.
380 * \param folder Folder to format.
381 * \return <i>TRUE</i> if no persons were formatted.
383 static gboolean exportldif_fmt_person(
384 ExportLdifCtl *ctl, FILE *stream, const ItemFolder *folder )
386 gboolean retVal = TRUE;
389 if( folder->listPerson == NULL ) return retVal;
391 node = folder->listPerson;
393 AddrItemObject *aio = node->data;
394 node = g_list_next( node );
396 if( aio && aio->type == ITEMTYPE_PERSON ) {
397 ItemPerson *person = ( ItemPerson * ) aio;
398 gboolean classPerson = FALSE;
399 gboolean classInetP = FALSE;
402 /* Check for E-Mail */
403 if( exportldif_test_email( person ) ) {
407 /* Bail if no E-Mail address */
408 if( ctl->excludeEMail ) continue;
413 dn = exportldif_find_dn( ctl, person );
416 dn = exportldif_fmt_dn( ctl, person );
418 if( dn == NULL ) continue;
419 ldif_write_value( stream, LDIF_TAG_DN, dn );
423 * Test for schema requirements. This is a simple
424 * test and does not trap all LDAP schema errors.
425 * These can be detected when the LDIF file is
426 * loaded into an LDAP server.
428 if( person->lastName ) {
429 if( strlen( person->lastName ) > 0 ) {
436 ldif_write_value( stream,
437 LDIF_TAG_OBJECTCLASS, LDIF_CLASS_PERSON );
440 ldif_write_value( stream,
441 LDIF_TAG_OBJECTCLASS, LDIF_CLASS_INET_PERSON );
444 /* Format person attributes */
446 stream, LDIF_TAG_COMMONNAME, ADDRITEM_NAME( person ) );
448 stream, LDIF_TAG_LASTNAME, person->lastName );
450 stream, LDIF_TAG_FIRSTNAME, person->firstName );
452 stream, LDIF_TAG_NICKNAME, person->nickName );
455 exportldif_fmt_email( person, stream );
458 ldif_write_eor( stream );
468 * Format an address book folder.
469 * \param ctl Export control data.
470 * \param stream Output stream.
471 * \param folder Folder to format.
472 * \return <i>TRUE</i> if no persons were formatted.
474 static void exportldif_fmt_folder(
475 ExportLdifCtl *ctl, FILE *stream, const ItemFolder *folder )
479 /* Export entries in this folder */
480 exportldif_fmt_person( ctl, stream, folder );
482 /* Export entries in sub-folders */
483 node = folder->listFolder;
485 AddrItemObject *aio = node->data;
487 node = g_list_next( node );
488 if( aio && aio->type == ITEMTYPE_FOLDER ) {
489 ItemFolder *subFolder = ( ItemFolder * ) aio;
490 exportldif_fmt_folder( ctl, stream, subFolder );
496 * Export address book to LDIF file.
497 * \param ctl Export control data.
498 * \param cache Address book/data source cache.
501 void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
503 ItemFolder *rootFolder;
506 ldifFile = fopen( ctl->path, "wb" );
508 /* Cannot open file */
509 ctl->retVal = MGU_OPEN_FILE;
513 rootFolder = cache->rootFolder;
514 exportldif_fmt_folder( ctl, ldifFile, rootFolder );
516 ctl->retVal = MGU_SUCCESS;
520 * Build full export file specification.
521 * \param ctl Export control data.
523 static void exportldif_build_filespec( ExportLdifCtl *ctl ) {
526 fileSpec = g_strconcat(
527 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileLdif, NULL );
528 ctl->path = mgu_replace_string( ctl->path, fileSpec );
533 * Parse directory and filename from full export file specification.
534 * \param ctl Export control data.
535 * \param fileSpec File spec.
537 void exportldif_parse_filespec( ExportLdifCtl *ctl, gchar *fileSpec ) {
541 mgu_replace_string( ctl->fileLdif, g_basename( fileSpec ) );
542 t = g_dirname( fileSpec );
543 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
545 ctl->path = mgu_replace_string( ctl->path, fileSpec );
549 * Test whether output directory exists.
550 * \param ctl Export control data.
551 * \return TRUE if exists.
553 gboolean exportldif_test_dir( ExportLdifCtl *ctl ) {
558 if((dp = opendir( ctl->dirOutput )) != NULL) {
566 * Create output directory.
567 * \param ctl Export control data.
568 * \return TRUE if directory created.
570 gboolean exportldif_create_dir( ExportLdifCtl *ctl ) {
571 gboolean retVal = FALSE;
574 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
578 ctl->rcCreate = errno;
584 * Retrieve create directory error message.
585 * \param ctl Export control data.
588 gchar *exportldif_get_create_msg( ExportLdifCtl *ctl ) {
591 if( ctl->rcCreate == EEXIST ) {
592 msg = _( "Name already exists but is not a directory." );
594 else if( ctl->rcCreate == EACCES ) {
595 msg = _( "No permissions to create directory." );
597 else if( ctl->rcCreate == ENAMETOOLONG ) {
598 msg = _( "Name is too long." );
601 msg = _( "Not specified." );
607 * Set default values.
608 * \param ctl Export control data.
610 static void exportldif_default_values( ExportLdifCtl *ctl ) {
614 g_get_home_dir(), G_DIR_SEPARATOR_S,
615 DFL_DIR_SYLPHEED_OUT, NULL );
617 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
621 mgu_replace_string( ctl->fileLdif, DFL_FILE_SYLPHEED_OUT );
622 ctl->suffix = mgu_replace_string( ctl->suffix, "" );
624 ctl->rdnIndex = EXPORT_LDIF_ID_UID;
626 ctl->retVal = MGU_SUCCESS;
630 * Load settings from XML properties file.
631 * \param ctl Export control data.
633 void exportldif_load_settings( ExportLdifCtl *ctl ) {
636 gchar buf[ XML_BUFSIZE ];
638 props = xmlprops_create();
639 xmlprops_set_path( props, ctl->settingsFile );
640 rc = xmlprops_load_file( props );
644 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
645 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
648 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
649 ctl->fileLdif = mgu_replace_string( ctl->fileLdif, buf );
652 xmlprops_get_property_s( props, EXMLPROP_SUFFIX, buf );
653 ctl->suffix = mgu_replace_string( ctl->suffix, buf );
656 xmlprops_get_property_i( props, EXMLPROP_RDN_INDEX );
658 xmlprops_get_property_b( props, EXMLPROP_USE_DN );
660 xmlprops_get_property_b( props, EXMLPROP_EXCL_EMAIL );
663 /* Set default values */
664 exportldif_default_values( ctl );
666 exportldif_build_filespec( ctl );
667 /* exportldif_print( ctl, stdout ); */
669 xmlprops_free( props );
673 * Save settings to XML properties file.
674 * \param ctl Export control data.
676 void exportldif_save_settings( ExportLdifCtl *ctl ) {
679 props = xmlprops_create();
680 xmlprops_set_path( props, ctl->settingsFile );
682 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
683 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileLdif );
684 xmlprops_set_property( props, EXMLPROP_SUFFIX, ctl->suffix );
685 xmlprops_set_property_i( props, EXMLPROP_RDN_INDEX, ctl->rdnIndex );
686 xmlprops_set_property_b( props, EXMLPROP_USE_DN, ctl->useDN );
687 xmlprops_set_property_b( props, EXMLPROP_EXCL_EMAIL, ctl->excludeEMail );
688 xmlprops_save_file( props );
689 xmlprops_free( props );
693 * ============================================================================
695 * ============================================================================