2005-10-04 [colin] 1.9.15cvs9
[claws.git] / src / etpan / imap-thread.c
index 71e0da9ca5340d0619fe2806528318a40d151329..97dee7c1f8d182e0ba7440c8c3ff813bab3ac696 100644 (file)
@@ -8,6 +8,9 @@
 #include <imap.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#if (defined (__NetBSD__) || defined (__FreeBSD__))
+#include <sys/socket.h>
+#endif
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/wait.h>
@@ -39,7 +42,7 @@ static gboolean thread_manager_event(GIOChannel * source,
 void imap_logger(int direction, const char * str, size_t size) 
 {
        gchar buf[512];
-       strncpy(buf, str, 511);
+       strncpy(buf, str, size > 510 ? 510:size);
        buf[511] = '\0';
        if (size < 511)
                buf[size] = '\0';
@@ -317,10 +320,54 @@ int imap_threaded_connect_ssl(Folder * folder, const char * server, int port)
        
        threaded_run(folder, &param, &result, connect_ssl_run);
        
-       debug_print("connect ok\n");
+       debug_print("connect %d\n", result.error);
        
        return result.error;
 }
+
+struct capa_param {
+       mailimap * imap;
+};
+
+struct capa_result {
+       int error;
+       struct mailimap_capability_data *caps;
+};
+
+static void capability_run(struct etpan_thread_op * op)
+{
+       int r;
+       struct capa_param * param;
+       struct capa_result * result;
+       struct mailimap_capability_data *caps;
+
+       param = op->param;
+       result = op->result;
+       
+       r = mailimap_capability(param->imap, &caps);
+       
+       result->error = r;
+       result->caps = (r == 0 ? caps : NULL);
+}
+
+
+struct mailimap_capability_data * imap_threaded_capability(Folder *folder)
+{
+       struct capa_param param;
+       struct capa_result result;
+       mailimap *imap;
+       
+       imap = get_imap(folder);
+       
+       param.imap = imap;
+       
+       threaded_run(folder, &param, &result, capability_run);
+       
+       debug_print("capa ok\n");
+       
+       return result.caps;
+       
+}
        
 struct disconnect_param {
        mailimap * imap;
@@ -435,6 +482,7 @@ struct login_param {
        mailimap * imap;
        const char * login;
        const char * password;
+       const char * type;
 };
 
 struct login_result {
@@ -456,10 +504,14 @@ static void login_run(struct etpan_thread_op * op)
        old_debug = mailstream_debug;
        mailstream_debug = 0;
 #endif
-       
-       r = mailimap_login(param->imap,
+       if (!strcmp(param->type, "LOGIN"))
+               r = mailimap_login(param->imap,
                           param->login, param->password);
-       
+       else 
+               r = mailimap_authenticate(param->imap,
+                       param->type, NULL, NULL, NULL,
+                       param->login, param->login,
+                       param->password, NULL);
 #ifdef DISABLE_LOG_DURING_LOGIN
        mailstream_debug = old_debug;
 #endif
@@ -470,7 +522,8 @@ static void login_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_login(Folder * folder,
-                       const char * login, const char * password)
+                       const char * login, const char * password,
+                       const char * type)
 {
        struct login_param param;
        struct login_result result;
@@ -480,7 +533,8 @@ int imap_threaded_login(Folder * folder,
        param.imap = get_imap(folder);
        param.login = login;
        param.password = password;
-       
+       param.type = type;
+
        threaded_run(folder, &param, &result, login_run);
        
        debug_print("imap login - end\n");
@@ -518,7 +572,8 @@ static void status_run(struct etpan_thread_op * op)
 }
 
 int imap_threaded_status(Folder * folder, const char * mb,
-                        struct mailimap_mailbox_data_status ** data_status)
+                        struct mailimap_mailbox_data_status ** data_status,
+                        guint mask)
 {
        struct status_param param;
        struct status_result result;
@@ -527,17 +582,26 @@ int imap_threaded_status(Folder * folder, const char * mb,
        debug_print("imap status - begin\n");
        
        status_att_list = mailimap_status_att_list_new_empty();
-       mailimap_status_att_list_add(status_att_list,
+       if (mask & 1 << 0) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_MESSAGES);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 1) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_RECENT);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 2) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UIDNEXT);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 3) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UIDVALIDITY);
-       mailimap_status_att_list_add(status_att_list,
+       }
+       if (mask & 1 << 4) {
+               mailimap_status_att_list_add(status_att_list,
                                     MAILIMAP_STATUS_ATT_UNSEEN);
-       
+       }
        param.imap = get_imap(folder);
        param.mb = mb;
        param.status_att_list = status_att_list;
