[Telepathy-commits] [telepathy-idle/master] Validate nicks per the IRC RFC

Jonathon Jongsma jonathon.jongsma at collabora.co.uk
Tue Feb 17 13:26:20 PST 2009


Previously we were allowing the nick to consist of any unicode alpha or digit
characters, but the IRC spec (RFC2812 section 2.3.1) is very explicit about
which characters are valid for a nick, and it's only ascii chars.

In addition, we weren't disallowing '-' as the first character of a nickname as
required by the IRC spec.

Added a few additional tests for valid and invalid nicks
---
 src/idle-handles.c                    |   19 ++++++++++---------
 tests/twisted/connect/invalid-nick.py |    8 ++++++++
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/src/idle-handles.c b/src/idle-handles.c
index e334a98..cdf6dec 100644
--- a/src/idle-handles.c
+++ b/src/idle-handles.c
@@ -28,17 +28,13 @@
 #include <telepathy-glib/handle-repo-dynamic.h>
 
 gboolean idle_nickname_is_valid(const gchar *nickname) {
-	gsize len;
-	gunichar ucs4char;
 	const gchar *char_pos = nickname;
 
-	len = g_utf8_strlen(nickname, -1);
-
-	if (!len)
+	/* FIXME: also check for max length? */
+	if (!nickname || *nickname == '\0')
 		return FALSE;
 
-	while (char_pos != NULL) {
-		ucs4char = g_utf8_get_char_validated(char_pos, -1);
+	while (TRUE) {
 
 		switch (*char_pos) {
 			case '[':
@@ -50,7 +46,12 @@ gboolean idle_nickname_is_valid(const gchar *nickname) {
 			case '{':
 			case '|':
 			case '}':
+				break;
+
+			/* - not allowed as first char in a nickname */
 			case '-':
+				if (char_pos == nickname)
+					return FALSE;
 				break;
 
 			case '\0':
@@ -58,12 +59,12 @@ gboolean idle_nickname_is_valid(const gchar *nickname) {
 				break;
 
 			default:
-				if (!(g_unichar_isalpha(ucs4char) || ((char_pos != nickname) && g_unichar_isdigit(ucs4char))))
+				if (!(g_ascii_isalpha(*char_pos) || ((char_pos != nickname) && g_ascii_isdigit(*char_pos))))
 					return FALSE;
 				break;
 		}
 
-		char_pos = g_utf8_find_next_char(char_pos, NULL);
+		++char_pos;
 	}
 
 	return TRUE;
diff --git a/tests/twisted/connect/invalid-nick.py b/tests/twisted/connect/invalid-nick.py
index e79227a..e547b62 100644
--- a/tests/twisted/connect/invalid-nick.py
+++ b/tests/twisted/connect/invalid-nick.py
@@ -51,8 +51,16 @@ def test():
     except dbus.DBusException, e:
         pass    # nick rejected properly with an error
 
+    try:
+        make_connection('-foo') # '-' not allowed as first char
+        raise RuntimeError('Invalid nick not rejected')
+    except dbus.DBusException, e:
+        pass    # nick rejected properly with an error
+
     # should pass succeed without an exception
     make_connection('good_nick')
+    make_connection('good-nick')
+    make_connection('{goodnick]`')
 
 if __name__ == '__main__':
     test()
-- 
1.5.6.5




More information about the telepathy-commits mailing list