aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imapp/camel-imapp-store.c
blob: ad52b8790e68e3540c4c9ae4b79d5daeff147b38 (plain) (blame)
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-imap-store.c : class for a imap store */

/* 
 * Authors: Michael Zucchi <notzed@ximian.com>
 *
 * Copyright (C) 2000-2002 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of version 2 of the GNU General Public 
 * License as published by the Free Software Foundation.
 *
 * 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
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "camel/camel-operation.h"

#include "camel/camel-stream-buffer.h"
#include "camel/camel-session.h"
#include "camel/camel-exception.h"
#include "camel/camel-url.h"
#include "camel/camel-sasl.h"
#include "camel/camel-data-cache.h"
#include "camel/camel-tcp-stream.h"
#include "camel/camel-tcp-stream-raw.h"
#ifdef HAVE_SSL
#include "camel/camel-tcp-stream-ssl.h"
#endif

#include "camel-imapp-store-summary.h"
#include "camel-imapp-store.h"
#include "camel-imapp-folder.h"
#include "camel-imapp-engine.h"
#include "camel-imapp-exception.h"
#include "camel-imapp-utils.h"
#include "camel-imapp-driver.h"

/* Specified in RFC 2060 section 2.1 */
#define IMAP_PORT 143

static CamelStoreClass *parent_class = NULL;

static void finalize (CamelObject *object);

static void imap_construct(CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex);
/* static char *imap_get_name(CamelService *service, gboolean brief);*/
static gboolean imap_connect (CamelService *service, CamelException *ex);
static gboolean imap_disconnect (CamelService *service, gboolean clean, CamelException *ex);
static GList *imap_query_auth_types (CamelService *service, CamelException *ex);

static void imap_init_trash (CamelStore *store);
static CamelFolder *imap_get_trash  (CamelStore *store, CamelException *ex);

static CamelFolder *imap_get_folder(CamelStore * store, const char *folder_name, guint32 flags, CamelException * ex);
static CamelFolder *imap_get_inbox (CamelStore *store, CamelException *ex);
static void imap_rename_folder(CamelStore *store, const char *old_name, const char *new_name, CamelException *ex);
static CamelFolderInfo *imap_get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex);
static void imap_delete_folder(CamelStore *store, const char *folder_name, CamelException *ex);
static void imap_rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex);
static CamelFolderInfo *imap_create_folder(CamelStore *store, const char *parent_name, const char *folder_name, CamelException *ex);

static int store_resp_list(CamelIMAPPEngine *ie, guint32 id, void *data);

/* yet to see if this should go global or not */
void camel_imapp_store_folder_selected(CamelIMAPPStore *store, CamelIMAPPFolder *folder, CamelIMAPPSelectResponse *select);

static void
camel_imapp_store_class_init (CamelIMAPPStoreClass *camel_imapp_store_class)
{
    CamelServiceClass *camel_service_class = CAMEL_SERVICE_CLASS(camel_imapp_store_class);
    CamelStoreClass *camel_store_class = CAMEL_STORE_CLASS(camel_imapp_store_class);

    parent_class = CAMEL_STORE_CLASS(camel_type_get_global_classfuncs(camel_store_get_type()));
    
    /* virtual method overload */
    camel_service_class->construct = imap_construct;
    /*camel_service_class->get_name = imap_get_name;*/
    camel_service_class->query_auth_types = imap_query_auth_types;
    camel_service_class->connect = imap_connect;
    camel_service_class->disconnect = imap_disconnect;

    camel_store_class->init_trash = imap_init_trash;
    camel_store_class->get_trash = imap_get_trash;

    camel_store_class->get_folder = imap_get_folder;
    camel_store_class->get_inbox = imap_get_inbox;

    camel_store_class->create_folder = imap_create_folder;
    camel_store_class->rename_folder = imap_rename_folder;
    camel_store_class->delete_folder = imap_delete_folder;
    camel_store_class->get_folder_info = imap_get_folder_info;
}

static void
camel_imapp_store_init (gpointer object, gpointer klass)
{
    CamelIMAPPStore *istore = object;
}

