aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/smtp/camel-smtp-transport.c
blob: 7e5eddb6a02bb0a7810530e09da469b2b3f5ed6b (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
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-smtp-transport.c : class for a smtp transport */

/* 
 * Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 2000 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/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#undef MIN
#undef MAX
#include "camel-mime-filter-crlf.h"
#include "camel-mime-filter-linewrap.h"
#include "camel-stream-filter.h"
#include "camel-smtp-transport.h"
#include "camel-mime-message.h"
#include "camel-multipart.h"
#include "camel-mime-part.h"
#include "camel-operation.h"
#include "camel-stream-buffer.h"
#include "camel-tcp-stream.h"
#include "camel-tcp-stream-raw.h"
#ifdef HAVE_SSL
#include "camel-tcp-stream-ssl.h"
#endif
#include "camel-session.h"
#include "camel-exception.h"
#include "camel-sasl.h"
#include "string-utils.h"

#define d(x) x

/* Specified in RFC 821 */
#define SMTP_PORT 25

/* camel smtp transport class prototypes */
static gboolean smtp_send_to (CamelTransport *transport, CamelMimeMessage *message,
                  CamelAddress *from, CamelAddress *recipients, CamelException *ex);

/* support prototypes */
static void smtp_construct (CamelService *service, CamelSession *session,
                CamelProvider *provider, CamelURL *url,
                CamelException *ex);
static gboolean smtp_connect (CamelService *service, CamelException *ex);
static gboolean smtp_disconnect (CamelService *service, gboolean clean, CamelException *ex);
static GHashTable *esmtp_get_authtypes (const unsigned char *buffer);
static GList *query_auth_types (CamelService *service, CamelException *ex);
static char *get_name (CamelService *service, gboolean brief);

static gboolean smtp_helo (CamelSmtpTransport *transport, CamelException *ex);
static gboolean smtp_auth (CamelSmtpTransport *transport, const char *mech, CamelException *ex);
static gboolean smtp_mail (CamelSmtpTransport *transport, const char *sender,
               gboolean has_8bit_parts, CamelException *ex);
static gboolean smtp_rcpt (CamelSmtpTransport *transport, const char *recipient, CamelException *ex);
static gboolean smtp_data (CamelSmtpTransport *transport, CamelMimeMessage *message,
               gboolean has_8bit_parts, CamelException *ex);
static gboolean smtp_rset (CamelSmtpTransport *transport, CamelException *ex);
static gboolean smtp_quit (CamelSmtpTransport *transport, CamelException *ex);

static void smtp_set_exception (CamelSmtpTransport *transport, const char *respbuf,
                const char *message, CamelException *ex);

/* private data members */
static CamelTransportClass *parent_class = NULL;

static void
camel_smtp_transport_class_init (CamelSmtpTransportClass *camel_smtp_transport_class)
{
    CamelTransportClass *camel_transport_class =
        CAMEL_TRANSPORT_CLASS (camel_smtp_transport_class);
    CamelServiceClass *camel_service_class =
        CAMEL_SERVICE_CLASS (camel_smtp_transport_class);
    
    parent_class = CAMEL_TRANSPORT_CLASS (camel_type_get_global_classfuncs (camel_transport_get_type ()));
    
    /* virtual method overload */
    camel_service_class->construct = smtp_construct;
    camel_service_class->connect = smtp_connect;
    camel_service_class->disconnect = smtp_disconnect;
    camel_service_class->query_auth_types = query_auth_types;
    camel_service_class->get_name = get_name;
    
    camel_transport_class->send_to = smtp_send_to;
}

static void
camel_smtp_transport_init (gpointer object)
{
    CamelSmtpTransport *smtp = CAMEL_SMTP_TRANSPORT (object);
    
    smtp->flags = 0;
}

CamelType
camel_smtp_transport_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        type =
            camel_type_register (CAMEL_TRANSPORT_TYPE,
                         "CamelSmtpTransport",
                         sizeof (CamelSmtpTransport),
                         sizeof (CamelSmtpTransportClass),
                         (CamelObjectClassInitFunc) camel_smtp_transport_class_init,
                         NULL,
                         (CamelObjectInitFunc) camel_smtp_transport_init,
                         NULL);
    }
    
    return type;
}

static void
smtp_construct (CamelService *service, CamelSession *session,
        CamelProvider *provider, CamelURL *url,
        CamelException *ex)
{
    CamelSmtpTransport *smtp_transport = CAMEL_SMTP_TRANSPORT (service);
    const char *use_ssl;
    
    CAMEL_SERVICE_CLASS (parent_class)->construct (service, session, provider, url, ex);
    
    if ((use_ssl = camel_url_get_param (url, "use_ssl"))) {
        /* Note: previous versions would use "" to toggle use_ssl to 'on' */
        if (!*use_ssl || !strcmp (use_ssl, "always"))
            smtp_transport->flags |= CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS;
        else if (!strcmp (use_ssl, "when-possible"))
            smtp_transport->flags |= CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE;
    }
}

