about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-06-18 00:32:36 +0100
committerJames Booth <boothj5@gmail.com>2014-06-18 00:32:36 +0100
commit7344d3b8987f6e2edb12a3861a07006e27fb80e5 (patch)
treee6a037f4c4c4b4083731c6f73adff66e9db6cb35
parent79ddf1048486d1a77a1aee9b8152c8ffb25d8a11 (diff)
downloadprofani-tty-7344d3b8987f6e2edb12a3861a07006e27fb80e5.tar.gz
Added deallocation function for string prefs, added otr policy enum
-rw-r--r--src/command/command.c12
-rw-r--r--src/command/commands.c20
-rw-r--r--src/config/preferences.c20
-rw-r--r--src/config/preferences.h1
-rw-r--r--src/otr/otr.c33
-rw-r--r--src/otr/otr.h8
-rw-r--r--src/otr/otrlibv4.c4
-rw-r--r--src/profanity.c16
-rw-r--r--src/server_events.c21
-rw-r--r--src/ui/console.c32
-rw-r--r--src/ui/core.c7
11 files changed, 113 insertions, 61 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 611ce7db..6882109f 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -1413,13 +1413,11 @@ cmd_execute_default(const char * const inp)
                 ui_current_print_line("You are not currently connected.");
             } else {
 #ifdef HAVE_LIBOTR
-                char *policy = otr_get_policy(recipient);
-                if ((strcmp(policy, "always") == 0) && !otr_is_secure(recipient)) {
+                prof_otrpolicy_t policy = otr_get_policy(recipient);
+                if (policy == PROF_OTRPOLICY_ALWAYS && !otr_is_secure(recipient)) {
                     cons_show_error("Failed to send message. Please check OTR policy");
-                    free(policy);
                     return TRUE;
                 }
-                free(policy);
                 if (otr_is_secure(recipient)) {
                     char *encrypted = otr_encrypt_message(recipient, inp);
                     if (encrypted != NULL) {
@@ -1428,11 +1426,13 @@ cmd_execute_default(const char * const inp)
                         if (prefs_get_boolean(PREF_CHLOG)) {
                             const char *jid = jabber_get_fulljid();
                             Jid *jidp = jid_create(jid);
-                            if (strcmp(prefs_get_string(PREF_OTR_LOG), "on") == 0) {
+                            char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
+                            if (strcmp(pref_otr_log, "on") == 0) {
                                 chat_log_chat(jidp->barejid, recipient, inp, PROF_OUT_LOG, NULL);
-                            } else if (strcmp(prefs_get_string(PREF_OTR_LOG), "redact") == 0) {
+                            } else if (strcmp(pref_otr_log, "redact") == 0) {
                                 chat_log_chat(jidp->barejid, recipient, "[redacted]", PROF_OUT_LOG, NULL);
                             }
+                            prefs_free_string(pref_otr_log);
                             jid_destroy(jidp);
                         }
 
diff --git a/src/command/commands.c b/src/command/commands.c
index aae32cb6..f979da41 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -1003,23 +1003,25 @@ cmd_msg(gchar **args, struct cmd_help_t help)
                     if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
                         const char *jid = jabber_get_fulljid();
                         Jid *jidp = jid_create(jid);
-                        if (strcmp(prefs_get_string(PREF_OTR_LOG), "on") == 0) {
+                        char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
+                        if (strcmp(pref_otr_log, "on") == 0) {
                             chat_log_chat(jidp->barejid, usr_jid, msg, PROF_OUT_LOG, NULL);
-                        } else if (strcmp(prefs_get_string(PREF_OTR_LOG), "redact") == 0) {
+                        } else if (strcmp(pref_otr_log, "redact") == 0) {
                             chat_log_chat(jidp->barejid, usr_jid, "[redacted]", PROF_OUT_LOG, NULL);
                         }
+                        prefs_free_string(pref_otr_log);
                         jid_destroy(jidp);
                     }
                 } else {
                     cons_show_error("Failed to encrypt and send message,");
                 }
             } else {
-                char *policy = otr_get_policy(usr_jid);
+                prof_otrpolicy_t policy = otr_get_policy(usr_jid);
 
-                if (strcmp(policy, "always") == 0) {
+                if (policy == PROF_OTRPOLICY_ALWAYS) {
                     cons_show_error("Failed to send message. Please check OTR policy");
                     return TRUE;
-                } else if (strcmp(policy, "opportunistic") == 0) {
+                } else if (policy == PROF_OTRPOLICY_OPPORTUNISTIC) {
                     char *otr_base_tag = OTRL_MESSAGE_TAG_BASE;
                     char *otr_v2_tag = OTRL_MESSAGE_TAG_V2;
                     int N = strlen(otr_base_tag) + strlen(otr_v2_tag) + strlen(msg) + 1;
@@ -1033,7 +1035,6 @@ cmd_msg(gchar **args, struct cmd_help_t help)
                     message_send(msg, usr_jid);
                 }
                 ui_outgoing_msg("me", usr_jid, msg);
-                free(policy);
 
                 if (((win_type == WIN_CHAT) || (win_type == WIN_CONSOLE)) && prefs_get_boolean(PREF_CHLOG)) {
                     const char *jid = jabber_get_fulljid();
@@ -1956,11 +1957,13 @@ cmd_tiny(gchar **args, struct cmd_help_t help)
                         if (prefs_get_boolean(PREF_CHLOG)) {
                             const char *jid = jabber_get_fulljid();
                             Jid *jidp = jid_create(jid);
-                            if (strcmp(prefs_get_string(PREF_OTR_LOG), "on") == 0) {
+                            char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
+                            if (strcmp(pref_otr_log, "on") == 0) {
                                 chat_log_chat(jidp->barejid, recipient, tiny, PROF_OUT_LOG, NULL);
-                            } else if (strcmp(prefs_get_string(PREF_OTR_LOG), "redact") == 0) {
+                            } else if (strcmp(pref_otr_log, "redact") == 0) {
                                 chat_log_chat(jidp->barejid, recipient, "[redacted]", PROF_OUT_LOG, NULL);
                             }
+                            prefs_free_string(pref_otr_log);
                             jid_destroy(jidp);
                         }
 
@@ -2723,6 +2726,7 @@ cmd_otr(gchar **args, struct cmd_help_t help)
         if (args[1] == NULL) {
             char *policy = prefs_get_string(PREF_OTR_POLICY);
             cons_show("OTR policy is now set to: %s", policy);
+            prefs_free_string(policy);
             return TRUE;
         }
 
diff --git a/src/config/preferences.c b/src/config/preferences.c
index 335915aa..49203bfb 100644
--- a/src/config/preferences.c
+++ b/src/config/preferences.c
@@ -164,20 +164,30 @@ prefs_get_string(preference_t pref)
     const char *key = _get_key(pref);
     char *def = _get_default_string(pref);
 
-    if (!g_key_file_has_key(prefs, group, key, NULL)) {
-        return def;
-    }
-
     char *result = g_key_file_get_string(prefs, group, key, NULL);
 
     if (result == NULL) {
-        return strdup(def);
+        if (def != NULL) {
+            return strdup(def);
+        } else {
+            return NULL;
+        }
     } else {
         return result;
     }
 }
 
 void
+prefs_free_string(char *pref)
+{
+    if (pref != NULL) {
+        free(pref);
+    }
+    pref = NULL;
+}
+
+
+void
 prefs_set_string(preference_t pref, char *value)
 {
     const char *group = _get_group(pref);
diff --git a/src/config/preferences.h b/src/config/preferences.h
index 3de860d2..cdb0f2de 100644
--- a/src/config/preferences.h
+++ b/src/config/preferences.h
@@ -115,6 +115,7 @@ void prefs_free_aliases(GList *aliases);
 gboolean prefs_get_boolean(preference_t pref);
 void prefs_set_boolean(preference_t pref, gboolean value);
 char * prefs_get_string(preference_t pref);
+void prefs_free_string(char *pref);
 void prefs_set_string(preference_t pref, char *value);
 
 #endif
diff --git a/src/otr/otr.c b/src/otr/otr.c
index 7a5733bf..b6b68dcd 100644
--- a/src/otr/otr.c
+++ b/src/otr/otr.c
@@ -520,43 +520,56 @@ _otr_get_their_fingerprint(const char * const recipient)
     }
 }
 
-static char *
+static prof_otrpolicy_t
 _otr_get_policy(const char * const recipient)
 {
     ProfAccount *account = accounts_get_account(jabber_get_account_name());
     // check contact specific setting
     if (g_list_find_custom(account->otr_manual, recipient, (GCompareFunc)g_strcmp0)) {
         account_free(account);
-        return strdup("manual");
+        return PROF_OTRPOLICY_MANUAL;
     }
     if (g_list_find_custom(account->otr_opportunistic, recipient, (GCompareFunc)g_strcmp0)) {
         account_free(account);
-        return strdup("opportunistic");
+        return PROF_OTRPOLICY_OPPORTUNISTIC;
     }
     if (g_list_find_custom(account->otr_always, recipient, (GCompareFunc)g_strcmp0)) {
         account_free(account);
-        return strdup("always");
+        return PROF_OTRPOLICY_ALWAYS;
     }
 
     // check default account setting
     if (account->otr_policy != NULL) {
-        char *result;
+        prof_otrpolicy_t result;
         if (g_strcmp0(account->otr_policy, "manual") == 0) {
-            result = "manual";
+            result = PROF_OTRPOLICY_MANUAL;
         }
         if (g_strcmp0(account->otr_policy, "opportunistic") == 0) {
-            result = "opportunistic";
+            result = PROF_OTRPOLICY_OPPORTUNISTIC;
         }
         if (g_strcmp0(account->otr_policy, "always") == 0) {
-            result = "always";
+            result = PROF_OTRPOLICY_ALWAYS;
         }
         account_free(account);
-        return strdup(result);
+        return result;
     }
     account_free(account);
 
     // check global setting
-    return prefs_get_string(PREF_OTR_POLICY);
+    char *pref_otr_policy = prefs_get_string(PREF_OTR_POLICY);
+
+    // pref defaults to manual
+    prof_otrpolicy_t result = PROF_OTRPOLICY_MANUAL;
+
+    if (strcmp(pref_otr_policy, "opportunistic") == 0) {
+        result = PROF_OTRPOLICY_OPPORTUNISTIC;
+    } else if (strcmp(pref_otr_policy, "always") == 0) {
+        result = PROF_OTRPOLICY_ALWAYS;
+    }
+
+    prefs_free_string(pref_otr_policy);
+
+    return result;
 }
 
 static char *
diff --git a/src/otr/otr.h b/src/otr/otr.h
index f3000786..a8553280 100644
--- a/src/otr/otr.h
+++ b/src/otr/otr.h
@@ -28,6 +28,12 @@
 
 #include "config/accounts.h"
 
+typedef enum {
+    PROF_OTRPOLICY_MANUAL,
+    PROF_OTRPOLICY_OPPORTUNISTIC,
+    PROF_OTRPOLICY_ALWAYS
+} prof_otrpolicy_t;
+
 void otr_init_module(void);
 
 OtrlUserState otr_userstate(void);
@@ -63,6 +69,6 @@ char * (*otr_decrypt_message)(const char * const from, const char * const messag
 
 void (*otr_free_message)(char *message);
 
-char * (*otr_get_policy)(const char * const recipient);
+prof_otrpolicy_t (*otr_get_policy)(const char * const recipient);
 
 #endif
diff --git a/src/otr/otrlibv4.c b/src/otr/otrlibv4.c
index 0bcd779c..a91bbe43 100644
--- a/src/otr/otrlibv4.c
+++ b/src/otr/otrlibv4.c
@@ -93,7 +93,7 @@ cb_otr_error_message_free(void *opdata, const char *err_msg)
 static void
 cb_timer_control(void *opdata, unsigned int interval)
 {
-    current_interval = interval; 
+    current_interval = interval;
 }
 
 static void
@@ -139,7 +139,7 @@ cb_handle_smp_event(void *opdata, OtrlSMPEvent smp_event,
                 ui_smp_answer_success(context->username);
             }
             break;
-            
+
         case OTRL_SMPEVENT_FAILURE:
             if (context->smstate->received_question == 0) {
                 if (nextMsg == OTRL_SMP_EXPECT3) {
diff --git a/src/profanity.c b/src/profanity.c
index 899e70c4..bcf4f93c 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -82,9 +82,7 @@ prof_run(const int disable_tls, char *log_level, char *account_name)
         snprintf(inp, sizeof(inp), "%s %s", cmd, pref_connect_account);
         process_input(inp);
     }
-    if (pref_connect_account != NULL) {
-        free(pref_connect_account);
-    }
+    prefs_free_string(pref_connect_account);
 
     while(cmd_result == TRUE) {
         wint_t ch = ERR;
@@ -255,12 +253,8 @@ _handle_idle_time()
             }
         }
     }
-    if (pref_autoaway_mode != NULL) {
-        free(pref_autoaway_mode);
-    }
-    if (pref_autoaway_message != NULL) {
-        free(pref_autoaway_message);
-    }
+    prefs_free_string(pref_autoaway_mode);
+    prefs_free_string(pref_autoaway_message);
 }
 
 static void
@@ -285,9 +279,9 @@ _init(const int disable_tls, char *log_level)
     chat_log_init();
     groupchat_log_init();
     accounts_load();
-    gchar *theme = prefs_get_string(PREF_THEME);
+    char *theme = prefs_get_string(PREF_THEME);
     theme_init(theme);
-    g_free(theme);
+    prefs_free_string(theme);
     ui_init();
     jabber_init(disable_tls);
     cmd_init();
diff --git a/src/server_events.c b/src/server_events.c
index 7182af87..e30e86c4 100644
--- a/src/server_events.c
+++ b/src/server_events.c
@@ -229,12 +229,12 @@ handle_incoming_message(char *from, char *message, gboolean priv)
     gboolean was_decrypted = FALSE;
     char *newmessage;
 
-    char *policy = otr_get_policy(from);
+    prof_otrpolicy_t policy = otr_get_policy(from);
     char *whitespace_base = strstr(message,OTRL_MESSAGE_TAG_BASE);
 
     if (!priv) {
         //check for OTR whitespace (opportunistic or always)
-        if (strcmp(policy, "opportunistic") == 0 || strcmp(policy, "always") == 0) {
+        if (policy == PROF_OTRPOLICY_OPPORTUNISTIC || policy == PROF_OTRPOLICY_ALWAYS) {
             if (whitespace_base) {
                 if (strstr(message, OTRL_MESSAGE_TAG_V2) || strstr(message, OTRL_MESSAGE_TAG_V1)) {
                     // Remove whitespace pattern for proper display in UI
@@ -254,18 +254,16 @@ handle_incoming_message(char *from, char *message, gboolean priv)
 
         // internal OTR message
         if (newmessage == NULL) {
-            free(policy);
             return;
         }
     } else {
         newmessage = message;
     }
-    if (strcmp(policy, "always") == 0 && !was_decrypted && !whitespace_base) {
+    if (policy == PROF_OTRPOLICY_ALWAYS && !was_decrypted && !whitespace_base) {
         char *otr_query_message = otr_start_query();
         cons_show("Attempting to start OTR session...");
         message_send(otr_query_message, from);
     }
-    free(policy);
 
     ui_incoming_msg(from, newmessage, NULL, priv);
 
@@ -274,11 +272,13 @@ handle_incoming_message(char *from, char *message, gboolean priv)
         const char *jid = jabber_get_fulljid();
         Jid *jidp = jid_create(jid);
 
-        if (!was_decrypted || (strcmp(prefs_get_string(PREF_OTR_LOG), "on") == 0)) {
+        char *pref_otr_log = prefs_get_string(PREF_OTR_LOG);
+        if (!was_decrypted || (strcmp(pref_otr_log, "on") == 0)) {
             chat_log_chat(jidp->barejid, from_jid->barejid, newmessage, PROF_IN_LOG, NULL);
-        } else if (strcmp(prefs_get_string(PREF_OTR_LOG), "redact") == 0) {
+        } else if (strcmp(pref_otr_log, "redact") == 0) {
             chat_log_chat(jidp->barejid, from_jid->barejid, "[redacted]", PROF_IN_LOG, NULL);
         }
+        prefs_free_string(pref_otr_log);
 
         jid_destroy(jidp);
         jid_destroy(from_jid);
@@ -394,6 +394,8 @@ handle_contact_offline(char *barejid, char *resource, char *status)
                 }
             }
         }
+        prefs_free_string(show_console);
+        prefs_free_string(show_chat_win);
         jid_destroy(jid);
     }
 }
@@ -433,6 +435,8 @@ handle_contact_online(char *barejid, Resource *resource,
                 }
             }
         }
+        prefs_free_string(show_console);
+        prefs_free_string(show_chat_win);
     }
 }
 
@@ -493,6 +497,7 @@ handle_room_member_presence(const char * const room,
             ui_room_member_presence(room, nick, show, status);
             ui_current_page_off();
         }
+        prefs_free_string(muc_status_pref);
     }
 }
 
