Charset conversion
authorMyhailo Danylenko <isbear@ukrpost.net>
Sun, 01 Mar 2009 18:35:43 +0200
changeset 13 0b716ba23b03
parent 12 a52d61f57e0d
child 14 ff13ba17fb6d
Charset conversion
TODO
main.c
--- a/TODO	Sun Mar 01 18:35:16 2009 +0200
+++ b/TODO	Sun Mar 01 18:35:43 2009 +0200
@@ -7,6 +7,7 @@
 do uninitialization of commands and features with objects?
 help system accessors (needs major rewrite and planning)
 object access to roster and buddies
-there are many places with code, that really works only in utf
 in mcabber add hook/call to also handle room status changes
+well, so, mcabber will pass all arguments to hooks in utf. but do we really need to convert names to utf?
+use glib filename charset conversion functions?
 
--- a/main.c	Sun Mar 01 18:35:16 2009 +0200
+++ b/main.c	Sun Mar 01 18:35:43 2009 +0200
@@ -41,12 +41,13 @@
 static int lua_global_print (lua_State *L)
 {
 	lua_concat (L, lua_gettop (L));
-	scr_LogPrint (LPRINT_LOGNORM, lua_tostring (L, -1));
+	scr_LogPrint (LPRINT_LOGNORM | LPRINT_NOTUTF8, lua_tostring (L, -1));
 	return 0;
 }
 
 /// dopath
 /// Loads lua file from default location.
+/// XXX: g_filename_from_utf8?
 /// A: string (filename, without ".lua")
 /// R: string (error message, optional)
 static int lua_global_dopath (lua_State *L)
@@ -78,6 +79,7 @@
 /// main.config_file
 /// Adds mcabber default config location path to config file name.
 /// Note: deprecated, use dopath.
+/// XXX: should we use g_filename_from_utf8?
 /// A: string (filename)
 /// R: string (full path)
 static int lua_main_config_file (lua_State *L)
@@ -115,6 +117,7 @@
 
 /// main.log
 /// Prints message to log.
