aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/imap4/camel-imap-store.c
blob: 7373088f79b6d4a4484405fa4e3030736138d920 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*  Camel
 *  Copyright (C) 1999-2004 Jeffrey Stedfast
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 */


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

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include <camel/camel-sasl.h>
#include <camel/camel-utf8.h>
#include <camel/camel-tcp-stream-raw.h>
#include <camel/camel-tcp-stream-ssl.h>

#include <camel/camel-private.h>

#include "camel-imap-store.h"
#include "camel-imap-engine.h"
#include "camel-imap-folder.h"
#include "camel-imap-stream.h"
#include "camel-imap-command.h"


static void camel_imap_store_class_init (CamelIMAPStoreClass *klass);
static void camel_imap_store_init (CamelIMAPStore *store, CamelIMAPStoreClass *klass);
static void camel_imap_store_finalize (CamelObject *object);

/* service methods */
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);

/* store methods */
static CamelFolder *imap_get_folder (CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex);
static CamelFolderInfo *imap_create_folder (CamelStore *store, const char *parent_name, const char *folder_name, 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_name, const char *new_name, CamelException *ex);
static void imap_sync (CamelStore *store, gboolean expunge, CamelException *ex);
static CamelFolderInfo *imap_get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex);
static void imap_subscribe_folder (CamelStore *store, const char *folder_name, CamelException *ex);
static void imap_unsubscribe_folder (CamelStore *store, const char *folder_name, CamelException *ex);
static void imap_noop (CamelStore *store, CamelException *ex);


static CamelStoreClass *parent_class = NULL;


CamelType
camel_imap_store_get_type (void)
{
    static CamelType type = 0;
    
    if (!type) {
        type = camel_type_register (CAMEL_TYPE_IMAP_STORE,
                        "CamelIMAPStore",
                        sizeof (CamelIMAPStore),
                        sizeof (CamelIMAPStoreClass),
                        (CamelObjectClassInitFunc) camel_imap_store_class_init,
                        NULL,
                        (CamelObjectInitFunc) camel_imap_store_init,
                        (CamelObjectFinalizeFunc) camel_imap_store_finalize);
    }
    
    return type;
}

static guint
imap_hash_folder_name (gconstpointer key)
{
    if (g_ascii_strcasecmp (key, "INBOX") == 0)
        return g_str_hash ("INBOX");
    else
        return g_str_hash (key);
}

static gint
imap_compare_folder_name (gconstpointer a, gconstpointer b)
{
    gconstpointer aname = a, bname = b;

    if (g_ascii_strcasecmp (a, "INBOX") == 0)
        aname = "INBOX";
    if (g_ascii_strcasecmp (b, "INBOX") == 0)
        bname = "INBOX";
    return g_str_equal (aname, bname);
}

static void
camel_imap_store_class_init (CamelIMAPStoreClass *klass)
{
    CamelServiceClass *service_class = (CamelServiceClass *) klass;
    CamelStoreClass *store_class = (CamelStoreClass *) klass;
    
    parent_class = (CamelStoreClass *) camel_type_get_global_classfuncs (CAMEL_STORE_TYPE);
    
    service_class->construct = imap_construct;
    service_class->get_name = imap_get_name;
    service_class->connect = imap_connect;
    service_class->disconnect = imap_disconnect;
    service_class->query_auth_types = imap_query_auth_types;
    
    store_class->hash_folder_name = imap_hash_folder_name;
    store_class->compare_folder_name = imap_compare_folder_name;
    
    store_class->get_folder = imap_get_folder;
    store_class->create_folder = imap_create_folder;
    store_class->delete_folder = imap_delete_folder;
    store_class->rename_folder = imap_rename_folder;
    store_class->sync = imap_sync;
    store_class->get_folder_info = imap_get_folder_info;
    store_class->subscribe_folder = imap_subscribe_folder;
    store_class->unsubscribe_folder = imap_unsubscribe_folder;
    store_class->noop = imap_noop;
}

static void
camel_imap_store_init (CamelIMAPStore *store, CamelIMAPStoreClass *klass)
{
    store->engine = NULL;
}

