From 397a851038f09c2a40ac70f8dbf04308622c2c02 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Thu, 27 Apr 2000 19:03:54 +0000 Subject: Move text_to_html to e-util. * mail-format.c: Move text_to_html to e-util. * mail-ops.c (send_to_url): New routine. Thin wrapper for e_msg_composer_new_from_url. * mail-display.c (on_link_clicked): print a warning for news or nntp URLs (which we'll deal with some day), and call send_to_url for mailto URLs. svn path=/trunk/; revision=2664 --- mail/ChangeLog | 11 ++- mail/mail-display.c | 9 ++- mail/mail-format.c | 222 ++++------------------------------------------------ mail/mail-ops.c | 44 +++++++++-- mail/mail-ops.h | 1 + 5 files changed, 70 insertions(+), 217 deletions(-) (limited to 'mail') diff --git a/mail/ChangeLog b/mail/ChangeLog index d5dcd9d896..af7b74ab6c 100644 --- a/mail/ChangeLog +++ b/mail/ChangeLog @@ -1,6 +1,15 @@ 2000-04-27 Dan Winship - * mail-format.c (text_to_html): Improve URL converstion code. + * mail-format.c: Move text_to_html to e-util. + + * mail-ops.c (send_to_url): New routine. Thin wrapper for + e_msg_composer_new_from_url. + + * mail-display.c (on_link_clicked): print a warning for news or + nntp URLs (which we'll deal with some day), and call send_to_url + for mailto URLs. + + * mail-format.c (text_to_html): Improve URL conversion code. Recognize https, recognize "www\..*" without a prefixed "http://". Properly escape &, <, >, etc in URL strings. Don't be fooled by "mailto:", "http://", etc with no following data. diff --git a/mail/mail-display.c b/mail/mail-display.c index d7ba658241..036ebad60b 100644 --- a/mail/mail-display.c +++ b/mail/mail-display.c @@ -13,6 +13,7 @@ #include "e-util/e-util.h" #include "mail-display.h" #include "mail-format.h" +#include "mail-ops.h" #define PARENT_TYPE (gtk_vbox_get_type ()) @@ -37,7 +38,13 @@ cid_stream (const char *cid, CamelMimeMessage *message) static void on_link_clicked (GtkHTML *html, const char *url, gpointer user_data) { - gnome_url_show (url); + if (!strncasecmp (url, "news:", 5) || + !strncasecmp (url, "nntp:", 5)) + g_warning ("Can't handle news URLs yet."); + else if (!strncasecmp (url, "mailto:", 7)) + send_to_url (url); + else + gnome_url_show (url); } static void diff --git a/mail/mail-format.c b/mail/mail-format.c index f8d9085d64..9e6e2cc785 100644 --- a/mail/mail-format.c +++ b/mail/mail-format.c @@ -26,6 +26,7 @@ #include "mail-format.h" #include "mail-display.h" #include "camel/hash-table-utils.h" +#include "e-util/e-html-utils.h" #include #include @@ -339,202 +340,6 @@ call_handler_function (CamelMimePart *part, CamelMimeMessage *root, handle_unknown_type (part, root, box); } -int tth_interesting[] = { - 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x00 - 0x0f */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */ - 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* sp - / */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 2, 1, 2, /* 0 - ? */ - 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* @ - O */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, /* P - _ */ - 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* ` - o */ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, /* p - del */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x80 - 0x8f */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x90 - 0x9f */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xa0 - 0xaf */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xb0 - 0xbf */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xc0 - 0xcf */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xd0 - 0xdf */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */ - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 /* 0xf0 - 0xff */ -}; -#define TTH_SPECIAL 1 -#define TTH_PUNCTUATION 2 -#define TTH_ESCAPED 3 -#define TTH_EOF 4 - -#define TEXT_TO_HTML_PRE (1 << 0) -#define TEXT_TO_HTML_CONVERT_NL (1 << 1) -#define TEXT_TO_HTML_CONVERT_SPACES (1 << 2) -#define TEXT_TO_HTML_CONVERT_URLS (1 << 3) - -static char * -check_size (char **buffer, int *buffer_size, char *out, int len) -{ - if (out + len > *buffer + *buffer_size) { - int index = out - *buffer; - - *buffer_size *= 2; - *buffer = g_realloc (*buffer, *buffer_size); - out = *buffer + index; - } - return out; -} - -char * -url_extract (const char **text, gboolean check) -{ - const char *end = *text, *p; - char *out; - - while (*end && !isspace (*end) && *end != '"') - end++; - - /* Back up if we probably went too far. */ - while (end > *text && strchr (",.!?;:>", *(end - 1))) - end--; - - if (check) { - /* Make sure we weren't fooled. */ - p = memchr (*text, ':', end - *text); - if (!p || end - p < 3) - return NULL; - } - - out = g_strndup (*text, end - *text); - *text = end; - return out; -} - -/* Convert plain text into equivalent-looking valid HTML. */ -static char * -text_to_html (const unsigned char *input, unsigned int flags) -{ - const unsigned char *cur = input, *end; - char *buffer = NULL; - char *out = NULL; - int buffer_size = 0; - - /* Allocate a translation buffer. */ - buffer_size = strlen (input) * 2 + 5; - buffer = g_malloc (buffer_size); - - out = buffer; - if (flags & TEXT_TO_HTML_PRE) - out += sprintf (out, "
\n");
-
-	while (*cur) {
-		if (isalpha (*cur) && (flags & TEXT_TO_HTML_CONVERT_URLS)) {
-			char *tmpurl = NULL, *refurl, *dispurl;
-
-			if (!strncasecmp (cur, "http://", 7) ||
-			    !strncasecmp (cur, "https://", 8) ||
-			    !strncasecmp (cur, "ftp://", 6) ||
-			    !strncasecmp (cur, "nntp://", 7) ||
-			    !strncasecmp (cur, "mailto:", 7) ||
-			    !strncasecmp (cur, "news:", 5)) {
-				tmpurl = url_extract (&cur, TRUE);
-				if (tmpurl) {
-					refurl = text_to_html (tmpurl, 0);
-					dispurl = g_strdup (refurl);
-				}
-			} else if (!strncasecmp (cur, "www.", 4) &&
-				   isalnum (*(cur + 4))) {
-				tmpurl = url_extract (&cur, FALSE);
-				dispurl = text_to_html (tmpurl, 0);
-				refurl = g_strdup_printf ("http://%s",
-							  dispurl);
-			}
-
-			if (tmpurl) {
-				out = check_size (&buffer, &buffer_size, out,
-						  strlen (refurl) +
-						  strlen (dispurl) + 15);
-				out += sprintf (out,
-						"%s",
-						refurl, dispurl);
-				g_free (tmpurl);
-				g_free (refurl);
-				g_free (dispurl);
-			}
-		}
-
-		/* Skip until we need to care. */
-		end = cur;
-		while (!tth_interesting[*end] ||
-			(tth_interesting[*end] == TTH_PUNCTUATION &&
-			 !(flags & TEXT_TO_HTML_CONVERT_URLS)))
-			end++;
-
-		out = check_size (&buffer, &buffer_size, out,
-				  end - cur + 10);
-		memcpy (out, cur, end - cur);
-		out += end - cur;
-
-		if (!*end)
-			break;
-		cur = end;
-
-		switch (*cur) {
-		case '<':
-			strcpy (out, "<");
-			out += 4;
-			break;
-
-		case '>':
-			strcpy (out, ">");
-			out += 4;
-			break;
-
-		case '&':
-			strcpy (out, "&");
-			out += 5;
-			break;
-
-		case '"':
-			strcpy (out, """);
-			out += 6;
-			break;
-
-		case '\n':
-			*out++ = *cur;
-			if (flags & TEXT_TO_HTML_CONVERT_NL) {
-				strcpy (out, "
"); - out += 4; - } - break; - - case ' ': - if (flags & TEXT_TO_HTML_CONVERT_SPACES) { - if (cur == input || *(cur + 1) == ' ') { - strcpy (out, " "); - out += 6; - break; - } - } - /* otherwise, FALL THROUGH */ - - default: - if ((*cur >= 0x20 && *cur < 0x80) || - (*cur == '\r' || *cur == '\t')) { - /* Default case, just copy. */ - *out++ = *cur; - } else - out += g_snprintf(out, 9, "&#%d;", *cur); - break; - } - - cur++; - } - - out = check_size (&buffer, &buffer_size, out, 7); - if (flags & TEXT_TO_HTML_PRE) - strcpy (out, "
"); - else - *out = '\0'; - - return buffer; -} - static void write_field_to_stream (const char *description, const char *value, gboolean bold, GtkHTML *html, @@ -545,8 +350,9 @@ write_field_to_stream (const char *description, const char *value, if (value) { unsigned char *p; - encoded_value = text_to_html (value, TEXT_TO_HTML_CONVERT_NL | - TEXT_TO_HTML_CONVERT_URLS); + encoded_value = e_text_to_html (value, + E_TEXT_TO_HTML_CONVERT_NL | + E_TEXT_TO_HTML_CONVERT_URLS); for (p = (unsigned char *)encoded_value; *p; p++) { if (!isprint (*p)) *p = '?'; @@ -686,8 +492,8 @@ handle_text_plain (CamelMimePart *part, CamelMimeMessage *root, GtkBox *box) text = get_data_wrapper_text (wrapper); if (text && *text) { - htmltext = text_to_html (text, TEXT_TO_HTML_CONVERT_URLS); - mail_html_write (html, stream, htmltext); + htmltext = e_text_to_html (text, E_TEXT_TO_HTML_CONVERT_URLS); + mail_html_write (html, stream, "%s", htmltext); g_free (htmltext); } else mail_html_write (html, stream, "(empty)"); @@ -753,10 +559,10 @@ handle_text_plain_flowed (CamelMimePart *part, CamelMimeMessage *root, p++; /* replace '<' with '<', etc. */ - text = text_to_html (p, TEXT_TO_HTML_CONVERT_SPACES | - TEXT_TO_HTML_CONVERT_URLS); + text = e_text_to_html (p, E_TEXT_TO_HTML_CONVERT_SPACES | + E_TEXT_TO_HTML_CONVERT_URLS); if (text && *text) - mail_html_write (html, stream, text); + mail_html_write (html, stream, "%s", text); g_free (text); len = strlen (p); @@ -887,8 +693,10 @@ handle_text_enriched (CamelMimePart *part, CamelMimeMessage *root, GtkBox *box) match = g_hash_table_lookup (translations, copy); g_free (copy); - if (match) - mail_html_write (html, stream, match); + if (match) { + mail_html_write (html, stream, "%s", + match); + } } p = strchr (p, '>'); @@ -1409,8 +1217,8 @@ mail_generate_reply (CamelMimeMessage *message, gboolean to_all) } /* Now convert that to HTML. */ - repl_text = text_to_html (quoted_text, - TEXT_TO_HTML_PRE); + repl_text = e_text_to_html (quoted_text, + E_TEXT_TO_HTML_PRE); g_free (quoted_text); } e_msg_composer_set_body_text (composer, repl_text); diff --git a/mail/mail-ops.c b/mail/mail-ops.c index 81724c4f43..b339bfd746 100644 --- a/mail/mail-ops.c +++ b/mail/mail-ops.c @@ -258,16 +258,32 @@ static void composer_send_cb (EMsgComposer *composer, gpointer data) { CamelMimeMessage *message; - CamelStream *stream; - int stdout_dup; + CamelTransport *transport; + CamelException *ex; + static gboolean loaded = FALSE; message = e_msg_composer_get_message (composer); - - stdout_dup = dup (1); - stream = camel_stream_fs_new_with_fd (stdout_dup); - camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (message), - stream); - camel_stream_close (stream); + camel_mime_message_set_from (message, + "Dan Winship "); + camel_medium_add_header (CAMEL_MEDIUM (message), "X-Mailer", + "Evolution 0.0.0.0.0.0.0.0.0.1"); + camel_mime_message_set_date (message, CAMEL_MESSAGE_DATE_CURRENT, 0); + + if (!loaded) { + camel_provider_register_as_module ("/usr/local/lib/evolution/camel-providers/0.0.1/libcamelsendmail.so"); + loaded = TRUE; + } + ex = camel_exception_new (); + transport = camel_session_get_transport_for_protocol ( + default_session->session, "sendmail", ex); + if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) + g_warning (camel_exception_get_description (ex)); + else { + camel_transport_send (transport, CAMEL_MEDIUM (message), ex); + if (camel_exception_get_id (ex) != CAMEL_EXCEPTION_NONE) + g_warning (camel_exception_get_description (ex)); + gtk_object_destroy (GTK_OBJECT (transport)); + } gtk_object_unref (GTK_OBJECT (message)); gtk_object_destroy (GTK_OBJECT (composer)); @@ -286,6 +302,18 @@ send_msg (GtkWidget *widget, gpointer user_data) gtk_widget_show (composer); } +/* Send according to a mailto (RFC 2368) URL. */ +void +send_to_url (const char *url) +{ + GtkWidget *composer; + + composer = e_msg_composer_new_from_url (url); + + gtk_signal_connect (GTK_OBJECT (composer), "send", + GTK_SIGNAL_FUNC (composer_send_cb), NULL); + gtk_widget_show (composer); +} static void reply (FolderBrowser *fb, gboolean to_all) diff --git a/mail/mail-ops.h b/mail/mail-ops.h index 86b6ac81ff..6206c252cd 100644 --- a/mail/mail-ops.h +++ b/mail/mail-ops.h @@ -1,5 +1,6 @@ void fetch_mail (GtkWidget *button, gpointer user_data); void send_msg (GtkWidget *button, gpointer user_data); +void send_to_url (const char *url); void forward_msg (GtkWidget *button, gpointer user_data); void reply_to_sender (GtkWidget *button, gpointer user_data); void reply_to_all (GtkWidget *button, gpointer user_data); -- cgit