changeset 9045:38d022e5eb19

[gaim-migrate @ 9821] nosnilmot wrote: " This patch prevents turning strings with '@' in them into mailto: links if they are not valid email addresses. An example would be copying from a shell something like: [user@host dir]$ thing would create a mailto link to "[user@host" It adds a gaim_email_is_valid function to util.c which could also be used elsewhere :) Thanks to Alver on #gaim for reporting this buglet." And also said: "Updated patch attached, tested against 86,171 valid email addresses and as many invalid addresses as I could make up" committer: Tailor Script <tailor@pidgin.im>
author Tim Ringenbach <marv@pidgin.im>
date Sun, 23 May 2004 22:17:38 +0000
parents 23bcfdcd530d
children c307cf4c84d2
files src/util.c src/util.h
diffstat 2 files changed, 56 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/util.c	Sun May 23 22:16:25 2004 +0000
+++ b/src/util.c	Sun May 23 22:17:38 2004 +0000
@@ -1518,8 +1518,12 @@
 						*d = '\0';
 
 					tmpurlbuf = gaim_unescape_html(url_buf);
-					g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
-							tmpurlbuf, url_buf);
+					if (gaim_email_is_valid(tmpurlbuf)) {
+						g_string_append_printf(ret, "<A HREF=\"mailto:%s\">%s</A>",
+								tmpurlbuf, url_buf);
+					} else {
+						g_string_append(ret, url_buf);
+					}
 					g_free(tmpurlbuf);
 					c = t;
 
@@ -2583,6 +2587,47 @@
 	return buf;
 }
 
+/* lifted from http://www.oreillynet.com/pub/a/network/excerpt/spcookbook_chap03/index3.html */
+gboolean
+gaim_email_is_valid(const char *address)
+{
+	int count = 0;
+	const char *c, *domain;
+	static char *rfc822_specials = "()<>@,;:\\\"[]";
+
+	/* first we validate the name portion (name@domain) */
+	for (c = address;  *c;  c++) {
+		if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
+			while (*++c) {
+				if (*c == '\"') break;
+				if (*c == '\\' && (*++c == ' ')) continue;
+				if (*c <= ' ' || *c >= 127) return FALSE;
+			}
+			if (!*c++) return FALSE;
+			if (*c == '@') break;
+			if (*c != '.') return FALSE;
+			continue;
+		}
+		if (*c == '@') break;
+		if (*c <= ' ' || *c >= 127) return FALSE;
+		if (strchr(rfc822_specials, *c)) return FALSE;
+	}
+	if (c == address || *(c - 1) == '.') return FALSE;
+
+	/* next we validate the domain portion (name@domain) */
+	if (!*(domain = ++c)) return FALSE;
+	do {
+		if (*c == '.') {
+			if (c == domain || *(c - 1) == '.') return FALSE;
+			count++;
+		}
+		if (*c <= ' ' || *c >= 127) return FALSE;
+		if (strchr(rfc822_specials, *c)) return FALSE;
+	} while (*++c);
+
+	return (count >= 1 ? TRUE : FALSE);
+}
+
 /**************************************************************************
  * UTF8 String Functions
  **************************************************************************/
--- a/src/util.h	Sun May 23 22:16:25 2004 +0000
+++ b/src/util.h	Sun May 23 22:17:38 2004 +0000
@@ -579,6 +579,15 @@
  */
 const char *gaim_url_encode(const char *str);
 
+/**
+ * Checks if the given email address is syntactically valid.
+ *
+ * @param address The email address to validate.
+ *
+ * @return True if the email address is syntactically correct.
+ */
+gboolean gaim_email_is_valid(const char *address);
+
 /*@}*/
 
 /**************************************************************************