static const char *
smtp_error_string (int error)
{
    /* SMTP error codes grabbed from rfc821 */
    switch (error) {
    case 0:
        /* looks like a read problem, check errno */
        if (errno)
            return g_strerror (errno);
        else
            return _("Unknown");
    case 500:
        return _("Syntax error, command unrecognized");
    case 501:
        return _("Syntax error in parameters or arguments");
    case 502:
        return _("Command not implemented");
    case 504:
        return _("Command parameter not implemented");
    case 211:
        return _("System status, or system help reply");
    case 214:
        return _("Help message");
    case 220:
        return _("Service ready");
    case 221:
        return _("Service closing transmission channel");
    case 421:
        return _("Service not available, closing transmission channel");
    case 250:
        return _("Requested mail action okay, completed");
    case 251:
        return _("User not local; will forward to <forward-path>");
    case 450:
        return _("Requested mail action not taken: mailbox unavailable");
    case 550:
        return _("Requested action not taken: mailbox unavailable");
    case 451:
        return _("Requested action aborted: error in processing");
    case 551:
        return _("User not local; please try <forward-path>");
    case 452:
        return _("Requested action not taken: insufficient system storage");
    case 552:
        return _("Requested mail action aborted: exceeded storage allocation");
    case 553:
        return _("Requested action not taken: mailbox name not allowed");
    case 354:
        return _("Start mail input; end with <CRLF>.<CRLF>");
    case 554:
        return _("Transaction failed");
        
    /* AUTH error codes: */
    case 432:
        return _("A password transition is needed");
    case 534:
        return _("Authentication mechanism is too weak");
    case 538:
        return _("Encryption required for requested authentication mechanism");
    case 454:
        return _("Temporary authentication failure");
    case 530:
        return _("Authentication required");
        
    default:
        return _("Unknown");
    }
}

static gboolean
connect_to_server (CamelService *service, int try_starttls, CamelException *ex)
{
    CamelSmtpTransport *transport = CAMEL_SMTP_TRANSPORT (service);
    CamelStream *tcp_stream;
    char *respbuf = NULL;
    struct hostent *h;
    int port, ret;
    
    if (!CAMEL_SERVICE_CLASS (parent_class)->connect (service, ex))
        return FALSE;
    
    h = camel_service_gethost (service, ex);
    if (!h)
        return FALSE;
    
    /* set some smtp transport defaults */
    transport->flags &= ~(CAMEL_SMTP_TRANSPORT_IS_ESMTP |
                  CAMEL_SMTP_TRANSPORT_8BITMIME |
                  CAMEL_SMTP_TRANSPORT_STARTTLS |
                  CAMEL_SMTP_TRANSPORT_ENHANCEDSTATUSCODES);
    
    transport->authtypes = NULL;
    
    port = service->url->port ? service->url->port : SMTP_PORT;
    
#ifdef HAVE_SSL
    if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL) {
        if (try_starttls)
            tcp_stream = camel_tcp_stream_ssl_new_raw (service, service->url->host);
        else {
            port = service->url->port ? service->url->port : 465;
            tcp_stream = camel_tcp_stream_ssl_new (service, service->url->host);
        }
    } 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) {
        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 (CAMEL_OBJECT (tcp_stream));
        
        return FALSE;
    }
    
    /* get the localaddr - needed later by smtp_helo */
    transport->localaddr = camel_tcp_stream_get_local_address (CAMEL_TCP_STREAM (tcp_stream));
    
    transport->ostream = tcp_stream;
    transport->istream = camel_stream_buffer_new (tcp_stream, CAMEL_STREAM_BUFFER_READ);
    
    /* Read the greeting, note whether the server is ESMTP or not. */
    do {
        /* Check for "220" */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        if (!respbuf || strncmp (respbuf, "220", 3)) {
            int error;
            
            error = respbuf ? atoi (respbuf) : 0;
            g_free (respbuf);
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("Welcome response error: %s: possibly non-fatal"),
                          smtp_error_string (error));
            return FALSE;
        }
        if (strstr (respbuf, "ESMTP"))
            transport->flags |= CAMEL_SMTP_TRANSPORT_IS_ESMTP;
    } while (*(respbuf+3) == '-'); /* if we got "220-" then loop again */
    g_free (respbuf);
    
    /* send HELO (or EHLO, depending on the service type) */
    if (!(transport->flags & CAMEL_SMTP_TRANSPORT_IS_ESMTP)) {
        /* If we did not auto-detect ESMTP, we should still send EHLO */
        transport->flags |= CAMEL_SMTP_TRANSPORT_IS_ESMTP;
        if (!smtp_helo (transport, NULL)) {
            /* Okay, apprently this server doesn't support ESMTP */
            transport->flags &= ~CAMEL_SMTP_TRANSPORT_IS_ESMTP;
            smtp_helo (transport, ex);
        }
    } else {
        /* send EHLO */
        smtp_helo (transport, ex);
    }
    