@@ -508,6 +513,7 @@ handle_room_member_online(const char * const room, const char * const nick,
         ui_room_member_online(room, nick, show, status);
         ui_current_page_off();
     }
+    prefs_free_string(muc_status_pref);
 }
 
 void
@@ -521,6 +527,7 @@ handle_room_member_offline(const char * const room, const char * const nick,
         ui_room_member_offline(room, nick);
         ui_current_page_off();
     }
+    prefs_free_string(muc_status_pref);
 }
 
 void
diff --git a/src/ui/console.c b/src/ui/console.c
index 3d26dcd9..3c0f6828 100644
--- a/src/ui/console.c
+++ b/src/ui/console.c
@@ -927,12 +927,13 @@ _cons_show_aliases(GList *aliases)
 static void
 _cons_theme_setting(void)
 {
-    gchar *theme = prefs_get_string(PREF_THEME);
+    char *theme = prefs_get_string(PREF_THEME);
     if (theme == NULL) {
         cons_show("Theme (/theme)                : default");
     } else {
         cons_show("Theme (/theme)                : %s", theme);
     }
+    prefs_free_string(theme);
 }
 
 static void
@@ -965,10 +966,13 @@ _cons_splash_setting(void)
 static void
 _cons_autoconnect_setting(void)
 {
-    if (prefs_get_string(PREF_CONNECT_ACCOUNT) != NULL)
-        cons_show("Autoconnect (/autoconnect)      : %s", prefs_get_string(PREF_CONNECT_ACCOUNT));
+    char *pref_connect_account = prefs_get_string(PREF_CONNECT_ACCOUNT);
+    if (pref_connect_account != NULL)
+        cons_show("Autoconnect (/autoconnect)      : %s", pref_connect_account);
     else
         cons_show("Autoconnect (/autoconnect)      : OFF");
+
+    prefs_free_string(pref_connect_account);
 }
 
 static void
