1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Chat room windows now created only after successful join

This commit is contained in:
James Booth 2014-04-21 00:37:04 +01:00
parent 9a55d8ad19
commit 2c15aba92a
17 changed files with 89 additions and 102 deletions

View File

@ -1591,9 +1591,10 @@ cmd_join(gchar **args, struct cmd_help_t help)
if (!muc_room_is_active(room)) {
presence_join_room(room, nick, passwd);
muc_join_room(room, nick, passwd, FALSE);
} else if (muc_get_roster_received(room)) {
ui_room_join(room, TRUE);
}
ui_room_join(room, TRUE);
muc_remove_invite(room);
jid_destroy(room_arg);
g_string_free(room_str, TRUE);

View File

@ -34,6 +34,7 @@ typedef struct _muc_room_t {
char *nick; // e.g. Some User
char *password;
char *subject;
gboolean autojoin;
gboolean pending_nick_change;
GHashTable *roster;
Autocomplete nick_ac;
@ -126,7 +127,8 @@ muc_clear_invites(void)
* Join the chat room with the specified nickname
*/
void
muc_join_room(const char * const room, const char * const nick, const char * const password)
muc_join_room(const char * const room, const char * const nick,
const char * const password, gboolean autojoin)
{
if (rooms == NULL) {
rooms = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
@ -149,6 +151,7 @@ muc_join_room(const char * const room, const char * const nick, const char * con
g_free, g_free);
new_room->roster_received = FALSE;
new_room->pending_nick_change = FALSE;
new_room->autojoin = autojoin;
g_hash_table_insert(rooms, strdup(room), new_room);
}
@ -183,6 +186,22 @@ muc_room_is_active(const char * const room)
}
}
gboolean
muc_room_is_autojoin(const char * const room)
{
if (rooms != NULL) {
ChatRoom *chat_room = g_hash_table_lookup(rooms, room);
if (chat_room != NULL) {
return chat_room->autojoin;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
char *
muc_get_old_nick(const char * const room, const char * const new_nick)
{
@ -504,4 +523,4 @@ gint _compare_participants(PContact a, PContact b)
g_free(key_b);
return result;
}
}

View File

@ -31,9 +31,11 @@
void muc_init(void);
void muc_close(void);
void muc_join_room(const char * const room, const char * const nick, const char * const password);
void muc_join_room(const char * const room, const char * const nick,
const char * const password, gboolean autojoin);
void muc_leave_room(const char * const room);
gboolean muc_room_is_active(const char * const room);
gboolean muc_room_is_autojoin(const char * const room);
GList* muc_get_active_room_list(void);
char * muc_get_room_nick(const char * const room);

View File

@ -36,23 +36,22 @@
#include "ui/ui.h"
void
handle_room_join_error(const char * const room, const char * const err)
{
if (muc_room_is_active(room)) {
muc_leave_room(room);
}
ui_handle_room_join_error(room, err);
}
// handle presence stanza errors
void
handle_presence_error(const char *from, const char * const type,
const char *err_msg)
{
// handle nickname conflict on entering room
if ((from != NULL) && g_strcmp0(err_msg, "conflict") == 0) {
// remove the room from muc
Jid *room_jid = jid_create(from);
if (!muc_get_roster_received(room_jid->barejid)) {
muc_leave_room(room_jid->barejid);
ui_handle_recipient_error(room_jid->barejid, err_msg);
}
jid_destroy(room_jid);
// handle any other error from recipient
} else if (from != NULL) {
// handle error from recipient
if (from != NULL) {
ui_handle_recipient_error(from, err_msg);
// handle errors from no recipient
@ -404,6 +403,12 @@ handle_room_nick_change(const char * const room,
void
handle_room_roster_complete(const char * const room)
{
if (muc_room_is_autojoin(room)) {
ui_room_join(room, FALSE);
} else {
ui_room_join(room, TRUE);
}
muc_remove_invite(room);
muc_set_roster_received(room);
GList *roster = muc_get_roster(room);
ui_room_roster(room, roster, NULL);
@ -499,13 +504,6 @@ handle_autoping_cancel(void)
ui_current_page_off();
}
void
handle_bookmark_autojoin(char *jid)
{
ui_room_join(jid, FALSE);
muc_remove_invite(jid);
}
void
handle_xmpp_stanza(const char * const msg)
{

View File

@ -43,6 +43,7 @@ void handle_room_history(const char * const room_jid, const char * const nick,
GTimeVal tv_stamp, const char * const message);
void handle_room_message(const char * const room_jid, const char * const nick,
const char * const message);
void handle_room_join_error(const char * const room, const char * const err);
void handle_duck_result(const char * const result);
void handle_incoming_message(char *from, char *message, gboolean priv);
void handle_delayed_message(char *from, char *message, GTimeVal tv_stamp,
@ -78,7 +79,6 @@ void handle_message_error(const char * const from, const char * const type,
const char * const err_msg);
void handle_presence_error(const char *from, const char * const type,
const char *err_msg);
void handle_bookmark_autojoin(char *jid);
void handle_xmpp_stanza(const char * const msg);
#endif

View File

@ -1272,7 +1272,7 @@ _ui_outgoing_msg(const char * const from, const char * const to,
}
static void
_ui_room_join(char *room, gboolean focus)
_ui_room_join(const char * const room, gboolean focus)
{
ProfWin *window = wins_get_by_recipient(room);
int num = 0;
@ -1289,7 +1289,8 @@ _ui_room_join(char *room, gboolean focus)
} else {
status_bar_active(num);
ProfWin *console = wins_get_console();
win_vprint_line(console, '!', COLOUR_ONLINE, "-> Autojoined %s (%d).", room, num);
char *nick = muc_get_room_nick(room);
win_vprint_line(console, '!', COLOUR_TYPING, "-> Autojoined %s as %s (%d).", room, nick, num);
win_update_virtual(console);
}
}
@ -1349,6 +1350,12 @@ _ui_room_roster(const char * const room, GList *roster, const char * const prese
}
}
static void
_ui_handle_room_join_error(const char * const room, const char * const err)
{
cons_show_error("Error joining room %s, reason: %s", room, err);
}
static void
_ui_room_member_offline(const char * const room, const char * const nick)
{
@ -1979,4 +1986,5 @@ ui_init_module(void)
ui_handle_stanza = _ui_handle_stanza;
ui_create_xmlconsole_win = _ui_create_xmlconsole_win;
ui_xmlconsole_exists = _ui_xmlconsole_exists;
ui_handle_room_join_error = _ui_handle_room_join_error;
}

View File

@ -102,7 +102,7 @@ void (*ui_disconnected)(void);
void (*ui_recipient_gone)(const char * const barejid);
void (*ui_outgoing_msg)(const char * const from, const char * const to,
const char * const message);
void (*ui_room_join)(char *room, gboolean focus);
void (*ui_room_join)(const char * const room, gboolean focus);
void (*ui_room_roster)(const char * const room, GList *roster, const char * const presence);
void (*ui_room_history)(const char * const room_jid, const char * const nick,
GTimeVal tv_stamp, const char * const message);
@ -132,6 +132,7 @@ void (*ui_handle_recipient_not_found)(const char * const recipient, const char *
void (*ui_handle_recipient_error)(const char * const recipient, const char * const err_msg);
void (*ui_handle_error)(const char * const err_msg);
void (*ui_clear_win_title)(void);
void (*ui_handle_room_join_error)(const char * const room, const char * const err);
// contact status functions
void (*ui_status_room)(const char * const contact);

View File

@ -260,7 +260,7 @@ _bookmark_handle_result(xmpp_conn_t * const conn,
room_jid = jid_create_from_bare_and_resource(jid, name);
if (!muc_room_is_active(room_jid->barejid)) {
presence_join_room(jid, name, NULL);
handle_bookmark_autojoin(jid);
muc_join_room(jid, name, NULL, TRUE);
}
jid_destroy(room_jid);
} else {

View File

@ -283,7 +283,6 @@ _presence_join_room(char *room, char *nick, char * passwd)
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
muc_join_room(jid->barejid, jid->resourcepart, passwd);
jid_destroy(jid);
}
@ -342,11 +341,31 @@ _presence_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
char *id = xmpp_stanza_get_id(stanza);
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR);
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X);
char *xmlns = xmpp_stanza_get_ns(x);
char *type = NULL;
if (error_stanza != NULL) {
type = xmpp_stanza_get_attribute(error_stanza, STANZA_ATTR_TYPE);
}
// handle MUC join errors
if (g_strcmp0(xmlns, STANZA_NS_MUC) == 0) {
Jid *fulljid = jid_create(from);
char *error_cond = NULL;
xmpp_stanza_t *reason_st = xmpp_stanza_get_child_by_ns(error_stanza, STANZA_NS_STANZAS);
if (reason_st != NULL) {
error_cond = xmpp_stanza_get_name(reason_st);
}
if (error_cond == NULL) {
error_cond = "unknown";
}
log_info("Error joining room: %s, reason: %s", fulljid->barejid, error_cond);
handle_room_join_error(fulljid->barejid, error_cond);
return 1;
}
// stanza_get_error never returns NULL
char *err_msg = stanza_get_error_message(stanza);
@ -676,7 +695,6 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
// handle roster complete
} else if (!muc_get_roster_received(room)) {
handle_room_roster_complete(room);
}
// handle presence from room members

View File

@ -124,6 +124,7 @@
#define STANZA_TEXT_XA "xa"
#define STANZA_TEXT_ONLINE "online"
#define STANZA_NS_STANZAS "urn:ietf:params:xml:ns:xmpp-stanzas"
#define STANZA_NS_CHATSTATES "http://jabber.org/protocol/chatstates"
#define STANZA_NS_MUC "http://jabber.org/protocol/muc"
#define STANZA_NS_MUC_USER "http://jabber.org/protocol/muc#user"

View File

@ -248,7 +248,7 @@ void cmd_bookmark_add_adds_bookmark_with_room_details(void **state)
gchar *args[] = { "add", NULL };
muc_init();
muc_join_room(jid, nick, NULL);
muc_join_room(jid, nick, NULL, TRUE);
mock_connection_status(JABBER_CONNECTED);
mock_current_win_type(WIN_MUC);
@ -275,7 +275,7 @@ void cmd_bookmark_add_adds_bookmark_with_room_details_autojoin(void **state)
gchar *args[] = { "add", "autojoin", NULL };
muc_init();
muc_join_room(jid, nick, NULL);
muc_join_room(jid, nick, NULL, TRUE);
mock_connection_status(JABBER_CONNECTED);
mock_current_win_type(WIN_MUC);

View File

@ -109,7 +109,6 @@ void cmd_join_uses_account_mucservice_when_no_service_specified(void **state)
mock_presence_join_room();
presence_join_room_expect(expected_room, nick, NULL);
ui_room_join_expect(expected_room, TRUE);
gboolean result = cmd_join(args, *help);
assert_true(result);
@ -136,7 +135,6 @@ void cmd_join_uses_supplied_nick(void **state)
mock_presence_join_room();
presence_join_room_expect(room, nick, NULL);
ui_room_join_expect(room, TRUE);
gboolean result = cmd_join(args, *help);
assert_true(result);
@ -147,7 +145,7 @@ void cmd_join_uses_supplied_nick(void **state)
void cmd_join_uses_account_nick_when_not_supplied(void **state)
{
char *account_name = "an_account";
char *room = "room@conf.server.org";
char *room = "room2@conf.server.org";
char *account_nick = "a_nick";
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { room, NULL };
@ -163,7 +161,6 @@ void cmd_join_uses_account_nick_when_not_supplied(void **state)
mock_presence_join_room();
presence_join_room_expect(room, account_nick, NULL);
ui_room_join_expect(room, TRUE);
gboolean result = cmd_join(args, *help);
assert_true(result);
@ -193,7 +190,6 @@ void cmd_join_uses_password_when_supplied(void **state)
mock_presence_join_room();
presence_join_room_expect(expected_room, account_nick, password);
ui_room_join_expect(expected_room, TRUE);
gboolean result = cmd_join(args, *help);
assert_true(result);

View File

@ -70,7 +70,7 @@ void test_muc_room_is_active(void **state)
{
char *room = "room@server.org";
char *nick = "bob";
muc_join_room(room, nick, NULL);
muc_join_room(room, nick, NULL, FALSE);
gboolean room_is_active = muc_room_is_active(room);

View File

@ -172,58 +172,7 @@ void handle_presence_error_when_no_recipient(void **state)
handle_presence_error(from, type, err_msg);
}
void handle_presence_error_when_no_recipient_and_conflict(void **state)
{
char *err_msg = "conflict";
char *from = NULL;
char *type = NULL;
expect_ui_handle_error(err_msg);
handle_presence_error(from, type, err_msg);
}
void handle_presence_error_when_nick_conflict_shows_recipient_error(void **state)
{
char *err_msg = "conflict";
char *from = "room@rooms.org/nick";
char *barejid = "room@rooms.org";
char *nick = "nick";
char *type = NULL;
muc_init();
muc_join_room(barejid, nick, NULL);
expect_ui_handle_recipient_error(barejid, err_msg);
handle_presence_error(from, type, err_msg);
muc_close();
}
void handle_presence_error_when_nick_conflict_does_not_join_room(void **state)
{
char *err_msg = "conflict";
char *from = "room@rooms.org/nick";
char *barejid = "room@rooms.org";
char *nick = "nick";
char *type = NULL;
Jid *jidp = jid_create(from);
stub_ui_handle_recipient_error();
muc_init();
muc_join_room(barejid, nick, NULL);
handle_presence_error(from, type, err_msg);
gboolean room_is_active = muc_room_is_active(jidp->barejid);
assert_false(room_is_active);
muc_close();
jid_destroy(jidp);
}
void handle_presence_error_when_from_recipient_not_conflict(void **state)
void handle_presence_error_when_from_recipient(void **state)
{
char *err_msg = "Some error.";
char *from = "bob@server.com";

View File

@ -5,11 +5,8 @@ void console_doesnt_show_dnd_presence_when_set_none(void **state);
void console_doesnt_show_dnd_presence_when_set_online(void **state);
void console_shows_dnd_presence_when_set_all(void **state);
void handle_message_error_when_no_recipient(void **state);
void handle_message_error_when_recipient_cancel(void **stanza);
void handle_message_error_when_recipient_cancel_disables_chat_session(void **stanza);
void handle_message_error_when_recipient_cancel(void **state);
void handle_message_error_when_recipient_cancel_disables_chat_session(void **state);
void handle_message_error_when_recipient_and_no_type(void **state);
void handle_presence_error_when_no_recipient(void **state);
void handle_presence_error_when_no_recipient_and_conflict(void **state);
void handle_presence_error_when_nick_conflict_shows_recipient_error(void **state);
void handle_presence_error_when_nick_conflict_does_not_join_room(void **state);
void handle_presence_error_when_from_recipient_not_conflict(void **state);
void handle_presence_error_when_from_recipient(void **state);

View File

@ -389,10 +389,7 @@ int main(int argc, char* argv[]) {
close_preferences),
unit_test(handle_message_error_when_recipient_and_no_type),
unit_test(handle_presence_error_when_no_recipient),
unit_test(handle_presence_error_when_no_recipient_and_conflict),
unit_test(handle_presence_error_when_nick_conflict_shows_recipient_error),
unit_test(handle_presence_error_when_nick_conflict_does_not_join_room),
unit_test(handle_presence_error_when_from_recipient_not_conflict),
unit_test(handle_presence_error_when_from_recipient),
unit_test(cmd_alias_add_shows_usage_when_no_args),
unit_test(cmd_alias_add_shows_usage_when_no_value),

View File

@ -166,7 +166,7 @@ gboolean _mock_ui_current_win_is_otr(void)
}
static
void _mock_ui_room_join(char *room, gboolean focus)
void _mock_ui_room_join(const char * const room, gboolean focus)
{
check_expected(room);
check_expected(focus);