CamelType
camel_imapp_store_get_type (void)
{
    static CamelType camel_imapp_store_type = CAMEL_INVALID_TYPE;

    if (!camel_imapp_store_type) {
        camel_imapp_store_type = camel_type_register(CAMEL_STORE_TYPE,
                                "CamelIMAPPStore",
                                sizeof (CamelIMAPPStore),
                                sizeof (CamelIMAPPStoreClass),
                                (CamelObjectClassInitFunc) camel_imapp_store_class_init,
                                NULL,
                                (CamelObjectInitFunc) camel_imapp_store_init,
                                finalize);
    }

    return camel_imapp_store_type;
}

static void
finalize (CamelObject *object)
{
    CamelIMAPPStore *imap_store = CAMEL_IMAPP_STORE (object);

    /* force disconnect so we dont have it run later, after we've cleaned up some stuff */
    /* SIGH */

    camel_service_disconnect((CamelService *)imap_store, TRUE, NULL);

    if (imap_store->driver)
        camel_object_unref(imap_store->driver);
    if (imap_store->cache)
        camel_object_unref(imap_store->cache);
}

static void imap_construct(CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
{
    char *root, *summary;
    CamelIMAPPStore *store = (CamelIMAPPStore *)service;

    CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
    if (camel_exception_is_set(ex))
        return;
    
    CAMEL_TRY {
        store->summary = camel_imapp_store_summary_new();
        root = camel_session_get_storage_path(service->session, service, ex);
        if (root) {
            summary = g_build_filename(root, ".ev-store-summary", NULL);
            camel_store_summary_set_filename((CamelStoreSummary *)store->summary, summary);
            /* FIXME: need to remove params, passwords, etc */
            camel_store_summary_set_uri_base((CamelStoreSummary *)store->summary, service->url);
            camel_store_summary_load((CamelStoreSummary *)store->summary);
        }
    } CAMEL_CATCH(e) {
        camel_exception_xfer(ex, e);
    } CAMEL_DONE;
}

enum {
    USE_SSL_NEVER,
    USE_SSL_ALWAYS,
    USE_SSL_WHEN_POSSIBLE
};

#define SSL_PORT_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_SSL2 | CAMEL_TCP_STREAM_SSL_ENABLE_SSL3)
#define STARTTLS_FLAGS (CAMEL_TCP_STREAM_SSL_ENABLE_TLS)

static void
connect_to_server (CamelService *service, int ssl_mode, int try_starttls)
/* throws IO exception */
{
    CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service);
    CamelStream * volatile tcp_stream = NULL;
    CamelIMAPPStream * volatile imap_stream = NULL;
    struct hostent *h = NULL;
    int ret, port;
    CamelException *ex;

    ex = camel_exception_new();
    CAMEL_TRY {
        /* parent class connect initialization */
        CAMEL_SERVICE_CLASS (parent_class)->connect (service, ex);
        if (ex->id)
            camel_exception_throw_ex(ex);

        h = camel_service_gethost(service, ex);
        if (ex->id)
            camel_exception_throw_ex(ex);
        
        port = service->url->port ? service->url->port : IMAP_PORT;
        
#ifdef HAVE_SSL 
        if (camel_url_get_param (service->url, "use_ssl")) {
            if (try_starttls)
                tcp_stream = camel_tcp_stream_ssl_new_raw (service->session, service->url->host, STARTTLS_FLAGS);
            else {
                port = service->url->port ? service->url->port : 995;
                tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS);
            }
        } else {
            tcp_stream = camel_tcp_stream_raw_new ();
        }
#else   
        tcp_stream = camel_tcp_stream_raw_new ();
#endif /* HAVE_SSL */
        
        ret = camel_tcp_stream_connect (CAMEL_TCP_STREAM (tcp_stream), h, port);
        camel_free_host (h);
        if (ret == -1) {
            if (errno == EINTR)
                camel_exception_throw(CAMEL_EXCEPTION_USER_CANCEL, _("Connection cancelled"));
            else
                camel_exception_throw(CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                              _("Could not connect to %s (port %d): %s"),
                              service->url->host, port, strerror(errno));
        }

        imap_stream = (CamelIMAPPStream *)camel_imapp_stream_new(tcp_stream);
        store->driver = camel_imapp_driver_new(imap_stream);

        camel_object_unref(imap_stream);
        camel_object_unref(tcp_stream);
    } CAMEL_CATCH(e) {
        if (tcp_stream)
            camel_object_unref(tcp_stream);
        if (imap_stream)
            camel_object_unref((CamelObject *)imap_stream);
        camel_exception_throw_ex(e);
    } CAMEL_DONE;

    camel_exception_free(ex);
}

