1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Create room list on muc_init

This commit is contained in:
James Booth 2014-09-26 00:43:00 +01:00
parent 70169f8ab9
commit ccbaa67a01

239
src/muc.c
View File

@ -69,16 +69,15 @@ void
muc_init(void) muc_init(void)
{ {
invite_ac = autocomplete_new(); invite_ac = autocomplete_new();
rooms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_free_room);
} }
void void
muc_close(void) muc_close(void)
{ {
autocomplete_free(invite_ac); autocomplete_free(invite_ac);
if (rooms) { g_hash_table_destroy(rooms);
g_hash_table_destroy(rooms); rooms = NULL;
rooms = NULL;
}
} }
void void
@ -145,11 +144,6 @@ void
muc_join_room(const char * const room, const char * const nick, muc_join_room(const char * const room, const char * const nick,
const char * const password, gboolean autojoin) 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)); ChatRoom *new_room = malloc(sizeof(ChatRoom));
new_room->room = strdup(room); new_room->room = strdup(room);
new_room->nick = strdup(nick); new_room->nick = strdup(nick);
@ -177,41 +171,28 @@ muc_join_room(const char * const room, const char * const nick,
void void
muc_leave_room(const char * const room) muc_leave_room(const char * const room)
{ {
if (rooms) { g_hash_table_remove(rooms, room);
g_hash_table_remove(rooms, room);
}
} }
gboolean gboolean
muc_requires_config(const char * const room) muc_requires_config(const char * const room)
{ {
if (!rooms) {
return FALSE;
}
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (!chat_room) { if (chat_room) {
return chat_room->pending_config;
} else {
return FALSE; return FALSE;
} }
return chat_room->pending_config;
} }
void void
muc_set_requires_config(const char * const room, gboolean val) muc_set_requires_config(const char * const room, gboolean val)
{ {
if (!rooms) {
return;
}
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (!chat_room) { if (chat_room) {
return; 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 gboolean
muc_room_is_active(const char * const room) muc_room_is_active(const char * const room)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); return (chat_room != NULL);
return (chat_room != NULL);
} else {
return FALSE;
}
} }
gboolean gboolean
muc_room_is_autojoin(const char * const room) muc_room_is_autojoin(const char * const room)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { return chat_room->autojoin;
return chat_room->autojoin; } else {
} return FALSE;
} }
return FALSE;
} }
void void
muc_set_subject(const char * const room, const char * const subject) muc_set_subject(const char * const room, const char * const subject)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { free(chat_room->subject);
free(chat_room->subject); chat_room->subject = strdup(subject);
chat_room->subject = strdup(subject);
}
} }
} }
char * char *
muc_get_subject(const char * const room) muc_get_subject(const char * const room)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { return chat_room->subject;
return chat_room->subject; } else {
} return NULL;
} }
return NULL;
} }
void void
muc_add_pending_broadcast(const char * const room, const char * const message) muc_add_pending_broadcast(const char * const room, const char * const message)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { chat_room->pending_broadcasts = g_list_append(chat_room->pending_broadcasts, strdup(message));
chat_room->pending_broadcasts = g_list_append(chat_room->pending_broadcasts, strdup(message));
}
} }
} }
GList * GList *
muc_get_pending_broadcasts(const char * const room) muc_get_pending_broadcasts(const char * const room)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { return chat_room->pending_broadcasts;
return chat_room->pending_broadcasts; } else {
} return NULL;
} }
return NULL;
} }
char * char *
muc_get_old_nick(const char * const room, const char * const new_nick) muc_get_old_nick(const char * const room, const char * const new_nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room && chat_room->pending_nick_change) { if (chat_room && chat_room->pending_nick_change) {
return g_hash_table_lookup(chat_room->nick_changes, new_nick); 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) muc_set_room_pending_nick_change(const char * const room, const char * const new_nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
chat_room->pending_nick_change = TRUE; chat_room->pending_nick_change = TRUE;
g_hash_table_insert(chat_room->nick_changes, strdup(new_nick), strdup(chat_room->nick)); 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) muc_is_room_pending_nick_change(const char * const room)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
return chat_room->pending_nick_change; return chat_room->pending_nick_change;
} else { } else {
@ -341,7 +305,6 @@ void
muc_complete_room_nick_change(const char * const room, const char * const nick) muc_complete_room_nick_change(const char * const room, const char * const nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
free(chat_room->nick); free(chat_room->nick);
chat_room->nick = strdup(nick); chat_room->nick = strdup(nick);
@ -358,11 +321,7 @@ muc_complete_room_nick_change(const char * const room, const char * const nick)
GList * GList *
muc_get_active_room_list(void) muc_get_active_room_list(void)
{ {
if (rooms) { return g_hash_table_get_keys(rooms);
return g_hash_table_get_keys(rooms);
} else {
return NULL;
}
} }
/* /*
@ -372,14 +331,12 @@ muc_get_active_room_list(void)
char * char *
muc_get_room_nick(const char * const room) muc_get_room_nick(const char * const room)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { return chat_room->nick;
return chat_room->nick; } else {
} return NULL;
} }
return NULL;
} }
/* /*
@ -389,14 +346,12 @@ muc_get_room_nick(const char * const room)
char * char *
muc_get_room_password(const char * const room) muc_get_room_password(const char * const room)
{ {
if (rooms) { ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); if (chat_room) {
if (chat_room) { return chat_room->password;
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) muc_nick_in_roster(const char * const room, const char * const nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
PContact contact = g_hash_table_lookup(chat_room->roster, nick); PContact contact = g_hash_table_lookup(chat_room->roster, nick);
return (contact != NULL); 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) muc_remove_from_roster(const char * const room, const char * const nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
g_hash_table_remove(chat_room->roster, nick); g_hash_table_remove(chat_room->roster, nick);
autocomplete_remove(chat_room->nick_ac, nick); autocomplete_remove(chat_room->nick_ac, nick);
@ -464,13 +417,12 @@ PContact
muc_get_participant(const char * const room, const char * const nick) muc_get_participant(const char * const room, const char * const nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
PContact participant = g_hash_table_lookup(chat_room->roster, nick); PContact participant = g_hash_table_lookup(chat_room->roster, nick);
return participant; return participant;
} else {
return NULL;
} }
return NULL;
} }
/* /*
@ -481,7 +433,6 @@ GList *
muc_get_roster(const char * const room) muc_get_roster(const char * const room)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
GList *result = NULL; GList *result = NULL;
GHashTableIter iter; GHashTableIter iter;
@ -506,7 +457,6 @@ Autocomplete
muc_get_roster_ac(const char * const room) muc_get_roster_ac(const char * const room)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
return chat_room->nick_ac; return chat_room->nick_ac;
} else { } else {
@ -521,7 +471,6 @@ void
muc_set_roster_received(const char * const room) muc_set_roster_received(const char * const room)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
chat_room->roster_received = TRUE; chat_room->roster_received = TRUE;
} }
@ -534,7 +483,6 @@ gboolean
muc_get_roster_received(const char * const room) muc_get_roster_received(const char * const room)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
return chat_room->roster_received; return chat_room->roster_received;
} else { } 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) const char * const new_nick, const char * const old_nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
g_hash_table_insert(chat_room->nick_changes, strdup(new_nick), strdup(old_nick)); g_hash_table_insert(chat_room->nick_changes, strdup(new_nick), strdup(old_nick));
muc_remove_from_roster(room, 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) const char * const nick)
{ {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room) { if (chat_room) {
char *old_nick = g_hash_table_lookup(chat_room->nick_changes, nick); char *old_nick = g_hash_table_lookup(chat_room->nick_changes, nick);
if (old_nick) { if (old_nick) {
@ -586,72 +532,53 @@ muc_complete_roster_nick_change(const char * const room,
void void
muc_autocomplete(char *input, int *size) muc_autocomplete(char *input, int *size)
{ {
if (!rooms) {
return;
}
char *recipient = ui_current_recipient(); char *recipient = ui_current_recipient();
ChatRoom *chat_room = g_hash_table_lookup(rooms, recipient); ChatRoom *chat_room = g_hash_table_lookup(rooms, recipient);
if (!chat_room) { if (chat_room && chat_room->nick_ac) {
return; input[*size] = '\0';
} char *search_str = NULL;
if (!chat_room->nick_ac) { gchar *last_space = g_strrstr(input, " ");
return; if (!last_space) {
} search_str = input;
if (!chat_room->autocomplete_prefix) {
input[*size] = '\0'; chat_room->autocomplete_prefix = strdup("");
char *search_str = NULL; }
} else {
gchar *last_space = g_strrstr(input, " "); search_str = last_space+1;
if (!last_space) { if (!chat_room->autocomplete_prefix) {
search_str = input; chat_room->autocomplete_prefix = g_strndup(input, search_str - input);
if (!chat_room->autocomplete_prefix) { }
chat_room->autocomplete_prefix = strdup("");
} }
} else {
search_str = last_space+1; char *result = autocomplete_complete(chat_room->nick_ac, search_str, FALSE);
if (!chat_room->autocomplete_prefix) { if (result) {
chat_room->autocomplete_prefix = g_strndup(input, search_str - input); 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);
} }
} }
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);
}
return;
} }
void void
muc_reset_autocomplete(const char * const room) muc_reset_autocomplete(const char * const room)
{ {
if (!rooms) {
return;
}
ChatRoom *chat_room = g_hash_table_lookup(rooms, room); 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) { if (chat_room->autocomplete_prefix) {
return; free(chat_room->autocomplete_prefix);
} chat_room->autocomplete_prefix = NULL;
}
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;
} }
} }