#ifdef HAVE_SSL
    if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE) {
        /* try_starttls is always TRUE here */
        if (transport->flags & CAMEL_SMTP_TRANSPORT_STARTTLS)
            goto starttls;
    } else if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS) {
        if (try_starttls) {
            if (transport->flags & CAMEL_SMTP_TRANSPORT_STARTTLS) {
                goto starttls;
            } else {
                /* server doesn't support STARTTLS, abort */
                camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                              _("Failed to connect to SMTP server %s in secure mode: %s"),
                              service->url->host, _("server does not appear to support SSL"));
                goto exception_cleanup;
            }
        }
    }
#endif /* HAVE_SSL */
    
    return TRUE;
    
#ifdef HAVE_SSL
 starttls:
    d(fprintf (stderr, "sending : STARTTLS\r\n"));
    if (camel_stream_write (tcp_stream, "STARTTLS\r\n", 10) == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("STARTTLS request timed out: %s"),
                      g_strerror (errno));
        goto exception_cleanup;
    }
    
    respbuf = NULL;
    
    do {
        /* Check for "220 Ready for TLS" */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "220", 3)) {
            smtp_set_exception (transport, respbuf, _("STARTTLS response error"), ex);
            g_free (respbuf);
            goto exception_cleanup;
        }
    } while (*(respbuf+3) == '-'); /* if we got "220-" then loop again */
    
    /* Okay, now toggle SSL/TLS mode */
    ret = camel_tcp_stream_ssl_enable_ssl (CAMEL_TCP_STREAM_SSL (tcp_stream));
    if (ret != -1)
        return TRUE;
    
    camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                  _("Failed to connect to SMTP server %s in secure mode: %s"),
                  service->url->host, g_strerror (errno));
    
 exception_cleanup:
    camel_object_unref (CAMEL_OBJECT (transport->istream));
    transport->istream = NULL;
    camel_object_unref (CAMEL_OBJECT (transport->ostream));
    transport->ostream = NULL;
    
    return FALSE;
#endif /* HAVE_SSL */
}

static gboolean
connect_to_server_wrapper (CamelService *service, CamelException *ex)
{
#ifdef HAVE_SSL
    CamelSmtpTransport *transport = (CamelSmtpTransport *) service;
    
    if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_ALWAYS) {
        /* First try connecting to the SSL port  */
        if (!connect_to_server (service, FALSE, ex)) {
            if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_SERVICE_UNAVAILABLE) {
                /* Seems the SSL port is unavailable, lets try STARTTLS */
                camel_exception_clear (ex);
                return connect_to_server (service, TRUE, ex);
            } else {
                return FALSE;
            }
        }
        
        return TRUE;
    } else if (transport->flags & CAMEL_SMTP_TRANSPORT_USE_SSL_WHEN_POSSIBLE) {
        /* If the server supports STARTTLS, use it */
        return connect_to_server (service, TRUE, ex);
    } else {
        /* User doesn't care about SSL */
        return connect_to_server (service, FALSE, ex);
    }
#else
    return connect_to_server (service, FALSE, ex);
#endif
}