#if 0

/* leave this stuff out for now */


static struct {
    char *value;
    int mode;
} ssl_options[] = {
    { "",              USE_SSL_ALWAYS        },
    { "always",        USE_SSL_ALWAYS        },
    { "when-possible", USE_SSL_WHEN_POSSIBLE },
    { "never",         USE_SSL_NEVER         },
    { NULL,            USE_SSL_NEVER         },
};

static gboolean
connect_to_server_wrapper (CamelService *service, CamelException *ex)
{
#ifdef HAVE_SSL
    const char *use_ssl;
    int i, ssl_mode;
    
    use_ssl = camel_url_get_param (service->url, "use_ssl");
    if (use_ssl) {
        for (i = 0; ssl_options[i].value; i++)
            if (!strcmp (ssl_options[i].value, use_ssl))
                break;
        ssl_mode = ssl_options[i].mode;
    } else
        ssl_mode = USE_SSL_NEVER;
    
    if (ssl_mode == USE_SSL_ALWAYS) {
        /* First try the ssl port */
        if (!connect_to_server (service, ssl_mode, FALSE, ex)) {
            if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) {
                /* The ssl port seems to be unavailable, lets try STARTTLS */
                camel_exception_clear (ex);
                return connect_to_server (service, ssl_mode, TRUE, ex);
            } else {
                return FALSE;
            }
        }
        
        return TRUE;
    } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
        /* If the server supports STARTTLS, use it */
        return connect_to_server (service, ssl_mode, TRUE, ex);
    } else {
        /* User doesn't care about SSL */
        return connect_to_server (service, ssl_mode, FALSE, ex);
    }
#else
    return connect_to_server (service, USE_SSL_NEVER, FALSE, ex);
#endif
}
#endif

extern CamelServiceAuthType camel_imapp_password_authtype;
extern CamelServiceAuthType camel_imapp_apop_authtype;

static GList *
imap_query_auth_types (CamelService *service, CamelException *ex)
{
    /*CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service);*/
    GList *types = NULL;

        types = CAMEL_SERVICE_CLASS (parent_class)->query_auth_types (service, ex);
    if (types == NULL)
        return NULL;

#if 0
    if (connect_to_server_wrapper (service, NULL)) {
        types = g_list_concat(types, g_list_copy(store->engine->auth));
        imap_disconnect (service, TRUE, NULL);
    } else {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      _("Could not connect to POP server on %s"),
                      service->url->host);
    }
#endif
    return types;
}

static void
store_get_pass(CamelIMAPPStore *store)
{
    if (((CamelService *)store)->url->passwd == NULL) {
        char *prompt;
        CamelException ex;

        camel_exception_init(&ex);
        
        prompt = g_strdup_printf (_("%sPlease enter the IMAP password for %s@%s"),
                      store->login_error?store->login_error:"",
                      ((CamelService *)store)->url->user,
                      ((CamelService *)store)->url->host);
        ((CamelService *)store)->url->passwd = camel_session_get_password(camel_service_get_session((CamelService *)store),
                                          prompt, CAMEL_SESSION_PASSWORD_SECRET,
                                          (CamelService*)store, "password", &ex);
        g_free (prompt);
        if (camel_exception_is_set(&ex))
            camel_exception_throw_ex(&ex);
    }
}

static struct _CamelSasl *
store_get_sasl(struct _CamelIMAPPDriver *driver, CamelIMAPPStore *store)
{
    store_get_pass(store);

    if (((CamelService *)store)->url->authmech)
        return camel_sasl_new("imap", ((CamelService *)store)->url->authmech, (CamelService *)store);

    return NULL;
}

static void
store_get_login(struct _CamelIMAPPDriver *driver, char **login, char **pass, CamelIMAPPStore *store)
{
    store_get_pass(store);
    
    *login = g_strdup(((CamelService *)store)->url->user);
    *pass = g_strdup(((CamelService *)store)->url->passwd);
}

