2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 the Claws Mail Team
4 * Copyright (C) 2014-2015 Charles Lehner
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <glib/gi18n.h>
28 #include "passwordstore.h"
29 #include "gtk/inputdialog.h"
35 #include "managesieve.h"
36 #include "sieve_editor.h"
37 #include "sieve_prefs.h"
39 GSList *sessions = NULL;
41 static void sieve_session_destroy(Session *session);
42 static gint sieve_pop_send_queue(SieveSession *session);
43 static void sieve_session_reset(SieveSession *session);
44 static void command_free(SieveCommand *cmd);
45 static void command_abort(SieveCommand *cmd);
46 static void command_cb(SieveCommand *cmd, gpointer result);
47 static gint sieve_session_recv_chunk(SieveSession *, guint len);
48 static void sieve_read_chunk(SieveSession *, gchar *data, guint len);
49 static gint sieve_read_chunk_done(SieveSession *session);
51 void sieve_sessions_close()
54 GSList *list = sessions;
56 g_slist_free_full(list, (GDestroyNotify)session_destroy);
60 /* remove all command callbacks with a given data pointer */
61 void sieve_sessions_discard_callbacks(gpointer user_data)
66 SieveSession *session;
69 for (item = sessions; item; item = item->next) {
70 session = (SieveSession *)item->data;
71 cmd = session->current_cmd;
72 /* abort current command handler */
73 if (cmd && cmd->data == user_data) {
75 session->current_cmd = NULL;
77 /* abort queued command handlers */
78 for (queue = session->send_queue; queue; queue = queue->next) {
79 cmd = (SieveCommand *)queue->data;
80 if (cmd && cmd->data == user_data) {
82 prev->next = queue->next;
84 session->send_queue = NULL;
86 g_slist_free_1(queue);
94 static void command_cb(SieveCommand *cmd, gpointer result)
97 cmd->cb(cmd->session, FALSE, result, cmd->data);
100 static void command_abort(SieveCommand *cmd)
102 cmd->cb(cmd->session, TRUE, NULL, cmd->data);
107 static void command_free(SieveCommand *cmd)
113 void sieve_session_handle_status(SieveSession *session,
114 sieve_session_error_cb_fn on_error,
115 sieve_session_connected_cb_fn on_connected,
118 session->on_error = on_error;
119 session->on_connected = on_connected;
120 session->cb_data = data;
123 static void sieve_error(SieveSession *session, const gchar *msg)
125 if (session->on_error)
126 session->on_error(session, msg, session->cb_data);
129 static void sieve_connected(SieveSession *session, gboolean connected)
131 if (session->on_connected)
132 session->on_connected(session, connected, session->cb_data);
135 static gboolean sieve_read_chunk_cb(SockInfo *source,
136 GIOCondition condition, gpointer data)
138 SieveSession *sieve_session = SIEVE_SESSION(data);
139 Session *session = &sieve_session->session;
143 cm_return_val_if_fail(condition == G_IO_IN, FALSE);
145 session_set_timeout(session, session->timeout_interval);
147 if (session->read_buf_len == 0) {
151 read_len = sock_read(session->sock,
153 SESSION_BUFFSIZE - 1);
155 if (read_len == -1 &&
156 session->state == SESSION_DISCONNECTED) {
157 g_warning ("sock_read: session disconnected");
158 if (session->io_tag > 0) {
159 g_source_remove(session->io_tag);
166 g_warning("sock_read: received EOF");
167 session->state = SESSION_EOF;
176 g_warning("sock_read: %s",
178 session->state = SESSION_ERROR;
183 session->read_buf_len = read_len;
186 data_len = MIN(session->read_buf_len,
187 sieve_session->octets_remaining);
188 sieve_session->octets_remaining -= data_len;
189 session->read_buf_len -= data_len;
190 session->read_buf_p[data_len] = '\0';
192 /* progress callback */
193 sieve_read_chunk(sieve_session, session->read_buf_p, data_len);
195 if (session->read_buf_len == 0) {
196 session->read_buf_p = session->read_buf;
198 session->read_buf_p += data_len;
201 /* incomplete read */
202 if (sieve_session->octets_remaining > 0)
206 if (session->io_tag > 0) {
207 g_source_remove(session->io_tag);
211 /* completion callback */
212 ret = sieve_read_chunk_done(sieve_session);
215 session->state = SESSION_ERROR;
220 static gboolean sieve_read_chunk_idle_cb(gpointer data)
222 Session *session = SESSION(data);
225 ret = sieve_read_chunk_cb(session->sock, G_IO_IN, session);
228 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
229 sieve_read_chunk_cb, session);
234 /* Get data of specified length.
235 * If needed elsewhere, this should be put in session.c */
236 static gint sieve_session_recv_chunk(SieveSession *sieve_session,
239 Session *session = &sieve_session->session;
240 cm_return_val_if_fail(session->read_msg_buf->len == 0, -1);
242 session->state = SESSION_RECV;
243 sieve_session->octets_remaining = bytes;
245 if (session->read_buf_len > 0)
246 g_idle_add(sieve_read_chunk_idle_cb, session);
248 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
249 sieve_read_chunk_cb, session);
254 static gint sieve_auth_recv(SieveSession *session, const gchar *msg)
256 gchar buf[MESSAGEBUFSIZE], *tmp;
258 switch (session->auth_type) {
259 case SIEVEAUTH_LOGIN:
260 session->state = SIEVE_AUTH_LOGIN_USER;
262 if (strstr(msg, "VXNlcm5hbWU6")) {
263 tmp = g_base64_encode(session->user, strlen(session->user));
264 g_snprintf(buf, sizeof(buf), "\"%s\"", tmp);
266 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0) {
271 log_print(LOG_PROTOCOL, "Sieve> [USERID]\n");
273 /* Server rejects AUTH */
274 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
277 log_print(LOG_PROTOCOL, "Sieve> *\n");
280 case SIEVEAUTH_CRAM_MD5:
281 session->state = SIEVE_AUTH_CRAM_MD5;
286 gchar *challenge, *tmp;
288 guchar hexdigest[33];
290 tmp = g_base64_decode(msg + 1, &challengelen);
291 challenge = g_strndup(tmp, challengelen);
293 log_print(LOG_PROTOCOL, "Sieve< [Decoded: %s]\n", challenge);
295 g_snprintf(buf, sizeof(buf), "%s", session->pass);
296 md5_hex_hmac(hexdigest, challenge, challengelen,
297 buf, strlen(session->pass));
300 response = g_strdup_printf
301 ("%s %s", session->user, hexdigest);
302 log_print(LOG_PROTOCOL, "Sieve> [Encoded: %s]\n", response);
304 response64 = g_base64_encode(response, strlen(response));
307 response = g_strdup_printf("\"%s\"", response64);
310 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
315 log_print(LOG_PROTOCOL, "Sieve> %s\n", response);
318 /* Server rejects AUTH */
319 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
322 log_print(LOG_PROTOCOL, "Sieve> *\n");
326 /* stop sieve_auth when no correct authtype */
327 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
329 log_print(LOG_PROTOCOL, "Sieve> *\n");
336 static gint sieve_auth_login_user_recv(SieveSession *session, const gchar *msg)
340 session->state = SIEVE_AUTH_LOGIN_PASS;
342 if (strstr(msg, "UGFzc3dvcmQ6")) {
343 tmp2 = g_base64_encode(session->pass, strlen(session->pass));
344 tmp = g_strdup_printf("\"%s\"", tmp2);
347 /* Server rejects AUTH */
348 tmp = g_strdup("\"*\"");
351 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, tmp) < 0) {
357 log_print(LOG_PROTOCOL, "Sieve> [PASSWORD]\n");
363 static gint sieve_auth_cram_md5(SieveSession *session)
365 session->state = SIEVE_AUTH;
366 session->auth_type = SIEVEAUTH_CRAM_MD5;
368 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
369 "Authenticate \"CRAM-MD5\"") < 0)
371 log_print(LOG_PROTOCOL, "Sieve> Authenticate CRAM-MD5\n");
376 static gint sieve_auth_plain(SieveSession *session)
378 gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
381 session->state = SIEVE_AUTH_PLAIN;
382 session->auth_type = SIEVEAUTH_PLAIN;
384 memset(buf, 0, sizeof buf);
386 /* "\0user\0password" */
387 len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
388 b64buf = g_base64_encode(buf, len);
389 out = g_strconcat("Authenticate \"PLAIN\" \"", b64buf, "\"", NULL);
392 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, out) < 0) {
399 log_print(LOG_PROTOCOL, "Sieve> [Authenticate PLAIN]\n");
404 static gint sieve_auth_login(SieveSession *session)
406 session->state = SIEVE_AUTH;
407 session->auth_type = SIEVEAUTH_LOGIN;
409 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
410 "Authenticate \"LOGIN\"") < 0)
412 log_print(LOG_PROTOCOL, "Sieve> Authenticate LOGIN\n");
417 static gint sieve_auth(SieveSession *session)
419 SieveAuthType forced_auth_type = session->forced_auth_type;
421 if (!session->use_auth) {
422 session->state = SIEVE_READY;
423 sieve_connected(session, TRUE);
427 session->state = SIEVE_AUTH;
428 sieve_error(session, _("Authenticating..."));
430 if ((forced_auth_type == SIEVEAUTH_CRAM_MD5 || forced_auth_type == 0) &&
431 (session->avail_auth_type & SIEVEAUTH_CRAM_MD5) != 0)
432 return sieve_auth_cram_md5(session);
433 else if ((forced_auth_type == SIEVEAUTH_LOGIN || forced_auth_type == 0) &&
434 (session->avail_auth_type & SIEVEAUTH_LOGIN) != 0)
435 return sieve_auth_login(session);
436 else if ((forced_auth_type == SIEVEAUTH_PLAIN || forced_auth_type == 0) &&
437 (session->avail_auth_type & SIEVEAUTH_PLAIN) != 0)
438 return sieve_auth_plain(session);
439 else if (forced_auth_type == 0) {
440 log_warning(LOG_PROTOCOL, _("No Sieve auth method available\n"));
441 session->state = SIEVE_RETRY_AUTH;
444 log_warning(LOG_PROTOCOL, _("Selected Sieve auth method not available\n"));
445 session->state = SIEVE_RETRY_AUTH;
452 static void sieve_session_putscript_cb(SieveSession *session, SieveResult *result)
454 /* Remove script name from the beginning the response,
455 * which is added by Dovecot/Pigeonhole */
456 gchar *start, *desc = result->description;
459 /* callback just for the status */
460 command_cb(session->current_cmd, result);
462 while (desc && desc[0]) {
463 if ((end = strchr(desc, '\r')) ||
464 (end = strchr(desc, '\n')))
465 while (*end == '\n' || *end == '\r')
467 if (g_str_has_prefix(desc, "NULL_") && (start = strchr(desc+5, ':'))) {
471 /* TODO: match against known script name, in case it contains
472 * weird text like ": line " */
473 } else if ((start = strstr(desc, ": line ")) ||
474 (start = strstr(desc, ": error"))) {
477 result->description = desc;
478 command_cb(session->current_cmd, result);
483 static inline gboolean response_is_ok(const char *msg)
485 return !strncmp(msg, "OK", 2) && (!msg[2] || msg[2] == ' ');
488 static inline gboolean response_is_no(const char *msg)
490 return !strncmp(msg, "NO", 2) && (!msg[2] || msg[2] == ' ');
493 static inline gboolean response_is_bye(const char *msg)
495 return !strncmp(msg, "BYE", 3) && (!msg[3] || msg[3] == ' ');
498 static void sieve_got_capability(SieveSession *session, gchar *cap_name,
501 if (strcmp(cap_name, "SASL") == 0) {
502 SieveAuthType auth_type = 0;
504 for (auth = cap_value; auth && auth[0]; auth = end) {
505 if ((end = strchr(auth, ' ')))
507 if (strcmp(auth, "PLAIN") == 0) {
508 auth_type |= SIEVEAUTH_PLAIN;
509 } else if (strcmp(auth, "CRAM-MD5") == 0) {
510 auth_type |= SIEVEAUTH_CRAM_MD5;
511 } else if (strcmp(auth, "LOGIN") == 0) {
512 auth_type |= SIEVEAUTH_LOGIN;
515 session->avail_auth_type = auth_type;
517 } else if (strcmp(cap_name, "STARTTLS") == 0) {
518 session->capability.starttls = TRUE;
522 static void log_send(SieveSession *session, SieveCommand *cmd)
524 gchar *end, *msg = cmd->msg;
525 if (cmd->next_state == SIEVE_PUTSCRIPT && (end = strchr(msg, '\n'))) {
526 /* Don't log the script data */
527 msg = g_strndup(msg, end - msg);
528 log_print(LOG_PROTOCOL, "Sieve> %s\n", msg);
532 log_print(LOG_PROTOCOL, "Sieve> %s\n", msg);
535 static gint sieve_pop_send_queue(SieveSession *session)
538 GSList *send_queue = session->send_queue;
540 if (session->current_cmd) {
541 command_free(session->current_cmd);
542 session->current_cmd = NULL;
548 cmd = (SieveCommand *)send_queue->data;
549 session->send_queue = g_slist_next(send_queue);
550 g_slist_free_1(send_queue);
552 log_send(session, cmd);
553 session->state = cmd->next_state;
554 session->current_cmd = cmd;
555 if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0)
561 static void parse_split(gchar *line, gchar **first_word, gchar **second_word)
568 if (line[0] == '"' && ((second = strchr(line + 1, '"')))) {
571 if (second[0] == ' ')
573 } else if ((second = strchr(line, ' '))) {
578 if (second && second[0] == '"' &&
579 ((end = strchr(second + 1, '"')))) {
585 *second_word = second;
588 static void unquote_inplace(gchar *str)
593 for (src = str+1, dest = str; src && *src && *src != '"'; src++) {
602 static void parse_response(gchar *msg, SieveResult *result)
606 cm_return_if_fail(msg != NULL);
608 /* response status */
609 if (isalpha(msg[0])) {
610 end = strchr(msg, ' ');
616 result->success = strcmp(msg, "OK") == 0;
617 result->has_status = TRUE;
620 result->has_status = FALSE;
624 if (msg && msg[0] == '(' && (end = strchr(msg, ')'))) {
628 strcmp(msg, "WARNINGS") == 0 ? SIEVE_CODE_WARNINGS :
629 strcmp(msg, "TRYLATER") == 0 ? SIEVE_CODE_TRYLATER :
635 result->code = SIEVE_CODE_NONE;
639 if (msg && msg[0] == '{' && (end = strchr(msg, '}'))) {
642 if (msg[0] == '0' && msg+1 == end) {
643 result->has_octets = TRUE;
647 (result->octets = g_ascii_strtoll(msg, NULL, 10)) != 0;
653 result->has_octets = FALSE;
659 unquote_inplace(msg);
660 result->description = msg;
662 result->description = NULL;
666 static gint sieve_session_recv_msg(Session *session, const gchar *msg)
668 SieveSession *sieve_session = SIEVE_SESSION(session);
672 log_print(LOG_PROTOCOL, "Sieve< %s\n", msg);
673 if (response_is_bye(msg)) {
675 parse_response((gchar *)msg, &result);
676 if (!result.description)
677 status = g_strdup(_("Disconnected"));
678 else if (g_str_has_prefix(result.description, "Disconnected"))
679 status = g_strdup(result.description);
681 status = g_strdup_printf(_("Disconnected: %s"), result.description);
682 sieve_session->error = SE_ERROR;
683 sieve_error(sieve_session, status);
684 sieve_session->state = SIEVE_DISCONNECTED;
689 switch (sieve_session->state) {
690 case SIEVE_CAPABILITIES:
691 if (response_is_ok(msg)) {
692 /* capabilities list done */
695 if (sieve_session->tls_init_done == FALSE &&
696 sieve_session->config->tls_type != SIEVE_TLS_NO) {
697 if (sieve_session->capability.starttls) {
698 log_print(LOG_PROTOCOL, "Sieve> STARTTLS\n");
699 session_send_msg(session, SESSION_SEND, "STARTTLS");
700 sieve_session->state = SIEVE_STARTTLS;
701 } else if (sieve_session->config->tls_type == SIEVE_TLS_YES) {
702 log_warning(LOG_PROTOCOL, "Sieve: does not support STARTTLS\n");
703 sieve_session->state = SIEVE_ERROR;
705 log_warning(LOG_PROTOCOL, "Sieve: continuing without TLS\n");
706 sieve_session->state = SIEVE_READY;
711 /* authenticate after getting capabilities */
712 if (!sieve_session->authenticated) {
713 ret = sieve_auth(sieve_session);
715 sieve_session->state = SIEVE_READY;
716 sieve_connected(sieve_session, TRUE);
719 /* got a capability */
720 gchar *cap_name, *cap_value;
721 parse_split((gchar *)msg, &cap_name, &cap_value);
722 sieve_got_capability(sieve_session, cap_name, cap_value);
728 log_warning(LOG_PROTOCOL,
729 _("unhandled message on Sieve session: %s\n"), msg);
733 if (session_start_tls(session) < 0) {
734 sieve_session->state = SIEVE_ERROR;
735 sieve_session->error = SE_ERROR;
736 sieve_error(sieve_session, _("TLS failed"));
739 sieve_session->tls_init_done = TRUE;
740 sieve_session->state = SIEVE_CAPABILITIES;
744 ret = sieve_auth_recv(sieve_session, msg);
746 case SIEVE_AUTH_LOGIN_USER:
747 ret = sieve_auth_login_user_recv(sieve_session, msg);
749 case SIEVE_AUTH_PLAIN:
750 case SIEVE_AUTH_LOGIN_PASS:
751 case SIEVE_AUTH_CRAM_MD5:
752 if (response_is_no(msg)) {
753 log_print(LOG_PROTOCOL, "Sieve auth failed\n");
754 session->state = SIEVE_RETRY_AUTH;
756 } else if (response_is_ok(msg)) {
757 log_print(LOG_PROTOCOL, "Sieve auth completed\n");
758 sieve_error(sieve_session, "");
759 sieve_session->authenticated = TRUE;
760 sieve_session->state = SIEVE_READY;
761 sieve_connected(sieve_session, TRUE);
765 if (!response_is_ok(msg)) {
766 sieve_session->state = SIEVE_ERROR;
768 sieve_session->state = SIEVE_READY;
770 case SIEVE_LISTSCRIPTS:
771 if (response_is_no(msg)) {
772 /* got an error. probably not authenticated. */
773 command_cb(sieve_session->current_cmd, NULL);
774 sieve_session->state = SIEVE_READY;
775 } else if (response_is_ok(msg)) {
777 sieve_session->state = SIEVE_READY;
778 sieve_session->error = SE_OK;
779 command_cb(sieve_session->current_cmd,
780 (gpointer)&(SieveScript){0});
782 /* got a script name */
784 gchar *script_status;
786 parse_split((gchar *)msg, &script.name, &script_status);
787 script.active = (script_status &&
788 strcasecmp(script_status, "active") == 0);
790 command_cb(sieve_session->current_cmd,
794 case SIEVE_RENAMESCRIPT:
795 if (response_is_no(msg)) {
797 command_cb(sieve_session->current_cmd, NULL);
798 } else if (response_is_ok(msg)) {
799 command_cb(sieve_session->current_cmd, (void*)TRUE);
801 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
803 sieve_session->state = SIEVE_READY;
805 case SIEVE_SETACTIVE:
806 parse_response((gchar *)msg, &result);
807 if (result.success) {
808 /* clear status possibly set when setting another
809 * script active. TODO: give textual feedback */
810 sieve_error(sieve_session, "");
812 command_cb(sieve_session->current_cmd, NULL);
813 } else if (result.description) {
814 command_cb(sieve_session->current_cmd,
817 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
819 if (result.has_octets) {
820 return sieve_session_recv_chunk(sieve_session,
823 sieve_session->state = SIEVE_READY;
826 case SIEVE_GETSCRIPT:
827 if (response_is_no(msg)) {
828 command_cb(sieve_session->current_cmd, (void *)-1);
829 sieve_session->state = SIEVE_READY;
831 parse_response((gchar *)msg, &result);
832 sieve_session->state = SIEVE_GETSCRIPT_DATA;
833 return sieve_session_recv_chunk(sieve_session,
837 case SIEVE_GETSCRIPT_DATA:
840 sieve_session->state = SIEVE_READY;
841 if (response_is_ok(msg)) {
842 command_cb(sieve_session->current_cmd, NULL);
844 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
847 case SIEVE_PUTSCRIPT:
850 parse_response((gchar *)msg, &result);
851 sieve_session_putscript_cb(sieve_session, &result);
852 if (result.has_octets) {
853 return sieve_session_recv_chunk(sieve_session,
856 sieve_session->state = SIEVE_READY;
859 case SIEVE_DELETESCRIPT:
860 parse_response((gchar *)msg, &result);
861 if (!result.success) {
862 command_cb(sieve_session->current_cmd,
865 command_cb(sieve_session->current_cmd, NULL);
867 sieve_session->state = SIEVE_READY;
870 log_warning(LOG_PROTOCOL, _("error occurred on Sieve session. data: %s\n"), msg);
871 sieve_session->error = SE_ERROR;
873 case SIEVE_RETRY_AUTH:
874 log_warning(LOG_PROTOCOL, _("unhandled message on Sieve session: %s\n"),
876 ret = sieve_auth(sieve_session);
879 log_warning(LOG_PROTOCOL, _("unhandled message on Sieve session: %d\n"),
880 sieve_session->state);
881 sieve_session->error = SE_ERROR;
885 if (ret == SE_OK && sieve_session->state == SIEVE_READY)
886 ret = sieve_pop_send_queue(sieve_session);
889 return session_recv_msg(session);
890 } else if (ret == SE_AUTHFAIL) {
891 sieve_error(sieve_session, _("Auth failed"));
892 sieve_session->state = SIEVE_ERROR;
893 sieve_session->error = SE_ERROR;
899 static gint sieve_recv_message(Session *session, const gchar *msg,
905 static void sieve_read_chunk(SieveSession *session, gchar *data, guint len)
907 log_print(LOG_PROTOCOL, "Sieve< [%u bytes]\n", len);
909 switch (session->state) {
910 case SIEVE_GETSCRIPT_DATA:
911 command_cb(session->current_cmd, (gchar *)data);
913 case SIEVE_SETACTIVE:
914 /* Dovecot shows a script's warnings when making it active */
915 /* TODO: append message in case it is very long*/
917 sieve_error(session, data);
919 case SIEVE_PUTSCRIPT: {
920 SieveResult result = {.description = (gchar *)data};
921 sieve_session_putscript_cb(session, &result);
925 log_warning(LOG_PROTOCOL,
926 _("error occurred on SIEVE session\n"));
930 static gint sieve_read_chunk_done(SieveSession *session)
934 switch (session->state) {
935 case SIEVE_GETSCRIPT_DATA:
936 /* wait for ending "OK" response */
938 case SIEVE_SETACTIVE:
939 case SIEVE_PUTSCRIPT:
940 session->state = SIEVE_READY;
943 log_warning(LOG_PROTOCOL,
944 _("error occurred on SIEVE session\n"));
947 if (ret == SE_OK && session->state == SIEVE_READY)
948 ret = sieve_pop_send_queue(session);
951 return session_recv_msg(SESSION(session));
956 static gint sieve_cmd_noop(SieveSession *session)
958 log_print(LOG_PROTOCOL, "Sieve> NOOP\n");
959 session->state = SIEVE_NOOP;
960 if (session_send_msg(SESSION(session), SESSION_SEND, "NOOP") < 0) {
961 session->state = SIEVE_ERROR;
962 session->error = SE_ERROR;
968 static gboolean sieve_ping(gpointer data)
970 Session *session = SESSION(data);
971 SieveSession *sieve_session = SIEVE_SESSION(session);
973 if (sieve_session->state == SIEVE_ERROR || session->state == SESSION_ERROR)
975 if (sieve_session->state != SIEVE_READY)
978 return sieve_cmd_noop(sieve_session) == 0;
981 static void sieve_session_destroy(Session *session)
983 SieveSession *sieve_session = SIEVE_SESSION(session);
984 g_free(sieve_session->pass);
985 if (sieve_session->current_cmd)
986 command_abort(sieve_session->current_cmd);
987 sessions = g_slist_remove(sessions, (gconstpointer)session);
988 g_slist_free_full(sieve_session->send_queue,
989 (GDestroyNotify)command_abort);
992 static void sieve_connect_finished(Session *session, gboolean success)
995 sieve_connected(SIEVE_SESSION(session), FALSE);
999 static gint sieve_session_connect(SieveSession *session)
1001 session->state = SIEVE_CAPABILITIES;
1002 session->authenticated = FALSE;
1004 session->tls_init_done = FALSE;
1006 return session_connect(SESSION(session), session->host,
1010 static SieveSession *sieve_session_new(PrefsAccount *account)
1012 SieveSession *session;
1013 session = g_new0(SieveSession, 1);
1014 session_init(SESSION(session), account, FALSE);
1016 session->account = account;
1018 SESSION(session)->recv_msg = sieve_session_recv_msg;
1019 SESSION(session)->destroy = sieve_session_destroy;
1020 SESSION(session)->connect_finished = sieve_connect_finished;
1021 session_set_recv_message_notify(SESSION(session), sieve_recv_message, NULL);
1023 sieve_session_reset(session);
1027 static void sieve_session_reset(SieveSession *session)
1029 PrefsAccount *account = session->account;
1030 SieveAccountConfig *config = sieve_prefs_account_get_config(account);
1031 gboolean reuse_auth = (config->auth == SIEVEAUTH_REUSE);
1033 g_slist_free_full(session->send_queue, (GDestroyNotify)command_abort);
1035 session_disconnect(SESSION(session));
1037 SESSION(session)->ssl_cert_auto_accept = account->ssl_certs_auto_accept;
1038 SESSION(session)->nonblocking = account->use_nonblocking_ssl;
1039 session->authenticated = FALSE;
1040 session->current_cmd = NULL;
1041 session->send_queue = NULL;
1042 session->state = SIEVE_CAPABILITIES;
1044 session->tls_init_done = FALSE;
1046 session->avail_auth_type = 0;
1047 session->auth_type = 0;
1048 session->config = config;
1049 session->host = config->use_host ? config->host : account->recv_server;
1050 session->port = config->use_port ? config->port : SIEVE_PORT;
1051 session->user = reuse_auth ? account->userid : session->config->userid;
1052 session->forced_auth_type = config->auth_type;
1053 session_register_ping(SESSION(session), sieve_ping);
1056 g_free(session->pass);
1057 if (config->auth == SIEVEAUTH_NONE) {
1058 session->pass = NULL;
1059 } else if (reuse_auth && (session->pass = passwd_store_get_account(
1060 account->account_id, PWS_ACCOUNT_RECV))) {
1061 } else if ((session->pass = passwd_store_get_account(
1062 account->account_id, "sieve"))) {
1063 } else if (password_get(session->user, session->host, "sieve",
1064 session->port, &session->pass)) {
1066 session->pass = input_dialog_query_password_keep(session->host,
1067 session->user, &(session->pass));
1069 if (!session->pass) {
1070 session->pass = g_strdup("");
1071 session->use_auth = FALSE;
1073 session->use_auth = TRUE;
1077 SESSION(session)->ssl_type =
1078 (config->tls_type == SIEVE_TLS_NO) ? SSL_NONE : SSL_STARTTLS;
1082 /* When an account config is changed, reset associated sessions. */
1083 void sieve_account_prefs_updated(PrefsAccount *account)
1086 SieveSession *session;
1088 for (item = sessions; item; item = item->next) {
1089 session = (SieveSession *)item->data;
1090 if (session->account == account) {
1091 log_print(LOG_PROTOCOL, "Sieve: resetting session\n");
1092 sieve_session_reset(session);
1097 SieveSession *sieve_session_get_for_account(PrefsAccount *account)
1099 SieveSession *session;
1103 for (item = sessions; item; item = item->next) {
1104 session = (SieveSession *)item->data;
1105 if (session->account == account) {
1111 session = sieve_session_new(account);
1112 sessions = g_slist_prepend(sessions, session);
1117 static void sieve_queue_send(SieveSession *session, SieveState next_state,
1118 gchar *msg, sieve_session_data_cb_fn cb, gpointer data)
1120 gboolean queue = FALSE;
1121 SieveCommand *cmd = g_new0(SieveCommand, 1);
1122 cmd->session = session;
1123 cmd->next_state = next_state;
1128 if (!session_is_connected(SESSION(session))) {
1129 log_print(LOG_PROTOCOL, "Sieve: connecting to %s:%hu\n",
1130 session->host, session->port);
1131 if (sieve_session_connect(session) < 0) {
1132 sieve_connect_finished(SESSION(session), FALSE);
1135 } else if (session->state == SIEVE_RETRY_AUTH) {
1136 log_print(LOG_PROTOCOL, _("Sieve: retrying auth\n"));
1137 if (sieve_auth(session) == SE_AUTHFAIL)
1138 sieve_error(session, _("Auth method not available"));
1140 } else if (session->state != SIEVE_READY) {
1141 log_print(LOG_PROTOCOL, "Sieve: in state %d\n", session->state);
1146 session->send_queue = g_slist_prepend(session->send_queue, cmd);
1148 if (session->current_cmd)
1149 command_free(session->current_cmd);
1150 session->current_cmd = cmd;
1151 session->state = next_state;
1152 log_send(session, cmd);
1153 if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0) {
1159 void sieve_session_list_scripts(SieveSession *session,
1160 sieve_session_data_cb_fn cb, gpointer data)
1162 gchar *msg = g_strdup("LISTSCRIPTS");
1163 sieve_queue_send(session, SIEVE_LISTSCRIPTS, msg, cb, data);
1166 void sieve_session_set_active_script(SieveSession *session,
1167 const gchar *filter_name,
1168 sieve_session_data_cb_fn cb, gpointer data)
1170 gchar *msg = g_strdup_printf("SETACTIVE \"%s\"",
1171 filter_name ? filter_name : "");
1173 cb(session, FALSE, (void*)FALSE, data);
1177 sieve_queue_send(session, SIEVE_SETACTIVE, msg, cb, data);
1180 void sieve_session_rename_script(SieveSession *session,
1181 const gchar *name_old, const char *name_new,
1182 sieve_session_data_cb_fn cb, gpointer data)
1184 gchar *msg = g_strdup_printf("RENAMESCRIPT \"%s\" \"%s\"",
1185 name_old, name_new);
1187 sieve_queue_send(session, SIEVE_RENAMESCRIPT, msg, cb, data);
1190 void sieve_session_get_script(SieveSession *session, const gchar *filter_name,
1191 sieve_session_data_cb_fn cb, gpointer data)
1193 gchar *msg = g_strdup_printf("GETSCRIPT \"%s\"",
1196 sieve_queue_send(session, SIEVE_GETSCRIPT, msg, cb, data);
1199 void sieve_session_put_script(SieveSession *session, const gchar *filter_name,
1200 gint len, const gchar *script_contents,
1201 sieve_session_data_cb_fn cb, gpointer data)
1203 /* TODO: refactor so don't have to copy the whole script here */
1204 gchar *msg = g_strdup_printf("PUTSCRIPT \"%s\" {%u+}%s%s",
1205 filter_name, len, len > 0 ? "\r\n" : "",
1208 sieve_queue_send(session, SIEVE_PUTSCRIPT, msg, cb, data);
1211 void sieve_session_check_script(SieveSession *session,
1212 gint len, const gchar *script_contents,
1213 sieve_session_data_cb_fn cb, gpointer data)
1215 gchar *msg = g_strdup_printf("CHECKSCRIPT {%u+}%s%s",
1216 len, len > 0 ? "\r\n" : "", script_contents);
1218 sieve_queue_send(session, SIEVE_PUTSCRIPT, msg, cb, data);
1221 void sieve_session_delete_script(SieveSession *session,
1222 const gchar *filter_name,
1223 sieve_session_data_cb_fn cb, gpointer data)
1225 gchar *msg = g_strdup_printf("DELETESCRIPT \"%s\"",
1228 sieve_queue_send(session, SIEVE_DELETESCRIPT, msg, cb, data);