@@ -999,6 +1003,10 @@ _cons_statuses_setting(void)
     cons_show("Console statuses (/statuses)  : %s", console);
     cons_show("Chat statuses (/statuses)     : %s", chat);
     cons_show("MUC statuses (/statuses)      : %s", muc);
+
+    prefs_free_string(console);
+    prefs_free_string(chat);
+    prefs_free_string(muc);
 }
 
 static void
@@ -1058,6 +1066,7 @@ _cons_notify_setting(void)
     } else {
     cons_show    ("Room messages (/notify room)        : %s", room_setting);
     }
+    prefs_free_string(room_setting);
 
     if (prefs_get_boolean(PREF_NOTIFY_ROOM_CURRENT))
         cons_show("Room current (/notify room)         : ON");
@@ -1234,20 +1243,24 @@ _cons_show_log_prefs(void)
 static void
 _cons_autoaway_setting(void)
 {
-    if (strcmp(prefs_get_string(PREF_AUTOAWAY_MODE), "off") == 0) {
+    char *pref_autoaway_mode = prefs_get_string(PREF_AUTOAWAY_MODE);
+    if (strcmp(pref_autoaway_mode, "off") == 0) {
         cons_show("Autoaway (/autoaway mode)            : OFF");
     } else {
-        cons_show("Autoaway (/autoaway mode)            : %s", prefs_get_string(PREF_AUTOAWAY_MODE));
+        cons_show("Autoaway (/autoaway mode)            : %s", pref_autoaway_mode);
     }
+    prefs_free_string(pref_autoaway_mode);
 
     cons_show("Autoaway minutes (/autoaway time)    : %d minutes", prefs_get_autoaway_time());
 
-    if ((prefs_get_string(PREF_AUTOAWAY_MESSAGE) == NULL) ||
-            (strcmp(prefs_get_string(PREF_AUTOAWAY_MESSAGE), "") == 0)) {
+    char *pref_autoaway_message = prefs_get_string(PREF_AUTOAWAY_MESSAGE);
+    if ((pref_autoaway_message == NULL) ||
+            (strcmp(pref_autoaway_message, "") == 0)) {
         cons_show("Autoaway message (/autoaway message) : OFF");
     } else {
-        cons_show("Autoaway message (/autoaway message) : \"%s\"", prefs_get_string(PREF_AUTOAWAY_MESSAGE));
+        cons_show("Autoaway message (/autoaway message) : \"%s\"", pref_autoaway_message);
     }
+    prefs_free_string(pref_autoaway_message);
 
     if (prefs_get_boolean(PREF_AUTOAWAY_CHECK)) {
         cons_show("Autoaway check (/autoaway check)     : ON");
@@ -1328,6 +1341,7 @@ _cons_show_otr_prefs(void)
 
     char *policy_value = prefs_get_string(PREF_OTR_POLICY);
     cons_show("OTR policy (/otr policy) : %s", policy_value);
+    prefs_free_string(policy_value);
 
     if (prefs_get_boolean(PREF_OTR_WARN)) {
         cons_show("Warn non-OTR (/otr warn) : ON");
@@ -1336,7 +1350,6 @@ _cons_show_otr_prefs(void)
     }
 
     char *log_value = prefs_get_string(PREF_OTR_LOG);
-
     if (strcmp(log_value, "on") == 0) {
         cons_show("OTR logging (/otr log)   : ON");
     } else if (strcmp(log_value, "off") == 0) {
@@ -1344,6 +1357,7 @@ _cons_show_otr_prefs(void)
     } else {
         cons_show("OTR logging (/otr log)   : Redacted");
     }
+    prefs_free_string(log_value);
 
     if (wins_is_current(console)) {
         win_update_virtual(console);
diff --git a/src/ui/core.c b/src/ui/core.c
index b6a0ab9c..84fcebfd 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -436,12 +436,13 @@ _ui_group_removed(const char * const contact, const char * const group)
 static void
 _ui_auto_away(void)
 {
-    if (prefs_get_string(PREF_AUTOAWAY_MESSAGE) != NULL) {
+    char *pref_autoaway_message = prefs_get_string(PREF_AUTOAWAY_MESSAGE);
+    if (pref_autoaway_message != NULL) {
         int pri =
             accounts_get_priority_for_presence_type(jabber_get_account_name(),
                 RESOURCE_AWAY);
         cons_show("Idle for %d minutes, status set to away (priority %d), \"%s\".",
-            prefs_get_autoaway_time(), pri, prefs_get_string(PREF_AUTOAWAY_MESSAGE));
+            prefs_get_autoaway_time(), pri, pref_autoaway_message);
         title_bar_set_presence(CONTACT_AWAY);
         ui_current_page_off();
     } else {
@@ -453,6 +454,7 @@ _ui_auto_away(void)
         title_bar_set_presence(CONTACT_AWAY);
         ui_current_page_off();
     }
+    prefs_free_string(pref_autoaway_message);
 }
 
 static void
@@ -1722,6 +1724,7 @@ _ui_room_message(const char * const room_jid, const char * const nick,
             g_free(message_lower);
             g_free(nick_lower);
         }
+        prefs_free_string(room_setting);
 
         if (notify) {
             gboolean is_current = wins_is_current(window);