about summary refs log tree commit diff stats
path: root/src/muc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/muc.c')
-rw-r--r--src/muc.c237
1 files changed, 82 insertions, 155 deletions
diff --git a/src/muc.c b/src/muc.c
index de5a1390..623f72f5 100644
--- a/src/muc.c
+++ b/src/muc.c
@@ -69,16 +69,15 @@ void
 muc_init(void)
 {
     invite_ac = autocomplete_new();
+    rooms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_free_room);
 }
 
 void
 muc_close(void)
 {
     autocomplete_free(invite_ac);
-    if (rooms) {
-        g_hash_table_destroy(rooms);
-        rooms = NULL;
-    }
+    g_hash_table_destroy(rooms);
+    rooms = NULL;
 }
 
 void
@@ -145,11 +144,6 @@ void
 muc_join_room(const char * const room, const char * const nick,
     const char * const password, gboolean autojoin)
 {
-    if (!rooms) {
-        rooms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
-            (GDestroyNotify)_free_room);
-    }
-
     ChatRoom *new_room = malloc(sizeof(ChatRoom));
     new_room->room = strdup(room);
     new_room->nick = strdup(nick);
@@ -177,41 +171,28 @@ muc_join_room(const char * const room, const char * const nick,
 void
 muc_leave_room(const char * const room)
 {
-    if (rooms) {
-        g_hash_table_remove(rooms, room);
-    }
+    g_hash_table_remove(rooms, room);
 }
 
 gboolean
 muc_requires_config(const char * const room)
 {
-    if (!rooms) {
-        return FALSE;
-    }
-
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-    if (!chat_room) {
+    if (chat_room) {
+        return chat_room->pending_config;
+    } else {
         return FALSE;
     }
 
-    return chat_room->pending_config;
 }
 
 void
 muc_set_requires_config(const char * const room, gboolean val)
 {
-    if (!rooms) {
-        return;
-    }
-
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-    if (!chat_room) {
-        return;
+    if (chat_room) {
+        chat_room->pending_config = val;
     }
-
-    chat_room->pending_config = val;
-
-    return;
 }
 
 /*
@@ -220,86 +201,71 @@ muc_set_requires_config(const char * const room, gboolean val)
 gboolean
 muc_room_is_active(const char * const room)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        return (chat_room != NULL);
-    } else {
-        return FALSE;
-    }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    return (chat_room != NULL);
 }
 
 gboolean
 muc_room_is_autojoin(const char * const room)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            return chat_room->autojoin;
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        return chat_room->autojoin;
+    } else {
+        return FALSE;
     }
-
-    return FALSE;
 }
 
 void
 muc_set_subject(const char * const room, const char * const subject)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            free(chat_room->subject);
-            chat_room->subject = strdup(subject);
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        free(chat_room->subject);
+        chat_room->subject = strdup(subject);
     }
 }
 
 char *
 muc_get_subject(const char * const room)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            return chat_room->subject;
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        return chat_room->subject;
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 void
 muc_add_pending_broadcast(const char * const room, const char * const message)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            chat_room->pending_broadcasts = g_list_append(chat_room->pending_broadcasts, strdup(message));
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        chat_room->pending_broadcasts = g_list_append(chat_room->pending_broadcasts, strdup(message));
     }
 }
 
 GList *
 muc_get_pending_broadcasts(const char * const room)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            return chat_room->pending_broadcasts;
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        return chat_room->pending_broadcasts;
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 char *
 muc_get_old_nick(const char * const room, const char * const new_nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room && chat_room->pending_nick_change) {
         return g_hash_table_lookup(chat_room->nick_changes, new_nick);
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 /*
@@ -310,7 +276,6 @@ void
 muc_set_room_pending_nick_change(const char * const room, const char * const new_nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         chat_room->pending_nick_change = TRUE;
         g_hash_table_insert(chat_room->nick_changes, strdup(new_nick), strdup(chat_room->nick));
@@ -325,7 +290,6 @@ gboolean
 muc_is_room_pending_nick_change(const char * const room)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         return chat_room->pending_nick_change;
     } else {
@@ -341,7 +305,6 @@ void
 muc_complete_room_nick_change(const char * const room, const char * const nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         free(chat_room->nick);
         chat_room->nick = strdup(nick);
@@ -358,11 +321,7 @@ muc_complete_room_nick_change(const char * const room, const char * const nick)
 GList *
 muc_get_active_room_list(void)
 {
-    if (rooms) {
-        return g_hash_table_get_keys(rooms);
-    } else {
-        return NULL;
-    }
+    return g_hash_table_get_keys(rooms);
 }
 
 /*
@@ -372,14 +331,12 @@ muc_get_active_room_list(void)
 char *
 muc_get_room_nick(const char * const room)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            return chat_room->nick;
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        return chat_room->nick;
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 /*
@@ -389,14 +346,12 @@ muc_get_room_nick(const char * const room)
 char *
 muc_get_room_password(const char * const room)
 {
-    if (rooms) {
-        ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-        if (chat_room) {
-            return chat_room->password;
-        }
+    ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        return chat_room->password;
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 /*
@@ -406,13 +361,12 @@ gboolean
 muc_nick_in_roster(const char * const room, const char * const nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         PContact contact = g_hash_table_lookup(chat_room->roster, nick);
         return (contact != NULL);
+    } else {
+        return FALSE;
     }
-
-    return FALSE;
 }
 
 /*
@@ -453,7 +407,6 @@ void
 muc_remove_from_roster(const char * const room, const char * const nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         g_hash_table_remove(chat_room->roster, nick);
         autocomplete_remove(chat_room->nick_ac, nick);
@@ -464,13 +417,12 @@ PContact
 muc_get_participant(const char * const room, const char * const nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         PContact participant = g_hash_table_lookup(chat_room->roster, nick);
         return participant;
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 /*
@@ -481,7 +433,6 @@ GList *
 muc_get_roster(const char * const room)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         GList *result = NULL;
         GHashTableIter iter;
@@ -506,7 +457,6 @@ Autocomplete
 muc_get_roster_ac(const char * const room)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         return chat_room->nick_ac;
     } else {
@@ -521,7 +471,6 @@ void
 muc_set_roster_received(const char * const room)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         chat_room->roster_received = TRUE;
     }
@@ -534,7 +483,6 @@ gboolean
 muc_get_roster_received(const char * const room)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         return chat_room->roster_received;
     } else {
@@ -551,7 +499,6 @@ muc_set_roster_pending_nick_change(const char * const room,
     const char * const new_nick, const char * const old_nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         g_hash_table_insert(chat_room->nick_changes, strdup(new_nick), strdup(old_nick));
         muc_remove_from_roster(room, old_nick);
@@ -569,7 +516,6 @@ muc_complete_roster_nick_change(const char * const room,
     const char * const nick)
 {
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
-
     if (chat_room) {
         char *old_nick = g_hash_table_lookup(chat_room->nick_changes, nick);
         if (old_nick) {
@@ -586,72 +532,53 @@ muc_complete_roster_nick_change(const char * const room,
 void
 muc_autocomplete(char *input, int *size)
 {
-    if (!rooms) {
-        return;
-    }
-
     char *recipient = ui_current_recipient();
     ChatRoom *chat_room = g_hash_table_lookup(rooms, recipient);
 
-    if (!chat_room) {
-        return;
-    }
-
-    if (!chat_room->nick_ac) {
-        return;
-    }
-
-    input[*size] = '\0';
-    char *search_str = NULL;
+    if (chat_room && chat_room->nick_ac) {
+        input[*size] = '\0';
+        char *search_str = NULL;
 
-    gchar *last_space = g_strrstr(input, " ");
-    if (!last_space) {
-        search_str = input;
-        if (!chat_room->autocomplete_prefix) {
-            chat_room->autocomplete_prefix = strdup("");
-        }
-    } else {
-        search_str = last_space+1;
-        if (!chat_room->autocomplete_prefix) {
-            chat_room->autocomplete_prefix = g_strndup(input, search_str - input);
+        gchar *last_space = g_strrstr(input, " ");
+        if (!last_space) {
+            search_str = input;
+            if (!chat_room->autocomplete_prefix) {
+                chat_room->autocomplete_prefix = strdup("");
+            }
+        } else {
+            search_str = last_space+1;
+            if (!chat_room->autocomplete_prefix) {
+                chat_room->autocomplete_prefix = g_strndup(input, search_str - input);
+            }
         }
-    }
 
-    char *result = autocomplete_complete(chat_room->nick_ac, search_str, FALSE);
-    if (result) {
-        GString *replace_with = g_string_new(chat_room->autocomplete_prefix);
-        g_string_append(replace_with, result);
-        if (!last_space || (*(last_space+1) == '\0')) {
-            g_string_append(replace_with, ": ");
+        char *result = autocomplete_complete(chat_room->nick_ac, search_str, FALSE);
+        if (result) {
+            GString *replace_with = g_string_new(chat_room->autocomplete_prefix);
+            g_string_append(replace_with, result);
+            if (!last_space || (*(last_space+1) == '\0')) {
+                g_string_append(replace_with, ": ");
+            }
+            ui_replace_input(input, replace_with->str, size);
+            g_string_free(replace_with, TRUE);
+            g_free(result);
         }
-        ui_replace_input(input, replace_with->str, size);
-        g_string_free(replace_with, TRUE);
-        g_free(result);
     }
-
-    return;
 }
 
 void
 muc_reset_autocomplete(const char * const room)
 {
-    if (!rooms) {
-        return;
-    }
-
     ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
+    if (chat_room) {
+        if (chat_room->nick_ac) {
+            autocomplete_reset(chat_room->nick_ac);
+        }
 
-    if (!chat_room) {
-        return;
-    }
-
-    if (chat_room->nick_ac) {
-        autocomplete_reset(chat_room->nick_ac);
-    }
-
-    if (chat_room->autocomplete_prefix) {
-        free(chat_room->autocomplete_prefix);
-        chat_room->autocomplete_prefix = NULL;
+        if (chat_room->autocomplete_prefix) {
+            free(chat_room->autocomplete_prefix);
+            chat_room->autocomplete_prefix = NULL;
+        }
     }
 }