# HG changeset patch # User Tim Ringenbach # Date 1085350658 0 # Node ID 38d022e5eb19dbdaf1f0397f644ffb7112eda779 # Parent 23bcfdcd530daae6eb6af13aa16a3301af03564c [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 diff -r 23bcfdcd530d -r 38d022e5eb19 src/util.c --- 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, "%s", - tmpurlbuf, url_buf); + if (gaim_email_is_valid(tmpurlbuf)) { + g_string_append_printf(ret, "%s", + 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 **************************************************************************/ diff -r 23bcfdcd530d -r 38d022e5eb19 src/util.h --- 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); + /*@}*/ /**************************************************************************