/pidgin/main: 182da0516150: Libpurple: username validation suppo...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Sun Jul 15 07:05:25 EDT 2012


Changeset: 182da05161507f4523db9981e05793a58e1e8531
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-07-15 13:05 +0200
Branch:	 default
URL: http://hg.pidgin.im/pidgin/main/rev/182da0516150

Description:

Libpurple: username validation support; Pidgin: use it in account setup dialog

diffstat:

 libpurple/prpl.h    |  17 +++++++++++++----
 libpurple/util.c    |  22 ++++++++++++++++++++++
 libpurple/util.h    |  11 +++++++++++
 pidgin/gtkaccount.c |   8 +++++---
 4 files changed, 51 insertions(+), 7 deletions(-)

diffs (120 lines):

diff --git a/libpurple/prpl.h b/libpurple/prpl.h
--- a/libpurple/prpl.h
+++ b/libpurple/prpl.h
@@ -467,11 +467,20 @@
 	void (*convo_closed)(PurpleConnection *, const char *who);
 
 	/**
-	 *  Convert the username @a who to its canonical form.  (For example,
-	 *  AIM treats "fOo BaR" and "foobar" as the same user; this function
-	 *  should return the same normalized string for both of those.)
+	 * Convert the username @a who to its canonical form. Also checks for
+	 * validity.
+	 *
+	 * For example, AIM treats "fOo BaR" and "foobar" as the same user; this
+	 * function should return the same normalized string for both of those.
+	 * On the other hand, both of these are invalid for protocols with
+	 * number-based usernames, so function should return NULL in such case.
+	 *
+	 * @param account  The account, that username is related with. Can
+	 *                 be NULL.
+	 * @param who      The username to convert.
+	 * @return         Normalized username, or NULL, if it's invalid.
 	 */
-	const char *(*normalize)(const PurpleAccount *, const char *who);
+	const char *(*normalize)(const PurpleAccount *account, const char *who);
 
 	/**
 	 * Set the buddy icon for the given connection to @a img.  The prpl
diff --git a/libpurple/util.c b/libpurple/util.c
--- a/libpurple/util.c
+++ b/libpurple/util.c
@@ -3487,6 +3487,28 @@
 	return buf;
 }
 
+gboolean
+purple_validate(const PurplePlugin *prpl, const char *str)
+{
+	PurplePluginProtocolInfo *prpl_info;
+	const char *normalized;
+	
+	g_return_val_if_fail(prpl != NULL, FALSE);
+	g_return_val_if_fail(str != NULL, FALSE);
+	
+	if (str[0] == '\0')
+		return FALSE;
+	
+	prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
+	
+	if (!prpl_info->normalize)
+		return TRUE;
+	
+	normalized = prpl_info->normalize(NULL, str);
+	
+	return (NULL != normalized);
+}
+
 gchar *
 purple_strdup_withhtml(const gchar *src)
 {
diff --git a/libpurple/util.h b/libpurple/util.h
--- a/libpurple/util.h
+++ b/libpurple/util.h
@@ -56,6 +56,7 @@
 #include "signals.h"
 #include "xmlnode.h"
 #include "notify.h"
+#include "plugin.h"
 
 
 typedef char *(*PurpleInfoFieldFormatCallback)(const char *field, size_t len);
@@ -960,6 +961,16 @@
 const char *purple_normalize_nocase(const PurpleAccount *account, const char *str);
 
 /**
+ * Checks, if a string is valid.
+ *
+ * @param prpl     The protocol plugin the string belongs to.
+ * @param str      The string to validate.
+ *
+ * @return TRUE, if string is valid, otherwise FALSE.
+ */
+gboolean purple_validate(const PurplePlugin *prpl, const char *str);
+
+/**
  * Compares two strings to see if the first contains the second as
  * a proper prefix.
  *
diff --git a/pidgin/gtkaccount.c b/pidgin/gtkaccount.c
--- a/pidgin/gtkaccount.c
+++ b/pidgin/gtkaccount.c
@@ -286,8 +286,10 @@
 static void
 username_changed_cb(GtkEntry *entry, AccountPrefsDialog *dialog)
 {
-	int opt_noscreenname = (dialog->prpl_info != NULL &&
+	gboolean opt_noscreenname = (dialog->prpl_info != NULL &&
 		(dialog->prpl_info->options & OPT_PROTO_REGISTER_NOSCREENNAME));
+	gboolean username_valid = purple_validate(dialog->plugin,
+		gtk_entry_get_text(entry));
 	
 	if (dialog->ok_button) {
 		if (opt_noscreenname && dialog->register_button &&
@@ -296,14 +298,14 @@
 			gtk_widget_set_sensitive(dialog->ok_button, TRUE);
 		else
 			gtk_widget_set_sensitive(dialog->ok_button,
-				*gtk_entry_get_text(entry) != '\0');
+				username_valid);
 	}
 	if (dialog->register_button) {
 		if (opt_noscreenname)
 			gtk_widget_set_sensitive(dialog->register_button, TRUE);
 		else
 			gtk_widget_set_sensitive(dialog->register_button,
-					*gtk_entry_get_text(entry) != '\0');
+				username_valid);
 	}
 }
 



More information about the Commits mailing list