static void
camel_imap_store_finalize (CamelObject *object)
{
    CamelIMAPStore *store = (CamelIMAPStore *) object;
    
    if (store->engine)
        camel_object_unref (store->engine);
}


static void
imap_construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
{
    CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
}

static char *
imap_get_name (CamelService *service, gboolean brief)
{
    if (brief)
        return g_strdup_printf (_("IMAP server %s"), service->url->host);
    else
        return g_strdup_printf (_("IMAP service for %s on %s"),
                    service->url->user, service->url->host);
}

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 gboolean
connect_to_server (CamelService *service, struct hostent *host, int ssl_mode, int try_starttls, CamelException *ex)
{
    CamelIMAPStore *store = (CamelIMAPStore *) service;
    CamelIMAPEngine *engine;
    CamelStream *tcp_stream;
    int port, ret;
    
    if (store->engine) {
        camel_object_unref (store->engine);
        store->engine = NULL;
    }
    
    port = service->url->port ? service->url->port : 143;
    
    if (ssl_mode) {
#ifdef HAVE_SSL
        if (try_starttls) {
            tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, STARTTLS_FLAGS);
        } else {
            port = service->url->port ? service->url->port : 993;
            tcp_stream = camel_tcp_stream_ssl_new (service->session, service->url->host, SSL_PORT_FLAGS);
        }
#else
        if (!try_starttls)
            port = service->url->port ? service->url->port : 993;
        
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      _("Could not connect to %s (port %d): %s"),
                      service->url->host, port,
                      _("SSL unavailable"));
        
        return FALSE;
#endif /* HAVE_SSL */
    } else {
        tcp_stream = camel_tcp_stream_raw_new ();
    }
    
    fprintf (stderr, "connecting to %s:%d\n", service->url->host, port);
    if ((ret = camel_tcp_stream_connect ((CamelTcpStream *) tcp_stream, host, port)) == -1) {
        if (errno == EINTR)
            camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                         _("Connection cancelled"));
        else
            camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                          _("Could not connect to %s (port %d): %s"),
                          service->url->host, port,
                          g_strerror (errno));
        
        camel_object_unref (tcp_stream);
        
        return FALSE;
    }
    
    engine = camel_imap_engine_new (service->session, service->url);
    if (camel_imap_engine_take_stream (engine, tcp_stream, ex) == -1) {
        camel_object_unref (engine);
        
        return FALSE;
    }
    
    if (camel_imap_engine_capability (engine, ex) == -1) {
        camel_object_unref (engine);
        
        return FALSE;
    }
    
    store->engine = engine;
    
#ifdef HAVE_SSL
    if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
        /* try_starttls is always TRUE here */
        if (engine->capa & CAMEL_IMAP_CAPABILITY_STARTTLS)
            goto starttls;
    } else if (ssl_mode == USE_SSL_ALWAYS) {
        if (try_starttls) {
            if (engine->capa & CAMEL_IMAP_CAPABILITY_STARTTLS) {
                goto starttls;
            } else {
                /* server doesn't support STARTTLS, abort */
                camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                              _("Failed to connect to IMAP server %s in secure mode: "
                            "Server does not support STARTTLS"),
                              service->url->host);
                goto exception;
            }
        }
    }
#endif /* HAVE_SSL */
    
    return TRUE;
    
#ifdef HAVE_SSL
 starttls:
    
    if (1) {
        CamelIMAPCommand *ic;
        int id;
        
        ic = camel_imap_engine_queue (engine, NULL, "STARTTLS\r\n");
        while ((id = camel_imap_engine_iterate (engine)) < ic->id && id != -1)
            ;
        
        if (id == -1 || ic->result != CAMEL_IMAP_RESULT_OK) {
            if (ic->result != CAMEL_IMAP_RESULT_OK) {
                camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                              _("Failed to connect to IMAP server %s in secure mode: %s"),
                              service->url->host, _("Unknown error"));
            } else {
                camel_exception_xfer (ex, &ic->ex);
            }
            
            camel_imap_command_unref (ic);
            
            goto exception;
        }
        
        camel_imap_command_unref (ic);
    }
    
    return TRUE;
    
 exception:
    
    camel_object_unref (store->engine);
    store->engine = NULL;
    
    return FALSE;