static gboolean
smtp_connect (CamelService *service, CamelException *ex)
{
    CamelSmtpTransport *transport = CAMEL_SMTP_TRANSPORT (service);
    gboolean has_authtypes;
    
    /* We (probably) need to check popb4smtp before we connect ... */
    if (service->url->authmech && !strcmp (service->url->authmech, "POPB4SMTP")) {
        int truth;
        GByteArray *chal;
        CamelSasl *sasl;
        
        sasl = camel_sasl_new ("smtp", "POPB4SMTP", service);
        chal = camel_sasl_challenge (sasl, NULL, ex);
        truth = camel_sasl_authenticated (sasl);
        if (chal)
            g_byte_array_free (chal, TRUE);
        camel_object_unref (CAMEL_OBJECT (sasl));
        
        if (!truth)
            return FALSE;
        
        return connect_to_server_wrapper (service, ex);
    }
    
    if (!connect_to_server_wrapper (service, ex))
        return FALSE;
    
    /* check to see if AUTH is required, if so...then AUTH ourselves */
    has_authtypes = g_hash_table_size (transport->authtypes) > 0;
    if (service->url->authmech && (transport->flags & CAMEL_SMTP_TRANSPORT_IS_ESMTP) && has_authtypes) {
        CamelSession *session = camel_service_get_session (service);
        CamelServiceAuthType *authtype;
        gboolean authenticated = FALSE;
        char *errbuf = NULL;
        
        if (!g_hash_table_lookup (transport->authtypes, service->url->authmech)) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                          _("SMTP server %s does not support requested "
                        "authentication type %s."),
                          service->url->host, service->url->authmech);
            camel_service_disconnect (service, TRUE, NULL);
            return FALSE;
        }
        
        authtype = camel_sasl_authtype (service->url->authmech);
        if (!authtype) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                          _("No support for authentication type %s"),
                          service->url->authmech);
            camel_service_disconnect (service, TRUE, NULL);
            return FALSE;
        }
        
        if (!authtype->need_password) {
            /* authentication mechanism doesn't need a password,
               so if it fails there's nothing we can do */
            authenticated = smtp_auth (transport, authtype->authproto, ex);
            if (!authenticated) {
                camel_service_disconnect (service, TRUE, NULL);
                return FALSE;
            }
        }
        
        /* keep trying to login until either we succeed or the user cancels */
        while (!authenticated) {
            if (errbuf) {
                /* We need to un-cache the password before prompting again */
                camel_session_forget_password (session, service, "password", ex);
                g_free (service->url->passwd);
                service->url->passwd = NULL;
            }
            
            if (!service->url->passwd) {
                char *prompt;
                
                prompt = g_strdup_printf (_("%sPlease enter the SMTP password for %s@%s"),
                              errbuf ? errbuf : "", service->url->user,
                              service->url->host);
                
                service->url->passwd = camel_session_get_password (session, prompt, TRUE,
                                           service, "password", ex);
                
                g_free (prompt);
                g_free (errbuf);
                errbuf = NULL;
                
                if (!service->url->passwd) {
                    camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL,
                                 _("You didn't enter a password."));
                    camel_service_disconnect (service, TRUE, NULL);
                    return FALSE;
                }
            }
            
            authenticated = smtp_auth (transport, authtype->authproto, ex);
            if (!authenticated) {
                errbuf = g_strdup_printf (_("Unable to authenticate "
                                "to SMTP server.\n%s\n\n"),
                              camel_exception_get_description (ex));
                camel_exception_clear (ex);
            }
        }
        
        /* The spec says we have to re-EHLO, but some servers
         * we won't bother to name don't want you to... so ignore
         * errors.
         */
        smtp_helo (transport, NULL);
    }
    
    return TRUE;
}

static gboolean
authtypes_free (gpointer key, gpointer value, gpointer data)
{
    g_free (value);
    
    return TRUE;
}

static gboolean
smtp_disconnect (CamelService *service, gboolean clean, CamelException *ex)
{
    CamelSmtpTransport *transport = CAMEL_SMTP_TRANSPORT (service);
    
    /*if (!service->connected)
     *  return TRUE;
     */
    
    if (clean) {
        /* send the QUIT command to the SMTP server */
        smtp_quit (transport, ex);
    }
    
    if (!CAMEL_SERVICE_CLASS (parent_class)->disconnect (service, clean, ex))
        return FALSE;
    
    if (transport->authtypes) {
        g_hash_table_foreach_remove (transport->authtypes, authtypes_free, NULL);
        g_hash_table_destroy (transport->authtypes);
        transport->authtypes = NULL;
    }
    
    camel_object_unref (CAMEL_OBJECT (transport->ostream));
    camel_object_unref (CAMEL_OBJECT (transport->istream));
    transport->ostream = NULL;
    transport->istream = NULL;

    camel_tcp_address_free (transport->localaddr);
    transport->localaddr = NULL;
    
    return TRUE;
}

static GHashTable *
esmtp_get_authtypes (const unsigned char *buffer)
{
    const unsigned char *start, *end;
    GHashTable *table = NULL;
    
    /* advance to the first token */
    start = buffer;
    while (isspace ((int) *start) || *start == '=')
        start++;
    
    if (!*start)
        return NULL;
    
    table = g_hash_table_new (g_str_hash, g_str_equal);
    
    for ( ; *start; ) {
        char *type;
        
        /* advance to the end of the token */
        end = start;
        while (*end && !isspace ((int) *end))
            end++;
        
        type = g_strndup (start, end - start);
        g_hash_table_insert (table, type, type);
        
        /* advance to the next token */
        start = end;
        while (isspace ((int) *start))
            start++;
    }
    
    return table;
}

static GList *
query_auth_types (CamelService *service, CamelException *ex)
{
    CamelSmtpTransport *transport = CAMEL_SMTP_TRANSPORT (service);
    CamelServiceAuthType *authtype;
    GList *types, *t, *next;
    
    if (!connect_to_server_wrapper (service, ex))
        return NULL;
    
    types = g_list_copy (service->provider->authtypes);
    for (t = types; t; t = next) {
        authtype = t->data;
        next = t->next;
        
        if (!g_hash_table_lookup (transport->authtypes, authtype->authproto)) {
            types = g_list_remove_link (types, t);
            g_list_free_1 (t);
        }
    }
    
    smtp_disconnect (service, TRUE, NULL);
    
    return types;
}

static char *
get_name (CamelService *service, gboolean brief)
{
    if (brief)
        return g_strdup_printf (_("SMTP server %s"), service->url->host);
    else {
        return g_strdup_printf (_("SMTP mail delivery via %s"),
                    service->url->host);
    }
}

