1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
|
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 2003 Match Grun
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Export address book to LDIF file.
*/
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <glib.h>
#include "intl.h"
#include "mgutils.h"
#include "utils.h"
#include "exportldif.h"
#include "xmlprops.h"
#include "ldif.h"
#define DFL_DIR_SYLPHEED_OUT "sylpheed-out"
#define DFL_FILE_SYLPHEED_OUT "addressbook.ldif"
#define FMT_BUFSIZE 2048
#define XML_BUFSIZE 2048
/* Settings - properties */
#define EXML_PROPFILE_NAME "exportldif.xml"
#define EXMLPROP_DIRECTORY "directory"
#define EXMLPROP_FILE "file"
#define EXMLPROP_SUFFIX "suffix"
#define EXMLPROP_RDN_INDEX "rdn"
#define EXMLPROP_USE_DN "use-dn"
#define EXMLPROP_EXCL_EMAIL "exclude-mail"
static gchar *_attrName_UID_ = "uid";
static gchar *_attrName_DName_ = "cn";
static gchar *_attrName_EMail_ = "mail";
/**
* Create initialized LDIF export control object.
* \return Initialized export control data.
*/
ExportLdifCtl *exportldif_create( void ) {
ExportLdifCtl *ctl = g_new0( ExportLdifCtl, 1 );
ctl->path = NULL;
ctl->dirOutput = NULL;
ctl->fileLdif = NULL;
ctl->suffix = NULL;
ctl->rdnIndex = EXPORT_LDIF_ID_UID;
ctl->useDN = FALSE;
ctl->excludeEMail = TRUE;
ctl->retVal = MGU_SUCCESS;
ctl->rcCreate = 0;
ctl->settingsFile = g_strconcat(
get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
return ctl;
}
/**
* Free up object by releasing internal memory.
* \return ctl Export control data.
*/
void exportldif_free( ExportLdifCtl *ctl ) {
g_return_if_fail( ctl != NULL );
g_free( ctl->path );
g_free( ctl->fileLdif );
g_free( ctl->dirOutput );
g_free( ctl->suffix );
g_free( ctl->settingsFile );
/* Clear pointers */
ctl->path = NULL;
ctl->dirOutput = NULL;
ctl->fileLdif = NULL;
ctl->suffix = NULL;
ctl->rdnIndex = EXPORT_LDIF_ID_UID;
ctl->useDN = FALSE;
ctl->excludeEMail = FALSE;
ctl->retVal = MGU_SUCCESS;
ctl->rcCreate = 0;
/* Now release object */
g_free( ctl );
}
/**
* Print control object.
* \param ctl Export control data.
* \param stream Output stream.
*/
void exportldif_print( ExportLdifCtl *ctl, FILE *stream ) {
fprintf( stream, "ExportLdifCtl:\n" );
fprintf( stream, " path: %s\n", ctl->path );
fprintf( stream, "directory: %s\n", ctl->dirOutput );
fprintf( stream, " file: %s\n", ctl->fileLdif );
fprintf( stream, " suffix: %s\n", ctl->suffix );
fprintf( stream, " rdn: %d\n", ctl->rdnIndex );
fprintf( stream, " use DN: %s\n", ctl->useDN ? "y" : "n" );
fprintf( stream, " ex EMail: %s\n", ctl->excludeEMail ? "y" : "n" );
fprintf( stream, " settings: %s\n", ctl->settingsFile );
}
/**
* Specify directory where LDIF files are created.
* \param ctl Export control data.
* \param value Full directory path.
*/
void exportldif_set_output_dir( ExportLdifCtl *ctl, const gchar *value ) {
g_return_if_fail( ctl != NULL );
ctl->dirOutput = mgu_replace_string( ctl->dirOutput, value );
g_strstrip( ctl->dirOutput );
}
/**
* Specify full file specification of LDIF file.
* \param ctl Export control data.
* \param value Full file specification.
*/
void exportldif_set_path( ExportLdifCtl *ctl, const gchar *value ) {
g_return_if_fail( ctl != NULL );
ctl->path = mgu_replace_string( ctl->path, value );
g_strstrip( ctl->path );
}
/**
* Specify file name of LDIF file.
* \param ctl Export control data.
* \param value File name.
*/
void exportldif_set_file_ldif( ExportLdifCtl *ctl, const gchar *value ) {
g_return_if_fail( ctl != NULL );
ctl->fileLdif = mgu_replace_string( ctl->fileLdif, value );
g_strstrip( ctl->fileLdif );
}
/**
* Specify suffix to be used for creating DN entries.
* \param ctl Export control data.
* \param value Suffix.
*/
void exportldif_set_suffix( ExportLdifCtl *ctl, const char *value ) {
g_return_if_fail( ctl != NULL );
ctl->suffix = mgu_replace_string( ctl->suffix, value );
g_strstrip( ctl->suffix );
}
/**
* Specify index of variable to be used for creating RDN entries.
* \param ctl Export control data.
* \param value Index to variable, as follows:
* <ul>
* <li><code>EXPORT_LDIF_ID_UID</code> - Use Sylpheed UID.</li>
* <li><code>EXPORT_LDIF_ID_DNAME</code> - Use Sylpheed display name.</li>
* <li><code>EXPORT_LDIF_ID_EMAIL</code> - Use first E-Mail address.</li>
* </ul>
*/
void exportldif_set_rdn( ExportLdifCtl *ctl, const gint value ) {
g_return_if_fail( ctl != NULL );
ctl->rdnIndex = value;
}
/**
* Specify that <code>DN</code> attribute, if present, should be used as the
* DN for the entry.
* \param ctl Export control data.
* \param value <i>TRUE</i> if DN should be used.
*/
void exportldif_set_use_dn( ExportLdifCtl *ctl, const gboolean value ) {
g_return_if_fail( ctl != NULL );
ctl->useDN = value;
}
/**
* Specify that records without E-Mail addresses should be excluded.
* \param ctl Export control data.
* \param value <i>TRUE</i> if records without E-Mail should be excluded.
*/
void exportldif_set_exclude_email( ExportLdifCtl *ctl, const gboolean value ) {
g_return_if_fail( ctl != NULL );
ctl->excludeEMail = value;
}
/**
* Format LDAP value name with no embedded commas.
* \param value Data value to format.
* \return Formatted string, should be freed after use.
*/
static gchar *exportldif_fmt_value( gchar *value ) {
gchar *dupval;
gchar *src;
gchar *dest;
gchar ch;
/* Duplicate incoming value */
dest = dupval = g_strdup( value );
/* Copy characters, ignoring commas */
src = value;
while( *src ) {
ch = *src;
if( ch != ',' ) {
*dest = ch;
dest++;
}
src++;
}
*dest = '\0';
return dupval;
}
/**
* Build DN for entry.
* \param ctl Export control data.
* \param person Person to format.
* \return Formatted DN entry.
*/
static gchar *exportldif_fmt_dn(
ExportLdifCtl *ctl, const ItemPerson *person )
{
gchar buf[ FMT_BUFSIZE ];
gchar *retVal = NULL;
gchar *attr = NULL;
gchar *value = NULL;
gchar *dupval = NULL;
/* Process RDN */
*buf = '\0';
if( ctl->rdnIndex == EXPORT_LDIF_ID_UID ) {
attr = _attrName_UID_;
value = ADDRITEM_ID( person );
}
else if( ctl->rdnIndex == EXPORT_LDIF_ID_DNAME ) {
attr = _attrName_DName_;
value = ADDRITEM_NAME( person );
dupval = exportldif_fmt_value( value );
}
else if( ctl->rdnIndex == EXPORT_LDIF_ID_EMAIL ) {
GList *node;
node = person->listEMail;
if( node ) {
ItemEMail *email = node->data;
attr = _attrName_EMail_;
value = email->address;
dupval = exportldif_fmt_value( value );
}
}
/* Format DN */
if( attr ) {
if( value ) {
if( strlen( value ) > 0 ) {
strcat( buf, attr );
strcat( buf, "=" );
if( dupval ) {
/* Format and free duplicated value */
strcat( buf, dupval );
g_free( dupval );
}
else {
/* Use original value */
strcat( buf, value );
}
/* Append suffix */
if( ctl->suffix ) {
if( strlen( ctl->suffix ) > 0 ) {
strcat( buf, "," );
strcat( buf, ctl->suffix );
}
}
retVal = g_strdup( buf );
}
}
}
return retVal;
}
/**
* Find DN by searching attribute list.
* \param ctl Export control data.
* \param person Person to format.
* \return Formatted DN entry, should be freed after use.
*/
static gchar *exportldif_find_dn(
ExportLdifCtl *ctl, const ItemPerson *person )
{
gchar *retVal = NULL;
const GList *node;
node = person->listAttrib;
while( node ) {
UserAttribute *attrib = node->data;
node = g_list_next( node );
if( g_strcasecmp( attrib->name, LDIF_TAG_DN ) == 0 ) {
retVal = g_strdup( attrib->value );
break;
}
}
return retVal;
}
/**
* Format E-Mail entries for person.
* \param person Person to format.
* \param stream Output stream.
* \return <i>TRUE</i> if entry formatted.
*/
static gboolean exportldif_fmt_email( const ItemPerson *person, FILE *stream ) {
gboolean retVal = FALSE;
const GList *node;
node = person->listEMail;
while( node ) {
ItemEMail *email = node->data;
node = g_list_next( node );
ldif_write_value( stream, LDIF_TAG_EMAIL, email->address );
retVal = TRUE;
}
return retVal;
}
/**
* Test for E-Mail entries for person.
* \param person Person to test.
* \return <i>TRUE</i> if person has E-Mail address.
*/
static gboolean exportldif_test_email( const ItemPerson *person )
{
gboolean retVal = FALSE;
const GList *node;
node = person->listEMail;
while( node ) {
ItemEMail *email = node->data;
node = g_list_next( node );
if( email->address ) {
if( strlen( email->address ) > 0 ) {
retVal = TRUE;
break;
}
}
retVal = TRUE;
}
return retVal;
}
/**
* Format persons in an address book folder.
* \param ctl Export control data.
* \param stream Output stream.
* \param folder Folder to format.
* \return <i>TRUE</i> if no persons were formatted.
*/
static gboolean exportldif_fmt_person(
ExportLdifCtl *ctl, FILE *stream, const ItemFolder *folder )
{
gboolean retVal = TRUE;
const GList *node;
if( folder->listPerson == NULL ) return retVal;
node = folder->listPerson;
while( node ) {
AddrItemObject *aio = node->data;
node = g_list_next( node );
if( aio && aio->type == ITEMTYPE_PERSON ) {
ItemPerson *person = ( ItemPerson * ) aio;
gboolean classPerson = FALSE;
gboolean classInetP = FALSE;
gchar *dn = NULL;
/* Check for E-Mail */
if( exportldif_test_email( person ) ) {
classInetP = TRUE;
}
else {
/* Bail if no E-Mail address */
if( ctl->excludeEMail ) continue;
}
/* Format DN */
if( ctl->useDN ) {
dn = exportldif_find_dn( ctl, person );
}
if( dn == NULL ) {
dn = exportldif_fmt_dn( ctl, person );
}
if( dn == NULL ) continue;
ldif_write_value( stream, LDIF_TAG_DN, dn );
g_free( dn );
/*
* Test for schema requirements. This is a simple
* test and does not trap all LDAP schema errors.
* These can be detected when the LDIF file is
* loaded into an LDAP server.
*/
if( person->lastName ) {
if( strlen( person->lastName ) > 0 ) {
classPerson = TRUE;
classInetP = TRUE;
}
}
if( classPerson ) {
ldif_write_value( stream,
LDIF_TAG_OBJECTCLASS, LDIF_CLASS_PERSON );
}
if( classInetP ) {
ldif_write_value( stream,
LDIF_TAG_OBJECTCLASS, LDIF_CLASS_INET_PERSON );
}
/* Format person attributes */
ldif_write_value(
stream, LDIF_TAG_COMMONNAME, ADDRITEM_NAME( person ) );
ldif_write_value(
stream, LDIF_TAG_LASTNAME, person->lastName );
ldif_write_value(
stream, LDIF_TAG_FIRSTNAME, person->firstName );
ldif_write_value(
stream, LDIF_TAG_NICKNAME, person->nickName );
/* Format E-Mail */
exportldif_fmt_email( person, stream );
/* End record */
ldif_write_eor( stream );
retVal = FALSE;
}
}
return retVal;
}
/**
* Format an address book folder.
* \param ctl Export control data.
* \param stream Output stream.
* \param folder Folder to format.
* \return <i>TRUE</i> if no persons were formatted.
*/
static void exportldif_fmt_folder(
ExportLdifCtl *ctl, FILE *stream, const ItemFolder *folder )
{
const GList *node;
/* Export entries in this folder */
exportldif_fmt_person( ctl, stream, folder );
/* Export entries in sub-folders */
node = folder->listFolder;
while( node ) {
AddrItemObject *aio = node->data;
node = g_list_next( node );
if( aio && aio->type == ITEMTYPE_FOLDER ) {
ItemFolder *subFolder = ( ItemFolder * ) aio;
exportldif_fmt_folder( ctl, stream, subFolder );
}
}
}
/**
* Export address book to LDIF file.
* \param ctl Export control data.
* \param cache Address book/data source cache.
* \return Status.
*/
void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
{
ItemFolder *rootFolder;
FILE *ldifFile;
ldifFile = fopen( ctl->path, "wb" );
if( ! ldifFile ) {
/* Cannot open file */
ctl->retVal = MGU_OPEN_FILE;
return;
}
rootFolder = cache->rootFolder;
exportldif_fmt_folder( ctl, ldifFile, rootFolder );
fclose( ldifFile );
ctl->retVal = MGU_SUCCESS;
}
/**
* Build full export file specification.
* \param ctl Export control data.
*/
static void exportldif_build_filespec( ExportLdifCtl *ctl ) {
gchar *fileSpec;
fileSpec = g_strconcat(
ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileLdif, NULL );
ctl->path = mgu_replace_string( ctl->path, fileSpec );
g_free( fileSpec );
}
/**
* Parse directory and filename from full export file specification.
* \param ctl Export control data.
* \param fileSpec File spec.
*/
void exportldif_parse_filespec( ExportLdifCtl *ctl, gchar *fileSpec ) {
gchar *t;
ctl->fileLdif =
mgu_replace_string( ctl->fileLdif, g_basename( fileSpec ) );
t = g_dirname( fileSpec );
ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
g_free( t );
ctl->path = mgu_replace_string( ctl->path, fileSpec );
}
/**
* Test whether output directory exists.
* \param ctl Export control data.
* \return TRUE if exists.
*/
gboolean exportldif_test_dir( ExportLdifCtl *ctl ) {
gboolean retVal;
DIR *dp;
retVal = FALSE;
if((dp = opendir( ctl->dirOutput )) != NULL) {
retVal = TRUE;
closedir( dp );
}
return retVal;
}
/**
* Create output directory.
* \param ctl Export control data.
* \return TRUE if directory created.
*/
gboolean exportldif_create_dir( ExportLdifCtl *ctl ) {
gboolean retVal = FALSE;
ctl->rcCreate = 0;
if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
retVal = TRUE;
}
else {
ctl->rcCreate = errno;
}
return retVal;
}
/**
* Retrieve create directory error message.
* \param ctl Export control data.
* \return Message.
*/
gchar *exportldif_get_create_msg( ExportLdifCtl *ctl ) {
gchar *msg;
if( ctl->rcCreate == EEXIST ) {
msg = _( "Name already exists but is not a directory." );
}
else if( ctl->rcCreate == EACCES ) {
msg = _( "No permissions to create directory." );
}
else if( ctl->rcCreate == ENAMETOOLONG ) {
msg = _( "Name is too long." );
}
else {
msg = _( "Not specified." );
}
return msg;
}
/**
* Set default values.
* \param ctl Export control data.
*/
static void exportldif_default_values( ExportLdifCtl *ctl ) {
gchar *str;
str = g_strconcat(
g_get_home_dir(), G_DIR_SEPARATOR_S,
DFL_DIR_SYLPHEED_OUT, NULL );
ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
g_free( str );
ctl->fileLdif =
mgu_replace_string( ctl->fileLdif, DFL_FILE_SYLPHEED_OUT );
ctl->suffix = mgu_replace_string( ctl->suffix, "" );
ctl->rdnIndex = EXPORT_LDIF_ID_UID;
ctl->useDN = FALSE;
ctl->retVal = MGU_SUCCESS;
}
/**
* Load settings from XML properties file.
* \param ctl Export control data.
*/
void exportldif_load_settings( ExportLdifCtl *ctl ) {
XmlProperty *props;
gint rc;
gchar buf[ XML_BUFSIZE ];
props = xmlprops_create();
xmlprops_set_path( props, ctl->settingsFile );
rc = xmlprops_load_file( props );
if( rc == 0 ) {
/* Read settings */
*buf = '\0';
xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
*buf = '\0';
xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
ctl->fileLdif = mgu_replace_string( ctl->fileLdif, buf );
*buf = '\0';
xmlprops_get_property_s( props, EXMLPROP_SUFFIX, buf );
ctl->suffix = mgu_replace_string( ctl->suffix, buf );
ctl->rdnIndex =
xmlprops_get_property_i( props, EXMLPROP_RDN_INDEX );
ctl->useDN =
xmlprops_get_property_b( props, EXMLPROP_USE_DN );
ctl->excludeEMail =
xmlprops_get_property_b( props, EXMLPROP_EXCL_EMAIL );
}
else {
/* Set default values */
exportldif_default_values( ctl );
}
exportldif_build_filespec( ctl );
/* exportldif_print( ctl, stdout ); */
xmlprops_free( props );
}
/**
* Save settings to XML properties file.
* \param ctl Export control data.
*/
void exportldif_save_settings( ExportLdifCtl *ctl ) {
XmlProperty *props;
props = xmlprops_create();
xmlprops_set_path( props, ctl->settingsFile );
xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileLdif );
xmlprops_set_property( props, EXMLPROP_SUFFIX, ctl->suffix );
xmlprops_set_property_i( props, EXMLPROP_RDN_INDEX, ctl->rdnIndex );
xmlprops_set_property_b( props, EXMLPROP_USE_DN, ctl->useDN );
xmlprops_set_property_b( props, EXMLPROP_EXCL_EMAIL, ctl->excludeEMail );
xmlprops_save_file( props );
xmlprops_free( props );
}
/*
* ============================================================================
* End of Source.
* ============================================================================
*/
|