+/// Note: most likely you need notutf8 flag enabled.
 /// A: log print type, message, message...
 static int lua_main_log (lua_State *L)
 {
@@ -132,21 +135,25 @@
 /// R: string (value, optional)
 static int lua_main_option (lua_State *L)
 {
-	const char *name = luaL_checkstring (L, 1);
+	char *name = to_utf8 (luaL_checkstring (L, 1));
 	if (lua_gettop (L) > 1) { // Set
 		if (lua_type (L, 2) == LUA_TNIL) // Unset
 			settings_del (SETTINGS_TYPE_OPTION, name);
 		else { // Set
-			const char *value = luaL_checkstring (L, 2);
+			char *value = to_utf8 (luaL_checkstring (L, 2));
 			settings_set (SETTINGS_TYPE_OPTION, name, value);
+			g_free (value);
 		}
+		g_free (name);
 		return 0;
 	} else { // Get
-		const char *value = settings_get (SETTINGS_TYPE_OPTION, name);
-		if (value)
+		char *value = from_utf8 (settings_get (SETTINGS_TYPE_OPTION, name));
+		if (value) {
 			lua_pushstring (L, value);
-		else
+			g_free (value);
+		} else
 			lua_pushnil (L);
+		g_free (name);
 		return 1;
 	}
 }
@@ -166,9 +173,13 @@
 /// A: string (jid), string (message)
 static int lua_main_print_info (lua_State *L)
 {
-	char *to = jidtodisp (luaL_checkstring (L, 1));
-	scr_WriteIncomingMessage (to, luaL_checkstring (L, 2), 0, HBB_PREFIX_INFO, 0);
+	char *jid  = to_utf8 (luaL_checkstring (L, 1));
+	char *to   = jidtodisp (jid);
+	char *mesg = to_utf8 (luaL_checkstring (L, 2));
+	scr_WriteIncomingMessage (to, mesg, 0, HBB_PREFIX_INFO, 0);
+	g_free (mesg);
 	g_free (to);
+	g_free (jid);
 	return 0;
 }
 
@@ -204,9 +215,11 @@
 // expects table on top
 static void lua_rosterlist_callback (gpointer buddy, lua_State *L)
 {
+	char *jid = from_utf8 (buddy_getjid (buddy));
 	lua_pushnumber (L, lua_objlen (L, -1) + 1);
-	lua_pushstring (L, buddy_getjid (buddy));
+	lua_pushstring (L, jid);
 	lua_settable (L, -3);
+	g_free (jid);
 }
 
 /// main.roster
@@ -226,12 +239,16 @@
 static int lua_main_current_buddy (lua_State *L)
 {
 	if (lua_gettop (L) > 0) { // Set
+		// XXX: we need not convert to utf, RS works on jids/names in locale charset,
+		// but will jidtodisp always correctly work on such tings?
 		char *jid = jidtodisp (luaL_checkstring (L, 1));
 		scr_RosterSearch (jid);
 		g_free (jid);
 		return 0;
 	} else { // Get
-		lua_pushstring (L, buddy_getjid (BUDDATA (current_buddy)));
+		char *jid = from_utf8 (buddy_getjid (BUDDATA (current_buddy)));
+		lua_pushstring (L, jid);
+		g_free (jid);
 		return 1;
 	}
 }
@@ -246,21 +263,29 @@
 	GList *buddy;
 	GSList *resources;
 	GSList *resource;
-	if (lua_gettop (L) > 0)
-		buddy = buddy_search_jid (luaL_checkstring (L, 1));
-	else
+	if (lua_gettop (L) > 0) {
+		char *jid = from_utf8 (luaL_checkstring (L, 1));
+		buddy = buddy_search_jid (jid);
+		g_free (jid);
+	} else
 		buddy = current_buddy;
 	if (!buddy)
 		return 0;
 	resources = buddy_getresources (BUDDATA (buddy));
 	if (!resources) {
-		lua_pushstring (L, buddy_getjid (BUDDATA (buddy)));
-		return 1;
+		char *loc = from_utf8 (buddy_getjid (BUDDATA (buddy)));
+		lua_pushstring (L, loc);
+		g_free (loc);
+	} else {
+		char *jid = from_utf8 (buddy_getjid (BUDDATA (buddy)));
+		char *res = from_utf8 (g_slist_last (resources)->data);
+		lua_pushfstring (L, "%s%c%s", jid, JID_RESOURCE_SEPARATOR, res);
+		for (resource = resources; resource; resource = g_slist_next (resource))
+			g_free (resource->data);
+		g_slist_free (resources);
+		g_free (jid);
+		g_free (res);
 	}
-	lua_pushfstring (L, "%s%c%s", buddy_getjid (BUDDATA (buddy)), JID_RESOURCE_SEPARATOR, g_slist_last(resources)->data);
-	for (resource = resources; resource; resource = g_slist_next (resource))
-		g_free (resource->data);
-	g_slist_free (resources);
 	return 1;
 }
 
@@ -272,7 +297,9 @@
 // expects table on top!
 static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d)
 {
-	lua_pushstring (d->L, resource);
+	char *loc = from_utf8 (resource);
+	lua_pushstring (d->L, loc);
+	g_free (loc);
 	lua_createtable (d->L, 0, 3);
 	lua_pushstring (d->L, "priority");
 	lua_pushnumber (d->L, buddy_getresourceprio (d->buddy, resource));
@@ -281,7 +308,9 @@
 	lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1);
 	lua_settable    (d->L, -3);
 	lua_pushstring (d->L, "message");
-	lua_pushstring (d->L, buddy_getstatusmsg (d->buddy, resource));
+	loc = from_utf8 (buddy_getstatusmsg (d->buddy, resource));
+	lua_pushstring (d->L, loc);
+	g_free (loc);
 	lua_settable   (d->L, -3);
 	lua_settable (d->L, -3);
 	g_free (resource);