static gboolean
smtp_send_to (CamelTransport *transport, CamelMimeMessage *message,
          CamelAddress *from, CamelAddress *recipients,
          CamelException *ex)
{
    CamelSmtpTransport *smtp_transport = CAMEL_SMTP_TRANSPORT (transport);
    const CamelInternetAddress *cia;
    gboolean has_8bit_parts;
    const char *addr;
    int i, len;
    
    if (!camel_internet_address_get (CAMEL_INTERNET_ADDRESS (from), 0, NULL, &addr)) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot send message: "
                    "sender address not valid."));
        return FALSE;
    }
    
    camel_operation_start (NULL, _("Sending message"));
    
    /* find out if the message has 8bit mime parts */
    has_8bit_parts = camel_mime_message_has_8bit_parts (message);
    
    /* rfc1652 (8BITMIME) requires that you notify the ESMTP daemon that
       you'll be sending an 8bit mime message at "MAIL FROM:" time. */
    smtp_mail (smtp_transport, addr, has_8bit_parts, ex);
    
    if (!recipients) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot send message: no recipients defined."));
        camel_operation_end (NULL);
        return FALSE;
    }
    
    len = camel_address_length (recipients);
    cia = CAMEL_INTERNET_ADDRESS (recipients);
    for (i = 0; i < len; i++) {
        if (!camel_internet_address_get (cia, i, NULL, &addr)) {
            camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Cannot send message: one or more invalid recipients"));
            camel_operation_end (NULL);
            return FALSE;
        }
        
        if (!smtp_rcpt (smtp_transport, addr, ex)) {
            camel_operation_end (NULL);
            return FALSE;
        }
    }
    
    /* passing in has_8bit_parts saves time as we don't have to
           recurse through the message all over again if the user is
           not sending 8bit mime parts */
    if (!smtp_data (smtp_transport, message, has_8bit_parts, ex)) {
        camel_operation_end (NULL);
        return FALSE;
    }
    
    /* reset the service for our next transfer session */
    smtp_rset (smtp_transport, ex);
    
    camel_operation_end (NULL);
    
    return TRUE;
}

static const char *
smtp_next_token (const char *buf)
{
    const unsigned char *token;
    
    token = (const unsigned char *) buf;
    while (*token && !isspace ((int) *token))
        token++;
    
    while (*token && isspace ((int) *token))
        token++;
    
    return (const char *) token;
}

#define HEXVAL(c) (isdigit (c) ? (c) - '0' : (c) - 'A' + 10)

/**
 * example (rfc2034):
 * 5.1.1 Mailbox "nosuchuser" does not exist
 *
 * The human-readable status code is what we want. Since this text
 * could possibly be encoded, we must decode it.
 *
 * "xtext" is formally defined as follows:
 *
 *   xtext = *( xchar / hexchar / linear-white-space / comment )
 *
 *   xchar = any ASCII CHAR between "!" (33) and "~" (126) inclusive,
 *        except for "+", "\" and "(".
 *
 * "hexchar"s are intended to encode octets that cannot be represented
 * as plain text, either because they are reserved, or because they are
 * non-printable.  However, any octet value may be represented by a
 * "hexchar".
 *
 *   hexchar = ASCII "+" immediately followed by two upper case
 *        hexadecimal digits
 **/
static char *
smtp_decode_status_code (const char *in, size_t len)
{
    unsigned char *inptr, *outptr;
    const unsigned char *inend;
    char *outbuf;
    
    outptr = outbuf = g_malloc (len + 1);
    
    inptr = (unsigned char *) in;
    inend = inptr + len;
    while (inptr < inend) {
        if (*inptr == '+') {
            if (isxdigit (inptr[1]) && isxdigit (inptr[2])) {
                *outptr++ = HEXVAL (inptr[1]) * 16 + HEXVAL (inptr[2]);
                inptr += 3;
            } else
                *outptr++ = *inptr++;
        } else
            *outptr++ = *inptr++;
    }
    
    *outptr = '\0';
    
    return outbuf;
}

static void
smtp_set_exception (CamelSmtpTransport *transport, const char *respbuf, const char *message, CamelException *ex)
{
    const char *token, *rbuf = respbuf;
    char *buffer = NULL;
    GString *string;
    int error;
    
    if (!respbuf || !(transport->flags & CAMEL_SMTP_TRANSPORT_ENHANCEDSTATUSCODES)) {
    fake_status_code:
        error = respbuf ? atoi (respbuf) : 0;
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      "%s: %s", message,
                      smtp_error_string (error));
    } else {
        string = g_string_new ("");
        do {
            token = smtp_next_token (rbuf + 4);
            if (*token == '\0') {
                g_free (buffer);
                g_string_free (string, TRUE);
                goto fake_status_code;
            }
            
            g_string_append (string, token);
            if (*(rbuf + 3) == '-') {
                g_free (buffer);
                buffer = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
            } else {
                g_free (buffer);
                buffer = NULL;
            }
            
            rbuf = buffer;
        } while (rbuf);
        
        buffer = smtp_decode_status_code (string->str, string->len);
        g_string_free (string, TRUE);
        if (!buffer)
            goto fake_status_code;
        
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      "%s: %s", message, buffer);
        
        g_free (buffer);
    }
}

