summary refs log tree commit diff stats
path: root/plugins/fishlim
diff options
context:
space:
mode:
authorPatrick Griffis <tingping@tingping.se>2016-08-29 01:40:58 -0400
committerPatrick Griffis <tingping@tingping.se>2016-10-08 14:48:49 -0400
commitc81c89dc807ec351de8836d1c81395644eb89f28 (patch)
tree08eb976756e4ea23c7d7531f4520f1bf2669dc2c /plugins/fishlim
parent31dd0201146418d66e7edeb351be4e0502f0b8ce (diff)
fishlim: Fix saving nicks containing [ or ]
Part of #1440
Diffstat (limited to 'plugins/fishlim')
-rw-r--r--plugins/fishlim/keystore.c38
1 files changed, 31 insertions, 7 deletions
diff --git a/plugins/fishlim/keystore.c b/plugins/fishlim/keystore.c
index 298b9fa3..6bb04ea6 100644
--- a/plugins/fishlim/keystore.c
+++ b/plugins/fishlim/keystore.c
@@ -63,6 +63,22 @@ static const char *get_keystore_password(void) {
 }
 
 
+static char *escape_nickname(const char *nick) {
+    char *escaped = g_strdup(nick);
+    char *p = escaped;
+
+    while (*p) {
+        if (*p == '[')
+            *p = '~';
+        else if (*p == ']')
+            *p = '!';
+
+        ++p;
+    }
+
+    return escaped;
+}
+
 /**
  * Gets a value for a nick/channel from addon_fishlim.conf. Unlike
  * g_key_file_get_string, this function is case insensitive.
@@ -90,9 +106,13 @@ static gchar *get_nick_value(GKeyFile *keyfile, const char *nick, const char *it
 char *keystore_get_key(const char *nick) {
     /* Get the key */
     GKeyFile *keyfile = getConfigFile();
-    gchar *value = get_nick_value(keyfile, nick, "key");
+    char *escaped_nick = escape_nickname(nick);
+    gchar *value = get_nick_value(keyfile, escaped_nick, "key");
     g_key_file_free(keyfile);
-    if (!value) return NULL;
+    g_free(escaped_nick);
+
+    if (!value)
+        return NULL;
     
     if (strncmp(value, "+OK ", 4) != 0) {
         /* Key is stored in plaintext */
@@ -173,9 +193,10 @@ gboolean keystore_store_key(const char *nick, const char *key) {
     char *wrapped;
     gboolean ok = FALSE;
     GKeyFile *keyfile = getConfigFile();
-    
+    char *escaped_nick = escape_nickname(nick);
+
     /* Remove old key */
-    delete_nick(keyfile, nick);
+    delete_nick(keyfile, escaped_nick);
     
     /* Add new key */
     password = get_keystore_password();
@@ -189,11 +210,11 @@ gboolean keystore_store_key(const char *nick, const char *key) {
         g_free(encrypted);
         
         /* Store encrypted in file */
-        g_key_file_set_string(keyfile, nick, "key", wrapped);
+        g_key_file_set_string(keyfile, escaped_nick, "key", wrapped);
         g_free(wrapped);
     } else {
         /* Store unencrypted in file */
-        g_key_file_set_string(keyfile, nick, "key", key);
+        g_key_file_set_string(keyfile, escaped_nick, "key", key);
     }
     
     /* Save key store file */
@@ -201,6 +222,7 @@ gboolean keystore_store_key(const char *nick, const char *key) {
     
   end:
     g_key_file_free(keyfile);
+    g_free(escaped_nick);
     return ok;
 }
 
@@ -209,13 +231,15 @@ gboolean keystore_store_key(const char *nick, const char *key) {
  */
 gboolean keystore_delete_nick(const char *nick) {
     GKeyFile *keyfile = getConfigFile();
+    char *escaped_nick = escape_nickname(nick);
     
     /* Delete entry */
-    gboolean ok = delete_nick(keyfile, nick);
+    gboolean ok = delete_nick(keyfile, escaped_nick);
     
     /* Save */
     if (ok) save_keystore(keyfile);
     
     g_key_file_free(keyfile);
+    g_free(escaped_nick);
     return ok;
 }