@@ -632,13 +696,13 @@ static void starttls_run(struct etpan_thread_op * op)
                fd = mailstream_low_get_fd(plain_low);
                if (fd == -1) {
                        debug_print("imap starttls run - can't get fd\n");
-                       result->error = MAIL_ERROR_STREAM;
+                       result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
                tls_low = mailstream_low_ssl_open(fd);
                if (tls_low == NULL) {
                        debug_print("imap starttls run - can't ssl_open\n");
-                       result->error = MAIL_ERROR_STREAM;
+                       result->error = MAILIMAP_ERROR_STREAM;
                        return;
                }
                mailstream_low_free(plain_low);
@@ -976,6 +1040,14 @@ static void search_run(struct etpan_thread_op * op)
                                                          NULL, NULL, NULL, NULL, NULL,
                                                          NULL, 0, NULL, NULL, NULL);
                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);
+               break;
        }
        
        if (search_type_key != NULL) {
@@ -1003,7 +1075,7 @@ int imap_threaded_search(Folder * folder, int search_type,
        mailimap * imap;
        
        debug_print("imap search - begin\n");
-       
+
        imap = get_imap(folder);
        param.imap = imap;
        param.set = set;
@@ -1486,7 +1558,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
                
                r = fclose(f);
                if (r == EOF) {
-                       unlink(param->filename);
+                       g_unlink(param->filename);
                        goto close;
                }
                goto free;
@@ -1497,7 +1569,7 @@ static void fetch_content_run(struct etpan_thread_op * op)
        close:
                close(fd);
        unlink:
-               unlink(param->filename);
+               g_unlink(param->filename);
        
        free:
                if (mmap_string_unref(content) != 0)
@@ -2122,33 +2194,54 @@ int imap_threaded_store(Folder * folder, struct mailimap_set * set,
 }
 
 
+#define ENV_BUFFER_SIZE 512
 
 static void do_exec_command(int fd, const char * command,
                            const char * servername, uint16_t port)
 {
        int i, maxopen;
-  
+#ifdef SOLARIS
+       char env_buffer[ENV_BUFFER_SIZE];
+#endif
+       
        if (fork() > 0) {
                /* Fork again to become a child of init rather than
                   the etpan client. */
                exit(0);
        }
   
+#ifdef SOLARIS
+       if (servername)
+               snprintf(env_buffer, ENV_BUFFER_SIZE,
+                        "ETPANSERVER=%s", servername);
+       else
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANSERVER=");
+       putenv(env_buffer);
+#else
        if (servername)
                setenv("ETPANSERVER", servername, 1);
        else
                unsetenv("ETPANSERVER");
+#endif
   
+#ifdef SOLARIS
+       if (port)
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=%d", port);
+       else
+               snprintf(env_buffer, ENV_BUFFER_SIZE, "ETPANPORT=");
+       putenv(env_buffer);
+#else
        if (port) {
                char porttext[20];
-    
+               
                snprintf(porttext, sizeof(porttext), "%d", port);
                setenv("ETPANPORT", porttext, 1);
        }
        else {
                unsetenv("ETPANPORT");
        }
-  
+#endif
+               
        /* Not a lot we can do if there's an error other than bail. */
        if (dup2(fd, 0) == -1)
                exit(1);