static gboolean
smtp_helo (CamelSmtpTransport *transport, CamelException *ex)
{
    /* say hello to the server */
    char *name, *cmdbuf, *respbuf = NULL;
    struct hostent *host;
    CamelException err;
    const char *token;
    
    camel_operation_start_transient (NULL, _("SMTP Greeting"));
    
    /* get the local host name */
    camel_exception_init (&err);
    host = camel_gethostbyaddr ((char *) &transport->localaddr->address,
                    transport->localaddr->length, AF_INET, &err);
    camel_exception_clear (&err);
    
    if (host && host->h_name) {
        name = g_strdup (host->h_name);
    } else {
        name = g_strdup_printf ("[%d.%d.%d.%d]",
                    transport->localaddr->address[0],
                    transport->localaddr->address[1],
                    transport->localaddr->address[2],
                    transport->localaddr->address[3]);
    }
    
    camel_free_host (host);
    
    /* hiya server! how are you today? */
    if (transport->flags & CAMEL_SMTP_TRANSPORT_IS_ESMTP)
        cmdbuf = g_strdup_printf ("EHLO %s\r\n", name);
    else
        cmdbuf = g_strdup_printf ("HELO %s\r\n", name);
    g_free (name);
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("HELO request timed out: %s: non-fatal"),
                      g_strerror (errno));
        camel_operation_end (NULL);
        return FALSE;
    }
    g_free (cmdbuf);
    
    do {
        /* Check for "250" */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "250", 3)) {
            int error;
            
            error = respbuf ? atoi (respbuf) : 0;
            g_free (respbuf);
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("HELO response error: %s: non-fatal"),
                          smtp_error_string (error));
            camel_operation_end (NULL);
            return FALSE;
        }
        
        token = respbuf + 4;
        
        if (transport->flags & CAMEL_SMTP_TRANSPORT_IS_ESMTP) {
            if (!strncmp (token, "8BITMIME", 8)) {
                d(fprintf (stderr, "This server supports 8bit MIME\n"));
                transport->flags |= CAMEL_SMTP_TRANSPORT_8BITMIME;
            } else if (!strncmp (token, "ENHANCEDSTATUSCODES", 19)) {
                d(fprintf (stderr, "This server supports enhanced status codes\n"));
                transport->flags |= CAMEL_SMTP_TRANSPORT_ENHANCEDSTATUSCODES;
            } else if (!strncmp (token, "STARTTLS", 8)) {
                d(fprintf (stderr, "This server supports STARTTLS\n"));
                transport->flags |= CAMEL_SMTP_TRANSPORT_STARTTLS;
            } else if (!transport->authtypes && !strncmp (token, "AUTH", 4)) {
                /* Don't bother parsing any authtypes if we already have a list.
                 * Some servers will list AUTH twice, once the standard way and
                 * once the way Microsoft Outlook requires them to be:
                 *
                 * 250-AUTH LOGIN PLAIN DIGEST-MD5 CRAM-MD5
                 * 250-AUTH=LOGIN PLAIN DIGEST-MD5 CRAM-MD5
                 **/
                
                /* parse for supported AUTH types */
                token += 5;
                
                transport->authtypes = esmtp_get_authtypes (token);
            }
        }
    } while (*(respbuf+3) == '-'); /* if we got "250-" then loop again */
    g_free (respbuf);
    
    camel_operation_end (NULL);
    
    return TRUE;
}