static gboolean
imap_connect (CamelService *service, CamelException *ex)
{
    CamelIMAPPStore *store = (CamelIMAPPStore *)service;
    volatile int ret = FALSE;

    CAMEL_TRY {
        volatile int retry = TRUE;

        if (store->cache == NULL) {
            char *root;
            
            root = camel_session_get_storage_path(service->session, service, ex);
            if (root) {
                store->cache = camel_data_cache_new(root, 0, ex);
                g_free(root);
                if (store->cache) {
                    /* Default cache expiry - 1 week or not visited in a day */
                    camel_data_cache_set_expire_age(store->cache, 60*60*24*7);
                    camel_data_cache_set_expire_access(store->cache, 60*60*24);
                }
            }
            if (camel_exception_is_set(ex))
                camel_exception_throw_ex(ex);
        }

        connect_to_server(service, USE_SSL_NEVER, FALSE);

        camel_imapp_driver_set_sasl_factory(store->driver, (CamelIMAPPSASLFunc)store_get_sasl, store);
        camel_imapp_driver_set_login_query(store->driver, (CamelIMAPPLoginFunc)store_get_login, store);
        store->login_error = NULL;

        do {
            CAMEL_TRY {
                if (store->driver->engine->state != IMAP_ENGINE_AUTH)
                    camel_imapp_driver_login(store->driver);
                ret = TRUE;
                retry = FALSE;
            } CAMEL_CATCH(e) {
                g_free(store->login_error);
                store->login_error = NULL;
                switch (e->id) {
                case CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE:
                    store->login_error = g_strdup_printf("%s\n\n", e->desc);
                    camel_session_forget_password(service->session, service, "password", ex);
                    camel_url_set_passwd(service->url, NULL);
                    break;
                default:
                    camel_exception_throw_ex(e);
                    break;
                }
            } CAMEL_DONE;
        } while (retry);
    } CAMEL_CATCH(e) {
        camel_exception_xfer(ex, e);
        camel_service_disconnect(service, TRUE, NULL);
        ret = FALSE;
    } CAMEL_DONE;

    g_free(store->login_error);
    store->login_error = NULL;

    return ret;
}

static gboolean
imap_disconnect (CamelService *service, gboolean clean, CamelException *ex)
{
    CamelIMAPPStore *store = CAMEL_IMAPP_STORE (service);

    /* FIXME: logout */
    
    if (!CAMEL_SERVICE_CLASS (parent_class)->disconnect (service, clean, ex))
        return FALSE;

    /* logout/disconnect */
    if (store->driver) {
        camel_object_unref(store->driver);
        store->driver = NULL;
    }
    
    return TRUE;
}

static void
imap_init_trash (CamelStore *store)
{
    /* no-op */
    ;
}

static CamelFolder *
imap_get_trash (CamelStore *store, CamelException *ex)
{
    /* no-op */
    return NULL;
}

static CamelFolder *
imap_get_folder (CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex)
{
    CamelIMAPPStore *istore = (CamelIMAPPStore *)store;
    CamelIMAPPFolder * volatile folder = NULL;

    /* ??? */

    /* 1. create the folder */
    /* 2. run select? */
    /* 3. update the folder */

    CAMEL_TRY {
        folder = (CamelIMAPPFolder *)camel_imapp_folder_new(store, folder_name);
        camel_imapp_driver_select(istore->driver, folder);
    } CAMEL_CATCH (e) {
        if (folder) {
            camel_object_unref(folder);
            folder = NULL;
        }
        camel_exception_xfer(ex, e);
    } CAMEL_DONE;

    return (CamelFolder *)folder;
}

static CamelFolder *
imap_get_inbox(CamelStore *store, CamelException *ex)
{
    camel_exception_setv(ex, 1, "get_inbox::unimplemented");

    return NULL;
}

/* 8 bit, string compare */
static int folders_build_cmp(const void *app, const void *bpp)
{
    struct _list_info *a = *((struct _list_info **)app);
    struct _list_info *b = *((struct _list_info **)bpp);
    unsigned char *ap = (unsigned char *)(a->name);
    unsigned char *bp = (unsigned char *)(b->name);

    printf("qsort, cmp '%s' <> '%s'\n", ap, bp);

    while (*ap && *ap == *bp) {
        ap++;
        bp++;
    }

    if (*ap < *bp)
        return -1;
    else if (*ap > *bp)
        return 1;
    return 0;
}

