[gnome-panel] mailcheck applet POP3_SSL patch



Hi, all

This is mailcheck applet patch for POP3s(ssl) support.
I don't know Makefile.am syntax. so just add -lssl ;-)

Regards
ddt

Common subdirectories: gen_util.old/CVS and gen_util/CVS
diff -u --new-file gen_util.old/Makefile.am gen_util/Makefile.am
--- gen_util.old/Makefile.am	2003-01-11 21:41:08.000000000 +0900
+++ gen_util/Makefile.am	2003-01-11 21:46:23.000000000 +0900
@@ -35,11 +35,13 @@
 	mailcheck.h \
 	popcheck.c \
 	popcheck.h \
+	popcheck_ssl.c \
+	popcheck_ssl.h \
 	remote-helper.c \
 	remote-helper.h \
 	$(EGGFILES)
 
-libgen_util_applet_2_la_LDFLAGS = -module -avoid-version
+libgen_util_applet_2_la_LDFLAGS = -module -avoid-version -lssl
 libgen_util_applet_2_la_LIBADD =			\
 	../../libpanel-applet/libpanel-applet-2.la	\
 	$(GEN_UTIL_LIBS)
Common subdirectories: gen_util.old/help and gen_util/help
diff -u --new-file gen_util.old/mailcheck.c gen_util/mailcheck.c
--- gen_util.old/mailcheck.c	2003-01-11 21:41:08.000000000 +0900
+++ gen_util/mailcheck.c	2003-01-11 21:45:38.000000000 +0900
@@ -25,6 +25,7 @@
 #include <libgnomeui/gnome-window-icon.h>
 
 #include "popcheck.h"
+#include "popcheck_ssl.h"
 #include "remote-helper.h"
 #include "mailcheck.h"
 
@@ -34,6 +35,7 @@
 	MAILBOX_LOCAL,
 	MAILBOX_LOCALDIR,
 	MAILBOX_POP3,
+	MAILBOX_POP3_SSL,
 	MAILBOX_IMAP
 } MailboxType;
 
@@ -138,7 +140,7 @@
 	GtkWidget *play_sound_check;
         
 	char *pre_remote_command, *remote_server, *remote_username, *remote_password, *real_password, *remote_folder;
-	MailboxType mailbox_type; /* local = 0; maildir = 1; pop3 = 2; imap = 3 */
+	MailboxType mailbox_type; /* local = 0; maildir = 1; pop3 = 2; pop3_ssl = 3; imap = 4 */
         MailboxType mailbox_type_temp;
 
 	gboolean play_sound;
@@ -272,6 +274,14 @@
 						       mc->remote_server,
 						       mc->remote_username,
 						       mc->real_password);