#endif /* HAVE_SSL */
}

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)
{
    const char *use_ssl;
    struct hostent *h;
    int ssl_mode;
    int ret, i;
    
    if (!(h = camel_service_gethost (service, ex)))
        return FALSE;
    
    if ((use_ssl = camel_url_get_param (service->url, "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 (!(ret = connect_to_server (service, h, 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);
                ret = connect_to_server (service, h, ssl_mode, TRUE, ex);
            }
        }
    } else if (ssl_mode == USE_SSL_WHEN_POSSIBLE) {
        /* If the server supports STARTTLS, use it */
        ret = connect_to_server (service, h, ssl_mode, TRUE, ex);
    } else {
        /* User doesn't care about SSL */
        ret = connect_to_server (service, h, USE_SSL_ALWAYS, FALSE, ex);
    }
    
    camel_free_host (h);
    
    return ret;
}

static int
sasl_auth (CamelIMAPEngine *engine, CamelIMAPCommand *ic, const unsigned char *linebuf, size_t linelen, CamelException *ex)
{
    /* Perform a single challenge iteration */
    CamelSasl *sasl = ic->user_data;
    char *challenge;
    
    if (camel_sasl_authenticated (sasl)) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                      _("Cannot authenticate to IMAP server %s using the %s authentication mechanism"),
                      engine->url->host, engine->url->authmech);
        return -1;
    }
    
    while (isspace (*linebuf))
        linebuf++;
    
    if (*linebuf == '\0')
        linebuf = NULL;
    
    if (!(challenge = camel_sasl_challenge_base64 (sasl, (const char *) linebuf, ex)))
        return -1;
    
    fprintf (stderr, "sending : %s\r\n", challenge);
    
    if (camel_stream_printf (engine->ostream, "%s\r\n", challenge) == -1) {
        g_free (challenge);
        return -1;
    }
    
    g_free (challenge);
    
    if (camel_stream_flush (engine->ostream) == -1)
        return -1;
    
    return 0;
}

static int
imap_try_authenticate (CamelService *service, gboolean reprompt, const char *errmsg, CamelException *ex)
{
    CamelIMAPStore *store = (CamelIMAPStore *) service;
    CamelSession *session = service->session;
    CamelSasl *sasl = NULL;
    CamelIMAPCommand *ic;
    int id;
    
    if (!service->url->passwd) {
        guint32 flags = CAMEL_SESSION_PASSWORD_SECRET;
        char *prompt;
        
        if (reprompt)
            flags |= CAMEL_SESSION_PASSWORD_REPROMPT;
        
        prompt = g_strdup_printf (_("%sPlease enter the IMAP password for %s on host %s"),
                      errmsg ? errmsg : "",
                      service->url->user,
                      service->url->host);
        
        service->url->passwd = camel_session_get_password (session, prompt, flags, service, "password", ex);
        
        g_free (prompt);
        
        if (!service->url->passwd)
            return FALSE;
    }
    
    if (service->url->authmech) {
        CamelServiceAuthType *mech;
        
        mech = g_hash_table_lookup (store->engine->authtypes, service->url->authmech);
        sasl = camel_sasl_new ("imap", mech->authproto, service);
        
        ic = camel_imap_engine_queue (store->engine, NULL, "AUTHENTICATE %s\r\n", service->url->authmech);
        ic->plus = sasl_auth;
        ic->user_data = sasl;
    } else {
        ic = camel_imap_engine_queue (store->engine, NULL, "LOGIN %S %S\r\n",
                           service->url->user, service->url->passwd);
    }
    
    while ((id = camel_imap_engine_iterate (store->engine)) < ic->id && id != -1)
        ;
    
    if (sasl != NULL)
        camel_object_unref (sasl);
    
    if (id == -1 || ic->status == CAMEL_IMAP_COMMAND_ERROR) {
        /* unrecoverable error */
        camel_exception_xfer (ex, &ic->ex);
        camel_imap_command_unref (ic);
        
        return FALSE;
    }
    
    if (ic->result != CAMEL_IMAP_RESULT_OK) {
        camel_imap_command_unref (ic);
        
        /* try again */
        
        return TRUE;
    }
    
    camel_imap_command_unref (ic);
    
    return FALSE;
}