static gboolean
smtp_auth (CamelSmtpTransport *transport, const char *mech, CamelException *ex)
{
    char *cmdbuf, *respbuf = NULL, *challenge;
    gboolean auth_challenge = FALSE;
    CamelSasl *sasl = NULL;
    
    camel_operation_start_transient (NULL, _("SMTP Authentication"));
    
    sasl = camel_sasl_new ("smtp", mech, CAMEL_SERVICE (transport));
    if (!sasl) {
        camel_operation_end (NULL);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Error creating SASL authentication object."));
        return FALSE;
    }
    
    challenge = camel_sasl_challenge_base64 (sasl, NULL, ex);
    if (challenge) {
        auth_challenge = TRUE;
        cmdbuf = g_strdup_printf ("AUTH %s %s\r\n", mech, challenge);
        g_free (challenge);
    } else {
        cmdbuf = g_strdup_printf ("AUTH %s\r\n", mech);
    }
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("AUTH request timed out: %s"),
                      g_strerror (errno));
        goto lose;
    }
    g_free (cmdbuf);
    
    respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
    d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
    
    while (!camel_sasl_authenticated (sasl)) {
        if (!respbuf) {
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("AUTH request timed out: %s"),
                          g_strerror (errno));
            goto lose;
        }
        
        /* the server challenge/response should follow a 334 code */
        if (strncmp (respbuf, "334", 3)) {
            g_free (respbuf);
            camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                          _("AUTH request failed."));
            goto lose;
        }
        
        if (FALSE) {
        broken_smtp_server:
            d(fprintf (stderr, "Your SMTP server's implementation of the %s SASL\n"
                   "authentication mechanism is broken. Please report this to the\n"
                   "appropriate vendor and suggest that they re-read rfc2222 again\n"
                   "for the first time (specifically Section 4, paragraph 2).\n",
                   mech));
        }
        
        /* eat whtspc */
        for (challenge = respbuf + 4; isspace (*challenge); challenge++);
        
        challenge = camel_sasl_challenge_base64 (sasl, challenge, ex);
        g_free (respbuf);
        if (challenge == NULL)
            goto break_and_lose;
        
        /* send our challenge */
        cmdbuf = g_strdup_printf ("%s\r\n", challenge);
        g_free (challenge);
        d(fprintf (stderr, "sending : %s", cmdbuf));
        if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
            g_free (cmdbuf);
            goto lose;
        }
        g_free (cmdbuf);
        
        /* get the server's response */
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
    }
    
    /* check that the server says we are authenticated */
    if (!respbuf || strncmp (respbuf, "235", 3)) {
        if (respbuf && auth_challenge && !strncmp (respbuf, "334", 3)) {
            /* broken server, but lets try and work around it anyway... */
            goto broken_smtp_server;
        }
        g_free (respbuf);
        goto lose;
    }
    
    camel_object_unref (CAMEL_OBJECT (sasl));
    camel_operation_end (NULL);
    
    return TRUE;
    
 break_and_lose:
    /* Get the server out of "waiting for continuation data" mode. */
    d(fprintf (stderr, "sending : *\n"));
    camel_stream_write (transport->ostream, "*\r\n", 3);
    respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
    d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
    
 lose:
    if (!camel_exception_is_set (ex)) {
        camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
                     _("Bad authentication response from server.\n"));
    }
    
    camel_object_unref (CAMEL_OBJECT (sasl));
    camel_operation_end (NULL);
    
    return FALSE;
}

static gboolean
smtp_mail (CamelSmtpTransport *transport, const char *sender, gboolean has_8bit_parts, CamelException *ex)
{
    /* we gotta tell the smtp server who we are. (our email addy) */
    char *cmdbuf, *respbuf = NULL;
    
    if (transport->flags & CAMEL_SMTP_TRANSPORT_8BITMIME && has_8bit_parts)
        cmdbuf = g_strdup_printf ("MAIL FROM:<%s> BODY=8BITMIME\r\n", sender);
    else
        cmdbuf = g_strdup_printf ("MAIL FROM:<%s>\r\n", sender);
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("MAIL FROM request timed out: %s: mail not sent"),
                      g_strerror (errno));
        return FALSE;
    }
    g_free (cmdbuf);
    
    do {
        /* Check for "250 Sender OK..." */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "250", 3)) {
            smtp_set_exception (transport, respbuf, _("MAIL FROM response error"), ex);
            g_free (respbuf);
            return FALSE;
        }
    } while (*(respbuf+3) == '-'); /* if we got "250-" then loop again */
    g_free (respbuf);
    
    return TRUE;
}

static gboolean
smtp_rcpt (CamelSmtpTransport *transport, const char *recipient, CamelException *ex)
{
    /* we gotta tell the smtp server who we are going to be sending
     * our email to */
    char *cmdbuf, *respbuf = NULL;
    
    cmdbuf = g_strdup_printf ("RCPT TO:<%s>\r\n", recipient);
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("RCPT TO request timed out: %s: mail not sent"),
                      g_strerror (errno));
        return FALSE;
    }
    g_free (cmdbuf);
    
    do {
        /* Check for "250 Recipient OK..." */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "250", 3)) {
            char *message;
            
            message = g_strdup_printf (_("RCPT TO <%s> failed"), recipient);
            smtp_set_exception (transport, respbuf, message, ex);
            g_free (message);
            g_free (respbuf);
            return FALSE;
        }
    } while (*(respbuf+3) == '-'); /* if we got "250-" then loop again */
    g_free (respbuf);
    
    return TRUE;
}