+	else if (mc->mailbox_type == MAILBOX_POP3_SSL)
+		mc->remote_handle = helper_pop3_ssl_check (got_remote_answer,
+							   mc,
+							   null_remote_handle,
+							   mc->pre_remote_command,
+							   mc->remote_server,
+							   mc->remote_username,
+							   mc->real_password);
 	else if (mc->mailbox_type == MAILBOX_IMAP)
 		helper_imap_check (got_remote_answer,
 				   mc,
@@ -421,6 +431,7 @@
 	int status;
 	
 	if ((mc->mailbox_type == MAILBOX_POP3) || 
+	    (mc->mailbox_type == MAILBOX_POP3_SSL) ||
 	    (mc->mailbox_type == MAILBOX_IMAP)) {
 		if (mc->remote_handle != NULL)
 			/* check in progress */
@@ -1082,6 +1093,7 @@
         make_remote_widgets_sensitive(mc);
         
         if ((mc->mailbox_type != MAILBOX_POP3) &&
+	    (mc->mailbox_type != MAILBOX_POP3_SSL) &&
 	    (mc->mailbox_type != MAILBOX_IMAP) &&
 	    (mc->remote_handle != NULL)) {
 		helper_whack_handle (mc->remote_handle);
@@ -1292,6 +1304,14 @@
 			    GINT_TO_POINTER(MAILBOX_POP3));
         
 	gtk_menu_shell_append (GTK_MENU_SHELL (l2), item);
+	item = gtk_menu_item_new_with_label(_("Remote POP3_SSL-server")); 
+	gtk_widget_show(item);
+	g_object_set_data(G_OBJECT(item), "MailCheck", mc);
+	g_signal_connect (G_OBJECT(item), "activate", 
+			    G_CALLBACK(set_mailbox_selection), 
+			    GINT_TO_POINTER(MAILBOX_POP3_SSL));
+        
+	gtk_menu_shell_append (GTK_MENU_SHELL (l2), item);
 	item = gtk_menu_item_new_with_label(_("Remote IMAP-server")); 
 	gtk_widget_show(item);
 	g_object_set_data(G_OBJECT(item), "MailCheck", mc);
diff -u --new-file gen_util.old/popcheck_ssl.c gen_util/popcheck_ssl.c
--- gen_util.old/popcheck_ssl.c	1970-01-01 09:00:00.000000000 +0900
+++ gen_util/popcheck_ssl.c	2003-01-11 21:48:59.000000000 +0900
@@ -0,0 +1,237 @@
+/* GNOME pop/imap-mail-check-library.
+ * (C) 1997, 1998 The Free Software Foundation
+ *
+ * Author: Lennart Poettering
+ *
+ */
+
+#include "config.h"
+
+#include <netdb.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <openssl/ssl.h>
+
+#include <glib.h>
+
+#include "popcheck_ssl.h"
+
+#define TIMEOUT 5
+static int get_server_port(const char *);
+static char* get_server_hostname(const char *);
+static int connect_socket(const char *, int);
+static char *read_line(SSL *);
+static int write_line(SSL *, char *);
+static int is_pop3_answer_ok(const char *);
+
+static int get_server_port(const char *h)
+ {
+  char *x;
+  x = strchr(h, ':');
+  if (x)
+   {
+    return atoi(x+1);
+   }
+  else
+   return 0;
+ }
+ 
+static char* get_server_hostname(const char *h)
+ {
+  char *e;
+  if (!h) return 0;
+  
+  e = strchr(h, ':');
+  if (e)
+   {
+    char *x;
+    int l = e-h;
+    x = g_malloc(l+5);
+    strncpy(x, h, l);
+    x[l] = 0;
+    return x;
+   }
+  else
+   return strcpy((char*) g_malloc(strlen(h)+1), h); 
+ }
+
+static int connect_socket(const char *h, int def)
+ {
+  struct hostent *he;
+  struct sockaddr_in peer;
+  int fd, p;
+  char *hn;
+  
+  hn = get_server_hostname(h);
+  if (!hn)
+   return -1;
+  
+  p = get_server_port(h); 
+  if (p == 0) p = def;
+
+  he = gethostbyname(hn);
+  g_free(hn);
+  
+  if (!he) 
+   return -1;
+
+  fd = socket(PF_INET, SOCK_STREAM, 0);
+  if (fd < 0) 
+   return -1;
+
+  peer.sin_family = AF_INET;
+  peer.sin_port = htons(p);
+  peer.sin_addr = *(struct in_addr*) he->h_addr;
+
+  if (connect(fd, (struct sockaddr*) &peer, sizeof(peer)) < 0) 
+   {
+    close(fd);
+    return -1;
+   }
+   
+  return fd; 
+ }  
+
+static char *read_line(SSL *ssl)
+ {
+  static char response[1024];
+  char *c;
+  int m = sizeof(response);
+  int s = SSL_get_fd (ssl);
+  int p = 1;
+  
+  c = response;
+  while (m--)
+   {
+    char ch;
+    fd_set fs;
+    struct timeval t;
+    
+    FD_ZERO(&fs);
+    FD_SET(s, &fs);
+    
+    t.tv_sec = TIMEOUT;
+    t.tv_usec = 0;
+    
+    if (select(FD_SETSIZE, &fs, 0, 0, &t) <= 0) 
+     return NULL;
+     
+    while (p)
+	 {
+	  if (SSL_read(ssl, &ch, sizeof(ch)) != sizeof(ch)) 
+	   return NULL;
+     
+      if (ch == 10)
+       {
+        *c = 0;
+        return response;
+       } 
+     
+      *(c++) = ch;
+      p = SSL_pending (ssl);
+      }
+     }    
+   
+  return NULL; 
+ }
+
+static int write_line(SSL *ssl, char *p)
+ {
+  char *p2;
+  p2 = g_malloc(strlen(p)+3);
+  strcat(strcpy(p2, p), "\r\n");
+
+  if (SSL_write(ssl, p2, strlen(p2)) ==  strlen(p2))
+   {
+    g_free(p2);
+    return 1;
+   }
+   
+  g_free(p2); 
+  return 0;
+ }
+
+
+static int is_pop3_answer_ok(const char *p)
+ {
+  if (p) 
+   if (p[0] == '+') return 1;
+  
+  return 0;
+ }
+ 
+int pop3_ssl_check(const char *h, const char* n, const char* e)
+{
+  int s;
+  char *c;
+  char *x;
+  int r = -1, msg = 0, last = 0;
+  SSL *ssl;
+  SSL_CTX *ctx;
+  SSL_METHOD *meth;
+  
+  if (!h || !n || !e) return -1;
+  
+  SSL_library_init ();
+  meth = SSLv2_client_method();
+  ctx = SSL_CTX_new (meth);
+  
+  s = connect_socket(h, 995);
+  ssl = SSL_new (ctx);
+  SSL_set_fd (ssl, s);
+  SSL_connect (ssl);
+  
+  if (s > 0) {
+    if (!is_pop3_answer_ok(read_line(ssl))) {
+      close(s);
+      return -1;
+    }
+
+    c = g_strdup_printf("USER %s", n);
+    if (!write_line(ssl, c) ||
+        !is_pop3_answer_ok(read_line(ssl))) {
+      close(s);
+      g_free(c);
+      return -1;
+    }
+    g_free(c);
+
+    c = g_strdup_printf("PASS %s", e);
+    if (!write_line(ssl, c) ||
+        !is_pop3_answer_ok(read_line(ssl))) {
+      close(s);
+      g_free(c);
+      return -1;
+    }
+    g_free(c);
+
+    if (write_line(ssl, "STAT") &&
+        is_pop3_answer_ok(x = read_line(ssl)) &&
+	x != NULL &&
+        sscanf(x, "%*s %d %*d", &msg) == 1)
+      r = ((unsigned int)msg & 0x0000FFFFL);
+
+    if (r != -1 &&
+        write_line(ssl, "LAST") &&
+        is_pop3_answer_ok(x = read_line(ssl)) &&
+	x != NULL &&
+        sscanf(x, "%*s %d", &last) == 1)
+      r |= (unsigned int)(msg - last) << 17;
+    
+	if (write_line(ssl, "QUIT"))
+      read_line(ssl);
+
+    SSL_shutdown (ssl);
+    close(s);
+    SSL_free (ssl);
+    SSL_CTX_free (ctx);
+  }     
+   
+  return r;
+}
diff -u --new-file gen_util.old/popcheck_ssl.h gen_util/popcheck_ssl.h
--- gen_util.old/popcheck_ssl.h	1970-01-01 09:00:00.000000000 +0900
+++ gen_util/popcheck_ssl.h	2003-01-11 21:48:59.000000000 +0900
@@ -0,0 +1,17 @@
+/* GNOME pop/imap-mail-check-library.
+ * (C) 1997, 1998 The Free Software Foundation
+ *
+ * Author: Lennart Poettering
+ *
+ */
+
+#ifndef _POPCHECK_SSL_H_
+#define _POPCHECK_SSL_H_
+
+/* Returns how many mails are available on POP3-server "h" with username "n" and password "e"
+ * The server-name may be given with or without port-number in form "host:port". 
+ */
+int pop3_ssl_check(const char *h, const char* n, const char* e);
+
+#endif
+
diff -u --new-file gen_util.old/remote-helper.c gen_util/remote-helper.c
--- gen_util.old/remote-helper.c	2003-01-11 21:41:08.000000000 +0900
+++ gen_util/remote-helper.c	2003-01-11 21:48:13.000000000 +0900
@@ -20,6 +20,7 @@
 #include <poll.h>
 
 #include "popcheck.h"
+#include "popcheck_ssl.h"
 
 #include "remote-helper.h"
 
@@ -210,6 +211,40 @@
 }
 
 gpointer
+helper_pop3_ssl_check (RemoteHandler handler, gpointer data,
+		       GDestroyNotify destroy_notify,
+		       const char *command,
+		       const char *h, const char* n, const char* e)
+{
+	RemoteHandlerData *handler_data;
+
+	handler_data = fork_new_handler (handler, data, destroy_notify);
+
+	if (handler_data == NULL) {
+		handler (pop3_ssl_check (h, n, e), data);
+		if (destroy_notify != NULL)
+			destroy_notify (data);
+		return NULL;
+	}
+
+	if (handler_data->pid == 0) {
+		int mails;
+
+		if (command != NULL &&
+		    command[0] != '\0')
+			system (command);
+	       
+		mails = pop3_ssl_check (h, n, e);
+
+		write (handler_data->fd, &mails, sizeof (mails));
+
+		_exit (0);
+	}
+
+	return handler_data;
+}
+
+gpointer
 helper_imap_check (RemoteHandler handler, gpointer data,
 		   GDestroyNotify destroy_notify,
 		   const char *command,
diff -u --new-file gen_util.old/remote-helper.h gen_util/remote-helper.h
--- gen_util.old/remote-helper.h	2003-01-11 21:41:08.000000000 +0900
+++ gen_util/remote-helper.h	2003-01-11 21:47:09.000000000 +0900
@@ -14,6 +14,10 @@
 			    GDestroyNotify destroy_notify,
 			    const char *command,
 			    const char *h, const char* n, const char* e);
+gpointer helper_pop3_ssl_check (RemoteHandler handler, gpointer data,
+				GDestroyNotify destroy_notify,
+				const char *command,
+				const char *h, const char* n, const char* e);
 gpointer helper_imap_check (RemoteHandler handler, gpointer data,
 			    GDestroyNotify destroy_notify,
 			    const char *command,



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