static gboolean
imap_connect (CamelService *service, CamelException *ex)
{
    CamelIMAPStore *store = (CamelIMAPStore *) service;
    CamelServiceAuthType *mech;
    gboolean reprompt = FALSE;
    char *errmsg = NULL;
    CamelException lex;
    
    CAMEL_SERVICE_LOCK (store, connect_lock);
    
    if (!connect_to_server_wrapper (service, ex)) {
        CAMEL_SERVICE_UNLOCK (store, connect_lock);
        return FALSE;
    }
    
#define CANT_USE_AUTHMECH (!(mech = g_hash_table_lookup (store->engine->authtypes, service->url->authmech)))
    if (service->url->authmech && CANT_USE_AUTHMECH) {
        /* Oops. We can't AUTH using the requested mechanism */
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                      _("Cannot authenticate to IMAP server %s using %s"),
                      service->url->host, service->url->authmech);
        
        camel_object_unref (store->engine);
        store->engine = NULL;
        
        CAMEL_SERVICE_UNLOCK (store, connect_lock);
        
        return FALSE;
    }
    
    camel_exception_init (&lex);
    while (imap_try_authenticate (service, reprompt, errmsg, &lex)) {
        g_free (errmsg);
        errmsg = g_strdup (lex.desc);
        camel_exception_clear (&lex);
        reprompt = TRUE;
    }
    g_free (errmsg);
    
    if (camel_exception_is_set (&lex)) {
        camel_exception_xfer (ex, &lex);
        camel_object_unref (store->engine);
        store->engine = NULL;
        
        CAMEL_SERVICE_UNLOCK (store, connect_lock);
        
        return FALSE;
    }
    
    if (camel_imap_engine_namespace (store->engine, ex) == -1) {
        camel_object_unref (store->engine);
        store->engine = NULL;
        
        CAMEL_SERVICE_UNLOCK (store, connect_lock);
        
        return FALSE;
    }
    
    CAMEL_SERVICE_UNLOCK (store, connect_lock);
    
    return TRUE;
}

static gboolean
imap_disconnect (CamelService *service, gboolean clean, CamelException *ex)
{
    CamelIMAPStore *store = (CamelIMAPStore *) service;
    CamelIMAPCommand *ic;
    int id;
    
    if (clean && !store->engine->istream->disconnected) {
        ic = camel_imap_engine_queue (store->engine, NULL, "LOGOUT\r\n");
        while ((id = camel_imap_engine_iterate (store->engine)) < ic->id && id != -1)
            ;
        
        camel_imap_command_unref (ic);
    }
    
    camel_object_unref (store->engine);
    
    return 0;
}

extern CamelServiceAuthType camel_imap_password_authtype;

static GList *
imap_query_auth_types (CamelService *service, CamelException *ex)
{
    CamelIMAPStore *store = (CamelIMAPStore *) service;
    CamelServiceAuthType *authtype;
    GList *sasl_types, *t, *next;
    gboolean connected;
    
    CAMEL_SERVICE_LOCK (store, connect_lock);
    connected = connect_to_server_wrapper (service, ex);
    CAMEL_SERVICE_UNLOCK (store, connect_lock);
    if (!connected)
        return NULL;
    
    sasl_types = camel_sasl_authtype_list (FALSE);
    for (t = sasl_types; t; t = next) {
        authtype = t->data;
        next = t->next;
        
        if (!g_hash_table_lookup (store->engine->authtypes, authtype->authproto)) {
            sasl_types = g_list_remove_link (sasl_types, t);
            g_list_free_1 (t);
        }
    }
    
    return g_list_prepend (sasl_types, &camel_imap_password_authtype);
}