/* FIXME: this should go via storesummary? */
static CamelFolderInfo *
folders_build_info(CamelURL *base, struct _list_info *li)
{
    char *path, *full_name, *name;
    CamelFolderInfo *fi;

    full_name = imapp_list_get_path(li);
    name = strrchr(full_name, '/');
    if (name)
        name++;
    else
        name = full_name;

    path = alloca(strlen(full_name)+2);
    sprintf(path, "/%s", full_name);
    camel_url_set_path(base, path);

    fi = g_malloc0(sizeof(*fi));
    fi->url = camel_url_to_string(base, CAMEL_URL_HIDE_ALL);
    fi->name = g_strdup(name);
    fi->full_name = full_name;
    fi->path = g_strdup(path);
    fi->unread_message_count = -1;
    fi->flags = li->flags;

    /* TODO: could look up count here ... */
    /* ?? */
    /*folder = camel_object_bag_get(store->folders, "INBOX");*/

    return fi;
}

/*
  a
  a/b
  a/b/c
  a/d
  b
  c/d

*/

/* note, pname is the raw name, not the folderinfo name */
/* note also this free's as we go, since we never go 'backwards' */
static CamelFolderInfo *
folders_build_rec(CamelURL *base, GPtrArray *folders, int *ip, CamelFolderInfo *pfi, char *pname)
{
    int plen = 0;
    CamelFolderInfo *last = NULL, *first = NULL;

    if (pfi)
        plen = strlen(pname);

    for(;(*ip)<(int)folders->len;) {
        CamelFolderInfo *fi;
        struct _list_info *li;

        li = folders->pdata[*ip];
        printf("checking '%s' is child of '%s'\n", li->name, pname);

        /* is this a child of the parent? */
        if (pfi != NULL
            && (strncmp(pname, li->name, strlen(pname)) != 0
            || li->name[plen] != li->separator)) {
            printf("  nope\n");
            break;
        }
        printf("  yep\n");

        /* is this not an immediate child of the parent? */
#if 0
        char *p;
        if (pfi != NULL
            && li->separator != 0
            && (p = strchr(li->name + plen + 1, li->separator)) != NULL) {
            if (last == NULL) {
                struct _list_info tli;

                tli.flags = CAMEL_FOLDER_NOSELECT|CAMEL_FOLDER_CHILDREN;
                tli.separator = li->separator;
                tli.name = g_strndup(li->name, p-li->name+1);
                fi = folders_build_info(base, &tli);
                fi->parent = pfi;
                if (pfi && pfi->child == NULL)
                    pfi->child = fi;
                i = folders_build_rec(folders, i, fi, tli.name);
                break;
            }
        }
#endif

        fi = folders_build_info(base, li);
        fi->parent = pfi;
        if (last != NULL)
            last->sibling = fi;
        last = fi;
        if (first == NULL)
            first = fi;

        (*ip)++;
        fi->child = folders_build_rec(base, folders, ip, fi, li->name);
        imap_free_list(li);
    }

    return first;
}

static void
folder_info_dump(CamelFolderInfo *fi, int depth)
{
    char *s;

    s = alloca(depth+1);
    memset(s, ' ', depth);
    s[depth] = 0;
    while (fi) {
        printf("%s%s (%s)\n", s, fi->name, fi->url);
        if (fi->child)
            folder_info_dump(fi->child, depth+2);
        fi = fi->sibling;
    }
    
}

