3b94b4708b9e898efe73bfea00c44c1143a06761
[claws.git] / src / common / smtp.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #ifdef ENABLE_NLS
27 #include <glib/gi18n.h>
28 #else
29 #define _(a) (a)
30 #define N_(a) (a)
31 #endif
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "smtp.h"
36 #include "md5.h"
37 #include "utils.h"
38 #include "log.h"
39
40 static void smtp_session_destroy(Session *session);
41
42 static gint smtp_auth(SMTPSession *session);
43 #ifdef USE_GNUTLS
44 static gint smtp_starttls(SMTPSession *session);
45 #endif
46 static gint smtp_auth_cram_md5(SMTPSession *session);
47 static gint smtp_auth_login(SMTPSession *session);
48 static gint smtp_auth_plain(SMTPSession *session);
49
50 static gint smtp_ehlo(SMTPSession *session);
51 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
52
53 static gint smtp_helo(SMTPSession *session);
54 static gint smtp_rcpt(SMTPSession *session);
55 static gint smtp_data(SMTPSession *session);
56 static gint smtp_send_data(SMTPSession *session);
57 static gint smtp_make_ready(SMTPSession *session);
58 static gint smtp_eom(SMTPSession *session);
59
60 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
61 static gint smtp_session_send_data_finished(Session *session, guint len);
62
63
64 Session *smtp_session_new(void *prefs_account)
65 {
66         SMTPSession *session;
67
68         session = g_new0(SMTPSession, 1);
69
70         session_init(SESSION(session), prefs_account, TRUE);
71
72         SESSION(session)->type             = SESSION_SMTP;
73
74         SESSION(session)->recv_msg         = smtp_session_recv_msg;
75
76         SESSION(session)->recv_data_finished = NULL;
77         SESSION(session)->send_data_finished = smtp_session_send_data_finished;
78
79         SESSION(session)->destroy          = smtp_session_destroy;
80
81         session->state                     = SMTP_READY;
82
83 #ifdef USE_GNUTLS
84         session->tls_init_done             = FALSE;
85 #endif
86
87         session->hostname                  = NULL;
88         session->user                      = NULL;
89         session->pass                      = NULL;
90
91         session->from                      = NULL;
92         session->to_list                   = NULL;
93         session->cur_to                    = NULL;
94
95         session->send_data                 = NULL;
96         session->send_data_len             = 0;
97
98         session->max_message_size          = -1;
99
100         session->avail_auth_type           = 0;
101         session->forced_auth_type          = 0;
102         session->auth_type                 = 0;
103         session->esmtp_flags               = 0;
104
105         session->error_val                 = SM_OK;
106         session->error_msg                 = NULL;
107
108         return SESSION(session);
109 }
110
111 static void smtp_session_destroy(Session *session)
112 {
113         SMTPSession *smtp_session = SMTP_SESSION(session);
114
115         g_free(smtp_session->hostname);
116         g_free(smtp_session->user);
117         g_free(smtp_session->pass);
118         g_free(smtp_session->from);
119
120         g_free(smtp_session->send_data);
121
122         g_free(smtp_session->error_msg);
123 }
124
125 gint smtp_from(SMTPSession *session)
126 {
127         gchar buf[MESSAGEBUFSIZE];
128         gchar *mail_size = NULL;
129
130         cm_return_val_if_fail(session->from != NULL, SM_ERROR);
131
132         session->state = SMTP_FROM;
133         
134         if (session->is_esmtp && (session->esmtp_flags & ESMTP_SIZE)!=0)
135                 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
136         else
137                 mail_size = g_strdup("");
138                 
139
140         if (strchr(session->from, '<'))
141                 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
142                            mail_size);
143         else
144                 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
145                            mail_size);
146
147         g_free(mail_size);
148
149         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
150                 return SM_ERROR;
151         log_print(LOG_PROTOCOL, "%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
152
153         return SM_OK;
154 }
155
156 static gint smtp_auth(SMTPSession *session)
157 {
158
159         cm_return_val_if_fail(session->user != NULL, SM_ERROR);
160
161         session->state = SMTP_AUTH;
162
163         if ((session->forced_auth_type == SMTPAUTH_CRAM_MD5
164              || session->forced_auth_type == 0)
165             &&
166              (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)
167                 smtp_auth_cram_md5(session);
168         else if ((session->forced_auth_type == SMTPAUTH_LOGIN
169                   || session->forced_auth_type == 0)
170                  &&
171                   (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)
172                 smtp_auth_login(session);
173         else if ((session->forced_auth_type == SMTPAUTH_PLAIN
174                   || session->forced_auth_type == 0)
175                  &&
176                   (session->avail_auth_type & SMTPAUTH_PLAIN) != 0)
177                 smtp_auth_plain(session);
178         else if (session->forced_auth_type == 0) {
179                 log_warning(LOG_PROTOCOL, _("No SMTP AUTH method available\n"));
180                 return SM_AUTHFAIL;
181         } else {
182                 log_warning(LOG_PROTOCOL, _("Selected SMTP AUTH method not available\n"));
183                 return SM_AUTHFAIL;
184         }
185
186         return SM_OK;
187 }
188
189 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
190 {
191         gchar buf[MESSAGEBUFSIZE], *tmp;
192
193         switch (session->auth_type) {
194         case SMTPAUTH_LOGIN:
195                 session->state = SMTP_AUTH_LOGIN_USER;
196
197                 if (!strncmp(msg, "334 ", 4)) {
198                         tmp = g_base64_encode(session->user, strlen(session->user));
199
200                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
201                                          tmp) < 0) {
202                                 g_free(tmp);
203                                 return SM_ERROR;
204                         }
205                         g_free(tmp);
206                         log_print(LOG_PROTOCOL, "ESMTP> [USERID]\n");
207                 } else {
208                         /* Server rejects AUTH */
209                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
210                                          "*") < 0)
211                                 return SM_ERROR;
212                         log_print(LOG_PROTOCOL, "ESMTP> *\n");
213                 }
214                 break;
215         case SMTPAUTH_CRAM_MD5:
216                 session->state = SMTP_AUTH_CRAM_MD5;
217
218                 if (!strncmp(msg, "334 ", 4)) {
219                         gchar *response;
220                         gchar *response64;
221                         gchar *challenge, *tmp;
222                         gsize challengelen;
223                         guchar hexdigest[33];
224
225                         tmp = g_base64_decode(msg + 4, &challengelen);
226                         challenge = g_strndup(tmp, challengelen);
227                         g_free(tmp);
228                         log_print(LOG_PROTOCOL, "ESMTP< [Decoded: %s]\n", challenge);
229
230                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
231                         md5_hex_hmac(hexdigest, challenge, challengelen,
232                                      buf, strlen(session->pass));
233                         g_free(challenge);
234
235                         response = g_strdup_printf
236                                 ("%s %s", session->user, hexdigest);
237                         log_print(LOG_PROTOCOL, "ESMTP> [Encoded: %s]\n", response);
238
239                         response64 = g_base64_encode(response, strlen(response));
240                         g_free(response);
241
242                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
243                                          response64) < 0) {
244                                 g_free(response64);
245                                 return SM_ERROR;
246                         }
247                         log_print(LOG_PROTOCOL, "ESMTP> %s\n", response64);
248                         g_free(response64);
249                 } else {
250                         /* Server rejects AUTH */
251                         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
252                                          "*") < 0)
253                                 return SM_ERROR;
254                         log_print(LOG_PROTOCOL, "ESMTP> *\n");
255                 }
256                 break;
257         case SMTPAUTH_DIGEST_MD5:
258         default:
259                 /* stop smtp_auth when no correct authtype */
260                 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
261                         return SM_ERROR;
262                 log_print(LOG_PROTOCOL, "ESMTP> *\n");
263                 break;
264         }
265
266         return SM_OK;
267 }
268
269 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
270 {
271         gchar buf[MESSAGEBUFSIZE], *tmp;
272         gsize len;
273
274         session->state = SMTP_AUTH_LOGIN_PASS;
275
276         if (!strncmp(msg, "334 ", 4)) {
277                 tmp = g_base64_encode(session->pass, strlen(session->pass));
278                 len = g_strlcat(buf, tmp, MESSAGEBUFSIZE);
279                 if (len >= MESSAGEBUFSIZE) {
280                         /* This should never happen, and even if it does, all it will do
281                          * is send an incorrect password so auth will fail. That's why
282                          * we're printing this debug message, so investigating user or dev
283                          * will know what's wrong. */
284                         debug_print("Truncation of password occured in g_strlcat().\n");
285                 }
286                 g_free(tmp);
287         } else {
288                 /* Server rejects AUTH */
289                 g_snprintf(buf, sizeof(buf), "*");
290         }
291
292         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
293                 return SM_ERROR;
294
295         log_print(LOG_PROTOCOL, "ESMTP> [PASSWORD]\n");
296
297         return SM_OK;
298 }
299
300 static gint smtp_ehlo(SMTPSession *session)
301 {
302         gchar buf[MESSAGEBUFSIZE];
303
304         session->state = SMTP_EHLO;
305
306         session->avail_auth_type = 0;
307
308         g_snprintf(buf, sizeof(buf), "EHLO %s",
309                    session->hostname ? session->hostname : get_domain_name());
310         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
311                 return SM_ERROR;
312         log_print(LOG_PROTOCOL, "ESMTP> %s\n", buf);
313
314         return SM_OK;
315 }
316
317 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
318 {
319         if (strncmp(msg, "250", 3) == 0) {
320                 const gchar *p = msg;
321                 p += 3;
322                 if (*p == '-' || *p == ' ') p++;
323                 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
324                         p += 5;
325                         if (strcasestr(p, "PLAIN"))
326                                 session->avail_auth_type |= SMTPAUTH_PLAIN;
327                         if (strcasestr(p, "LOGIN"))
328                                 session->avail_auth_type |= SMTPAUTH_LOGIN;
329                         if (strcasestr(p, "CRAM-MD5"))
330                                 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
331                         if (strcasestr(p, "DIGEST-MD5"))
332                                 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
333                 }
334                 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
335                         p += 5;
336                         session->max_message_size = atoi(p);
337                         session->esmtp_flags |= ESMTP_SIZE;
338                 }
339                 if (g_ascii_strncasecmp(p, "STARTTLS", 8) == 0) {
340                         p += 9;
341                         session->avail_auth_type |= SMTPAUTH_TLS_AVAILABLE;
342                 }
343                 return SM_OK;
344         } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
345             (msg[3] == ' ' || msg[3] == '\0'))
346                 return SM_OK;
347         else if (msg[0] == '5' && msg[1] == '0' &&
348                  (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
349                 return SM_ERROR;
350
351         return SM_ERROR;
352 }
353
354 #ifdef USE_GNUTLS
355 static gint smtp_starttls(SMTPSession *session)
356 {
357         session->state = SMTP_STARTTLS;
358
359         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS") < 0)
360                 return SM_ERROR;
361         log_print(LOG_PROTOCOL, "ESMTP> STARTTLS\n");
362
363         return SM_OK;
364 }
365 #endif
366
367 static gint smtp_auth_cram_md5(SMTPSession *session)
368 {
369         session->state = SMTP_AUTH;
370         session->auth_type = SMTPAUTH_CRAM_MD5;
371
372         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5") < 0)
373                 return SM_ERROR;
374         log_print(LOG_PROTOCOL, "ESMTP> AUTH CRAM-MD5\n");
375
376         return SM_OK;
377 }
378
379 static gint smtp_auth_plain(SMTPSession *session)
380 {
381         gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
382         gint len;
383
384         session->state = SMTP_AUTH_PLAIN;
385         session->auth_type = SMTPAUTH_PLAIN;
386
387         memset(buf, 0, sizeof buf);
388
389         /* "\0user\0password" */
390         len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
391         b64buf = g_base64_encode(buf, len);
392         out = g_strconcat("AUTH PLAIN ", b64buf, NULL);
393         g_free(b64buf);
394
395         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, out) < 0) {
396                 g_free(out);
397                 return SM_ERROR;
398         }
399
400         g_free(out);
401
402         log_print(LOG_PROTOCOL, "ESMTP> [AUTH PLAIN]\n");
403
404         g_free(b64buf);
405
406         return SM_OK;
407 }
408
409 static gint smtp_auth_login(SMTPSession *session)
410 {
411         session->state = SMTP_AUTH;
412         session->auth_type = SMTPAUTH_LOGIN;
413
414         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN") < 0)
415                 return SM_ERROR;
416         log_print(LOG_PROTOCOL, "ESMTP> AUTH LOGIN\n");
417
418         return SM_OK;
419 }
420
421 static gint smtp_helo(SMTPSession *session)
422 {
423         gchar buf[MESSAGEBUFSIZE];
424
425         session->state = SMTP_HELO;
426
427         g_snprintf(buf, sizeof(buf), "HELO %s",
428                    session->hostname ? session->hostname : get_domain_name());
429         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
430                 return SM_ERROR;
431         log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
432
433         return SM_OK;
434 }
435
436 static gint smtp_rcpt(SMTPSession *session)
437 {
438         gchar buf[MESSAGEBUFSIZE];
439         gchar *to;
440
441         cm_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
442
443         session->state = SMTP_RCPT;
444
445         to = (gchar *)session->cur_to->data;
446
447         if (strchr(to, '<'))
448                 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
449         else
450                 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
451         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0)
452                 return SM_ERROR;
453         log_print(LOG_PROTOCOL, "SMTP> %s\n", buf);
454
455         session->cur_to = session->cur_to->next;
456
457         return SM_OK;
458 }
459
460 static gint smtp_data(SMTPSession *session)
461 {
462         session->state = SMTP_DATA;
463
464         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA") < 0)
465                 return SM_ERROR;
466         log_print(LOG_PROTOCOL, "SMTP> DATA\n");
467
468         return SM_OK;
469 }
470
471 static gint smtp_send_data(SMTPSession *session)
472 {
473         session->state = SMTP_SEND_DATA;
474
475         session_send_data(SESSION(session), session->send_data,
476                           session->send_data_len);
477
478         return SM_OK;
479 }
480
481 static gint smtp_make_ready(SMTPSession *session)
482 {
483         session->state = SMTP_MAIL_SENT_OK;
484
485         return SM_OK;
486 }
487
488 gint smtp_quit(SMTPSession *session)
489 {
490         session->state = SMTP_QUIT;
491
492         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
493         log_print(LOG_PROTOCOL, "SMTP> QUIT\n");
494
495         return SM_OK;
496 }
497
498 static gint smtp_eom(SMTPSession *session)
499 {
500         session->state = SMTP_EOM;
501
502         if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".") < 0)
503                 return SM_ERROR;
504         log_print(LOG_PROTOCOL, "SMTP> . (EOM)\n");
505
506         return SM_OK;
507 }
508
509 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
510 {
511         SMTPSession *smtp_session = SMTP_SESSION(session);
512         gboolean cont = FALSE;
513         gint ret = 0;
514         
515         if (strlen(msg) < 4) {
516                 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
517                 return -1;
518         }
519
520         switch (smtp_session->state) {
521         case SMTP_EHLO:
522         case SMTP_STARTTLS:
523         case SMTP_AUTH:
524         case SMTP_AUTH_PLAIN:
525         case SMTP_AUTH_LOGIN_USER:
526         case SMTP_AUTH_LOGIN_PASS:
527         case SMTP_AUTH_CRAM_MD5:
528                 log_print(LOG_PROTOCOL, "ESMTP< %s\n", msg);
529                 break;
530         default:
531                 log_print(LOG_PROTOCOL, "SMTP< %s\n", msg);
532                 break;
533         }
534
535         /* ignore all multiline responses except for EHLO */
536         if (msg[3] == '-' && smtp_session->state != SMTP_EHLO)
537                 return session_recv_msg(session);
538
539         if (msg[0] == '5' && msg[1] == '0' &&
540             (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
541                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
542                 smtp_session->state = SMTP_ERROR;
543                 smtp_session->error_val = SM_ERROR;
544                 g_free(smtp_session->error_msg);
545                 smtp_session->error_msg = g_strdup(msg);
546                 return -1;
547         }
548
549         if (!strncmp(msg, "535", 3)) {
550                 log_warning(LOG_PROTOCOL, _("error occurred on authentication\n"));
551                 smtp_session->state = SMTP_ERROR;
552                 smtp_session->error_val = SM_AUTHFAIL;
553                 g_free(smtp_session->error_msg);
554                 smtp_session->error_msg = g_strdup(msg);
555                 return -1;
556         }
557
558         if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
559                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
560                 smtp_session->state = SMTP_ERROR;
561                 smtp_session->error_val = SM_ERROR;
562                 g_free(smtp_session->error_msg);
563                 smtp_session->error_msg = g_strdup(msg);
564                 return -1;
565         }
566
567         if (msg[3] == '-')
568                 cont = TRUE;
569         else if (msg[3] != ' ' && msg[3] != '\0') {
570                 log_warning(LOG_PROTOCOL, _("bad SMTP response\n"));
571                 smtp_session->state = SMTP_ERROR;
572                 smtp_session->error_val = SM_UNRECOVERABLE;
573                 return -1;
574         }
575
576         switch (smtp_session->state) {
577         case SMTP_READY:
578                 if (strstr(msg, "ESMTP"))
579                         smtp_session->is_esmtp = TRUE;
580 #ifdef USE_GNUTLS
581                 if (smtp_session->user || session->ssl_type != SSL_NONE ||
582                     smtp_session->is_esmtp)
583 #else
584                 if (smtp_session->user || smtp_session->is_esmtp)
585 #endif
586                         ret = smtp_ehlo(smtp_session);
587                 else
588                         ret = smtp_helo(smtp_session);
589                 break;
590         case SMTP_HELO:
591                 ret = smtp_from(smtp_session);
592                 break;
593         case SMTP_EHLO:
594                 ret = smtp_ehlo_recv(smtp_session, msg);
595                 if (cont == TRUE)
596                         break;
597                 if (smtp_session->max_message_size > 0
598                 && smtp_session->max_message_size < 
599                    smtp_session->send_data_len) {
600                         log_warning(LOG_PROTOCOL, _("Message is too big "
601                               "(Maximum size is %s)\n"),
602                               to_human_readable(
603                                (goffset)(smtp_session->max_message_size)));
604                         smtp_session->state = SMTP_ERROR;
605                         smtp_session->error_val = SM_ERROR;
606                         return -1;
607                 }
608 #ifdef USE_GNUTLS
609                 if (session->ssl_type == SSL_STARTTLS &&
610                     smtp_session->tls_init_done == FALSE) {
611                         ret = smtp_starttls(smtp_session);
612                         break;
613                 }
614 #endif
615                 if (smtp_session->user) {
616                         if (smtp_auth(smtp_session) != SM_OK) {
617 #ifdef USE_GNUTLS
618                                 if (session->ssl_type == SSL_NONE
619                                 &&  smtp_session->tls_init_done == FALSE
620                                 &&  (smtp_session->avail_auth_type & SMTPAUTH_TLS_AVAILABLE))
621                                         ret = smtp_starttls(smtp_session);
622                                 else
623 #endif
624                                         ret = smtp_from(smtp_session);
625                         }
626                 } else
627                         ret = smtp_from(smtp_session);
628                 break;
629         case SMTP_STARTTLS:
630 #ifdef USE_GNUTLS
631                 if (session_start_tls(session) < 0) {
632                         log_warning(LOG_PROTOCOL, _("couldn't start TLS session\n"));
633                         smtp_session->state = SMTP_ERROR;
634                         smtp_session->error_val = SM_ERROR;
635                         return -1;
636                 }
637                 smtp_session->tls_init_done = TRUE;
638                 ret = smtp_ehlo(smtp_session);
639 #endif
640                 break;
641         case SMTP_AUTH:
642                 ret = smtp_auth_recv(smtp_session, msg);
643                 break;
644         case SMTP_AUTH_LOGIN_USER:
645                 ret = smtp_auth_login_user_recv(smtp_session, msg);
646                 break;
647         case SMTP_AUTH_PLAIN:
648         case SMTP_AUTH_LOGIN_PASS:
649         case SMTP_AUTH_CRAM_MD5:
650                 ret = smtp_from(smtp_session);
651                 break;
652         case SMTP_FROM:
653                 if (smtp_session->cur_to)
654                         ret = smtp_rcpt(smtp_session);
655                 break;
656         case SMTP_RCPT:
657                 if (smtp_session->cur_to)
658                         ret = smtp_rcpt(smtp_session);
659                 else
660                         ret = smtp_data(smtp_session);
661                 break;
662         case SMTP_DATA:
663                 ret = smtp_send_data(smtp_session);
664                 break;
665         case SMTP_EOM:
666                 smtp_make_ready(smtp_session);
667                 break;
668         case SMTP_QUIT:
669                 session_disconnect(session);
670                 break;
671         case SMTP_ERROR:
672         default:
673                 log_warning(LOG_PROTOCOL, _("error occurred on SMTP session\n"));
674                 smtp_session->error_val = SM_ERROR;
675                 return -1;
676         }
677
678         if (cont && ret == SM_OK)
679                 return session_recv_msg(session);
680
681         if (ret != SM_OK)
682                 smtp_session->error_val = SM_ERROR;
683
684         return ret == SM_OK ? 0 : -1;
685 }
686
687 static gint smtp_session_send_data_finished(Session *session, guint len)
688 {
689         return smtp_eom(SMTP_SESSION(session));
690 }