static char *
imap_folder_utf7_name (CamelStore *store, const char *folder_name)
{
    char *real_name, *p;
    
    if (store->dir_sep != '/') {
        p = real_name = g_alloca (strlen (folder_name) + 1);
        strcpy (real_name, folder_name);
        while (*p != '\0') {
            if (*p == '/')
                *p = store->dir_sep;
            p++;
        }
        
        folder_name = real_name;
    }
    
    return camel_utf8_utf7 (folder_name);
}

static CamelFolder *
imap_get_folder (CamelStore *store, const char *folder_name, guint32 flags, CamelException *ex)
{
    CamelIMAPStore *imap_store = (CamelIMAPStore *) store;
    
    /* FIXME: implement me */
    
    return NULL;
}

static CamelFolderInfo *
imap_create_folder (CamelStore *store, const char *parent_name, const char *folder_name, CamelException *ex)
{
    /* FIXME: also need to deal with parent folders that can't
     * contain subfolders - delete them and re-create with the
     * proper hint */
    CamelIMAPEngine *engine = ((CamelIMAPStore *) store)->engine;
    CamelFolderInfo *fi = NULL;
    CamelIMAPCommand *ic;
    char *utf7_name;
    const char *c;
    char *name;
    int id;
    
    c = folder_name;
    while (*c != '\0') {
        if (*c == store->dir_sep || strchr ("#%*", *c)) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_PATH,
                          _("The folder name \"%s\" is invalid because "
                        "it containes the character \"%c\""),
                          folder_name, *c);
            return NULL;
        }
        
        c++;
    }
    
    if (parent_name != NULL && *parent_name)
        name = g_strdup_printf ("%s/%s", parent_name, folder_name);
    else
        name = g_strdup (folder_name);
    
    utf7_name = imap_folder_utf7_name (store, name);
    g_free (name);
    
    ic = camel_imap_engine_queue (engine, NULL, "CREATE %S\r\n", utf7_name);
    g_free (utf7_name);
    
    while ((id = camel_imap_engine_iterate (engine)) < ic->id && id != -1)
        ;
    
    if (id == -1 || ic->status != CAMEL_IMAP_COMMAND_COMPLETE) {
        camel_exception_xfer (ex, &ic->ex);
        camel_imap_command_unref (ic);
        return NULL;
    }
    
    switch (ic->result) {
    case CAMEL_IMAP_RESULT_OK:
        /* FIXME: allocate fi */
        break;
    case CAMEL_IMAP_RESULT_NO:
        /* FIXME: would be good to save the NO reason into the err message */
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot create folder `%s': Invalid mailbox name"),
                      folder_name);
        break;
    case CAMEL_IMAP_RESULT_BAD:
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot create folder `%s': Bad command"),
                      folder_name);
        break;
    default:
        g_assert_not_reached ();
    }
    
    camel_imap_command_unref (ic);
    
    return fi;
}

