Changeset 2317

Show
Ignore:
Timestamp:
06/29/07 15:22:34
Author:
pvanhoof
Message:

Bugfix in previous commit. Unescaping characters like %20

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libtinymail-camel/tny-camel-store-account.c

    r2315 r2317  
    2121#include <glib/gi18n-lib.h> 
    2222#include <glib.h> 
     23 
     24#ifndef _GNU_SOURCE 
     25#define _GNU_SOURCE 
     26#endif 
     27 
    2328#include <string.h> 
    2429 
     
    12451250} 
    12461251 
     1252 
     1253static int 
     1254hex_to_int (gchar c) 
     1255{ 
     1256        return  c >= '0' && c <= '9' ? c - '0' 
     1257                : c >= 'A' && c <= 'F' ? c - 'A' + 10 
     1258                : c >= 'a' && c <= 'f' ? c - 'a' + 10 
     1259                : -1; 
     1260} 
     1261 
     1262static int 
     1263unescape_character (const char *scanner) 
     1264{ 
     1265        int first_digit; 
     1266        int second_digit; 
     1267 
     1268        first_digit = hex_to_int (*scanner++); 
     1269        if (first_digit < 0) { 
     1270                return -1; 
     1271        } 
     1272 
     1273        second_digit = hex_to_int (*scanner++); 
     1274        if (second_digit < 0) { 
     1275                return -1; 
     1276        } 
     1277 
     1278        return (first_digit << 4) | second_digit; 
     1279} 
     1280 
     1281 
     1282static char * 
     1283unescape_string (const gchar *escaped_string, 
     1284                const gchar *illegal_characters) 
     1285{ 
     1286        const gchar *in; 
     1287        gchar *out, *result; 
     1288        gint character; 
     1289 
     1290        if (escaped_string == NULL) { 
     1291                return NULL; 
     1292        } 
     1293 
     1294        result = g_malloc (strlen (escaped_string) + 1); 
     1295 
     1296        out = result; 
     1297        for (in = escaped_string; *in != '\0'; in++) { 
     1298                character = *in; 
     1299                if (*in == '%') { 
     1300                        character = unescape_character (in + 1); 
     1301 
     1302                        /* Check for an illegal character. We consider '\0' illegal here. */ 
     1303                        if (character <= 0 
     1304                            || (illegal_characters != NULL 
     1305                                && strchr (illegal_characters, (char)character) != NULL)) { 
     1306                                g_free (result); 
     1307                                return NULL; 
     1308                        } 
     1309                        in += 2; 
     1310                } 
     1311                *out++ = (char)character; 
     1312        } 
     1313 
     1314        *out = '\0'; 
     1315        g_assert (out - result <= strlen (escaped_string)); 
     1316        return result; 
     1317 
     1318} 
     1319 
    12471320static TnyFolder* 
    12481321tny_camel_store_account_find_folder_default (TnyStoreAccount *self, const gchar *url_string, GError **err) 
     
    12521325        CamelException ex = CAMEL_EXCEPTION_INITIALISER;     
    12531326        CamelFolderInfo *iter = NULL; guint32 flags; CamelStore *store; 
    1254         char *str = NULL
     1327        char *str = NULL, *nnstr = NULL
    12551328 
    12561329        g_assert (CAMEL_IS_SESSION (apriv->session)); 
     
    12931366                flags |= CAMEL_STORE_FOLDER_INFO_SUBSCRIBED; 
    12941367 
    1295         str = strchr (url_string, '/'); 
    1296  
    1297         if (str && strlen (str) > 1) { 
    1298                 str++; 
    1299                 str = strchr (str, '/'); 
    1300         } else  
    1301                 str = NULL; 
    1302  
    1303         if (str && strlen (str) > 1) { 
    1304                 str++; 
    1305                 str = strchr (str, '/'); 
    1306         } else 
    1307                 str = NULL; 
    1308  
    1309         if (str && strlen (str) > 1) { 
    1310                 str++; 
     1368        if (strcasestr (url_string, "maildir")) 
     1369        { 
     1370                // maildir:/home/xxx/.modest/local_folders#New%20folder/1183044323.16675_6.mindcrime_2_S_2_S 
     1371 
     1372                str = strchr (url_string, '#'); 
     1373                if (str && strlen (str) > 1) 
     1374                { 
     1375                        char *nstr; 
     1376 
     1377                        nnstr = unescape_string (str, NULL); 
     1378                        if (nnstr && strlen (nnstr) > 1) 
     1379                        { 
     1380                                nnstr++; 
     1381                                nstr = strchr (nnstr, '/'); 
     1382                                if (nstr) 
     1383                                        *nstr = '\0'; 
     1384                        } 
     1385                        str = nnstr; 
     1386                } else  
     1387                        str = NULL; 
     1388        } else { 
     1389                str = strchr (url_string, '/'); 
     1390 
     1391                if (str && strlen (str) > 1) { 
     1392                        str++; 
     1393                        str = strchr (str, '/'); 
     1394                } else  
     1395                        str = NULL; 
     1396 
     1397                if (str && strlen (str) > 1) { 
     1398                        str++; 
     1399                        str = strchr (str, '/'); 
     1400                } else 
     1401                        str = NULL; 
     1402 
     1403                if (str && strlen (str) > 1) { 
     1404                        char *nstr; 
     1405                        str++; 
     1406                        nstr = strchr (str, '/'); 
     1407                        if (nstr) 
     1408                                *nstr = '\0'; 
     1409                } else  
     1410                        str = NULL; 
     1411        } 
     1412 
     1413        if (str)  
     1414        { 
    13111415                iter = camel_store_get_folder_info (store, str, flags, &ex); 
     1416 
     1417                if (nnstr) 
     1418                        g_free (nnstr); 
     1419 
    13121420        } else { 
    13131421                g_set_error (err, TNY_FOLDER_STORE_ERROR,  
     
    13151423                        "Invalid URL string"); 
    13161424                _tny_session_stop_operation (apriv->session); 
     1425 
     1426                if (nnstr) 
     1427                        g_free (nnstr); 
     1428 
    13171429                return NULL; 
    13181430        }