static gboolean
smtp_data (CamelSmtpTransport *transport, CamelMimeMessage *message, gboolean has_8bit_parts, CamelException *ex)
{
    /* now we can actually send what's important :p */
    char *cmdbuf, *respbuf = NULL;
    CamelStreamFilter *filtered_stream;
    CamelMimeFilter *crlffilter;
    struct _header_raw *header;
    GSList *h, *bcc = NULL;
    int ret;
    
    /* if the message contains 8bit mime parts and the server
           doesn't support it, encode 8bit parts to the best
           encoding.  This will also enforce an encoding to keep the lines in limit */
    if (has_8bit_parts && !(transport->flags & CAMEL_SMTP_TRANSPORT_8BITMIME))
        camel_mime_message_encode_8bit_parts (message);
    
    cmdbuf = g_strdup ("DATA\r\n");
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("DATA request timed out: %s: mail not sent"),
                      g_strerror (errno));
        return FALSE;
    }
    g_free (cmdbuf);
    
    respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
    
    d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
    
    if (!respbuf || strncmp (respbuf, "354", 3)) {
        /* we should have gotten instructions on how to use the DATA command:
         * 354 Enter mail, end with "." on a line by itself
         */
        smtp_set_exception (transport, respbuf, _("DATA response error"), ex);
        g_free (respbuf);
        return FALSE;
    }
    
    g_free (respbuf);
    respbuf = NULL;
    
    /* setup stream filtering */
    crlffilter = camel_mime_filter_crlf_new (CAMEL_MIME_FILTER_CRLF_ENCODE, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS);
    filtered_stream = camel_stream_filter_new_with_stream (transport->ostream);
    camel_stream_filter_add (filtered_stream, CAMEL_MIME_FILTER (crlffilter));
    camel_object_unref (CAMEL_OBJECT (crlffilter));
    
    /* copy and remove the bcc headers */
    header = CAMEL_MIME_PART (message)->headers;
    while (header) {
        if (!g_strcasecmp (header->name, "Bcc"))
            bcc = g_slist_append (bcc, g_strdup (header->value));
        header = header->next;
    }
    
    camel_medium_remove_header (CAMEL_MEDIUM (message), "Bcc");
    
    /* write the message */
    ret = camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message), CAMEL_STREAM (filtered_stream));
    
    /* add the bcc headers back */
    if (bcc) {
        h = bcc;
        while (h) {
            camel_medium_add_header (CAMEL_MEDIUM (message), "Bcc", h->data);
            g_free (h->data);
            h = h->next;
        }
        g_slist_free (bcc);
    }
    
    if (ret == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("DATA send timed out: message termination: "
                    "%s: mail not sent"),
                      g_strerror (errno));
        
        camel_object_unref (CAMEL_OBJECT (filtered_stream));
        
        return FALSE;
    }
    
    camel_stream_flush (CAMEL_STREAM (filtered_stream));
    camel_object_unref (CAMEL_OBJECT (filtered_stream));
    
    /* terminate the message body */
    
    d(fprintf (stderr, "sending : \\r\\n.\\r\\n\n"));
    
    if (camel_stream_write (transport->ostream, "\r\n.\r\n", 5) == -1) {
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("DATA send timed out: message termination: "
                    "%s: mail not sent"),
                      g_strerror (errno));
        return FALSE;
    }
    
    do {
        /* Check for "250 Sender OK..." */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "250", 3)) {
            smtp_set_exception (transport, respbuf, _("DATA termination response error"), ex);
            g_free (respbuf);
            return FALSE;
        }
    } while (*(respbuf+3) == '-'); /* if we got "250-" then loop again */
    g_free (respbuf);
    
    return TRUE;
}

static gboolean
smtp_rset (CamelSmtpTransport *transport, CamelException *ex)
{
    /* we are going to reset the smtp server (just to be nice) */
    char *cmdbuf, *respbuf = NULL;
    
    cmdbuf = g_strdup ("RSET\r\n");
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("RSET request timed out: %s"),
                      g_strerror (errno));
        return FALSE;
    }
    g_free (cmdbuf);
    
    do {
        /* Check for "250" */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "250", 3)) {
            smtp_set_exception (transport, respbuf, _("RSET response error"), ex);
            g_free (respbuf);
            return FALSE;
        }
    } while (*(respbuf+3) == '-'); /* if we got "250-" then loop again */
    g_free (respbuf);
    
    return TRUE;
}

static gboolean
smtp_quit (CamelSmtpTransport *transport, CamelException *ex)
{
    /* we are going to reset the smtp server (just to be nice) */
    char *cmdbuf, *respbuf = NULL;
    
    cmdbuf = g_strdup ("QUIT\r\n");
    
    d(fprintf (stderr, "sending : %s", cmdbuf));
    
    if (camel_stream_write (transport->ostream, cmdbuf, strlen (cmdbuf)) == -1) {
        g_free (cmdbuf);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
                      _("QUIT request timed out: %s: non-fatal"),
                      g_strerror (errno));
        return FALSE;
    }
    g_free (cmdbuf);
    
    do {
        /* Check for "221" */
        g_free (respbuf);
        respbuf = camel_stream_buffer_read_line (CAMEL_STREAM_BUFFER (transport->istream));
        
        d(fprintf (stderr, "received: %s\n", respbuf ? respbuf : "(null)"));
        
        if (!respbuf || strncmp (respbuf, "221", 3)) {
            smtp_set_exception (transport, respbuf, _("QUIT response error"), ex);
            g_free (respbuf);
            return FALSE;
        }
    } while (*(respbuf+3) == '-'); /* if we got "221-" then loop again */
    g_free (respbuf);
    
    return TRUE;
}