1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Refactor muc user handler

This commit is contained in:
James Booth 2014-10-07 00:46:08 +01:00
parent ea511f8fbc
commit d6a4af9992
5 changed files with 68 additions and 130 deletions

View File

@ -595,7 +595,7 @@ muc_occupants_by_affiliation(const char * const room, muc_affiliation_t affiliat
* is in progress
*/
void
muc_roster_nick_change_start(const char * const room,
muc_occupant_nick_change_start(const char * const room,
const char * const new_nick, const char * const old_nick)
{
ChatRoom *chat_room = g_hash_table_lookup(rooms, room);

View File

@ -105,7 +105,7 @@ const char * muc_occupant_role_str(Occupant *occupant);
GSList * muc_occupants_by_role(const char * const room, muc_role_t role);
GSList * muc_occupants_by_affiliation(const char * const room, muc_affiliation_t affiliation);
void muc_roster_nick_change_start(const char * const room, const char * const new_nick, const char * const old_nick);
void muc_occupant_nick_change_start(const char * const room, const char * const new_nick, const char * const old_nick);
char* muc_roster_nick_change_complete(const char * const room, const char * const nick);
void muc_invites_add(const char * const room);

View File

@ -521,21 +521,6 @@ handle_leave_room(const char * const room)
ui_leave_room(room);
}
void
handle_room_nick_change(const char * const room,
const char * const nick)
{
muc_nick_change_complete(room, nick);
ui_room_nick_change(room, nick);
}
void
handle_room_requires_config(const char * const room)
{
muc_set_requires_config(room, TRUE);
ui_room_requires_config(room);
}
void
handle_room_destroy(const char * const room)
{
@ -596,63 +581,7 @@ handle_room_kick_result_error(const char * const room, const char * const nick,
}
void
handle_room_roster_complete(const char * const room)
{
if (muc_autojoin(room)) {
ui_room_join(room, FALSE);
} else {
ui_room_join(room, TRUE);
}
muc_invites_remove(room);
muc_roster_set_complete(room);
GList *roster = muc_roster(room);
ui_room_roster(room, roster, NULL);
char *subject = muc_subject(room);
if (subject != NULL) {
ui_room_subject(room, NULL, subject);
}
GList *pending_broadcasts = muc_pending_broadcasts(room);
if (pending_broadcasts != NULL) {
GList *curr = pending_broadcasts;
while (curr != NULL) {
ui_room_broadcast(room, curr->data);
curr = g_list_next(curr);
}
}
}
void
handle_room_member_presence(const char * const room, const char * const nick, const char * const jid,
const char * const role, const char * const affiliation, const char * const show, const char * const status)
{
gboolean updated = muc_roster_add(room, nick, jid, role, affiliation, show, status);
if (updated) {
char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
if (g_strcmp0(muc_status_pref, "all") == 0) {
ui_room_member_presence(room, nick, show, status);
}
prefs_free_string(muc_status_pref);
}
}
void
handle_room_member_online(const char * const room, const char * const nick, const char * const jid,
const char * const role, const char * const affiliation, const char * const show, const char * const status)
{
muc_roster_add(room, nick, jid, role, affiliation, show, status);
char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
if (g_strcmp0(muc_status_pref, "none") != 0) {
ui_room_member_online(room, nick, show, status);
}
prefs_free_string(muc_status_pref);
}
void
handle_room_member_offline(const char * const room, const char * const nick,
handle_room_occupant_offline(const char * const room, const char * const nick,
const char * const show, const char * const status)
{
muc_roster_remove(room, nick);
@ -680,13 +609,6 @@ handle_room_occupent_banned(const char * const room, const char * const nick, co
ui_room_member_banned(room, nick, actor, reason);
}
void
handle_room_member_nick_change(const char * const room,
const char * const old_nick, const char * const nick)
{
ui_room_member_nick_change(room, old_nick, nick);
}
void
handle_group_add(const char * const contact,
const char * const group)
@ -752,15 +674,40 @@ handle_muc_self_online(const char * const room, const char * const nick, gboolea
{
// handle self nick change
if (muc_nick_change_pending(room)) {
handle_room_nick_change(room, nick);
muc_nick_change_complete(room, nick);
ui_room_nick_change(room, nick);
// handle roster complete
} else if (!muc_roster_complete(room)) {
handle_room_roster_complete(room);
if (muc_autojoin(room)) {
ui_room_join(room, FALSE);
} else {
ui_room_join(room, TRUE);
}
muc_invites_remove(room);
muc_roster_set_complete(room);
GList *roster = muc_roster(room);
ui_room_roster(room, roster, NULL);
char *subject = muc_subject(room);
if (subject != NULL) {
ui_room_subject(room, NULL, subject);
}
GList *pending_broadcasts = muc_pending_broadcasts(room);
if (pending_broadcasts != NULL) {
GList *curr = pending_broadcasts;
while (curr != NULL) {
ui_room_broadcast(room, curr->data);
curr = g_list_next(curr);
}
}
// room configuration required
if (config_required) {
handle_room_requires_config(room);
muc_set_requires_config(room, TRUE);
ui_room_requires_config(room);
}
}
@ -770,22 +717,36 @@ handle_muc_self_online(const char * const room, const char * const nick, gboolea
void
handle_muc_occupant_online(const char * const room, const char * const nick, const char * const jid,
const char * const role, const char * const affiliation, const char * const show_str, const char * const status_str)
const char * const role, const char * const affiliation, const char * const show, const char * const status)
{
if (!muc_roster_complete(room)) {
muc_roster_add(room, nick, jid, role, affiliation, show_str, status_str);
muc_roster_add(room, nick, jid, role, affiliation, show, status);
} else {
char *old_nick = muc_roster_nick_change_complete(room, nick);
if (old_nick) {
muc_roster_add(room, nick, jid, role, affiliation, show_str, status_str);
handle_room_member_nick_change(room, old_nick, nick);
muc_roster_add(room, nick, jid, role, affiliation, show, status);
ui_room_member_nick_change(room, old_nick, nick);
free(old_nick);
} else {
if (!muc_roster_contains_nick(room, nick)) {
handle_room_member_online(room, nick, jid, role, affiliation, show_str, status_str);
muc_roster_add(room, nick, jid, role, affiliation, show, status);
char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
if (g_strcmp0(muc_status_pref, "none") != 0) {
ui_room_member_online(room, nick, show, status);
}
prefs_free_string(muc_status_pref);
} else {
handle_room_member_presence(room, nick, jid, role, affiliation, show_str, status_str);
gboolean updated = muc_roster_add(room, nick, jid, role, affiliation, show, status);
if (updated) {
char *muc_status_pref = prefs_get_string(PREF_STATUSES_MUC);
if (g_strcmp0(muc_status_pref, "all") == 0) {
ui_room_member_presence(room, nick, show, status);
}
prefs_free_string(muc_status_pref);
}
}
}
}

View File

@ -82,19 +82,9 @@ void handle_contact_offline(char *contact, char *resource, char *status);
void handle_contact_online(char *contact, Resource *resource,
GDateTime *last_activity);
void handle_leave_room(const char * const room);
void handle_room_nick_change(const char * const room,
const char * const nick);
void handle_room_requires_config(const char * const room);
void handle_room_destroy(const char * const room);
void handle_room_roster_complete(const char * const room);
void handle_room_member_presence(const char * const room, const char * const nick, const char * const jid,
const char * const role, const char * const affiliation, const char * const show, const char * const status);
void handle_room_member_online(const char * const room, const char * const nick, const char * const jid,
const char * const role, const char * const affiliation, const char * const show, const char * const status);
void handle_room_member_offline(const char * const room, const char * const nick,
void handle_room_occupant_offline(const char * const room, const char * const nick,
const char * const show, const char * const status);
void handle_room_member_nick_change(const char * const room,
const char * const old_nick, const char * const nick);
void handle_room_destroyed(const char * const room, const char * const new_jid, const char * const password,
const char * const reason);
void handle_room_kicked(const char * const room, const char * const actor, const char * const reason);

View File

@ -694,6 +694,20 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void *
char *room = from_jid->barejid;
char *nick = from_jid->resourcepart;
char *jid = NULL;
char *role = NULL;
char *affiliation = NULL;
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
if (x) {
xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(x, STANZA_NAME_ITEM);
if (item) {
jid = xmpp_stanza_get_attribute(item, "jid");
role = xmpp_stanza_get_attribute(item, "role");
affiliation = xmpp_stanza_get_attribute(item, "affiliation");
}
}
// handle self presence
if (stanza_is_muc_self_presence(stanza, jabber_get_fulljid())) {
log_debug("Room self presence received from %s", from_jid->fulljid);
@ -742,19 +756,6 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void *
// self online
} else {
gboolean config_required = stanza_muc_requires_config(stanza);
char *role = NULL;
char *affiliation = NULL;
// get own affiliation and role
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
if (x) {
xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(x, STANZA_NAME_ITEM);
if (item) {
role = xmpp_stanza_get_attribute(item, "role");
affiliation = xmpp_stanza_get_attribute(item, "affiliation");
}
}
handle_muc_self_online(room, nick, config_required, role, affiliation);
}
@ -768,7 +769,7 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void *
// handle nickname change
char *new_nick = stanza_get_new_nick(stanza);
if (new_nick) {
muc_roster_nick_change_start(room, new_nick, nick);
muc_occupant_nick_change_start(room, new_nick, nick);
// handle left room
} else {
@ -790,7 +791,7 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void *
// normal exit
} else {
handle_room_member_offline(room, nick, "offline", status_str);
handle_room_occupant_offline(room, nick, "offline", status_str);
}
}
@ -803,20 +804,6 @@ _muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void *
}
char *show_str = stanza_get_show(stanza, "online");
char *jid = NULL;
char *role = NULL;
char *affiliation = NULL;
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
if (x) {
xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(x, STANZA_NAME_ITEM);
if (item) {
jid = xmpp_stanza_get_attribute(item, "jid");
role = xmpp_stanza_get_attribute(item, "role");
affiliation = xmpp_stanza_get_attribute(item, "affiliation");
}
}
handle_muc_occupant_online(room, nick, jid, role, affiliation, show_str, status_str);
free(show_str);