[Telepathy-commits] [telepathy-idle/master] Expose the nick validation function and use it in the CM

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


Add a filter for the 'account' param of the RequestConnection method so that it
can check if the passed nickname is valid for an IRC nick and reject it if not.
Previously, telepathy-idle happily accepted invalid nicknames and then didn't
provide any decent feedback about what was wrong when the 'login' failed.
---
 src/idle-connection-manager.c |   18 +++++++++++++++++-
 src/idle-handles.c            |    4 ++--
 src/idle-handles.h            |    1 +
 3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/idle-connection-manager.c b/src/idle-connection-manager.c
index 72b9b09..fbd7ccd 100644
--- a/src/idle-connection-manager.c
+++ b/src/idle-connection-manager.c
@@ -25,6 +25,7 @@
 #include <telepathy-glib/enums.h>
 
 #include "idle-connection.h"
+#include "idle-handles.h" /* to check for valid nick */
 
 G_DEFINE_TYPE(IdleConnectionManager, idle_connection_manager, TP_TYPE_BASE_CONNECTION_MANAGER)
 
@@ -71,8 +72,23 @@ enum {
 	LAST_PARAM
 };
 
+gboolean
+filter_nick(const TpCMParamSpec *paramspec, GValue *value, GError **error)
+{
+	g_assert(value);
+	g_assert(G_VALUE_HOLDS_STRING(value));
+
+	const gchar* nick = g_value_get_string (value);
+	if (!idle_nickname_is_valid(nick)) {
+		g_set_error(error, TP_ERRORS, TP_ERROR_INVALID_HANDLE, "Invalid account name '%s'", nick);
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
 static const TpCMParamSpec _params[] = {
-	{"account", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, TP_CONN_MGR_PARAM_FLAG_REQUIRED, NULL, G_STRUCT_OFFSET(Params, account)},
+	{"account", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, TP_CONN_MGR_PARAM_FLAG_REQUIRED, NULL, G_STRUCT_OFFSET(Params, account), filter_nick},
 	{"server",  DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, TP_CONN_MGR_PARAM_FLAG_REQUIRED, NULL, G_STRUCT_OFFSET(Params, server)},
 	{"port", DBUS_TYPE_UINT16_AS_STRING, G_TYPE_UINT, TP_CONN_MGR_PARAM_FLAG_HAS_DEFAULT, GINT_TO_POINTER(6667), G_STRUCT_OFFSET(Params, port)},
 	{"password", DBUS_TYPE_STRING_AS_STRING, G_TYPE_STRING, 0, NULL, G_STRUCT_OFFSET(Params, password)},
diff --git a/src/idle-handles.c b/src/idle-handles.c
index 2dd9ea3..e334a98 100644
--- a/src/idle-handles.c
+++ b/src/idle-handles.c
@@ -27,7 +27,7 @@
 #include <telepathy-glib/errors.h>
 #include <telepathy-glib/handle-repo-dynamic.h>
 
-static gboolean _nickname_is_valid(const gchar *nickname) {
+gboolean idle_nickname_is_valid(const gchar *nickname) {
 	gsize len;
 	gunichar ucs4char;
 	const gchar *char_pos = nickname;
@@ -108,7 +108,7 @@ static gboolean _channelname_is_valid(const gchar *channel) {
 }
 
 static gchar *_nick_normalize_func(TpHandleRepoIface *repo, const gchar *id, gpointer ctx, GError **error) {
-	if (!_nickname_is_valid(id)) {
+	if (!idle_nickname_is_valid(id)) {
 		g_set_error(error, TP_ERRORS, TP_ERROR_INVALID_HANDLE, "invalid nickname");
 		return NULL;
 	}
diff --git a/src/idle-handles.h b/src/idle-handles.h
index 516ad9c..24e277b 100644
--- a/src/idle-handles.h
+++ b/src/idle-handles.h
@@ -27,6 +27,7 @@
 G_BEGIN_DECLS
 
 void idle_handle_repos_init(TpHandleRepoIface **handles);
+gboolean idle_nickname_is_valid(const gchar *nickname);
 
 G_END_DECLS
 
-- 
1.5.6.5




More information about the telepathy-commits mailing list