/*
* Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2005-2009 DINH Viet Hoa and the Claws Mail team
+ * Copyright (C) 2005-2012 DINH Viet Hoa and the Claws Mail team
*
* 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
#ifdef HAVE_CONFIG_H
# include "config.h"
+#include "claws-features.h"
#endif
#ifdef HAVE_LIBETPAN
+#include <glib.h>
+#include <glib/gi18n.h>
#include "imap-thread.h"
#include <imap.h>
#include <sys/types.h>
#include "ssl_certificate.h"
#include "socket.h"
#include "remotefolder.h"
+#include "tags.h"
#define DISABLE_LOG_DURING_LOGIN
static void delete_imap(Folder *folder, mailimap *imap)
{
chashdatum key;
- chashdatum value;
key.data = &folder;
key.len = sizeof(folder);
- value.data = imap;
- value.len = 0;
chash_delete(session_hash, &key, NULL);
key.data = &imap;
mailstream_network_delay.tv_usec = 0;
}
-void imap_main_done(void)
+void imap_main_done(gboolean have_connectivity)
{
- imap_disconnect_all();
+ imap_disconnect_all(have_connectivity);
etpan_thread_manager_stop(thread_manager);
#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
return;
static int etpan_certificate_check(const unsigned char *certificate, int len, void *data)
{
-#ifdef USE_OPENSSL
- struct connect_param *param = (struct connect_param *)data;
- X509 *cert = NULL;
-
- if (certificate == NULL || len < 0) {
- g_warning("no cert presented.\n");
- return 0;
- }
- cert = d2i_X509(NULL, (const unsigned char **)&certificate, len);
- if (cert == NULL) {
- g_warning("IMAP: can't get cert\n");
- return 0;
- } else if (ssl_certificate_check(cert, NULL,
- (gchar *)param->server, (gushort)param->port) == TRUE) {
- X509_free(cert);
- return 0;
- } else {
- X509_free(cert);
- return -1;
- }
-#elif USE_GNUTLS
+#ifdef USE_GNUTLS
struct connect_param *param = (struct connect_param *)data;
gnutls_x509_crt cert = NULL;
gnutls_datum tmp;
if (gnutls_x509_crt_import(cert, &tmp, GNUTLS_X509_FMT_DER) < 0) {
g_warning("IMAP: can't get cert\n");
return 0;
- } else if (ssl_certificate_check(cert, (guint)-1, NULL,
- (gchar *)param->server, (gushort)param->port) == TRUE) {
+ } else if (ssl_certificate_check(cert, (guint)-1, (gchar *)param->server,
+ (gushort)param->port) == TRUE) {
gnutls_x509_crt_deinit(cert);
return 0;
} else {
static void connect_ssl_context_cb(struct mailstream_ssl_context * ssl_context, void * data)
{
-#if (defined(USE_OPENSSL) || defined(USE_GNUTLS))
+#ifdef USE_GNUTLS
PrefsAccount *account = (PrefsAccount *)data;
const gchar *cert_path = NULL;
const gchar *password = NULL;
-#ifdef USE_OPENSSL
- X509 *x509 = NULL;
- EVP_PKEY *pkey = NULL;
-#else
gnutls_x509_crt x509 = NULL;
gnutls_x509_privkey pkey = NULL;
-#endif
if (account->in_ssl_client_cert_file && *account->in_ssl_client_cert_file)
cert_path = account->in_ssl_client_cert_file;
unsigned char *x509_der = NULL, *pkey_der = NULL;
size_t x509_len, pkey_len;
-#ifndef USE_GNUTLS
- x509_len = (size_t)i2d_X509(x509, &x509_der);
- pkey_len = (size_t)i2d_PrivateKey(pkey, &pkey_der);
-#else
x509_len = (size_t)gnutls_i2d_X509(x509, &x509_der);
pkey_len = (size_t)gnutls_i2d_PrivateKey(pkey, &pkey_der);
-#endif
if (x509_len > 0 && pkey_len > 0) {
if (mailstream_ssl_set_client_certificate_data(ssl_context, x509_der, x509_len) < 0 ||
mailstream_ssl_set_client_private_key_data(ssl_context, pkey_der, pkey_len) < 0)
- log_error(LOG_PROTOCOL, "Impossible to set the client certificate.\n");
+ log_error(LOG_PROTOCOL, _("Impossible to set the client certificate.\n"));
g_free(x509_der);
g_free(pkey_der);
}
-#ifdef USE_GNUTLS
gnutls_x509_crt_deinit(x509);
gnutls_x509_privkey_deinit(pkey);
-#endif
}
#endif
}
struct connect_param param;
struct starttls_result result;
int cert_len;
- unsigned char *certificate;
+ unsigned char *certificate = NULL;
debug_print("imap starttls - begin\n");
flag->fl_flag->fl_type == 6 &&
!strcmp(flag->fl_flag->fl_data.fl_extension, "*"))
*can_create_flags = TRUE;
- if (ok_flags) {
+ if (flag->fl_flag && ok_flags) {
MsgPermFlags c_flag = 0;
switch (flag->fl_flag->fl_type) {
case MAILIMAP_FLAG_ANSWERED:
c_flag = IMAP_FLAG_SEEN;
break;
case MAILIMAP_FLAG_KEYWORD:
- if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "$Forwarded"))
+ if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
c_flag = IMAP_FLAG_FORWARDED;
- if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "Junk"))
+ if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
c_flag = IMAP_FLAG_SPAM;
- if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NonJunk") ||
- !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NoJunk") ||
- !strcasecmp(flag->fl_flag->fl_data.fl_keyword, "NotJunk"))
+ if (!strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
+ !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
+ !strcasecmp(flag->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
c_flag = IMAP_FLAG_HAM;
break;
default:
mailimap * imap;
int type;
struct mailimap_set * set;
+ IMAPSearchKey* key;
};
struct search_result {
int r;
struct mailimap_search_key * key = NULL;
struct mailimap_search_key * uid_key = NULL;
- struct mailimap_search_key * search_type_key;
+ struct mailimap_search_key * search_type_key = NULL;
clist * search_result;
-
+ static const char *charset = NULL;
+
param = op->param;
result = op->result;
} else if (param->type == IMAP_SEARCH_TYPE_SIMPLE) {
uid_key = mailimap_search_key_new_all();
}
- search_type_key = NULL;
switch (param->type) {
case IMAP_SEARCH_TYPE_SIMPLE:
search_type_key = NULL;
break;
-
case IMAP_SEARCH_TYPE_SEEN:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_SEEN,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_READ, NULL, NULL, 0);
break;
-
case IMAP_SEARCH_TYPE_UNSEEN:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_UNSEEN,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_UNREAD, NULL, NULL, 0);
break;
-
case IMAP_SEARCH_TYPE_ANSWERED:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_ANSWERED,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_REPLIED, NULL, NULL, 0);
break;
-
case IMAP_SEARCH_TYPE_FLAGGED:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_FLAGGED,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_MARKED, NULL, NULL, 0);
break;
case IMAP_SEARCH_TYPE_DELETED:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_DELETED,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_DELETED, NULL, NULL, 0);
break;
case IMAP_SEARCH_TYPE_FORWARDED:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
- NULL, NULL, NULL, NULL, NULL,
- strdup("$Forwarded"), NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_FORWARDED, 0);
break;
case IMAP_SEARCH_TYPE_SPAM:
- search_type_key = mailimap_search_key_new(MAILIMAP_SEARCH_KEY_KEYWORD,
- NULL, NULL, NULL, NULL, NULL,
- strdup("Junk"), NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, 0,
- NULL, NULL, NULL, NULL, NULL,
- NULL, 0, NULL, NULL, NULL);
+ search_type_key = imap_search_new(IMAP_SEARCH_CRITERIA_TAG, NULL, RTAG_JUNK, 0);
+ break;
+ case IMAP_SEARCH_TYPE_KEYED:
+ charset = "UTF-8";
+ search_type_key = param->key;
break;
}
} else {
mailstream_logger = imap_logger_uid;
- r = mailimap_uid_search(param->imap, NULL, key, &search_result);
+ r = mailimap_uid_search(param->imap, charset, key, &search_result);
mailstream_logger = imap_logger_cmd;
debug_print("imap search run - end %i\n", result->error);
}
-int imap_threaded_search(Folder * folder, int search_type,
+int imap_threaded_search(Folder * folder, int search_type, IMAPSearchKey* key,
struct mailimap_set * set, clist ** search_result)
{
struct search_param param;
param.imap = imap;
param.set = set;
param.type = search_type;
-
+ param.key = key;
+
threaded_run(folder, ¶m, &result, search_run);
if (result.error != MAILIMAP_NO_ERROR)
}
+struct _IMAPSearchKey {
+ struct mailimap_search_key* key;
+};
+
+IMAPSearchKey* imap_search_new(gint criteria,
+ const gchar *header,
+ const gchar *expr,
+ int value)
+{
+ char* sk_bcc = NULL;
+ struct mailimap_date* sk_before = NULL;
+ char* sk_body = NULL;
+ char* sk_cc = NULL;
+ char* sk_from = NULL;
+ char* sk_keyword = NULL;
+ struct mailimap_date* sk_on = NULL;
+ struct mailimap_date* sk_since = NULL;
+ char* sk_subject = NULL;
+ char* sk_text = NULL;
+ char* sk_to = NULL;
+ char* sk_unkeyword = NULL;
+ char* sk_header_name = NULL;
+ char* sk_header_value = NULL;
+ uint32_t sk_larger = 0;
+ struct mailimap_search_key* sk_not = NULL;
+ struct mailimap_search_key* sk_or1 = NULL;
+ struct mailimap_search_key* sk_or2 = NULL;
+ struct mailimap_date* sk_sentbefore = NULL;
+ struct mailimap_date* sk_senton = NULL;
+ struct mailimap_date* sk_sentsince = NULL;
+ uint32_t sk_smaller = 0;
+ struct mailimap_set* sk_uid = NULL;
+ struct mailimap_set* sk_set = NULL;
+ clist* sk_multiple = NULL;
+ int etpan_matcher_type;
+
+ switch (criteria) {
+ case IMAP_SEARCH_CRITERIA_ALL: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ALL; break;
+ case IMAP_SEARCH_CRITERIA_READ: etpan_matcher_type = MAILIMAP_SEARCH_KEY_SEEN; break;
+ case IMAP_SEARCH_CRITERIA_UNREAD: etpan_matcher_type = MAILIMAP_SEARCH_KEY_UNSEEN; break;
+ case IMAP_SEARCH_CRITERIA_NEW: etpan_matcher_type = MAILIMAP_SEARCH_KEY_NEW; break;
+ case IMAP_SEARCH_CRITERIA_MARKED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_FLAGGED; break;
+ case IMAP_SEARCH_CRITERIA_REPLIED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_ANSWERED; break;
+ case IMAP_SEARCH_CRITERIA_DELETED: etpan_matcher_type = MAILIMAP_SEARCH_KEY_DELETED; break;
+
+ case IMAP_SEARCH_CRITERIA_TAG:
+ sk_keyword = strdup(expr);
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_KEYWORD;
+ break;
+
+ case IMAP_SEARCH_CRITERIA_SUBJECT:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_SUBJECT;
+ sk_subject = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_TO:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_TO;
+ sk_to = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_CC:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_CC;
+ sk_cc = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_AGE_GREATER:
+ case IMAP_SEARCH_CRITERIA_AGE_LOWER:
+ {
+ struct tm tm;
+ time_t limit = time(NULL) - 60 * 60 * 24 * value;
+
+ tzset();
+ localtime_r(&limit, &tm);
+ if (criteria == IMAP_SEARCH_CRITERIA_AGE_GREATER) {
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTBEFORE;
+ sk_sentbefore = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
+ } else {
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_SENTSINCE;
+ sk_sentsince = mailimap_date_new(tm.tm_mday, tm.tm_mon, tm.tm_year + 1900);
+ }
+ break;
+ }
+
+ case IMAP_SEARCH_CRITERIA_BODY:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_BODY;
+ sk_body = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_MESSAGE:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_TEXT;
+ sk_text = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_HEADER:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_HEADER;
+ sk_header_name = strdup(header);
+ sk_header_value = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_FROM:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_FROM;
+ sk_from = strdup(expr);
+ break;
+
+ case IMAP_SEARCH_CRITERIA_SIZE_GREATER:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_LARGER;
+ sk_larger = value;
+ break;
+
+ case IMAP_SEARCH_CRITERIA_SIZE_SMALLER:
+ etpan_matcher_type = MAILIMAP_SEARCH_KEY_SMALLER;
+ sk_smaller = value;
+ break;
+
+ default:
+ return NULL;
+ }
+
+ return mailimap_search_key_new(etpan_matcher_type,
+ sk_bcc, sk_before, sk_body, sk_cc, sk_from, sk_keyword,
+ sk_on, sk_since, sk_subject, sk_text, sk_to,
+ sk_unkeyword, sk_header_name,sk_header_value, sk_larger,
+ sk_not, sk_or1, sk_or2, sk_sentbefore, sk_senton,
+ sk_sentsince, sk_smaller, sk_uid, sk_set, sk_multiple);
+}
+
+IMAPSearchKey* imap_search_not(IMAPSearchKey* key)
+{
+ return mailimap_search_key_new_not(key);
+}
+
+IMAPSearchKey* imap_search_or(IMAPSearchKey* l, IMAPSearchKey* r)
+{
+ return mailimap_search_key_new_or(l, r);
+}
+
+IMAPSearchKey* imap_search_and(IMAPSearchKey* l, IMAPSearchKey* r)
+{
+ IMAPSearchKey* result = mailimap_search_key_new_multiple_empty();
+ mailimap_search_key_multiple_add(result, l);
+ mailimap_search_key_multiple_add(result, r);
+
+ return result;
+}
+
+void imap_search_free(IMAPSearchKey* key)
+{
+ if (!key)
+ return;
+
+ mailimap_search_key_free(key);
+}
+
+
static int imap_get_msg_att_info(struct mailimap_msg_att * msg_att,
uint32_t * puid,
free_list:
imap_fetch_uid_flags_list_free(tab);
- slist_free_strings(tags);
- g_slist_free(tags);
+ slist_free_strings_full(tags);
err:
return res;
}
flags &= ~MSG_NEW;
break;
case MAILIMAP_FLAG_KEYWORD:
- if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "$Forwarded"))
+ if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_FORWARDED))
flags |= MSG_FORWARDED;
- else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "Junk"))
+ else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_JUNK))
flags |= MSG_SPAM;
- else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NonJunk") ||
- !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NoJunk") ||
- !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, "NotJunk"))
+ else if (!strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NON_JUNK) ||
+ !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NO_JUNK) ||
+ !strcasecmp(flag_fetch->fl_flag->fl_data.fl_keyword, RTAG_NOT_JUNK))
flags &= ~MSG_SPAM;
else if (s_tags)
tags = g_slist_prepend(tags, g_strdup(flag_fetch->fl_flag->fl_data.fl_keyword));
carray ** p_env_list)
{
clistiter * cur;
- unsigned int i;
carray * env_list;
- i = 0;
env_list = carray_new(16);
if (fetch_result) {
static int imap_add_envelope_fetch_att(struct mailimap_fetch_type * fetch_type)
{
struct mailimap_fetch_att * fetch_att;
- int r;
+ int i;
char * header;
clist * hdrlist;
struct mailimap_header_list * imap_hdrlist;
struct mailimap_section * section;
+ char *headers[] = {
+ "Date", "From", "To", "Cc", "Subject", "Message-ID",
+ "References", "In-Reply-To", NULL
+ };
hdrlist = clist_new();
-
- header = strdup("Date");
- r = clist_append(hdrlist, header);
- header = strdup("From");
- r = clist_append(hdrlist, header);
- header = strdup("To");
- r = clist_append(hdrlist, header);
- header = strdup("Cc");
- r = clist_append(hdrlist, header);
- header = strdup("Subject");
- r = clist_append(hdrlist, header);
- header = strdup("Message-ID");
- r = clist_append(hdrlist, header);
- header = strdup("References");
- r = clist_append(hdrlist, header);
- header = strdup("In-Reply-To");
- r = clist_append(hdrlist, header);
+ i = 0;
+ while (headers[i] != NULL) {
+ header = strdup(headers[i]);
+ if (header == NULL || clist_append(hdrlist, header) != 0)
+ return MAIL_ERROR_MEMORY;
+ ++i;
+ }
imap_hdrlist = mailimap_header_list_new(hdrlist);
section = mailimap_section_new_header_fields(imap_hdrlist);
void imap_main_init(void)
{
}
-void imap_main_done(void)
+void imap_main_done(gboolean have_connectivity)
{
}
void imap_main_set_timeout(int sec)