2005-11-07 [colin] 1.9.99cvs17
[claws.git] / src / ldif.c
index 193e577144652dedf87291cf15a0241d5f4737a9..60c277cabe94b53bf13162ed992e80e457ec6c59 100644 (file)
@@ -14,7 +14,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 /*
@@ -33,6 +33,7 @@
 #include "addrcache.h"
 
 #include "base64.h"
+#include "utils.h"
 
 #define        LDIF_SEP_TAG    ':'
 #define        LDIF_LANG_TAG   ';'
@@ -267,7 +268,7 @@ void ldif_print_file( LdifFile *ldifFile, FILE *stream ) {
 static gint ldif_open_file( LdifFile* ldifFile ) {
        /* printf( "Opening file\n" ); */
        if( ldifFile->path ) {
-               ldifFile->file = fopen( ldifFile->path, "rb" );
+               ldifFile->file = g_fopen( ldifFile->path, "rb" );
                if( ! ldifFile->file ) {
                        /* printf( "can't open %s\n", ldifFile->path ); */
                        ldifFile->retVal = MGU_OPEN_FILE;
@@ -305,26 +306,28 @@ static void ldif_close_file( LdifFile *ldifFile ) {
 static gchar *ldif_get_line( LdifFile *ldifFile ) {
        gchar buf[ LDIFBUFSIZE ];
        gint ch;
-       gchar *ptr;
+       int i = 0;
 
-       if( feof( ldifFile->file ) ) return NULL;
+       if( feof( ldifFile->file ) ) 
+               return NULL;
 
-       ptr = buf;
-       while( TRUE ) {
-               *ptr = '\0';
+       while( i < LDIFBUFSIZE-1 ) {
                ch = fgetc( ldifFile->file );
                if( ch == '\0' || ch == EOF ) {
-                       if( *buf == '\0' ) return NULL;
+                       if( i == 0 ) return NULL;
                        break;
                }
 #if HAVE_DOSISH_SYSTEM
 #else
-               if( ch == '\r' ) continue;
+               if( ch == '\r' ) 
+                       continue;
 #endif
-               if( ch == '\n' ) break;
-               *ptr = ch;
-               ptr++;
+               if( ch == '\n' ) 
+                       break;
+               buf[i] = ch;
+               i++;
        }
+       buf[i] = '\0';
 
        /* Return a copy of buffer */
        return g_strdup( buf );
@@ -482,6 +485,14 @@ static void ldif_build_items(
                        fullName = g_strdup_printf( "%s", lastName );
                }
        }
+       
+       if (!fullName || strlen(fullName) == 0) {
+               g_free(fullName);
+               fullName = NULL;
+               if (rec->listCName)
+                       fullName = g_strdup(rec->listCName->data);
+       }
+       
        if( fullName ) {
                g_strchug( fullName ); g_strchomp( fullName );
        }
@@ -722,7 +733,17 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                        if( lastTag ) {
                                /* Save record */
                                fullValue = mgu_list_coalesce( listValue );
-
+                               if (fullValue && last64) {
+                                       gchar *out = g_malloc(strlen(fullValue));
+                                       int len = 0;
+                                       if ((len = base64_decode(out, fullValue,
+                                                       strlen(fullValue))) >= 0) {
+                                               g_free(fullValue);
+                                               fullValue = out;
+                                               fullValue[len] = '\0';
+                                       } else
+                                               g_free(out);
+                               }
                                /* Base-64 encoded data */
                                /*
                                if( last64 ) {
@@ -763,6 +784,17 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                                        /* Save data */
                                                        fullValue =
                                                                mgu_list_coalesce( listValue );
+                                                       if (fullValue && last64) {
+                                                               gchar *out = g_malloc(strlen(fullValue));
+                                                               int len = 0;
+                                                               if ((len = base64_decode(out, fullValue,
+                                                                               strlen(fullValue))) >= 0) {
+                                                                       g_free(fullValue);
+                                                                       fullValue = out;
+                                                                       fullValue[len] = '\0';
+                                                               } else
+                                                                       g_free(out);
+                                                       }
                                                        /* Base-64 encoded data */
                                                        /*
                                                        if( last64 ) {
@@ -907,7 +939,6 @@ static void ldif_read_tag_list( LdifFile *ldifFile ) {
        /* Process file */
        while( ! flagEOF ) {
                gchar *line = ldif_get_line( ldifFile );
-
                posCur = ftell( ldifFile->file );
                if( ldifFile->cbProgress ) {
                        /* Call progress indicator */
@@ -947,6 +978,7 @@ static void ldif_read_tag_list( LdifFile *ldifFile ) {
                                if( tagName ) {
                                        /* Add tag to list */
                                        listTags = g_slist_append( listTags, tagName );
+
                                        if( g_utf8_collate(
                                                tagName, LDIF_TAG_EMAIL ) == 0 )
                                        {