static void
imap_delete_folder (CamelStore *store, const char *folder_name, CamelException *ex)
{
    CamelIMAPEngine *engine = ((CamelIMAPStore *) store)->engine;
    CamelIMAPCommand *ic;
    char *utf7_name;
    int id;
    
    if (!g_ascii_strcasecmp (folder_name, "INBOX")) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot delete folder `%s': Special folder"),
                      folder_name);
        
        return;
    }
    
    utf7_name = imap_folder_utf7_name (store, folder_name);
    ic = camel_imap_engine_queue (engine, NULL, "DELETE %S\r\n", utf7_name);
    g_free (utf7_name);
    
    while ((id = camel_imap_engine_iterate (engine)) < ic->id && id != -1)
        ;
    
    if (id == -1 || ic->status != CAMEL_IMAP_COMMAND_COMPLETE) {
        camel_exception_xfer (ex, &ic->ex);
        camel_imap_command_unref (ic);
        return;
    }
    
    switch (ic->result) {
    case CAMEL_IMAP_RESULT_OK:
        /* deleted */
        /*fi = imap_build_folder_info (store, folder_name);
        camel_object_trigger_event (store, "folder_deleted", fi);
        camel_folder_info_free (fi);*/
        break;
    case CAMEL_IMAP_RESULT_NO:
        /* FIXME: would be good to save the NO reason into the err message */
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot delete folder `%s': Invalid mailbox name"),
                      folder_name);
        break;
    case CAMEL_IMAP_RESULT_BAD:
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot delete folder `%s': Bad command"),
                      folder_name);
        break;
    }
    
    camel_imap_command_unref (ic);
}

static void
imap_rename_folder (CamelStore *store, const char *old_name, const char *new_name, CamelException *ex)
{

}

static void
imap_sync (CamelStore *store, gboolean expunge, CamelException *ex)
{

}

static CamelFolderInfo *
imap_get_folder_info (CamelStore *store, const char *top, guint32 flags, CamelException *ex)
{
    return NULL;
}

static void
imap_subscribe_folder (CamelStore *store, const char *folder_name, CamelException *ex)
{
    CamelIMAPEngine *engine = ((CamelIMAPStore *) store)->engine;
    CamelIMAPCommand *ic;
    char *utf7_name;
    int id;
    
    utf7_name = imap_folder_utf7_name (store, folder_name);
    ic = camel_imap_engine_queue (engine, NULL, "SUBSCRIBE %S\r\n", utf7_name);
    g_free (utf7_name);
    
    while ((id = camel_imap_engine_iterate (engine)) < ic->id && id != -1)
        ;
    
    if (id == -1 || ic->status != CAMEL_IMAP_COMMAND_COMPLETE) {
        camel_exception_xfer (ex, &ic->ex);
        camel_imap_command_unref (ic);
        return;
    }
    
    switch (ic->result) {
    case CAMEL_IMAP_RESULT_OK:
        /* subscribed */
        /*fi = imap_build_folder_info (store, folder_name);
          fi->flags |= CAMEL_FOLDER_NOCHILDREN;
          camel_object_trigger_event (store, "folder_subscribed", fi);
          camel_folder_info_free (fi);*/
        break;
    case CAMEL_IMAP_RESULT_NO:
        /* FIXME: would be good to save the NO reason into the err message */
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot subscribe to folder `%s': Invalid mailbox name"),
                      folder_name);
        break;
    case CAMEL_IMAP_RESULT_BAD:
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot subscribe to folder `%s': Bad command"),
                      folder_name);
        break;
    }
    
    camel_imap_command_unref (ic);
}

static void
imap_unsubscribe_folder (CamelStore *store, const char *folder_name, CamelException *ex)
{
    CamelIMAPEngine *engine = ((CamelIMAPStore *) store)->engine;
    CamelIMAPCommand *ic;
    char *utf7_name;
    int id;
    
    utf7_name = imap_folder_utf7_name (store, folder_name);
    ic = camel_imap_engine_queue (engine, NULL, "UNSUBSCRIBE %S\r\n", utf7_name);
    g_free (utf7_name);
    
    while ((id = camel_imap_engine_iterate (engine)) < ic->id && id != -1)
        ;
    
    if (id == -1 || ic->status != CAMEL_IMAP_COMMAND_COMPLETE) {
        camel_exception_xfer (ex, &ic->ex);
        camel_imap_command_unref (ic);
        return;
    }
    
    switch (ic->result) {
    case CAMEL_IMAP_RESULT_OK:
        /* unsubscribed */
        /*fi = imap_build_folder_info (store, folder_name);
          camel_object_trigger_event (store, "folder_unsubscribed", fi);
          camel_folder_info_free (fi);*/
        break;
    case CAMEL_IMAP_RESULT_NO:
        /* FIXME: would be good to save the NO reason into the err message */
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot unsubscribe from folder `%s': Invalid mailbox name"),
                      folder_name);
        break;
    case CAMEL_IMAP_RESULT_BAD:
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot unsubscribe from folder `%s': Bad command"),
                      folder_name);
        break;
    }
    
    camel_imap_command_unref (ic);
}

static void
imap_noop (CamelStore *store, CamelException *ex)
{

}