PATCH: Fix pop3 mailcheck



Hi,

 I revisited the pop3 fetching code and came up with a way to properly sort
pop3 into the inbox (that one was a fixme, anyway).

Well, pop3 mailboxes are special in that they never contain any messages.
The exist in the object structure of Balsa, but they are not mailboxes as
other types are mailboxes.

Pop3 "mailboxes" cannot ever be in a balsa-mblist because there is no
associated storage. In order to do a check, they have to use another
mailbox's storage. Libmutt delivers pop3 mail as an mbox-formatted text
file, so the obvious solution was to append that text to the system spool.
As long as there is no remote storage involved, this is just fine, even
though it does already defeat the purpose of being able to set the inbox to
an arbitrary mailbox. Even a maildir-inbox cannot be used with pop3, it will
alsways go to the mbox-formatted system mail spool.
The proper solution would have been to extend the pop3 mailbox class in such
a ways that it _does_ have associated storage, however, I'm not good enough
at Balsa coding yet to attempt that.

This is the approach I have taken:
A new function, libbalsa_mailbox_pop3_set_inbox(), is used to tell the pop3
mailbox where the received mail is to go.
In order to convert the received mail into the proper format, I chose the
easy way: Pop3 mail is spooled to a file in /tmp, then a temporary mailbox
is created, using the (also new) function
libbalsa_mailbox_new_local_from_data().
That mailbox references the spool file in /tmp, so that
libbalsa_messages_move() can be used to move the entire contents to the
previously set inbox. After that, the now empty temporary mailbox is
deleted, along with the temporary file.

Melanie
diff -b -B -r -u -P --exclude-from=ignore ../balsa-cvs/libbalsa/mailbox.c ./libbalsa/mailbox.c
--- ../balsa-cvs/libbalsa/mailbox.c	Sun Aug 19 19:45:48 2001
+++ ./libbalsa/mailbox.c	Tue Aug 21 08:41:03 2001
@@ -296,6 +296,39 @@
 	(*GTK_OBJECT_CLASS(parent_class)->destroy) (GTK_OBJECT(object));
 }
 
+/* Create a new mailbox */
+LibBalsaMailbox *
+libbalsa_mailbox_new_local_from_data(gint type, gchar *path, gchar *name)
+{
+    LibBalsaMailbox *mailbox;
+
+    if (type == 0) {
+	libbalsa_information(LIBBALSA_INFORMATION_WARNING,
+			     _("No such mailbox type: %d"), type);
+	gnome_config_pop_prefix();
+	return NULL;
+    }
+
+    /* Handle Local mailboxes. 
+     * They are now separate classes for each type 
+     * FIXME: This should be removed in som efuture release.
+     */
+    if ( type == LIBBALSA_TYPE_MAILBOX_LOCAL ) {
+	type = libbalsa_mailbox_type_from_path(path);
+    } else {
+	libbalsa_information(LIBBALSA_INFORMATION_WARNING,
+			     _("Cannot create mailbox type from data: %d"), type);
+	}
+    mailbox = LIBBALSA_MAILBOX(libbalsa_mailbox_local_new(path, FALSE));
+    if (mailbox == NULL)
+	libbalsa_information(LIBBALSA_INFORMATION_WARNING,
+			     _("Could not create a mailbox of type %d"),
+			     type);
+
+	mailbox->name=g_strdup(name);
+    return mailbox;
+}
+
 /* Create a new mailbox by loading it from a config entry... */
 LibBalsaMailbox *
 libbalsa_mailbox_new_from_config(const gchar * prefix)
diff -b -B -r -u -P --exclude-from=ignore ../balsa-cvs/libbalsa/mailbox.h ./libbalsa/mailbox.h
--- ../balsa-cvs/libbalsa/mailbox.h	Sun Aug 19 19:45:48 2001
+++ ./libbalsa/mailbox.h	Tue Aug 21 08:41:14 2001
@@ -131,6 +131,8 @@
 GtkType libbalsa_mailbox_get_type(void);
 
 LibBalsaMailbox *libbalsa_mailbox_new_from_config(const gchar * prefix);
