*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
#ifdef HAVE_CONFIG_H
#include <log.h>
#include "etpan-thread-manager.h"
#include "utils.h"
+#include "mainwindow.h"
#include "ssl_certificate.h"
#include "socket.h"
+#include "remotefolder.h"
#define DISABLE_LOG_DURING_LOGIN
static guint thread_manager_signal = 0;
static GIOChannel * io_channel = NULL;
-void delete_imap(Folder *folder, mailimap *imap)
+static void delete_imap(Folder *folder, mailimap *imap)
{
chashdatum key;
chashdatum value;
key.data = &imap;
key.len = sizeof(imap);
chash_delete(courier_workaround_hash, &key, NULL);
- if (imap->imap_stream) {
+ if (imap && imap->imap_stream) {
/* we don't want libetpan to logout */
mailstream_close(imap->imap_stream);
imap->imap_stream = NULL;
int i = 0;
if (size > 8192) {
- log_print("IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
+ log_print(LOG_PROTOCOL, "IMAP4%c [CMD data - %zd bytes]\n", direction?'>':'<', size);
return;
}
buf = malloc(size+1);
lines = g_strsplit(buf, "\n", -1);
while (lines[i] && *lines[i]) {
- log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+ log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
i++;
}
g_strfreev(lines);
gchar **lines;
int i = 0;
- if (size > 8192) {
- log_print("IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
+ if (size > 128 && !direction) {
+ log_print(LOG_PROTOCOL, "IMAP4%c [FETCH data - %zd bytes]\n", direction?'>':'<', size);
return;
}
if (direction != 0 || (buf[0] == '*' && buf[1] == ' ') || size < 32) {
while (lines[i] && *lines[i]) {
- log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+ log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
i++;
}
} else {
- log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+ log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
}
g_strfreev(lines);
free(buf);
int i = 0;
if (size > 8192) {
- log_print("IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
+ log_print(LOG_PROTOCOL, "IMAP4%c [UID data - %zd bytes]\n", direction?'>':'<', size);
return;
}
buf = malloc(size+1);
while (lines[i] && *lines[i]) {
int llen = strlen(lines[i]);
if (llen < 64)
- log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+ log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
else {
gchar tmp[64];
strncpy2(tmp, lines[i], 63);
- log_print("IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
+ log_print(LOG_PROTOCOL, "IMAP4%c %s[... - %zd bytes more]\n", direction?'>':'<', tmp,
llen-64);
}
i++;
free(buf);
}
-void imap_logger_append(int direction, const char * str, size_t size)
+static void imap_logger_append(int direction, const char * str, size_t size)
{
gchar *buf;
gchar **lines;
int i = 0;
if (size > 8192) {
- log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+ log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
return;
} else if (direction == 0 && size > 64) {
- log_print("IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
+ log_print(LOG_PROTOCOL, "IMAP4%c [APPEND data - %zd bytes]\n", direction?'>':'<', size);
return;
}
buf = malloc(size+1);
if (direction == 0 || (buf[0] == '*' && buf[1] == ' ') || size < 64) {
while (lines[i] && *lines[i]) {
- log_print("IMAP4%c %s\n", direction?'>':'<', lines[i]);
+ log_print(LOG_PROTOCOL, "IMAP4%c %s\n", direction?'>':'<', lines[i]);
i++;
}
} else {
- log_print("IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
+ log_print(LOG_PROTOCOL, "IMAP4%c [data - %zd bytes]\n", direction?'>':'<', size);
}
g_strfreev(lines);
free(buf);
static void generic_cb(int cancelled, void * result, void * callback_data)
{
- int * p_finished;
+ struct etpan_thread_op * op;
- p_finished = callback_data;
+ op = (struct etpan_thread_op *) callback_data;
debug_print("generic_cb\n");
-
- * p_finished = 1;
+ if (op->imap && op->imap->imap_response_info &&
+ op->imap->imap_response_info->rsp_alert) {
+ log_error(LOG_PROTOCOL, "IMAP4< Alert: %s\n",
+ op->imap->imap_response_info->rsp_alert);
+ mainwindow_show_error();
+ }
+ op->finished = 1;
}
static void threaded_run(Folder * folder, void * param, void * result,
{
struct etpan_thread_op * op;
struct etpan_thread * thread;
- int finished;
imap_folder_ref(folder);
op = etpan_thread_op_new();
+
+ op->imap = get_imap(folder);
op->param = param;
op->result = result;
op->cancellable = 0;
op->run = func;
op->callback = generic_cb;
- op->callback_data = &finished;
+ op->callback_data = op;
op->cleanup = NULL;
- finished = 0;
+ op->finished = 0;
thread = get_thread(folder);
etpan_thread_op_schedule(thread, op);
- while (!finished) {
+ while (!op->finished) {
gtk_main_iteration();
}
debug_print("imap noop run - end %i\n", r);
}
-int imap_threaded_noop(Folder * folder, unsigned int * p_exists)
+int imap_threaded_noop(Folder * folder, unsigned int * p_exists,
+ unsigned int *p_recent,
+ unsigned int *p_expunge,
+ unsigned int *p_unseen,
+ unsigned int *p_uidnext,
+ unsigned int *p_uidval)
{
struct noop_param param;
struct noop_result result;
imap = get_imap(folder);
param.imap = imap;
-
+
threaded_run(folder, ¶m, &result, noop_run);
- if (imap->imap_selection_info != NULL) {
+ if (result.error == 0 && imap && imap->imap_selection_info != NULL) {
* p_exists = imap->imap_selection_info->sel_exists;
- }
- else {
+ * p_recent = imap->imap_selection_info->sel_recent;
+ * p_unseen = imap->imap_selection_info->sel_unseen;
+ * p_uidnext = imap->imap_selection_info->sel_uidnext;
+ * p_uidval = imap->imap_selection_info->sel_uidvalidity;
+ } else {
* p_exists = 0;
- }
-
- debug_print("imap noop - end\n");
+ * p_recent = 0;
+ * p_unseen = 0;
+ * p_uidnext = 0;
+ * p_uidval = 0;
+ }
+ if (result.error == 0 && imap && imap->imap_response_info != NULL &&
+ imap->imap_response_info->rsp_expunged != NULL) {
+ * p_expunge = clist_count(imap->imap_response_info->rsp_expunged);
+ } else {
+ * p_expunge = 0;
+ }
+ debug_print("imap noop - end [EXISTS %d RECENT %d EXPUNGE %d UNSEEN %d UIDNEXT %d UIDVAL %d]\n",
+ *p_exists, *p_recent, *p_expunge, *p_unseen,
+ *p_uidnext, *p_uidval);
return result.error;
}
debug_print("imap starttls - end\n");
- if (result.error == 0 && !etpan_skip_ssl_cert_check) {
+ if (result.error == 0 && param.imap && !etpan_skip_ssl_cert_check) {
cert_len = (int)mailstream_ssl_get_certificate(param.imap->imap_stream, &certificate);
if (etpan_certificate_check(certificate, cert_len, ¶m) < 0)
result.error = MAILIMAP_ERROR_STREAM;
if (result.error != MAILIMAP_NO_ERROR)
return result.error;
- if (imap->imap_selection_info == NULL)
+ if (!imap || imap->imap_selection_info == NULL)
return MAILIMAP_ERROR_PARSE;
* exists = imap->imap_selection_info->sel_exists;
return result.error;
}
+static void close_run(struct etpan_thread_op * op)
+{
+ struct select_param * param;
+ struct select_result * result;
+ int r;
+
+ param = op->param;
+ result = op->result;
+
+ CHECK_IMAP();
+
+ r = mailimap_close(param->imap);
+
+ result->error = r;
+ debug_print("imap close run - end %i\n", r);
+}
+int imap_threaded_close(Folder * folder)
+{
+ struct select_param param;
+ struct select_result result;
+ mailimap * imap;
+
+ debug_print("imap close - begin\n");
+
+ imap = get_imap(folder);
+ param.imap = imap;
+
+ threaded_run(folder, ¶m, &result, close_run);
+
+ if (result.error != MAILIMAP_NO_ERROR)
+ return result.error;
+
+ debug_print("imap close - end\n");
+
+ return result.error;
+}
struct examine_param {
mailimap * imap;
if (result.error != MAILIMAP_NO_ERROR)
return result.error;
- if (imap->imap_selection_info == NULL)
+ if (!imap || imap->imap_selection_info == NULL)
return MAILIMAP_ERROR_PARSE;
* exists = imap->imap_selection_info->sel_exists;
+static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
+ uint32_t * puid,
+ char ** pheaders,
+ size_t * pref_size,
+ struct mailimap_msg_att_dynamic ** patt_dyn);
static int
-uid_list_to_env_list(clist * fetch_result, carray ** result)
+result_to_uid_list(clist * fetch_result, carray ** result)
{
clistiter * cur;
int r;
int res;
carray * tab;
- unsigned int i;
tab = carray_new(128);
if (tab == NULL) {
for(cur = clist_begin(fetch_result) ; cur != NULL ;
cur = clist_next(cur)) {
struct mailimap_msg_att * msg_att;
- clistiter * item_cur;
uint32_t uid;
- size_t size;
uint32_t * puid;
msg_att = clist_content(cur);
uid = 0;
- size = 0;
- for(item_cur = clist_begin(msg_att->att_list) ;
- item_cur != NULL ;
- item_cur = clist_next(item_cur)) {
- struct mailimap_msg_att_item * item;
-
- item = clist_content(item_cur);
-
- switch (item->att_type) {
- case MAILIMAP_MSG_ATT_ITEM_STATIC:
- switch (item->att_data.att_static->att_type) {
- case MAILIMAP_MSG_ATT_UID:
- uid = item->att_data.att_static->att_data.att_uid;
- break;
- }
- }
- }
+ imap_get_msg_att_info(msg_att, &uid, NULL, NULL, NULL);
puid = malloc(sizeof(* puid));
if (puid == NULL) {
return MAILIMAP_NO_ERROR;
free_list:
- for(i = 0 ; i < carray_count(tab) ; i++)
- mailmessage_free(carray_get(tab, i));
+ imap_fetch_uid_list_free(tab);
err:
return res;
}
goto free_fetch_type;
}
+ mailstream_logger = imap_logger_fetch;
+
r = mailimap_uid_fetch(imap, set,
fetch_type, &fetch_result);
+ mailstream_logger = imap_logger_cmd;
mailimap_fetch_type_free(fetch_type);
mailimap_set_free(set);
}
env_list = NULL;
- r = uid_list_to_env_list(fetch_result, &env_list);
+ r = result_to_uid_list(fetch_result, &env_list);
mailimap_fetch_list_free(fetch_result);
* result = env_list;
CHECK_IMAP();
fetch_result = NULL;
+ mailstream_logger = NULL;
+ log_print(LOG_PROTOCOL, "IMAP4- [fetching UIDs...]\n");
+
r = imap_get_messages_list(param->imap, param->first_index,
&fetch_result);
+ mailstream_logger = imap_logger_cmd;
+
result->error = r;
result->fetch_result = fetch_result;
debug_print("imap fetch_uid run - end %i\n", r);
+
+static int imap_flags_to_flags(struct mailimap_msg_att_dynamic * att_dyn);
+
+static int
+result_to_uid_flags_list(clist * fetch_result, carray ** result)
+{
+ clistiter * cur;
+ int r;
+ int res;
+ carray * tab;
+
+ tab = carray_new(128);
+ if (tab == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto err;
+ }
+
+ for(cur = clist_begin(fetch_result) ; cur != NULL ;
+ cur = clist_next(cur)) {
+ struct mailimap_msg_att * msg_att;
+ uint32_t uid;
+ uint32_t * puid;
+ struct mailimap_msg_att_dynamic * att_dyn;
+ int flags;
+ int * pflags;
+
+ msg_att = clist_content(cur);
+
+ uid = 0;
+ att_dyn = NULL;
+ imap_get_msg_att_info(msg_att, &uid, NULL, NULL, &att_dyn);
+ if (uid == 0)
+ continue;
+ if (att_dyn == NULL)
+ continue;
+
+ flags = imap_flags_to_flags(att_dyn);
+
+ puid = malloc(sizeof(* puid));
+ if (puid == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_list;
+ }
+ * puid = uid;
+
+ r = carray_add(tab, puid, NULL);
+ if (r < 0) {
+ free(puid);
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_list;
+ }
+ pflags = malloc(sizeof(* pflags));
+ if (pflags == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_list;
+ }
+ * pflags = flags;
+ r = carray_add(tab, pflags, NULL);
+ if (r < 0) {
+ free(pflags);
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_list;
+ }
+ }
+
+ * result = tab;
+
+ return MAILIMAP_NO_ERROR;
+
+ free_list:
+ imap_fetch_uid_flags_list_free(tab);
+ err:
+ return res;
+}
+
+static int imap_get_messages_flags_list(mailimap * imap,
+ uint32_t first_index,
+ carray ** result)
+{
+ carray * env_list;
+ int r;
+ struct mailimap_fetch_att * fetch_att;
+ struct mailimap_fetch_type * fetch_type;
+ struct mailimap_set * set;
+ clist * fetch_result;
+ int res;
+
+ set = mailimap_set_new_interval(first_index, 0);
+ if (set == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto err;
+ }
+
+ fetch_type = mailimap_fetch_type_new_fetch_att_list_empty();
+ if (fetch_type == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_set;
+ }
+
+ fetch_att = mailimap_fetch_att_new_flags();
+ if (fetch_att == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_fetch_type;
+ }
+
+ r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
+ if (r != MAILIMAP_NO_ERROR) {
+ mailimap_fetch_att_free(fetch_att);
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_fetch_type;
+ }
+
+ fetch_att = mailimap_fetch_att_new_uid();
+ if (fetch_att == NULL) {
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_fetch_type;
+ }
+
+ r = mailimap_fetch_type_new_fetch_att_list_add(fetch_type, fetch_att);
+ if (r != MAILIMAP_NO_ERROR) {
+ mailimap_fetch_att_free(fetch_att);
+ res = MAILIMAP_ERROR_MEMORY;
+ goto free_fetch_type;
+ }
+
+ mailstream_logger = imap_logger_fetch;
+
+ r = mailimap_uid_fetch(imap, set,
+ fetch_type, &fetch_result);
+
+ mailstream_logger = imap_logger_cmd;
+ mailimap_fetch_type_free(fetch_type);
+ mailimap_set_free(set);
+
+ if (r != MAILIMAP_NO_ERROR) {
+ res = r;
+ goto err;
+ }
+
+ env_list = NULL;
+ r = result_to_uid_flags_list(fetch_result, &env_list);
+ mailimap_fetch_list_free(fetch_result);
+
+ * result = env_list;
+
+ return MAILIMAP_NO_ERROR;
+
+ free_fetch_type:
+ mailimap_fetch_type_free(fetch_type);
+ free_set:
+ mailimap_set_free(set);
+ err:
+ return res;
+}
+
+
+
+static void fetch_uid_flags_run(struct etpan_thread_op * op)
+{
+ struct fetch_uid_param * param;
+ struct fetch_uid_result * result;
+ carray * fetch_result;
+ int r;
+
+ param = op->param;
+ result = op->result;
+
+ CHECK_IMAP();
+
+ fetch_result = NULL;
+ r = imap_get_messages_flags_list(param->imap, param->first_index,
+ &fetch_result);
+
+ result->error = r;
+ result->fetch_result = fetch_result;
+ debug_print("imap fetch_uid run - end %i\n", r);
+}
+
+int imap_threaded_fetch_uid_flags(Folder * folder, uint32_t first_index,
+ carray ** fetch_result)
+{
+ struct fetch_uid_param param;
+ struct fetch_uid_result result;
+ mailimap * imap;
+
+ debug_print("imap fetch_uid - begin\n");
+
+ imap = get_imap(folder);
+ param.imap = imap;
+ param.first_index = first_index;
+
+ mailstream_logger = NULL;
+ log_print(LOG_PROTOCOL, "IMAP4- [fetching flags...]\n");
+
+ threaded_run(folder, ¶m, &result, fetch_uid_flags_run);
+
+ mailstream_logger = imap_logger_cmd;
+
+
+ if (result.error != MAILIMAP_NO_ERROR)
+ return result.error;
+
+ debug_print("imap fetch_uid - end\n");
+
+ * fetch_result = result.fetch_result;
+
+ return result.error;
+}
+
+
+void imap_fetch_uid_flags_list_free(carray * uid_flags_list)
+{
+ unsigned int i;
+
+ for(i = 0 ; i < carray_count(uid_flags_list) ; i ++) {
+ void * data;
+
+ data = carray_get(uid_flags_list, i);
+ free(data);
+ }
+ carray_free(uid_flags_list);
+}
+
+
+
static int imap_fetch(mailimap * imap,
uint32_t msg_index,
char ** result,
goto free_fetch_att;
}
+ mailstream_logger = imap_logger_fetch;
+
r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
+ mailstream_logger = imap_logger_cmd;
mailimap_fetch_type_free(fetch_type);
mailimap_set_free(set);
return MAIL_NO_ERROR;
}
-int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
+static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
{
struct mailimap_fetch_att * fetch_att;
int r;
return MAIL_NO_ERROR;
}
-int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
+static int imap_add_header_fetch_att(struct mailimap_fetch_type * fetch_type)
{
struct mailimap_fetch_att * fetch_att;
struct mailimap_section * section;
else
r = imap_add_header_fetch_att(fetch_type);
+ mailstream_logger = imap_logger_fetch;
+
r = mailimap_uid_fetch(imap, set, fetch_type, &fetch_result);
+ mailstream_logger = imap_logger_cmd;
switch (r) {
case MAILIMAP_NO_ERROR:
break;
return sockfds[0];
}
-int socket_connect_cmd(mailimap * imap, const char * command,
+static int socket_connect_cmd(mailimap * imap, const char * command,
const char * server, int port)
{
int fd;
if (r != MAILIMAP_NO_ERROR_AUTHENTICATED
&& r != MAILIMAP_NO_ERROR_NON_AUTHENTICATED) {
mailstream_close(s);
- imap->imap_stream = NULL;
+ if (imap)
+ imap->imap_stream = NULL;
return r;
}
return result.error;
}
+
+void imap_threaded_cancel(Folder * folder)
+{
+ mailimap * imap;
+
+ imap = get_imap(folder);
+ if (imap->imap_stream != NULL)
+ mailstream_cancel(imap->imap_stream);
+}
+
#else
void imap_main_init(void)
{
}
+void imap_threaded_cancel(Folder * folder);
+{
+}
+
#endif