@@ -297,11 +326,13 @@
 /// R: table
 static int lua_main_buddy_info (lua_State *L)
 {
-	char                  *jid   = jidtodisp (luaL_checkstring (L, 1));
+	char                  *loc   = to_utf8 (luaL_checkstring (L, 1));
+	char                  *jid   = jidtodisp (loc);
 	GSList                *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
 	GSList                *resources;
 	lua_state_and_buddy_t  snb;
 	g_free (jid);
+	g_free (loc);
 
 	if (!buddy) {
 		lua_pushnil (L);
@@ -313,7 +344,9 @@
 	luaL_pushenum  (L, buddy_gettype (BUDDATA (buddy)), lua_roster_type);
 	lua_settable   (L, -3);
 	lua_pushstring (L, "name");
-	lua_pushstring (L, buddy_getname (BUDDATA (buddy)));
+	loc = from_utf8 (buddy_getname (BUDDATA (buddy)));
+	lua_pushstring (L, loc);
+	g_free (loc);
 	lua_settable   (L, -3);
 	lua_pushstring  (L, "onserver");
 	lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy)));
@@ -339,9 +372,9 @@
 /// A: string (xmlns)
 static int lua_main_add_feature (lua_State *L)
 {
-	const char *xmlns = luaL_checkstring (L, 1);
+	char *xmlns = to_utf8 (luaL_checkstring (L, 1));
 	xmpp_add_feature (xmlns);
-	lua_added_features = g_slist_prepend (lua_added_features, g_strdup (xmlns));
+	lua_added_features = g_slist_prepend (lua_added_features, xmlns);
 	return 0;
 }
 
@@ -350,8 +383,8 @@
 /// A: stirng (xmlns)
 static int lua_main_del_feature (lua_State *L)
 {
-	const char *xmlns = luaL_checkstring (L, 1);
-	GSList     *el    = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp);
+	char   *xmlns = to_utf8 (luaL_checkstring (L, 1));
+	GSList *el    = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp);
 	xmpp_del_feature (xmlns);
 	if (el) {
 		g_free (el->data);
@@ -415,9 +448,10 @@
 /// A: integer (completion group id), string (word)
 static int lua_main_add_completion (lua_State *L)
 {
-	guint       cid  = luaL_checkinteger (L, 1);
-	const char *word = luaL_checkstring (L, 2);
+	guint  cid  = luaL_checkinteger (L, 1);
+	char  *word = to_utf8 (luaL_checkstring (L, 2)); // XXX
 	compl_add_category_word (cid, word);
+	g_free (word);
 }
 
 /// main.del_completion
@@ -425,9 +459,10 @@
 /// A: integer (completion group id), string (word)
 static int lua_main_del_completion (lua_State *L)
 {
-	guint       cid  = luaL_checkinteger (L, 1);
-	const char *word = luaL_checkstring (L, 2);
+	guint  cid  = luaL_checkinteger (L, 1);
+	char  *word = to_utf8 (luaL_checkstring (L, 2)); // XXX
 	compl_del_category_word (cid, word);
+	g_free (word);
 }
 
 /// main.command
@@ -441,7 +476,7 @@
 /// R: completion type (integer completion group id or string for builtin types, optional)
 static int lua_main_command (lua_State *L)
 {
-	const char             *name = luaL_checkstring (L, 1);
+	const char             *name = luaL_checkstring (L, 1); // XXX: to_utf8? looks like no :/
 	lua_command_callback_t *cb;
 	if (lua_gettop (L) > 1) { // Register
 		guint cid = 0;
@@ -453,8 +488,10 @@
 				if (cid) {
 					lua_pushnil (L);
 					while (lua_next (L, 3)) {
-						compl_add_category_word (cid, luaL_checkstring (L, -1));
+						char *word = to_utf8 (luaL_checkstring (L, -1));
+						compl_add_category_word (cid, word);
 						lua_pop (L, 1);
+						g_free (word);
 					}
 				}
 			} else
@@ -679,9 +716,13 @@
 	}
 	lua_newtable (L);
 	while (arg->name != NULL) {
-		lua_pushstring (L, arg->name);
-		lua_pushstring (L, arg->value);
+		char *name  = from_utf8 (arg->name);
+		char *value = from_utf8 (arg->value);
+		lua_pushstring (L, name);
+		lua_pushstring (L, value);
 		lua_settable (L, -3);
+		g_free (name);
+		g_free (value);
 		arg++;
 	}
 	if (lua_pcall (lua, 1, 0, 0)) {