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>
27 #include "gtk/inputdialog.h"
33 #include "managesieve.h"
34 #include "sieve_editor.h"
35 #include "sieve_prefs.h"
37 GSList *sessions = NULL;
39 static void sieve_session_destroy(Session *session);
40 static gint sieve_pop_send_queue(SieveSession *session);
41 static void sieve_session_reset(SieveSession *session);
42 static void command_free(SieveCommand *cmd);
43 static void command_abort(SieveCommand *cmd);
44 static void command_cb(SieveCommand *cmd, gpointer result);
46 void sieve_sessions_close()
49 g_slist_free_full(sessions, (GDestroyNotify)session_destroy);
54 /* remove all command callbacks with a given data pointer */
55 void sieve_sessions_discard_callbacks(gpointer user_data)
60 SieveSession *session;
63 for (item = sessions; item; item = item->next) {
64 session = (SieveSession *)item->data;
65 cmd = session->current_cmd;
66 /* abort current command handler */
67 if (cmd && cmd->data == user_data) {
69 session->current_cmd = NULL;
71 /* abort queued command handlers */
72 for (queue = session->send_queue; queue; queue = queue->next) {
73 cmd = (SieveCommand *)queue->data;
74 if (cmd && cmd->data == user_data) {
76 prev->next = queue->next;
78 session->send_queue = NULL;
80 g_slist_free_1(queue);
88 static void command_cb(SieveCommand *cmd, gpointer result)
91 cmd->cb(cmd->session, FALSE, result, cmd->data);
94 static void command_abort(SieveCommand *cmd)
96 cmd->cb(cmd->session, TRUE, NULL, cmd->data);
101 static void command_free(SieveCommand *cmd)
107 void sieve_session_handle_status(SieveSession *session,
108 sieve_session_error_cb_fn on_error,
109 sieve_session_connected_cb_fn on_connected,
112 session->on_error = on_error;
113 session->on_connected = on_connected;
114 session->cb_data = data;
117 static void sieve_error(SieveSession *session, const gchar *msg)
119 if (session->on_error)
120 session->on_error(session, msg, session->cb_data);
123 static void sieve_connected(SieveSession *session, gboolean connected)
125 if (session->on_connected)
126 session->on_connected(session, connected, session->cb_data);
129 static gint sieve_auth_recv(SieveSession *session, const gchar *msg)
131 gchar buf[MESSAGEBUFSIZE], *tmp;
133 switch (session->auth_type) {
134 case SIEVEAUTH_LOGIN:
135 session->state = SIEVE_AUTH_LOGIN_USER;
137 if (strstr(msg, "VXNlcm5hbWU6")) {
138 tmp = g_base64_encode(session->user, strlen(session->user));
139 g_snprintf(buf, sizeof(buf), "\"%s\"", tmp);
141 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf) < 0) {
146 log_print(LOG_PROTOCOL, "Sieve> [USERID]\n");
148 /* Server rejects AUTH */
149 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
152 log_print(LOG_PROTOCOL, "Sieve> *\n");
155 case SIEVEAUTH_CRAM_MD5:
156 session->state = SIEVE_AUTH_CRAM_MD5;
161 gchar *challenge, *tmp;
163 guchar hexdigest[33];
165 tmp = g_base64_decode(msg + 1, &challengelen);
166 challenge = g_strndup(tmp, challengelen);
168 log_print(LOG_PROTOCOL, "Sieve< [Decoded: %s]\n", challenge);
170 g_snprintf(buf, sizeof(buf), "%s", session->pass);
171 md5_hex_hmac(hexdigest, challenge, challengelen,
172 buf, strlen(session->pass));
175 response = g_strdup_printf
176 ("%s %s", session->user, hexdigest);
177 log_print(LOG_PROTOCOL, "Sieve> [Encoded: %s]\n", response);
179 response64 = g_base64_encode(response, strlen(response));
182 response = g_strdup_printf("\"%s\"", response64);
185 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
190 log_print(LOG_PROTOCOL, "Sieve> %s\n", response);
193 /* Server rejects AUTH */
194 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
197 log_print(LOG_PROTOCOL, "Sieve> *\n");
201 /* stop sieve_auth when no correct authtype */
202 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*") < 0)
204 log_print(LOG_PROTOCOL, "Sieve> *\n");
211 static gint sieve_auth_login_user_recv(SieveSession *session, const gchar *msg)
215 session->state = SIEVE_AUTH_LOGIN_PASS;
217 if (strstr(msg, "UGFzc3dvcmQ6")) {
218 tmp2 = g_base64_encode(session->pass, strlen(session->pass));
219 tmp = g_strdup_printf("\"%s\"", tmp2);
222 /* Server rejects AUTH */
223 tmp = g_strdup("\"*\"");
226 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, tmp) < 0) {
232 log_print(LOG_PROTOCOL, "Sieve> [PASSWORD]\n");
238 static gint sieve_auth_cram_md5(SieveSession *session)
240 session->state = SIEVE_AUTH;
241 session->auth_type = SIEVEAUTH_CRAM_MD5;
243 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
244 "Authenticate \"CRAM-MD5\"") < 0)
246 log_print(LOG_PROTOCOL, "Sieve> Authenticate CRAM-MD5\n");
251 static gint sieve_auth_plain(SieveSession *session)
253 gchar buf[MESSAGEBUFSIZE], *b64buf, *out;
256 session->state = SIEVE_AUTH_PLAIN;
257 session->auth_type = SIEVEAUTH_PLAIN;
259 memset(buf, 0, sizeof buf);
261 /* "\0user\0password" */
262 len = sprintf(buf, "%c%s%c%s", '\0', session->user, '\0', session->pass);
263 b64buf = g_base64_encode(buf, len);
264 out = g_strconcat("Authenticate \"PLAIN\" \"", b64buf, "\"", NULL);
267 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL, out) < 0) {
274 log_print(LOG_PROTOCOL, "Sieve> [Authenticate PLAIN]\n");
279 static gint sieve_auth_login(SieveSession *session)
281 session->state = SIEVE_AUTH;
282 session->auth_type = SIEVEAUTH_LOGIN;
284 if (session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
285 "Authenticate \"LOGIN\"") < 0)
287 log_print(LOG_PROTOCOL, "Sieve> Authenticate LOGIN\n");
292 static gint sieve_auth(SieveSession *session)
294 SieveAuthType forced_auth_type = session->forced_auth_type;
296 if (!session->use_auth) {
297 session->state = SIEVE_READY;
298 sieve_connected(session, TRUE);
302 session->state = SIEVE_AUTH;
303 sieve_error(session, _("Authenticating..."));
305 if ((forced_auth_type == SIEVEAUTH_CRAM_MD5 || forced_auth_type == 0) &&
306 (session->avail_auth_type & SIEVEAUTH_CRAM_MD5) != 0)
307 return sieve_auth_cram_md5(session);
308 else if ((forced_auth_type == SIEVEAUTH_LOGIN || forced_auth_type == 0) &&
309 (session->avail_auth_type & SIEVEAUTH_LOGIN) != 0)
310 return sieve_auth_login(session);
311 else if ((forced_auth_type == SIEVEAUTH_PLAIN || forced_auth_type == 0) &&
312 (session->avail_auth_type & SIEVEAUTH_PLAIN) != 0)
313 return sieve_auth_plain(session);
314 else if (forced_auth_type == 0) {
315 log_warning(LOG_PROTOCOL, _("No Sieve auth method available\n"));
316 session->state = SIEVE_RETRY_AUTH;
319 log_warning(LOG_PROTOCOL, _("Selected Sieve auth method not available\n"));
320 session->state = SIEVE_RETRY_AUTH;
327 static void sieve_session_putscript_cb(SieveSession *session, SieveResult *result)
329 /* Remove script name from the beginning the response,
330 * which are added by Dovecot/Pigeonhole */
331 gchar *start, *desc = result->description;
333 if (g_str_has_prefix(desc, "NULL_") && (start = strchr(desc+5, ':'))) {
337 /* TODO: match against known script name, in case it contains
338 * weird text like ": line " */
339 } else if ((start = strstr(desc, ": line ")) ||
340 (start = strstr(desc, ": error"))) {
343 result->description = desc;
345 /* pass along the callback */
346 command_cb(session->current_cmd, result);
349 static inline gboolean response_is_ok(const char *msg)
351 return !strncmp(msg, "OK", 2) && (!msg[2] || msg[2] == ' ');
354 static inline gboolean response_is_no(const char *msg)
356 return !strncmp(msg, "NO", 2) && (!msg[2] || msg[2] == ' ');
359 static inline gboolean response_is_bye(const char *msg)
361 return !strncmp(msg, "BYE", 3) && (!msg[3] || msg[3] == ' ');
364 static void sieve_got_capability(SieveSession *session, gchar *cap_name,
367 if (strcmp(cap_name, "SASL") == 0) {
368 SieveAuthType auth_type = 0;
370 for (auth = cap_value; auth && auth[0]; auth = end) {
371 if ((end = strchr(auth, ' ')))
373 if (strcmp(auth, "PLAIN") == 0) {
374 auth_type |= SIEVEAUTH_PLAIN;
375 } else if (strcmp(auth, "CRAM-MD5") == 0) {
376 auth_type |= SIEVEAUTH_CRAM_MD5;
377 } else if (strcmp(auth, "LOGIN") == 0) {
378 auth_type |= SIEVEAUTH_LOGIN;
381 session->avail_auth_type = auth_type;
383 } else if (strcmp(cap_name, "STARTTLS") == 0) {
384 session->capability.starttls = TRUE;
388 static void log_send(SieveSession *session, SieveCommand *cmd)
390 gchar *end, *msg = cmd->msg;
391 if (cmd->next_state == SIEVE_PUTSCRIPT && (end = strchr(msg, '\n'))) {
392 /* Don't log the script data */
393 msg = g_strndup(msg, end - msg);
394 log_print(LOG_PROTOCOL, "Sieve> %s\n", msg);
398 log_print(LOG_PROTOCOL, "Sieve> %s\n", msg);
401 static gint sieve_pop_send_queue(SieveSession *session)
404 GSList *send_queue = session->send_queue;
406 if (session->current_cmd) {
407 command_free(session->current_cmd);
408 session->current_cmd = NULL;
414 cmd = (SieveCommand *)send_queue->data;
415 session->send_queue = g_slist_next(send_queue);
416 g_slist_free_1(send_queue);
418 log_send(session, cmd);
419 session->state = cmd->next_state;
420 session->current_cmd = cmd;
421 if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0)
427 static void parse_split(gchar *line, gchar **first_word, gchar **second_word)
434 if (line[0] == '"' && ((second = strchr(line + 1, '"')))) {
437 if (second[0] == ' ')
439 } else if ((second = strchr(line, ' '))) {
444 if (second && second[0] == '"' &&
445 ((end = strchr(second + 1, '"')))) {
451 *second_word = second;
454 static void unquote_inplace(gchar *str)
459 for (src = str+1, dest = str; src && *src && *src != '"'; src++) {
468 static void parse_response(gchar *msg, SieveResult *result)
472 cm_return_if_fail(msg != NULL);
474 /* response status */
475 if (isalpha(msg[0])) {
476 end = strchr(msg, ' ');
482 result->success = strcmp(msg, "OK") == 0;
483 result->has_status = TRUE;
486 result->has_status = FALSE;
490 if (msg[0] == '(' && (end = strchr(msg, ')'))) {
494 strcmp(msg, "WARNINGS") == 0 ? SIEVE_CODE_WARNINGS :
495 strcmp(msg, "TRYLATER") == 0 ? SIEVE_CODE_TRYLATER :
501 result->code = SIEVE_CODE_NONE;
505 if (msg[0] == '{' && (end = strchr(msg, '}'))) {
508 if (msg[0] == '0' && msg+1 == end) {
509 result->has_octets = TRUE;
513 (result->octets = g_ascii_strtoll(msg, NULL, 10)) != 0;
519 result->has_octets = FALSE;
525 unquote_inplace(msg);
526 result->description = msg;
528 result->description = NULL;
532 static gint sieve_session_recv_msg(Session *session, const gchar *msg)
534 SieveSession *sieve_session = SIEVE_SESSION(session);
538 switch (sieve_session->state) {
539 case SIEVE_GETSCRIPT_DATA:
540 log_print(LOG_PROTOCOL, "Sieve< [GETSCRIPT data]\n");
543 log_print(LOG_PROTOCOL, "Sieve< %s\n", msg);
544 if (response_is_bye(msg)) {
546 parse_response((gchar *)msg, &result);
547 if (!result.description)
548 status = g_strdup(_("Disconnected"));
549 else if (g_str_has_prefix(result.description, "Disconnected"))
550 status = g_strdup(result.description);
552 status = g_strdup_printf(_("Disconnected: %s"), result.description);
553 sieve_session->error = SE_ERROR;
554 sieve_error(sieve_session, status);
555 sieve_session->state = SIEVE_DISCONNECTED;
561 switch (sieve_session->state) {
562 case SIEVE_CAPABILITIES:
563 if (response_is_ok(msg)) {
564 /* capabilities list done */
567 if (sieve_session->tls_init_done == FALSE &&
568 sieve_session->config->tls_type != SIEVE_TLS_NO) {
569 if (sieve_session->capability.starttls) {
570 log_print(LOG_PROTOCOL, "Sieve> STARTTLS\n");
571 session_send_msg(session, SESSION_SEND, "STARTTLS");
572 sieve_session->state = SIEVE_STARTTLS;
573 } else if (sieve_session->config->tls_type == SIEVE_TLS_YES) {
574 log_warning(LOG_PROTOCOL, "Sieve: does not support STARTTLS\n");
575 sieve_session->state = SIEVE_ERROR;
577 log_warning(LOG_PROTOCOL, "Sieve: continuing without TLS\n");
578 sieve_session->state = SIEVE_READY;
583 /* authenticate after getting capabilities */
584 if (!sieve_session->authenticated) {
585 ret = sieve_auth(sieve_session);
587 sieve_session->state = SIEVE_READY;
588 sieve_connected(sieve_session, TRUE);
591 /* got a capability */
592 gchar *cap_name, *cap_value;
593 parse_split((gchar *)msg, &cap_name, &cap_value);
594 sieve_got_capability(sieve_session, cap_name, cap_value);
598 log_warning(LOG_PROTOCOL,
599 _("unhandled message on Sieve session: %s\n"), msg);
603 if (session_start_tls(session) < 0) {
604 sieve_session->state = SIEVE_ERROR;
605 sieve_session->error = SE_ERROR;
606 sieve_error(sieve_session, _("TLS failed"));
609 sieve_session->tls_init_done = TRUE;
610 sieve_session->state = SIEVE_CAPABILITIES;
614 ret = sieve_auth_recv(sieve_session, msg);
616 case SIEVE_AUTH_LOGIN_USER:
617 ret = sieve_auth_login_user_recv(sieve_session, msg);
619 case SIEVE_AUTH_PLAIN:
620 case SIEVE_AUTH_LOGIN_PASS:
621 case SIEVE_AUTH_CRAM_MD5:
622 if (response_is_no(msg)) {
623 log_print(LOG_PROTOCOL, "Sieve auth failed\n");
624 session->state = SIEVE_RETRY_AUTH;
626 } else if (response_is_ok(msg)) {
627 log_print(LOG_PROTOCOL, "Sieve auth completed\n");
628 sieve_error(sieve_session, "");
629 sieve_session->authenticated = TRUE;
630 sieve_session->state = SIEVE_READY;
631 sieve_connected(sieve_session, TRUE);
635 if (!response_is_ok(msg)) {
636 sieve_session->state = SIEVE_ERROR;
638 sieve_session->state = SIEVE_READY;
640 case SIEVE_LISTSCRIPTS:
641 if (response_is_no(msg)) {
642 /* got an error. probably not authenticated. */
643 command_cb(sieve_session->current_cmd, NULL);
644 sieve_session->state = SIEVE_READY;
645 } else if (response_is_ok(msg)) {
647 sieve_session->state = SIEVE_READY;
648 sieve_session->error = SE_OK;
649 command_cb(sieve_session->current_cmd,
650 (gpointer)&(SieveScript){0});
652 /* got a script name */
654 gchar *script_status;
656 parse_split((gchar *)msg, &script.name, &script_status);
657 script.active = (script_status &&
658 strcasecmp(script_status, "active") == 0);
660 command_cb(sieve_session->current_cmd,
664 case SIEVE_RENAMESCRIPT:
665 if (response_is_no(msg)) {
667 command_cb(sieve_session->current_cmd, NULL);
668 } else if (response_is_ok(msg)) {
669 command_cb(sieve_session->current_cmd, (void*)TRUE);
671 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
673 sieve_session->state = SIEVE_READY;
675 case SIEVE_SETACTIVE:
676 if (response_is_no(msg)) {
678 command_cb(sieve_session->current_cmd, NULL);
679 } else if (response_is_ok(msg)) {
680 command_cb(sieve_session->current_cmd, (void*)TRUE);
682 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
684 sieve_session->state = SIEVE_READY;
686 case SIEVE_GETSCRIPT:
687 if (response_is_no(msg)) {
688 command_cb(sieve_session->current_cmd, (void *)-1);
689 sieve_session->state = SIEVE_READY;
691 parse_response((gchar *)msg, &result);
692 sieve_session->state = SIEVE_GETSCRIPT_DATA;
693 /* account for newline */
694 sieve_session->octets_remaining = result.octets + 1;
697 case SIEVE_GETSCRIPT_DATA:
698 if (sieve_session->octets_remaining > 0) {
699 command_cb(sieve_session->current_cmd, (gchar *)msg);
700 sieve_session->octets_remaining -= strlen(msg) + 1;
701 } else if (response_is_ok(msg)) {
702 sieve_session->state = SIEVE_READY;
703 command_cb(sieve_session->current_cmd, NULL);
705 log_warning(LOG_PROTOCOL, _("error occurred on SIEVE session\n"));
708 case SIEVE_PUTSCRIPT:
709 parse_response((gchar *)msg, &result);
710 if (result.has_octets) {
711 sieve_session->state = SIEVE_PUTSCRIPT_DATA;
713 sieve_session->state = SIEVE_READY;
715 sieve_session_putscript_cb(sieve_session, &result);
717 case SIEVE_PUTSCRIPT_DATA:
719 sieve_session->state = SIEVE_READY;
721 result.has_status = FALSE;
722 result.has_octets = FALSE;
724 result.code = SIEVE_CODE_NONE;
725 result.description = (gchar *)msg;
726 sieve_session_putscript_cb(sieve_session, &result);
729 case SIEVE_DELETESCRIPT:
730 parse_response((gchar *)msg, &result);
731 if (!result.success) {
732 command_cb(sieve_session->current_cmd,
735 command_cb(sieve_session->current_cmd, NULL);
737 sieve_session->state = SIEVE_READY;
740 log_warning(LOG_PROTOCOL, _("error occurred on Sieve session. data: %s\n"), msg);
741 sieve_session->error = SE_ERROR;
743 case SIEVE_RETRY_AUTH:
744 log_warning(LOG_PROTOCOL, _("unhandled message on Sieve session: %s\n"),
746 ret = sieve_auth(sieve_session);
749 log_warning(LOG_PROTOCOL, _("unhandled message on Sieve session: %d\n"),
750 sieve_session->state);
751 sieve_session->error = SE_ERROR;
755 if (ret == SE_OK && sieve_session->state == SIEVE_READY)
756 ret = sieve_pop_send_queue(sieve_session);
759 return session_recv_msg(session);
760 } else if (ret == SE_AUTHFAIL) {
761 sieve_error(sieve_session, _("Auth failed"));
762 sieve_session->state = SIEVE_ERROR;
763 sieve_session->error = SE_ERROR;
769 static gint sieve_recv_message(Session *session, const gchar *msg,
775 static gint sieve_cmd_noop(SieveSession *session)
777 log_print(LOG_PROTOCOL, "Sieve> NOOP\n");
778 session->state = SIEVE_NOOP;
779 if (session_send_msg(SESSION(session), SESSION_SEND, "NOOP") < 0) {
780 session->state = SIEVE_ERROR;
781 session->error = SE_ERROR;
787 static gboolean sieve_ping(gpointer data)
789 Session *session = SESSION(data);
790 SieveSession *sieve_session = SIEVE_SESSION(session);
792 if (sieve_session->state == SIEVE_ERROR || session->state == SESSION_ERROR)
794 if (sieve_session->state != SIEVE_READY)
797 return sieve_cmd_noop(sieve_session) == 0;
800 static void sieve_session_destroy(Session *session)
802 SieveSession *sieve_session = SIEVE_SESSION(session);
803 g_free(sieve_session->pass);
804 if (sieve_session->current_cmd)
805 command_abort(sieve_session->current_cmd);
806 sessions = g_slist_remove(sessions, (gconstpointer)session);
807 g_slist_free_full(sieve_session->send_queue,
808 (GDestroyNotify)command_abort);
811 static void sieve_connect_finished(Session *session, gboolean success)
814 sieve_connected(SIEVE_SESSION(session), FALSE);
818 static gint sieve_session_connect(SieveSession *session)
820 session->state = SIEVE_CAPABILITIES;
821 session->authenticated = FALSE;
822 session->tls_init_done = FALSE;
823 return session_connect(SESSION(session), session->host,
827 static SieveSession *sieve_session_new(PrefsAccount *account)
829 SieveSession *session;
830 session = g_new0(SieveSession, 1);
831 session_init(SESSION(session), account, FALSE);
833 session->account = account;
835 SESSION(session)->recv_msg = sieve_session_recv_msg;
836 SESSION(session)->destroy = sieve_session_destroy;
837 SESSION(session)->connect_finished = sieve_connect_finished;
838 session_set_recv_message_notify(SESSION(session), sieve_recv_message, NULL);
840 sieve_session_reset(session);
844 static void sieve_session_reset(SieveSession *session)
846 PrefsAccount *account = session->account;
847 SieveAccountConfig *config = sieve_prefs_account_get_config(account);
848 gboolean reuse_auth = (config->auth == SIEVEAUTH_REUSE);
850 g_slist_free_full(session->send_queue, (GDestroyNotify)command_abort);
852 session_disconnect(SESSION(session));
854 SESSION(session)->ssl_cert_auto_accept = account->ssl_certs_auto_accept;
855 SESSION(session)->nonblocking = account->use_nonblocking_ssl;
856 session->authenticated = FALSE;
857 session->current_cmd = NULL;
858 session->send_queue = NULL;
859 session->state = SIEVE_CAPABILITIES;
860 session->tls_init_done = FALSE;
861 session->avail_auth_type = 0;
862 session->auth_type = 0;
863 session->config = config;
864 session->host = config->use_host ? config->host : account->recv_server;
865 session->port = config->use_port ? config->port : SIEVE_PORT;
866 session->user = reuse_auth ? account->userid : session->config->userid;
867 session->forced_auth_type = config->auth_type;
868 session_register_ping(SESSION(session), sieve_ping);
871 g_free(session->pass);
872 if (config->auth == SIEVEAUTH_NONE) {
873 session->pass = NULL;
874 } else if (reuse_auth && account->passwd) {
875 session->pass = g_strdup(account->passwd);
876 } else if (config->passwd && config->passwd[0]) {
877 session->pass = g_strdup(config->passwd);
878 } else if (password_get(session->user, session->host, "sieve",
879 session->port, &session->pass)) {
881 session->pass = input_dialog_query_password_keep(session->host,
882 session->user, &(session->pass));
884 if (!session->pass) {
885 session->pass = g_strdup("");
886 session->use_auth = FALSE;
888 session->use_auth = TRUE;
892 SESSION(session)->ssl_type =
893 (config->tls_type == SIEVE_TLS_NO) ? SSL_NONE : SSL_STARTTLS;
897 /* When an account config is changed, reset associated sessions. */
898 void sieve_account_prefs_updated(PrefsAccount *account)
901 SieveSession *session;
903 for (item = sessions; item; item = item->next) {
904 session = (SieveSession *)item->data;
905 if (session->account == account) {
906 log_print(LOG_PROTOCOL, "Sieve: resetting session\n");
907 sieve_session_reset(session);
912 SieveSession *sieve_session_get_for_account(PrefsAccount *account)
914 SieveSession *session;
918 for (item = sessions; item; item = item->next) {
919 session = (SieveSession *)item->data;
920 if (session->account == account) {
926 session = sieve_session_new(account);
927 sessions = g_slist_prepend(sessions, session);
932 static void sieve_queue_send(SieveSession *session, SieveState next_state,
933 gchar *msg, sieve_session_data_cb_fn cb, gpointer data)
935 gboolean queue = FALSE;
936 SieveCommand *cmd = g_new0(SieveCommand, 1);
937 cmd->session = session;
938 cmd->next_state = next_state;
943 if (!session_is_connected(SESSION(session))) {
944 log_print(LOG_PROTOCOL, "Sieve: connecting to %s:%hu\n",
945 session->host, session->port);
946 if (sieve_session_connect(session) < 0) {
947 sieve_connect_finished(SESSION(session), FALSE);
950 } else if (session->state == SIEVE_RETRY_AUTH) {
951 log_print(LOG_PROTOCOL, _("Sieve: retrying auth\n"));
952 if (sieve_auth(session) == SE_AUTHFAIL)
953 sieve_error(session, _("Auth method not available"));
955 } else if (session->state != SIEVE_READY) {
956 log_print(LOG_PROTOCOL, "Sieve: in state %d\n", session->state);
961 session->send_queue = g_slist_prepend(session->send_queue, cmd);
963 if (session->current_cmd)
964 command_free(session->current_cmd);
965 session->current_cmd = cmd;
966 session->state = next_state;
967 log_send(session, cmd);
968 if (session_send_msg(SESSION(session), SESSION_SEND, cmd->msg) < 0) {
974 void sieve_session_list_scripts(SieveSession *session,
975 sieve_session_data_cb_fn cb, gpointer data)
977 gchar *msg = g_strdup("LISTSCRIPTS");
978 sieve_queue_send(session, SIEVE_LISTSCRIPTS, msg, cb, data);
981 void sieve_session_set_active_script(SieveSession *session,
982 const gchar *filter_name,
983 sieve_session_data_cb_fn cb, gpointer data)
985 gchar *msg = g_strdup_printf("SETACTIVE \"%s\"",
986 filter_name ? filter_name : "");
988 cb(session, FALSE, (void*)FALSE, data);
992 sieve_queue_send(session, SIEVE_SETACTIVE, msg, cb, data);
995 void sieve_session_rename_script(SieveSession *session,
996 const gchar *name_old, const char *name_new,
997 sieve_session_data_cb_fn cb, gpointer data)
999 gchar *msg = g_strdup_printf("RENAMESCRIPT \"%s\" \"%s\"",
1000 name_old, name_new);
1002 sieve_queue_send(session, SIEVE_RENAMESCRIPT, msg, cb, data);
1005 void sieve_session_get_script(SieveSession *session, const gchar *filter_name,
1006 sieve_session_data_cb_fn cb, gpointer data)
1008 gchar *msg = g_strdup_printf("GETSCRIPT \"%s\"",
1011 sieve_queue_send(session, SIEVE_GETSCRIPT, msg, cb, data);
1014 void sieve_session_put_script(SieveSession *session, const gchar *filter_name,
1015 gint len, const gchar *script_contents,
1016 sieve_session_data_cb_fn cb, gpointer data)
1018 /* TODO: refactor so don't have to copy the whole script here */
1019 gchar *msg = g_strdup_printf("PUTSCRIPT \"%s\" {%u+}\r\n%s",
1020 filter_name, len, script_contents);
1022 sieve_queue_send(session, SIEVE_PUTSCRIPT, msg, cb, data);
1025 void sieve_session_check_script(SieveSession *session,
1026 gint len, const gchar *script_contents,
1027 sieve_session_data_cb_fn cb, gpointer data)
1029 gchar *msg = g_strdup_printf("CHECKSCRIPT {%u+}\r\n%s",
1030 len, script_contents);
1032 sieve_queue_send(session, SIEVE_PUTSCRIPT, msg, cb, data);
1035 void sieve_session_delete_script(SieveSession *session,
1036 const gchar *filter_name,
1037 sieve_session_data_cb_fn cb, gpointer data)
1039 gchar *msg = g_strdup_printf("DELETESCRIPT \"%s\"",
1042 sieve_queue_send(session, SIEVE_DELETESCRIPT, msg, cb, data);