static CamelFolderInfo *
imap_get_folder_info(CamelStore *store, const char *top, guint32 flags, CamelException *ex)
{
    CamelIMAPPStore *istore = (CamelIMAPPStore *)store;
    CamelFolderInfo * fi= NULL;
    char *name;

    /* FIXME: temporary, since this is not a disco store */
    if (istore->driver == NULL
        && !camel_service_connect((CamelService *)store, ex))
        return NULL;

    name = (char *)top;
    if (name == NULL || name[0] == 0) {
        /* namespace? */
        name = "";
    }

    name = "";

    CAMEL_TRY {
        CamelURL *base;
        int i;
        GPtrArray *folders;

        /* FIXME: subscriptions? lsub? */
        folders = camel_imapp_driver_list(istore->driver, name, flags);

        /* this greatly simplifies the tree algorithm ... but it might
           be faster just to use a hashtable to find parents? */
        qsort(folders->pdata, folders->len, sizeof(folders->pdata[0]), folders_build_cmp);

        i = 0;
        base = camel_url_copy(((CamelService *)store)->url);
        fi = folders_build_rec(base, folders, &i, NULL, NULL);
        camel_url_free(base);
        g_ptr_array_free(folders, TRUE);
    } CAMEL_CATCH(e) {
        camel_exception_xfer(ex, e);
    } CAMEL_DONE;

    printf("built folder info:\n");
    folder_info_dump(fi, 2);

    return fi;

#if 0
    if (top == NULL || !g_ascii_strcasecmp(top, "inbox")) {
        CamelURL *uri = camel_url_copy(((CamelService *)store)->url);

        camel_url_set_path(uri, "/INBOX");
        fi = g_malloc0(sizeof(*fi));
        fi->url = camel_url_to_string(uri, CAMEL_URL_HIDE_ALL);
        camel_url_free(uri);
        fi->name = g_strdup("INBOX");
        fi->full_name = g_strdup("INBOX");
        fi->path = g_strdup("/INBOX");
        fi->unread_message_count = -1;
        fi->flags = 0;

        folder = camel_object_bag_get(store->folders, "INBOX");
        if (folder) {
            /*if (!cflags & FAST)*/
            camel_imapp_driver_update(istore->driver, (CamelIMAPPFolder *)folder);
            fi->unread_message_count = camel_folder_get_unread_message_count(folder);
            camel_object_unref(folder);
        }
    } else {
        camel_exception_setv(ex, 1, "not implemented");
    }
#endif
    return fi;

#if 0
    istore->pending_list = g_ptr_array_new();

    CAMEL_TRY {
        ic = camel_imapp_engine_command_new(istore->driver->engine, "LIST", NULL, "LIST \"\" %f", top);
        camel_imapp_engine_command_queue(istore->driver->engine, ic);
        while (camel_imapp_engine_iterate(istore->driver->engine, ic) > 0)
            ;

        if (ic->status->result != IMAP_OK)
            camel_exception_throw(1, "list failed: %s", ic->status->text);
    } CAMEL_CATCH (e) {
        camel_exception_xfer(ex, e);
    } CAMEL_DONE;
    
    camel_imapp_engine_command_free(istore->driver->engine, ic);

    printf("got folder list:\n");
    for (i=0;i<(int)istore->pending_list->len;i++) {
        struct _list_info *linfo = istore->pending_list->pdata[i];

        printf("%s (%c)\n", linfo->name, linfo->separator);
        imap_free_list(linfo);
    }
    istore->pending_list = NULL;

    return NULL;
#endif
}

static void
imap_delete_folder(CamelStore *store, const char *folder_name, CamelException *ex)
{
    camel_exception_setv(ex, 1, "delete_folder::unimplemented");
}

static void
imap_rename_folder(CamelStore *store, const char *old, const char *new, CamelException *ex)
{
    camel_exception_setv(ex, 1, "rename_folder::unimplemented");
}

static CamelFolderInfo *
imap_create_folder(CamelStore *store, const char *parent_name, const char *folder_name, CamelException *ex)
{
    camel_exception_setv(ex, 1, "create_folder::unimplemented");
    return NULL;
}

