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 "gtk/inputdialog.h"
34 #include "managesieve.h"
35 #include "sieve_editor.h"
36 #include "sieve_prefs.h"
38 GSList *sessions = NULL;
40 static void sieve_session_destroy(Session *session);
41 static gint sieve_pop_send_queue(SieveSession *session);
42 static void sieve_session_reset(SieveSession *session);
43 static void command_free(SieveCommand *cmd);
44 static void command_abort(SieveCommand *cmd);
45 static void command_cb(SieveCommand *cmd, gpointer result);
46 static gint sieve_session_recv_chunk(SieveSession *, guint len);
47 static void sieve_read_chunk(SieveSession *, gchar *data, guint len);
48 static gint sieve_read_chunk_done(SieveSession *session);
50 void sieve_sessions_close()
53 GSList *list = sessions;
55 g_slist_free_full(list, (GDestroyNotify)session_destroy);
59 /* remove all command callbacks with a given data pointer */
60 void sieve_sessions_discard_callbacks(gpointer user_data)
65 SieveSession *session;
68 for (item = sessions; item; item = item->next) {
69 session = (SieveSession *)item->data;
70 cmd = session->current_cmd;
71 /* abort current command handler */
72 if (cmd && cmd->data == user_data) {
74 session->current_cmd = NULL;
76 /* abort queued command handlers */
77 for (queue = session->send_queue; queue; queue = queue->next) {
78 cmd = (SieveCommand *)queue->data;
79 if (cmd && cmd->data == user_data) {
81 prev->next = queue->next;
83 session->send_queue = NULL;
85 g_slist_free_1(queue);
93 static void command_cb(SieveCommand *cmd, gpointer result)
96 cmd->cb(cmd->session, FALSE, result, cmd->data);
99 static void command_abort(SieveCommand *cmd)
101 cmd->cb(cmd->session, TRUE, NULL, cmd->data);
106 static void command_free(SieveCommand *cmd)
112 void sieve_session_handle_status(SieveSession *session,
113 sieve_session_error_cb_fn on_error,
114 sieve_session_connected_cb_fn on_connected,
117 session->on_error = on_error;
118 session->on_connected = on_connected;
119 session->cb_data = data;
122 static void sieve_error(SieveSession *session, const gchar *msg)
124 if (session->on_error)
125 session->on_error(session, msg, session->cb_data);
128 static void sieve_connected(SieveSession *session, gboolean connected)
130 if (session->on_connected)
131 session->on_connected(session, connected, session->cb_data);
134 static gboolean sieve_read_chunk_cb(SockInfo *source,
135 GIOCondition condition, gpointer data)
137 SieveSession *sieve_session = SIEVE_SESSION(data);
138 Session *session = &sieve_session->session;
142 cm_return_val_if_fail(condition == G_IO_IN, FALSE);
144 session_set_timeout(session, session->timeout_interval);
146 if (session->read_buf_len == 0) {
150 read_len = sock_read(session->sock,
152 SESSION_BUFFSIZE - 1);
154 if (read_len == -1 &&
155 session->state == SESSION_DISCONNECTED) {
156 g_warning ("sock_read: session disconnected\n");
157 if (session->io_tag > 0) {
158 g_source_remove(session->io_tag);
165 g_warning("sock_read: received EOF\n");
166 session->state = SESSION_EOF;
175 g_warning("sock_read: %s\n",
177 session->state = SESSION_ERROR;
182 session->read_buf_len = read_len;
185 data_len = MIN(session->read_buf_len,
186 sieve_session->octets_remaining);
187 sieve_session->octets_remaining -= data_len;
188 session->read_buf_len -= data_len;
189 session->read_buf_p[data_len] = '\0';
191 /* progress callback */
192 sieve_read_chunk(sieve_session, session->read_buf_p, data_len);
194 if (session->read_buf_len == 0) {
195 session->read_buf_p = session->read_buf;
197 session->read_buf_p += data_len;
200 /* incomplete read */
201 if (sieve_session->octets_remaining > 0)
205 if (session->io_tag > 0) {
206 g_source_remove(session->io_tag);
210 /* completion callback */
211 ret = sieve_read_chunk_done(sieve_session);
214 session->state = SESSION_ERROR;
219 static gboolean sieve_read_chunk_idle_cb(gpointer data)
221 Session *session = SESSION(data);
224 ret = sieve_read_chunk_cb(session->sock, G_IO_IN, session);
227 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
228 sieve_read_chunk_cb, session);
233 /* Get data of specified length.
234 * If needed elsewhere, this should be put in session.c */
235 static gint sieve_session_recv_chunk(SieveSession *sieve_session,
238 Session *session = &sieve_session->session;
239 cm_return_val_if_fail(session->read_msg_buf->len == 0, -1);
241 session->state = SESSION_RECV;
242 sieve_session->octets_remaining = bytes;
244 if (session->read_buf_len > 0)
245 g_idle_add(sieve_read_chunk_idle_cb, session);
247 session->io_tag = sock_add_watch(session->sock, G_IO_IN,
248 sieve_read_chunk_cb, session);
253 static gint sieve_auth_recv(SieveSession *session, const gchar *msg)
255 gchar buf[MESSAGEBUFSIZE], *tmp;
257 switch (session->auth_type) {
258 case SIEVEAUTH_LOGIN:
259 session->state = SIEVE_AUTH_LOGIN_USER;
261 if (strstr(msg, "VXNlcm5hbWU6")) {
262 tmp = g_base64_encode(session->user, strlen(session->user));
263 g_snprintf(buf, sizeof(buf), "\"%s\"", tmp);
265 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0) {
270 log_print(LOG_PROTOCOL, "Sieve> [USERID]\n");
272 /* Server rejects AUTH */
273 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
276 log_print(LOG_PROTOCOL, "Sieve> *\n");
279 case SIEVEAUTH_CRAM_MD5:
280 session->state = SIEVE_AUTH_CRAM_MD5;
285 gchar *challenge, *tmp;
287 guchar hexdigest[33];
289 tmp = g_base64_decode(msg + 1, &challengelen);
290 challenge = g_strndup(tmp, challengelen);
292 log_print(LOG_PROTOCOL, "Sieve< [Decoded: %s]\n", challenge);
294 g_snprintf(buf, sizeof(buf), "%s", session->pass);
295 md5_hex_hmac(hexdigest, challenge, challengelen,
296 buf, strlen(session->pass));
299 response = g_strdup_printf
300 ("%s %s", session->user, hexdigest);
301 log_print(LOG_PROTOCOL, "Sieve> [Encoded: %s]\n", response);
303 response64 = g_base64_encode(response, strlen(response));
306 response = g_strdup_printf("\"%s\"", response64);
309 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
314 log_print(LOG_PROTOCOL, "Sieve> %s\n", response);
317 /* Server rejects AUTH */
318 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
321 log_print(LOG_PROTOCOL, "Sieve> *\n");
325 /* stop sieve_auth when no correct authtype */
326 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
328 log_print(LOG_PROTOCOL, "Sieve> *\n");
335 static gint sieve_auth_login_user_recv(SieveSession *session, const gchar *msg)
339 session->state = SIEVE_AUTH_LOGIN_PASS;
341 if (strstr(msg, "UGFzc3dvcmQ6")) {
342 tmp2 = g_base64_encode(session->pass, strlen(session->pass));
343 tmp = g_strdup_printf("\"%s\"", tmp2);
346 /* Server rejects AUTH */
347 tmp = g_strdup("\"*\"");
350 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, tmp) < 0) {
356 log_print(LOG_PROTOCOL, "Sieve> [PASSWORD]\n");
362 static gint sieve_auth_cram_md5(SieveSession *session)
364 session->state = SIEVE_AUTH;
365 session->auth_type = SIEVEAUTH_CRAM_MD5;
367 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
368 "Authenticate \"CRAM-MD5\"") < 0)
370 log_print(LOG_PROTOCOL, "Sieve> Authenticate CRAM-MD5\n");
375 static gint sieve_auth_plain(SieveSession *session)
377 gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
380 session->state = SIEVE_AUTH_PLAIN;
381 session->auth_type = SIEVEAUTH_PLAIN;
383 memset(buf, 0, sizeof buf);
385 /* "\0user\0password" */
386 len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
387 b64buf = g_base64_encode(buf, len);
388 out = g_strconcat("Authenticate \"PLAIN\" \"", b64buf, "\"", NULL);
391 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, out) < 0) {
398 log_print(LOG_PROTOCOL, "Sieve> [Authenticate PLAIN]\n");
403 static gint sieve_auth_login(SieveSession *session)
405 session->state = SIEVE_AUTH;
406 session->auth_type = SIEVEAUTH_LOGIN;
408 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
409 "Authenticate \"LOGIN\"") < 0)
411 log_print(LOG_PROTOCOL, "Sieve> Authenticate LOGIN\n");
416 static gint sieve_auth(SieveSession *session)
418 SieveAuthType forced_auth_type = session->forced_auth_type;
420 if (!session->use_auth) {
421 session->state = SIEVE_READY;
422 sieve_connected(session, TRUE);
426 session->state = SIEVE_AUTH;
427 sieve_error(session, _("Authenticating..."));
429 if ((forced_auth_type == SIEVEAUTH_CRAM_MD5 || forced_auth_type == 0) &&
430 (session->avail_auth_type & SIEVEAUTH_CRAM_MD5) != 0)
431 return sieve_auth_cram_md5(session);
432 else if ((forced_auth_type == SIEVEAUTH_LOGIN || forced_auth_type == 0) &&
433 (session->avail_auth_type & SIEVEAUTH_LOGIN) != 0)
434 return sieve_auth_login(session);
435 else if ((forced_auth_type == SIEVEAUTH_PLAIN || forced_auth_type == 0) &&
436 (session->avail_auth_type & SIEVEAUTH_PLAIN) != 0)
437 return sieve_auth_plain(session);
438 else if (forced_auth_type == 0) {
439 log_warning(LOG_PROTOCOL, _("No Sieve auth method available\n"));
440 session->state = SIEVE_RETRY_AUTH;
443 log_warning(LOG_PROTOCOL, _("Selected Sieve auth method not available\n"));
444 session->state = SIEVE_RETRY_AUTH;
451 static void sieve_session_putscript_cb(SieveSession *session, SieveResult *result)
453 /* Remove script name from the beginning the response,
454 * which is added by Dovecot/Pigeonhole */
455 gchar *start, *desc = result->description;
458 /* callback just for the status */
459 command_cb(session->current_cmd, result);
461 while (desc && desc[0]) {
462 if ((end = strchr(desc, '\r')) ||
463 (end = strchr(desc, '\n')))
464 while (*end == '\n' || *end == '\r')
466 if (g_str_has_prefix(desc, "NULL_") && (start = strchr(desc+5, ':'))) {
470 /* TODO: match against known script name, in case it contains
471 * weird text like ": line " */
472 } else if ((start = strstr(desc, ": line ")) ||
473 (start = strstr(desc, ": error"))) {
476 result->description = desc;
477 command_cb(session->current_cmd, result);
482 static inline gboolean response_is_ok(const char *msg)
484 return !strncmp(msg, "OK", 2) && (!msg[2] || msg[2] == ' ');
487 static inline gboolean response_is_no(const char *msg)
489 return !strncmp(msg, "NO", 2) && (!msg[2] || msg[2] == ' ');
492 static inline gboolean response_is_bye(const char *msg)
494 return !strncmp(msg, "BYE", 3) && (!msg[3] || msg[3] == ' ');
497 static void sieve_got_capability(SieveSession *session, gchar *cap_name,
500 if (strcmp(cap_name, "SASL") == 0) {
501 SieveAuthType auth_type = 0;
503 for (auth = cap_value; auth && auth[0]; auth = end) {
504 if ((end = strchr(auth, ' ')))
506 if (strcmp(auth, "PLAIN") == 0) {
507 auth_type |= SIEVEAUTH_PLAIN;
508 } else if (strcmp(auth, "CRAM-MD5") == 0) {
509 auth_type |= SIEVEAUTH_CRAM_MD5;
510 } else if (strcmp(auth, "LOGIN") == 0) {
511 auth_type |= SIEVEAUTH_LOGIN;
514 session->avail_auth_type = auth_type;
516 } else if (strcmp(cap_name, "STARTTLS") == 0) {
517 session->capability.starttls = TRUE;
521 static void log_send(SieveSession *session, SieveCommand *cmd)
523 gchar *end, *msg = cmd->msg;
524 if (cmd->next_state == SIEVE_PUTSCRIPT && (end = strchr(msg, '\n'))) {
525 /* Don't log the script data */
526 msg = g_strndup(msg, end - msg);
527 log_print(LOG_PROTOCOL, "Sieve> %s\n", msg);
531 log_print(LOG_PROTOCOL, "Sieve> %s\n", msg);
534 static gint sieve_pop_send_queue(SieveSession *session)
537 GSList *send_queue = session->send_queue;
539 if (session->current_cmd) {
540 command_free(session->current_cmd);
541 session->current_cmd = NULL;
547 cmd = (SieveCommand *)send_queue->data;
548 session->send_queue = g_slist_next(send_queue);
549 g_slist_free_1(send_queue);
551 log_send(session, cmd);
552 session->state = cmd->next_state;
553 session->current_cmd = cmd;
554 if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0)
560 static void parse_split(gchar *line, gchar **first_word, gchar **second_word)
567 if (line[0] == '"' && ((second = strchr(line + 1, '"')))) {
570 if (second[0] == ' ')
572 } else if ((second = strchr(line, ' '))) {
577 if (second && second[0] == '"' &&
578 ((end = strchr(second + 1, '"')))) {
584 *second_word = second;
587 static void unquote_inplace(gchar *str)
592 for (src = str+1, dest = str; src && *src && *src != '"'; src++) {
601 static void parse_response(gchar *msg, SieveResult *result)
605 cm_return_if_fail(msg != NULL);
607 /* response status */
608 if (isalpha(msg[0])) {
609 end = strchr(msg, ' ');
615 result->success = strcmp(msg, "OK") == 0;
616 result->has_status = TRUE;
619 result->has_status = FALSE;
623 if (msg[0] == '(' && (end = strchr(msg, ')'))) {
627 strcmp(msg, "WARNINGS") == 0 ? SIEVE_CODE_WARNINGS :
628 strcmp(msg, "TRYLATER") == 0 ? SIEVE_CODE_TRYLATER :
634 result->code = SIEVE_CODE_NONE;
638 if (msg[0] == '{' && (end = strchr(msg, '}'))) {
641 if (msg[0] == '0' && msg+1 == end) {
642 result->has_octets = TRUE;
646 (result->octets = g_ascii_strtoll(msg, NULL, 10)) != 0;
652 result->has_octets = FALSE;
658 unquote_inplace(msg);
659 result->description = msg;
661 result->description = NULL;
665 static gint sieve_session_recv_msg(Session *session, const gchar *msg)
667 SieveSession *sieve_session = SIEVE_SESSION(session);
671 log_print(LOG_PROTOCOL, "Sieve< %s\n", msg);
672 if (response_is_bye(msg)) {
674 parse_response((gchar *)msg, &result);
675 if (!result.description)
676 status = g_strdup(_("Disconnected"));
677 else if (g_str_has_prefix(result.description, "Disconnected"))
678 status = g_strdup(result.description);
680 status = g_strdup_printf(_("Disconnected: %s"), result.description);
681 sieve_session->error = SE_ERROR;
682 sieve_error(sieve_session, status);
683 sieve_session->state = SIEVE_DISCONNECTED;
688 switch (sieve_session->state) {
689 case SIEVE_CAPABILITIES:
690 if (response_is_ok(msg)) {
691 /* capabilities list done */
694 if (sieve_session->tls_init_done == FALSE &&
695 sieve_session->config->tls_type != SIEVE_TLS_NO) {
696 if (sieve_session->capability.starttls) {
697 log_print(LOG_PROTOCOL, "Sieve> STARTTLS\n");
698 session_send_msg(session, SESSION_SEND, "STARTTLS");
699 sieve_session->state = SIEVE_STARTTLS;
700 } else if (sieve_session->config->tls_type == SIEVE_TLS_YES) {
701 log_warning(LOG_PROTOCOL, "Sieve: does not support STARTTLS\n");
702 sieve_session->state = SIEVE_ERROR;
704 log_warning(LOG_PROTOCOL, "Sieve: continuing without TLS\n");
705 sieve_session->state = SIEVE_READY;
710 /* authenticate after getting capabilities */
711 if (!sieve_session->authenticated) {
712 ret = sieve_auth(sieve_session);
714 sieve_session->state = SIEVE_READY;
715 sieve_connected(sieve_session, TRUE);
718 /* got a capability */
719 gchar *cap_name, *cap_value;
720 parse_split((gchar *)msg, &cap_name, &cap_value);
721 sieve_got_capability(sieve_session, cap_name, cap_value);
727 log_warning(LOG_PROTOCOL,
728 _("unhandled message on Sieve session: %s\n"), msg);
732 if (session_start_tls(session) < 0) {
733 sieve_session->state = SIEVE_ERROR;
734 sieve_session->error = SE_ERROR;
735 sieve_error(sieve_session, _("TLS failed"));
738 sieve_session->tls_init_done = TRUE;
739 sieve_session->state = SIEVE_CAPABILITIES;
743 ret = sieve_auth_recv(sieve_session, msg);
745 case SIEVE_AUTH_LOGIN_USER:
746 ret = sieve_auth_login_user_recv(sieve_session, msg);
748 case SIEVE_AUTH_PLAIN:
749 case SIEVE_AUTH_LOGIN_PASS:
750 case SIEVE_AUTH_CRAM_MD5:
751 if (response_is_no(msg)) {
752 log_print(LOG_PROTOCOL, "Sieve auth failed\n");
753 session->state = SIEVE_RETRY_AUTH;
755 } else if (response_is_ok(msg)) {
756 log_print(LOG_PROTOCOL, "Sieve auth completed\n");
757 sieve_error(sieve_session, "");
758 sieve_session->authenticated = TRUE;
759 sieve_session->state = SIEVE_READY;
760 sieve_connected(sieve_session, TRUE);
764 if (!response_is_ok(msg)) {
765 sieve_session->state = SIEVE_ERROR;
767 sieve_session->state = SIEVE_READY;
769 case SIEVE_LISTSCRIPTS:
770 if (response_is_no(msg)) {
771 /* got an error. probably not authenticated. */
772 command_cb(sieve_session->current_cmd, NULL);
773 sieve_session->state = SIEVE_READY;
774 } else if (response_is_ok(msg)) {
776 sieve_session->state = SIEVE_READY;
777 sieve_session->error = SE_OK;
778 command_cb(sieve_session->current_cmd,
779 (gpointer)&(SieveScript){0});
781 /* got a script name */
783 gchar *script_status;
785 parse_split((gchar *)msg, &script.name, &script_status);
786 script.active = (script_status &&
787 strcasecmp(script_status, "active") == 0);
789 command_cb(sieve_session->current_cmd,
793 case SIEVE_RENAMESCRIPT:
794 if (response_is_no(msg)) {
796 command_cb(sieve_session->current_cmd, NULL);
797 } else if (response_is_ok(msg)) {
798 command_cb(sieve_session->current_cmd, (void*)TRUE);
800 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
802 sieve_session->state = SIEVE_READY;
804 case SIEVE_SETACTIVE:
805 parse_response((gchar *)msg, &result);
806 if (result.success) {
807 /* clear status possibly set when setting another
808 * script active. TODO: give textual feedback */
809 sieve_error(sieve_session, "");
811 command_cb(sieve_session->current_cmd, NULL);
812 } else if (result.description) {
813 command_cb(sieve_session->current_cmd,
816 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
818 if (result.has_octets) {
819 return sieve_session_recv_chunk(sieve_session,
822 sieve_session->state = SIEVE_READY;
825 case SIEVE_GETSCRIPT:
826 if (response_is_no(msg)) {
827 command_cb(sieve_session->current_cmd, (void *)-1);
828 sieve_session->state = SIEVE_READY;
830 parse_response((gchar *)msg, &result);
831 sieve_session->state = SIEVE_GETSCRIPT_DATA;
832 return sieve_session_recv_chunk(sieve_session,
836 case SIEVE_GETSCRIPT_DATA:
839 sieve_session->state = SIEVE_READY;
840 if (response_is_ok(msg)) {
841 command_cb(sieve_session->current_cmd, NULL);
843 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
846 case SIEVE_PUTSCRIPT:
849 parse_response((gchar *)msg, &result);
850 sieve_session_putscript_cb(sieve_session, &result);
851 if (result.has_octets) {
852 return sieve_session_recv_chunk(sieve_session,
855 sieve_session->state = SIEVE_READY;
858 case SIEVE_DELETESCRIPT:
859 parse_response((gchar *)msg, &result);
860 if (!result.success) {
861 command_cb(sieve_session->current_cmd,
864 command_cb(sieve_session->current_cmd, NULL);
866 sieve_session->state = SIEVE_READY;
869 log_warning(LOG_PROTOCOL, _("error occurred on Sieve session. data: %s\n"), msg);
870 sieve_session->error = SE_ERROR;
872 case SIEVE_RETRY_AUTH:
873 log_warning(LOG_PROTOCOL, _("unhandled message on Sieve session: %s\n"),
875 ret = sieve_auth(sieve_session);
878 log_warning(LOG_PROTOCOL, _("unhandled message on Sieve session: %d\n"),
879 sieve_session->state);
880 sieve_session->error = SE_ERROR;
884 if (ret == SE_OK && sieve_session->state == SIEVE_READY)
885 ret = sieve_pop_send_queue(sieve_session);
888 return session_recv_msg(session);
889 } else if (ret == SE_AUTHFAIL) {
890 sieve_error(sieve_session, _("Auth failed"));
891 sieve_session->state = SIEVE_ERROR;
892 sieve_session->error = SE_ERROR;
898 static gint sieve_recv_message(Session *session, const gchar *msg,
904 static void sieve_read_chunk(SieveSession *session, gchar *data, guint len)
906 log_print(LOG_PROTOCOL, "Sieve< [%u bytes]\n", len);
908 switch (session->state) {
909 case SIEVE_GETSCRIPT_DATA:
910 command_cb(session->current_cmd, (gchar *)data);
912 case SIEVE_SETACTIVE:
913 /* Dovecot shows a script's warnings when making it active */
914 /* TODO: append message in case it is very long*/
916 sieve_error(session, data);
918 case SIEVE_PUTSCRIPT: {
919 SieveResult result = {.description = (gchar *)data};
920 sieve_session_putscript_cb(session, &result);
924 log_warning(LOG_PROTOCOL,
925 _("error occurred on SIEVE session\n"));
929 static gint sieve_read_chunk_done(SieveSession *session)
933 switch (session->state) {
934 case SIEVE_GETSCRIPT_DATA:
935 /* wait for ending "OK" response */
937 case SIEVE_SETACTIVE:
938 case SIEVE_PUTSCRIPT:
939 session->state = SIEVE_READY;
942 log_warning(LOG_PROTOCOL,
943 _("error occurred on SIEVE session\n"));
946 if (ret == SE_OK && session->state == SIEVE_READY)
947 ret = sieve_pop_send_queue(session);
950 return session_recv_msg(SESSION(session));
955 static gint sieve_cmd_noop(SieveSession *session)
957 log_print(LOG_PROTOCOL, "Sieve> NOOP\n");
958 session->state = SIEVE_NOOP;
959 if (session_send_msg(SESSION(session), SESSION_SEND, "NOOP") < 0) {
960 session->state = SIEVE_ERROR;
961 session->error = SE_ERROR;
967 static gboolean sieve_ping(gpointer data)
969 Session *session = SESSION(data);
970 SieveSession *sieve_session = SIEVE_SESSION(session);
972 if (sieve_session->state == SIEVE_ERROR || session->state == SESSION_ERROR)
974 if (sieve_session->state != SIEVE_READY)
977 return sieve_cmd_noop(sieve_session) == 0;
980 static void sieve_session_destroy(Session *session)
982 SieveSession *sieve_session = SIEVE_SESSION(session);
983 g_free(sieve_session->pass);
984 if (sieve_session->current_cmd)
985 command_abort(sieve_session->current_cmd);
986 sessions = g_slist_remove(sessions, (gconstpointer)session);
987 g_slist_free_full(sieve_session->send_queue,
988 (GDestroyNotify)command_abort);
991 static void sieve_connect_finished(Session *session, gboolean success)
994 sieve_connected(SIEVE_SESSION(session), FALSE);
998 static gint sieve_session_connect(SieveSession *session)
1000 session->state = SIEVE_CAPABILITIES;
1001 session->authenticated = FALSE;
1003 session->tls_init_done = FALSE;
1005 return session_connect(SESSION(session), session->host,
1009 static SieveSession *sieve_session_new(PrefsAccount *account)
1011 SieveSession *session;
1012 session = g_new0(SieveSession, 1);
1013 session_init(SESSION(session), account, FALSE);
1015 session->account = account;
1017 SESSION(session)->recv_msg = sieve_session_recv_msg;
1018 SESSION(session)->destroy = sieve_session_destroy;
1019 SESSION(session)->connect_finished = sieve_connect_finished;
1020 session_set_recv_message_notify(SESSION(session), sieve_recv_message, NULL);
1022 sieve_session_reset(session);
1026 static void sieve_session_reset(SieveSession *session)
1028 PrefsAccount *account = session->account;
1029 SieveAccountConfig *config = sieve_prefs_account_get_config(account);
1030 gboolean reuse_auth = (config->auth == SIEVEAUTH_REUSE);
1032 g_slist_free_full(session->send_queue, (GDestroyNotify)command_abort);
1034 session_disconnect(SESSION(session));
1036 SESSION(session)->ssl_cert_auto_accept = account->ssl_certs_auto_accept;
1037 SESSION(session)->nonblocking = account->use_nonblocking_ssl;
1038 session->authenticated = FALSE;
1039 session->current_cmd = NULL;
1040 session->send_queue = NULL;
1041 session->state = SIEVE_CAPABILITIES;
1043 session->tls_init_done = FALSE;
1045 session->avail_auth_type = 0;
1046 session->auth_type = 0;
1047 session->config = config;
1048 session->host = config->use_host ? config->host : account->recv_server;
1049 session->port = config->use_port ? config->port : SIEVE_PORT;
1050 session->user = reuse_auth ? account->userid : session->config->userid;
1051 session->forced_auth_type = config->auth_type;
1052 session_register_ping(SESSION(session), sieve_ping);
1055 g_free(session->pass);
1056 if (config->auth == SIEVEAUTH_NONE) {
1057 session->pass = NULL;
1058 } else if (reuse_auth && account->passwd) {
1059 session->pass = g_strdup(account->passwd);
1060 } else if (config->passwd && config->passwd[0]) {
1061 session->pass = g_strdup(config->passwd);
1062 } else if (password_get(session->user, session->host, "sieve",
1063 session->port, &session->pass)) {
1065 session->pass = input_dialog_query_password_keep(session->host,
1066 session->user, &(session->pass));
1068 if (!session->pass) {
1069 session->pass = g_strdup("");
1070 session->use_auth = FALSE;
1072 session->use_auth = TRUE;
1076 SESSION(session)->ssl_type =
1077 (config->tls_type == SIEVE_TLS_NO) ? SSL_NONE : SSL_STARTTLS;
1081 /* When an account config is changed, reset associated sessions. */
1082 void sieve_account_prefs_updated(PrefsAccount *account)
1085 SieveSession *session;
1087 for (item = sessions; item; item = item->next) {
1088 session = (SieveSession *)item->data;
1089 if (session->account == account) {
1090 log_print(LOG_PROTOCOL, "Sieve: resetting session\n");
1091 sieve_session_reset(session);
1096 SieveSession *sieve_session_get_for_account(PrefsAccount *account)
1098 SieveSession *session;
1102 for (item = sessions; item; item = item->next) {
1103 session = (SieveSession *)item->data;
1104 if (session->account == account) {
1110 session = sieve_session_new(account);
1111 sessions = g_slist_prepend(sessions, session);
1116 static void sieve_queue_send(SieveSession *session, SieveState next_state,
1117 gchar *msg, sieve_session_data_cb_fn cb, gpointer data)
1119 gboolean queue = FALSE;
1120 SieveCommand *cmd = g_new0(SieveCommand, 1);
1121 cmd->session = session;
1122 cmd->next_state = next_state;
1127 if (!session_is_connected(SESSION(session))) {
1128 log_print(LOG_PROTOCOL, "Sieve: connecting to %s:%hu\n",
1129 session->host, session->port);
1130 if (sieve_session_connect(session) < 0) {
1131 sieve_connect_finished(SESSION(session), FALSE);
1134 } else if (session->state == SIEVE_RETRY_AUTH) {
1135 log_print(LOG_PROTOCOL, _("Sieve: retrying auth\n"));
1136 if (sieve_auth(session) == SE_AUTHFAIL)
1137 sieve_error(session, _("Auth method not available"));
1139 } else if (session->state != SIEVE_READY) {
1140 log_print(LOG_PROTOCOL, "Sieve: in state %d\n", session->state);
1145 session->send_queue = g_slist_prepend(session->send_queue, cmd);
1147 if (session->current_cmd)
1148 command_free(session->current_cmd);
1149 session->current_cmd = cmd;
1150 session->state = next_state;
1151 log_send(session, cmd);
1152 if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0) {
1158 void sieve_session_list_scripts(SieveSession *session,
1159 sieve_session_data_cb_fn cb, gpointer data)
1161 gchar *msg = g_strdup("LISTSCRIPTS");
1162 sieve_queue_send(session, SIEVE_LISTSCRIPTS, msg, cb, data);
1165 void sieve_session_set_active_script(SieveSession *session,
1166 const gchar *filter_name,
1167 sieve_session_data_cb_fn cb, gpointer data)
1169 gchar *msg = g_strdup_printf("SETACTIVE \"%s\"",
1170 filter_name ? filter_name : "");
1172 cb(session, FALSE, (void*)FALSE, data);
1176 sieve_queue_send(session, SIEVE_SETACTIVE, msg, cb, data);
1179 void sieve_session_rename_script(SieveSession *session,
1180 const gchar *name_old, const char *name_new,
1181 sieve_session_data_cb_fn cb, gpointer data)
1183 gchar *msg = g_strdup_printf("RENAMESCRIPT \"%s\" \"%s\"",
1184 name_old, name_new);
1186 sieve_queue_send(session, SIEVE_RENAMESCRIPT, msg, cb, data);
1189 void sieve_session_get_script(SieveSession *session, const gchar *filter_name,
1190 sieve_session_data_cb_fn cb, gpointer data)
1192 gchar *msg = g_strdup_printf("GETSCRIPT \"%s\"",
1195 sieve_queue_send(session, SIEVE_GETSCRIPT, msg, cb, data);
1198 void sieve_session_put_script(SieveSession *session, const gchar *filter_name,
1199 gint len, const gchar *script_contents,
1200 sieve_session_data_cb_fn cb, gpointer data)
1202 /* TODO: refactor so don't have to copy the whole script here */
1203 gchar *msg = g_strdup_printf("PUTSCRIPT \"%s\" {%u+}%s%s",
1204 filter_name, len, len > 0 ? "\r\n" : "",
1207 sieve_queue_send(session, SIEVE_PUTSCRIPT, msg, cb, data);
1210 void sieve_session_check_script(SieveSession *session,
1211 gint len, const gchar *script_contents,
1212 sieve_session_data_cb_fn cb, gpointer data)
1214 gchar *msg = g_strdup_printf("CHECKSCRIPT {%u+}%s%s",
1215 len, len > 0 ? "\r\n" : "", script_contents);
1217 sieve_queue_send(session, SIEVE_PUTSCRIPT, msg, cb, data);
1220 void sieve_session_delete_script(SieveSession *session,
1221 const gchar *filter_name,
1222 sieve_session_data_cb_fn cb, gpointer data)
1224 gchar *msg = g_strdup_printf("DELETESCRIPT \"%s\"",
1227 sieve_queue_send(session, SIEVE_DELETESCRIPT, msg, cb, data);