2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 Match Grun and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Export address book to LDIF file.
33 #include <glib/gi18n.h>
37 #include "exportldif.h"
42 #ifdef MKDIR_TAKES_ONE_ARG
44 #define mkdir(a,b) mkdir(a)
47 #define DFL_DIR_CLAWS_OUT "claws-mail-out"
48 #define DFL_FILE_CLAWS_OUT "addressbook.ldif"
50 #define FMT_BUFSIZE 2048
51 #define XML_BUFSIZE 2048
53 /* Settings - properties */
54 #define EXML_PROPFILE_NAME "exportldif.xml"
55 #define EXMLPROP_DIRECTORY "directory"
56 #define EXMLPROP_FILE "file"
57 #define EXMLPROP_SUFFIX "suffix"
58 #define EXMLPROP_RDN_INDEX "rdn"
59 #define EXMLPROP_USE_DN "use-dn"
60 #define EXMLPROP_EXCL_EMAIL "exclude-mail"
62 static gchar *_attrName_UID_ = "uid";
63 static gchar *_attrName_DName_ = "cn";
64 static gchar *_attrName_EMail_ = "mail";
67 * Create initialized LDIF export control object.
68 * \return Initialized export control data.
70 ExportLdifCtl *exportldif_create( void ) {
71 ExportLdifCtl *ctl = g_new0( ExportLdifCtl, 1 );
74 ctl->dirOutput = NULL;
77 ctl->rdnIndex = EXPORT_LDIF_ID_UID;
79 ctl->excludeEMail = TRUE;
80 ctl->retVal = MGU_SUCCESS;
82 ctl->settingsFile = g_strconcat(
83 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
89 * Free up object by releasing internal memory.
90 * \return ctl Export control data.
92 void exportldif_free( ExportLdifCtl *ctl ) {
93 g_return_if_fail( ctl != NULL );
96 g_free( ctl->fileLdif );
97 g_free( ctl->dirOutput );
98 g_free( ctl->suffix );
99 g_free( ctl->settingsFile );
103 ctl->dirOutput = NULL;
104 ctl->fileLdif = NULL;
106 ctl->rdnIndex = EXPORT_LDIF_ID_UID;
108 ctl->excludeEMail = FALSE;
109 ctl->retVal = MGU_SUCCESS;
112 /* Now release object */
117 * Specify suffix to be used for creating DN entries.
118 * \param ctl Export control data.
119 * \param value Suffix.
121 void exportldif_set_suffix( ExportLdifCtl *ctl, const char *value ) {
122 g_return_if_fail( ctl != NULL );
123 ctl->suffix = mgu_replace_string( ctl->suffix, value );
124 g_strstrip( ctl->suffix );
128 * Specify index of variable to be used for creating RDN entries.
129 * \param ctl Export control data.
130 * \param value Index to variable, as follows:
132 * <li><code>EXPORT_LDIF_ID_UID</code> - Use Sylpheed UID.</li>
133 * <li><code>EXPORT_LDIF_ID_DNAME</code> - Use Sylpheed display name.</li>
134 * <li><code>EXPORT_LDIF_ID_EMAIL</code> - Use first Email address.</li>
137 void exportldif_set_rdn( ExportLdifCtl *ctl, const gint value ) {
138 g_return_if_fail( ctl != NULL );
139 ctl->rdnIndex = value;
143 * Specify that <code>DN</code> attribute, if present, should be used as the
145 * \param ctl Export control data.
146 * \param value <i>TRUE</i> if DN should be used.
148 void exportldif_set_use_dn( ExportLdifCtl *ctl, const gboolean value ) {
149 g_return_if_fail( ctl != NULL );
154 * Specify that records without E-Mail addresses should be excluded.
155 * \param ctl Export control data.
156 * \param value <i>TRUE</i> if records without E-Mail should be excluded.
158 void exportldif_set_exclude_email( ExportLdifCtl *ctl, const gboolean value ) {
159 g_return_if_fail( ctl != NULL );
160 ctl->excludeEMail = value;
164 * Format LDAP value name with no embedded commas.
165 * \param value Data value to format.
166 * \return Formatted string, should be freed after use.
168 static gchar *exportldif_fmt_value( gchar *value ) {
174 /* Duplicate incoming value */
175 dest = dupval = g_strdup( value );
177 /* Copy characters, ignoring commas */
192 * Build DN for entry.
193 * \param ctl Export control data.
194 * \param person Person to format.
195 * \return Formatted DN entry.
197 static gchar *exportldif_fmt_dn(
198 ExportLdifCtl *ctl, const ItemPerson *person )
200 gchar buf[ FMT_BUFSIZE ];
201 gchar *retVal = NULL;
204 gchar *dupval = NULL;
208 if( ctl->rdnIndex == EXPORT_LDIF_ID_UID ) {
209 attr = _attrName_UID_;
210 value = ADDRITEM_ID( person );
212 else if( ctl->rdnIndex == EXPORT_LDIF_ID_DNAME ) {
213 attr = _attrName_DName_;
214 value = ADDRITEM_NAME( person );
215 dupval = exportldif_fmt_value( value );
217 else if( ctl->rdnIndex == EXPORT_LDIF_ID_EMAIL ) {
220 node = person->listEMail;
222 ItemEMail *email = node->data;
224 attr = _attrName_EMail_;
225 value = email->address;
226 dupval = exportldif_fmt_value( value );
233 if( strlen( value ) > 0 ) {
237 /* Format and free duplicated value */
238 strcat( buf, dupval );
242 /* Use original value */
243 strcat( buf, value );
248 if( strlen( ctl->suffix ) > 0 ) {
250 strcat( buf, ctl->suffix );
254 retVal = g_strdup( buf );
262 * Find DN by searching attribute list.
263 * \param ctl Export control data.
264 * \param person Person to format.
265 * \return Formatted DN entry, should be freed after use.
267 static gchar *exportldif_find_dn(
268 ExportLdifCtl *ctl, const ItemPerson *person )
270 gchar *retVal = NULL;
273 node = person->listAttrib;
275 UserAttribute *attrib = node->data;
277 node = g_list_next( node );
278 if( g_utf8_collate( attrib->name, LDIF_TAG_DN ) == 0 ) {
279 retVal = g_strdup( attrib->value );
287 * Format E-Mail entries for person.
288 * \param person Person to format.
289 * \param stream Output stream.
290 * \return <i>TRUE</i> if entry formatted.
292 static gboolean exportldif_fmt_email( const ItemPerson *person, FILE *stream ) {
293 gboolean retVal = FALSE;
296 node = person->listEMail;
298 ItemEMail *email = node->data;
300 node = g_list_next( node );
301 ldif_write_value( stream, LDIF_TAG_EMAIL, email->address );
308 * Test for E-Mail entries for person.
309 * \param person Person to test.
310 * \return <i>TRUE</i> if person has E-Mail address.
312 static gboolean exportldif_test_email( const ItemPerson *person )
314 gboolean retVal = FALSE;
317 node = person->listEMail;
319 ItemEMail *email = node->data;
321 node = g_list_next( node );
322 if( email->address ) {
323 if( strlen( email->address ) > 0 ) {
334 * Format persons in an address book folder.
335 * \param ctl Export control data.
336 * \param stream Output stream.
337 * \param folder Folder to format.
338 * \return <i>TRUE</i> if no persons were formatted.
340 static gboolean exportldif_fmt_person(
341 ExportLdifCtl *ctl, FILE *stream, const ItemFolder *folder )
343 gboolean retVal = TRUE;
346 if( folder->listPerson == NULL ) return retVal;
348 node = folder->listPerson;
350 AddrItemObject *aio = node->data;
351 node = g_list_next( node );
353 if( aio && aio->type == ITEMTYPE_PERSON ) {
354 ItemPerson *person = ( ItemPerson * ) aio;
355 gboolean classPerson = FALSE;
356 gboolean classInetP = FALSE;
359 /* Check for E-Mail */
360 if( exportldif_test_email( person ) ) {
364 /* Bail if no E-Mail address */
365 if( ctl->excludeEMail ) continue;
370 dn = exportldif_find_dn( ctl, person );
373 dn = exportldif_fmt_dn( ctl, person );
375 if( dn == NULL ) continue;
376 ldif_write_value( stream, LDIF_TAG_DN, dn );
380 * Test for schema requirements. This is a simple
381 * test and does not trap all LDAP schema errors.
382 * These can be detected when the LDIF file is
383 * loaded into an LDAP server.
385 if( person->lastName ) {
386 if( strlen( person->lastName ) > 0 ) {
393 ldif_write_value( stream,
394 LDIF_TAG_OBJECTCLASS, LDIF_CLASS_PERSON );
397 ldif_write_value( stream,
398 LDIF_TAG_OBJECTCLASS, LDIF_CLASS_INET_PERSON );
401 /* Format person attributes */
403 stream, LDIF_TAG_COMMONNAME, ADDRITEM_NAME( person ) );
405 stream, LDIF_TAG_LASTNAME, person->lastName );
407 stream, LDIF_TAG_FIRSTNAME, person->firstName );
409 stream, LDIF_TAG_NICKNAME, person->nickName );
412 exportldif_fmt_email( person, stream );
415 ldif_write_eor( stream );
425 * Format an address book folder.
426 * \param ctl Export control data.
427 * \param stream Output stream.
428 * \param folder Folder to format.
429 * \return <i>TRUE</i> if no persons were formatted.
431 static void exportldif_fmt_folder(
432 ExportLdifCtl *ctl, FILE *stream, const ItemFolder *folder )
436 /* Export entries in this folder */
437 exportldif_fmt_person( ctl, stream, folder );
439 /* Export entries in sub-folders */
440 node = folder->listFolder;
442 AddrItemObject *aio = node->data;
444 node = g_list_next( node );
445 if( aio && aio->type == ITEMTYPE_FOLDER ) {
446 ItemFolder *subFolder = ( ItemFolder * ) aio;
447 exportldif_fmt_folder( ctl, stream, subFolder );
453 * Export address book to LDIF file.
454 * \param ctl Export control data.
455 * \param cache Address book/data source cache.
458 void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
460 ItemFolder *rootFolder;
463 ldifFile = g_fopen( ctl->path, "wb" );
465 /* Cannot open file */
466 ctl->retVal = MGU_OPEN_FILE;
470 rootFolder = cache->rootFolder;
471 exportldif_fmt_folder( ctl, ldifFile, rootFolder );
473 ctl->retVal = MGU_SUCCESS;
477 * Build full export file specification.
478 * \param ctl Export control data.
480 static void exportldif_build_filespec( ExportLdifCtl *ctl ) {
483 fileSpec = g_strconcat(
484 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileLdif, NULL );
485 ctl->path = mgu_replace_string( ctl->path, fileSpec );
490 * Parse directory and filename from full export file specification.
491 * \param ctl Export control data.
492 * \param fileSpec File spec.
494 void exportldif_parse_filespec( ExportLdifCtl *ctl, gchar *fileSpec ) {
496 gchar *base = g_path_get_basename(fileSpec);
499 mgu_replace_string( ctl->fileLdif, base );
501 t = g_path_get_dirname( fileSpec );
502 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
504 ctl->path = mgu_replace_string( ctl->path, fileSpec );
508 * Test whether output directory exists.
509 * \param ctl Export control data.
510 * \return TRUE if exists.
512 gboolean exportldif_test_dir( ExportLdifCtl *ctl ) {
517 if((dp = opendir( ctl->dirOutput )) != NULL) {
525 * Create output directory.
526 * \param ctl Export control data.
527 * \return TRUE if directory created.
529 gboolean exportldif_create_dir( ExportLdifCtl *ctl ) {
530 gboolean retVal = FALSE;
533 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
537 ctl->rcCreate = errno;
543 * Retrieve create directory error message.
544 * \param ctl Export control data.
547 gchar *exportldif_get_create_msg( ExportLdifCtl *ctl ) {
550 if( ctl->rcCreate == EEXIST ) {
551 msg = _( "Name already exists but is not a directory." );
553 else if( ctl->rcCreate == EACCES ) {
554 msg = _( "No permissions to create directory." );
556 else if( ctl->rcCreate == ENAMETOOLONG ) {
557 msg = _( "Name is too long." );
560 msg = _( "Not specified." );
566 * Set default values.
567 * \param ctl Export control data.
569 static void exportldif_default_values( ExportLdifCtl *ctl ) {
573 get_home_dir(), G_DIR_SEPARATOR_S,
574 DFL_DIR_CLAWS_OUT, NULL );
576 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
580 mgu_replace_string( ctl->fileLdif, DFL_FILE_CLAWS_OUT );
581 ctl->suffix = mgu_replace_string( ctl->suffix, "" );
583 ctl->rdnIndex = EXPORT_LDIF_ID_UID;
585 ctl->retVal = MGU_SUCCESS;
589 * Load settings from XML properties file.
590 * \param ctl Export control data.
592 void exportldif_load_settings( ExportLdifCtl *ctl ) {
595 gchar buf[ XML_BUFSIZE ];
597 props = xmlprops_create();
598 xmlprops_set_path( props, ctl->settingsFile );
599 rc = xmlprops_load_file( props );
603 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
604 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
607 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
608 ctl->fileLdif = mgu_replace_string( ctl->fileLdif, buf );
611 xmlprops_get_property_s( props, EXMLPROP_SUFFIX, buf );
612 ctl->suffix = mgu_replace_string( ctl->suffix, buf );
615 xmlprops_get_property_i( props, EXMLPROP_RDN_INDEX );
617 xmlprops_get_property_b( props, EXMLPROP_USE_DN );
619 xmlprops_get_property_b( props, EXMLPROP_EXCL_EMAIL );
622 /* Set default values */
623 exportldif_default_values( ctl );
625 exportldif_build_filespec( ctl );
626 /* exportldif_print( ctl, stdout ); */
628 xmlprops_free( props );
632 * Save settings to XML properties file.
633 * \param ctl Export control data.
635 void exportldif_save_settings( ExportLdifCtl *ctl ) {
638 props = xmlprops_create();
639 xmlprops_set_path( props, ctl->settingsFile );
641 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
642 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileLdif );
643 xmlprops_set_property( props, EXMLPROP_SUFFIX, ctl->suffix );
644 xmlprops_set_property_i( props, EXMLPROP_RDN_INDEX, ctl->rdnIndex );
645 xmlprops_set_property_b( props, EXMLPROP_USE_DN, ctl->useDN );
646 xmlprops_set_property_b( props, EXMLPROP_EXCL_EMAIL, ctl->excludeEMail );
647 xmlprops_save_file( props );
648 xmlprops_free( props );
652 * ============================================================================
654 * ============================================================================