+LibBalsaMailbox *libbalsa_mailbox_new_local_from_data(gint type, gchar *path,
+		gchar *name);
 
 /* 
  * open and close a mailbox 
diff -b -B -r -u -P --exclude-from=ignore ../balsa-cvs/libbalsa/mailbox_pop3.c ./libbalsa/mailbox_pop3.c
--- ../balsa-cvs/libbalsa/mailbox_pop3.c	Sun Aug 19 19:45:48 2001
+++ ./libbalsa/mailbox_pop3.c	Tue Aug 21 08:42:08 2001
@@ -30,6 +30,10 @@
 #include "libbalsa_private.h"
 #include "mailbackend.h"
 #include "pop3.h"
+#include "mailbox.h"
+#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
 
 #ifdef BALSA_USE_THREADS
 #include "threads.h"
@@ -109,6 +113,7 @@
     LibBalsaMailboxRemote *remote;
     mailbox->check = FALSE;
     mailbox->delete_from_server = FALSE;
+	mailbox->inbox = NULL;
 
     remote = LIBBALSA_MAILBOX_REMOTE(mailbox);
     remote->server =
@@ -194,12 +199,14 @@
 
 /* libbalsa_mailbox_pop3_check:
    checks=downloads POP3 mail. 
-   FIXME: uses mutt's Spoolfile.
 */
 static void
 libbalsa_mailbox_pop3_check(LibBalsaMailbox * mailbox)
 {
     gchar uid[80];
+	gchar *tmp_path;
+	gint tmp_file;
+	LibBalsaMailbox *tmp_mailbox;
     PopStatus status;
     LibBalsaMailboxPop3 *m = LIBBALSA_MAILBOX_POP3(mailbox);
     LibBalsaServer *server;
@@ -226,9 +233,22 @@
 	strncpy(uid, m->last_popped_uid, sizeof(uid));
     else uid[0] = '\0';
 
+	tmp_path=g_strdup("/tmp/pop-XXXXX");
+	sprintf(tmp_path, "/tmp/pop-%d", getpid());
+	tmp_file=open(tmp_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+	if(tmp_file == -1) {
+		libbalsa_information(LIBBALSA_INFORMATION_WARNING,
+				 _("POP3 mailbox %s temp file error:\n%s"), 
+				 mailbox->name,
+				 sys_errlist[errno]);
+		g_free(tmp_path);
+		return;
+	}
+	close(tmp_file);
+
     status =  LIBBALSA_MAILBOX_POP3(mailbox)->filter 
 	? libbalsa_fetch_pop_mail_filter (m, progress_cb, uid)
-	: libbalsa_fetch_pop_mail_direct (m, Spoolfile, progress_cb, uid);
+	: libbalsa_fetch_pop_mail_direct (m, tmp_path, progress_cb, uid);
 
     if(status != POP_OK)
 	libbalsa_information(LIBBALSA_INFORMATION_WARNING,
@@ -256,8 +277,28 @@
 #endif
     } 
 
+	tmp_mailbox=libbalsa_mailbox_new_local_from_data(
+			LIBBALSA_TYPE_MAILBOX_LOCAL, tmp_path, "POPTMP");
+	if(!tmp_mailbox)  {
+		libbalsa_information(LIBBALSA_INFORMATION_WARNING,
+				 _("POP3 mailbox %s temp mailbox error:\n"), 
+				 mailbox->name);
+		g_free(tmp_path);
+		return;
+	}
     /* Regrab the gdk lock before leaving */
     gdk_threads_enter();
+	
+	libbalsa_mailbox_open(tmp_mailbox);
+	if(m->inbox && tmp_mailbox->messages) {
+		libbalsa_messages_move(tmp_mailbox->message_list, m->inbox);
+	}
+	libbalsa_mailbox_close(tmp_mailbox);
+	gtk_object_destroy(GTK_OBJECT(tmp_mailbox));
+	unlink(tmp_path);
+
+	/* FIXME: Is this needed here? */
+	g_free(tmp_path);
 }
 
 
@@ -337,4 +378,16 @@
     if (LIBBALSA_MAILBOX_CLASS(parent_class)->load_config)
 	LIBBALSA_MAILBOX_CLASS(parent_class)->load_config(mailbox, prefix);
 
+}
+void
+libbalsa_mailbox_pop3_set_inbox(LibBalsaMailbox *mailbox,
+		LibBalsaMailbox *inbox)
+{
+    LibBalsaMailboxPop3 *pop;
+
+    g_return_if_fail(LIBBALSA_IS_MAILBOX_POP3(mailbox));
+
+    pop = LIBBALSA_MAILBOX_POP3(mailbox);
+
+	pop->inbox=inbox;
 }
diff -b -B -r -u -P --exclude-from=ignore ../balsa-cvs/libbalsa/mailbox_pop3.h ./libbalsa/mailbox_pop3.h
--- ../balsa-cvs/libbalsa/mailbox_pop3.h	Sun Aug 19 19:45:48 2001
+++ ./libbalsa/mailbox_pop3.h	Tue Aug 21 07:46:27 2001
@@ -42,6 +42,7 @@
     gchar *last_popped_uid;
     gboolean use_apop;
     gboolean filter; /* filter through procmail? */
+	LibBalsaMailbox *inbox;
 };
 
 struct _LibBalsaMailboxPop3Class {
@@ -49,5 +50,6 @@
 };
 
 GtkObject *libbalsa_mailbox_pop3_new(void);
+void libbalsa_mailbox_pop3_set_inbox(LibBalsaMailbox *mailbox, LibBalsaMailbox *inbox);
 
 #endif				/* __LIBBALSA_MAILBOX_POP3_H__ */
diff -b -B -r -u -P --exclude-from=ignore ../balsa-cvs/src/main-window.c ./src/main-window.c
--- ../balsa-cvs/src/main-window.c	Sun Aug 19 19:45:48 2001
+++ ./src/main-window.c	Tue Aug 21 07:47:11 2001
@@ -1450,6 +1450,7 @@
 	mailbox = BALSA_MAILBOX_NODE(list->data)->mailbox;
 
 	gdk_threads_enter();
+	libbalsa_mailbox_pop3_set_inbox(mailbox, balsa_app.inbox);
 	libbalsa_mailbox_check(mailbox);
 	gdk_threads_leave();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]