Mercurial > pidgin
diff libpurple/protocols/myspace/message.c @ 17968:a2298513db8b
Move msim_escape() and msim_unescape() to message.c.
author | Jeffrey Connelly <jaconnel@calpoly.edu> |
---|---|
date | Sun, 22 Jul 2007 07:00:10 +0000 |
parents | 6d1d86ab6a59 |
children | acff371d7908 |
line wrap: on
line diff
--- a/libpurple/protocols/myspace/message.c Sun Jul 22 06:53:48 2007 +0000 +++ b/libpurple/protocols/myspace/message.c Sun Jul 22 07:00:10 2007 +0000 @@ -22,6 +22,7 @@ #include "myspace.h" #include "message.h" +static gchar *msim_unescape_or_escape(gchar *msg, gboolean escape); static void msim_msg_free_element(gpointer data, gpointer user_data); static void msim_msg_debug_string_element(gpointer data, gpointer user_data); static gchar *msim_msg_pack_using(MsimMessage *msg, GFunc gf, const gchar *sep, const gchar *begin, const gchar *end); @@ -29,6 +30,62 @@ static GList *msim_msg_get_node(MsimMessage *msg, const gchar *name); static MsimMessage *msim_msg_new_v(va_list argp); +/* Replacement codes to be replaced with associated replacement text, + * used for protocol message escaping / unescaping. */ +static gchar* msim_replacement_code[] = { "/1", "/2", /* "/3", */ NULL }; +static gchar* msim_replacement_text[] = { "/", "\\", /* "|", */ NULL }; + +/** + * Unescape or escape a protocol message. + * + * @param msg The message to be unescaped or escaped. WILL BE FREED. + * @param escape TRUE to escape, FALSE to unescape. + * + * @return The unescaped or escaped message. Caller must g_free(). + */ +static gchar * +msim_unescape_or_escape(gchar *msg, gboolean escape) +{ + gchar *tmp, *code, *text; + guint i; + + /* Replace each code in msim_replacement_code with + * corresponding entry in msim_replacement_text. */ + for (i = 0; (code = msim_replacement_code[i]) + && (text = msim_replacement_text[i]); ++i) + { + if (escape) + { + tmp = str_replace(msg, text, code); + } + else + { + tmp = str_replace(msg, code, text); + } + g_free(msg); + msg = tmp; + } + + return msg; +} + +/** + * Escape a protocol message. + * + * @return The escaped message. Caller must g_free(). + */ +gchar * +msim_escape(const gchar *msg) +{ + return msim_unescape_or_escape(g_strdup(msg), TRUE); +} + +gchar * +msim_unescape(const gchar *msg) +{ + return msim_unescape_or_escape(g_strdup(msg), FALSE); +} + /** Create a new MsimMessage. * * @param not_empty FALSE if message is empty, TRUE if variadic arguments follow.