/* ********************************************************************** */
#if 0
static int store_resp_fetch(CamelIMAPPEngine *ie, guint32 id, void *data)
{
    struct _fetch_info *finfo;
    CamelIMAPPStore *istore = data;
    CamelMessageInfo *info;
    struct _pending_fetch *pending;

    finfo = imap_parse_fetch(ie->stream);
    if (istore->selected) {
        if ((finfo->got & FETCH_UID) == 0) {
            printf("didn't get uid in fetch response?\n");
        } else {
            info = camel_folder_summary_index(((CamelFolder *)istore->selected)->summary, id-1);
            /* exists, check/update */
            if (info) {
                if (strcmp(finfo->uid, camel_message_info_uid(info)) != 0) {
                    printf("summary at index %d has uid %s expected %s\n", id, camel_message_info_uid(info), finfo->uid);
                    /* uid mismatch???  try do it based on uid instead? try to reorder?  i dont know? */
                    camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
                    info = camel_folder_summary_uid(((CamelFolder *)istore->selected)->summary, finfo->uid);
                }
            }

            if (info) {
                if (finfo->got & (FETCH_FLAGS)) {
                    printf("updating flags for uid '%s'\n", finfo->uid);
                    info->flags = finfo->flags;
                    camel_folder_change_info_change_uid(istore->selected->changes, finfo->uid);
                }
                if (finfo->got & FETCH_MINFO) {
                    printf("got envelope unexpectedly?\n");
                }
                /* other things go here, like body fetches */
            } else {
                pending = g_hash_table_lookup(istore->pending_fetch_table, finfo->uid);

                /* we need to create a new info, we only care about flags and minfo */

                if (pending)
                    info = pending->info;
                else {
                    info = camel_folder_summary_info_new(((CamelFolder *)istore->selected)->summary);
                    camel_message_info_set_uid(info, g_strdup(finfo->uid));
                }

                if (finfo->got & FETCH_FLAGS)
                    info->flags = finfo->flags;

                if (finfo->got & FETCH_MINFO) {
                    /* if we only use ENVELOPE? */
                    camel_message_info_set_subject(info, g_strdup(camel_message_info_subject(finfo->minfo)));
                    camel_message_info_set_from(info, g_strdup(camel_message_info_from(finfo->minfo)));
                    camel_message_info_set_to(info, g_strdup(camel_message_info_to(finfo->minfo)));
                    camel_message_info_set_cc(info, g_strdup(camel_message_info_cc(finfo->minfo)));
                    info->date_sent = finfo->minfo->date_sent;
                    camel_folder_summary_add(((CamelFolder *)istore->selected)->summary, info);
                    camel_folder_change_info_add_uid(istore->selected->changes, finfo->uid);
                    if (pending) {
                        e_dlist_remove((EDListNode *)pending);
                        g_hash_table_remove(istore->pending_fetch_table, finfo->uid);
                        /*e_memchunk_free(istore->pending_fetch_chunks, pending);*/
                    }
                } else if (finfo->got & FETCH_HEADER) {
                    /* if we only use HEADER? */
                    CamelMimeParser *mp;

                    if (pending == NULL)
                        camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
                    mp = camel_mime_parser_new();
                    camel_mime_parser_init_with_stream(mp, finfo->header);
                    info = camel_folder_summary_info_new_from_parser(((CamelFolder *)istore->selected)->summary, mp);
                    camel_object_unref(mp);
                    camel_message_info_set_uid(info, g_strdup(finfo->uid));

                    camel_folder_summary_add(((CamelFolder *)istore->selected)->summary, info);
                    camel_folder_change_info_add_uid(istore->selected->changes, finfo->uid);
                    if (pending) {
                        /* FIXME: use a dlist */
                        e_dlist_remove((EDListNode *)pending);
                        g_hash_table_remove(istore->pending_fetch_table, camel_message_info_uid(pending->info));
                        camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, pending->info);
                        /*e_memchunk_free(istore->pending_fetch_chunks, pending);*/
                    }
                } else if (finfo->got & FETCH_FLAGS) {
                    if (pending == NULL) {
                        pending = e_memchunk_alloc(istore->pending_fetch_chunks);
                        pending->info = info;
                        g_hash_table_insert(istore->pending_fetch_table, (char *)camel_message_info_uid(info), pending);
                        e_dlist_addtail(&istore->pending_fetch_list, (EDListNode *)pending);
                    }
                } else {
                    if (pending == NULL)
                        camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
                    printf("got unexpected fetch response?\n");
                    imap_dump_fetch(finfo);
                }
            }
        }
    } else {
        printf("unexpected fetch response, no folder selected?\n");
    }
    /*imap_dump_fetch(finfo);*/
    imap_free_fetch(finfo);

    return camel_imapp_engine_skip(ie);
}
#endif

/* ********************************************************************** */

/* should be moved to imapp-utils?
   stuff in imapp-utils should be moved to imapp-parse? */

/* ********************************************************************** */

#if 0
void
camel_imapp_store_folder_selected(CamelIMAPPStore *store, CamelIMAPPFolder *folder, CamelIMAPPSelectResponse *select)
{
    CamelIMAPPCommand * volatile ic = NULL;
    CamelIMAPPStore *istore = (CamelIMAPPStore *)store;
    int i;
    struct _uidset_state ss;
    GPtrArray *fetch;
    CamelMessageInfo *info;
    struct _pending_fetch *fw, *fn;

    printf("imap folder selected\n");

    if (select->uidvalidity == folder->uidvalidity
        && select->exists == folder->exists
        && select->recent == folder->recent
        && select->unseen == folder->unseen) {
        /* no work to do? */
        return;
    }

    istore->pending_fetch_table = g_hash_table_new(g_str_hash, g_str_equal);
    istore->pending_fetch_chunks = e_memchunk_new(256, sizeof(struct _pending_fetch));

    /* perform an update - flags first (and see what we have) */
    CAMEL_TRY {
        ic = camel_imapp_engine_command_new(istore->engine, "FETCH", NULL, "FETCH 1:%d (UID FLAGS)", select->exists);
        camel_imapp_engine_command_queue(istore->engine, ic);
        while (camel_imapp_engine_iterate(istore->engine, ic) > 0)
            ;

        if (ic->status->result != IMAP_OK)
            camel_exception_throw(1, "fetch failed: %s", ic->status->text);

        /* pending_fetch_list now contains any new messages */
        /* FIXME: how do we work out no-longer present messages? */
        printf("now fetching info for messages?\n");
        uidset_init(&ss, store->engine);
        ic = camel_imapp_engine_command_new(istore->engine, "FETCH", NULL, "UID FETCH ");
        fw = (struct _pending_fetch *)istore->pending_fetch_list.head;
        fn = fw->next;
        while (fn) {
            info = fw->info;
            /* if the uid set fills, then flush the command out */
            if (uidset_add(&ss, ic, camel_message_info_uid(info))
                || (fn->next == NULL && uidset_done(&ss, ic))) {
                camel_imapp_engine_command_add(istore->engine, ic, " (FLAGS RFC822.HEADER)");
                camel_imapp_engine_command_queue(istore->engine, ic);
                while (camel_imapp_engine_iterate(istore->engine, ic) > 0)
                    ;
                if (ic->status->result != IMAP_OK)
                    camel_exception_throw(1, "fetch failed: %s", ic->status->text);
                /* if not end ... */
                camel_imapp_engine_command_free(istore->engine, ic);
                ic = camel_imapp_engine_command_new(istore->engine, "FETCH", NULL, "UID FETCH ");
            }
            fw = fn;
            fn = fn->next;
        }

        printf("The pending list should now be empty: %s\n", e_dlist_empty(&istore->pending_fetch_list)?"TRUE":"FALSE");
        for (i=0;i<10;i++) {
            info = camel_folder_summary_index(((CamelFolder *)istore->selected)->summary, i);
            if (info) {
                printf("message info [%d] =\n", i);
                camel_message_info_dump(info);
                camel_folder_summary_info_free(((CamelFolder *)istore->selected)->summary, info);
            }
        }
    } CAMEL_CATCH (e) {
        /* FIXME: cleanup */
        camel_exception_throw_ex(e);
    } CAMEL_DONE;

    g_hash_table_destroy(istore->pending_fetch_table);
    istore->pending_fetch_table = NULL;
    e_memchunk_destroy(istore->pending_fetch_chunks);

    camel_imapp_engine_command_free(istore->engine, ic);
}
#endif

#if 0
/*char *uids[] = {"1", "2", "4", "5", "6", "7", "9", "11", "12", 0};*/
/*char *uids[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 0};*/
char *uids[] = {"1", "3", "5", "7", "9", "11", "12", "13", "14", "15", "20", "21", "24", "25", "26", 0};

void
uidset_test(CamelIMAPPEngine *ie)
{
    struct _uidset_state ss;
    CamelIMAPPCommand *ic;
    int i;

    /*ic = camel_imapp_engine_command_new(ie, 0, "FETCH", NULL, "FETCH ");*/
    uidset_init(&ss, 0, 0);
    for (i=0;uids[i];i++) {
        if (uidset_add(&ss, uids[i])) {
            printf("\n[%d] flushing uids\n", i);
        }
    }

    if (uidset_done(&ss)) {
        printf("\nflushing uids\n");
    }
}
#endif