From 965f048b18f7f7456bcb59dca841784aad3bfa11 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 6 Oct 2014 21:42:09 +0100 Subject: Implemented setting and listing roles --- src/xmpp/iq.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 126 insertions(+), 11 deletions(-) (limited to 'src/xmpp/iq.c') diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index c24e4ecc..b9a646af 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -84,6 +84,10 @@ static int _room_affiliation_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _room_affiliation_set_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_role_set_result_handler(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata); +static int _room_role_list_result_handler(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata); static int _room_kick_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _manual_pong_handler(xmpp_conn_t *const conn, @@ -309,9 +313,9 @@ _iq_room_kick_occupant(const char * const room, const char * const nick, const c xmpp_stanza_release(iq); } -struct affiliation_set_t { - char *jid; - char *affiliation; +struct privilege_set_t { + char *item; + char *privilege; }; static void @@ -324,9 +328,9 @@ _iq_room_affiliation_set(const char * const room, const char * const jid, char * char *id = xmpp_stanza_get_id(iq); - struct affiliation_set_t *affiliation_set = malloc(sizeof(struct affiliation_set_t)); - affiliation_set->jid = strdup(jid); - affiliation_set->affiliation = strdup(affiliation); + struct privilege_set_t *affiliation_set = malloc(sizeof(struct privilege_set_t)); + affiliation_set->item = strdup(jid); + affiliation_set->privilege = strdup(affiliation); xmpp_id_handler_add(conn, _room_affiliation_set_result_handler, id, affiliation_set); @@ -334,6 +338,40 @@ _iq_room_affiliation_set(const char * const room, const char * const jid, char * xmpp_stanza_release(iq); } +static void +_iq_room_role_set(const char * const room, const char * const nick, char *role, + const char * const reason) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_room_role_set_iq(ctx, room, nick, role, reason); + + char *id = xmpp_stanza_get_id(iq); + + struct privilege_set_t *role_set = malloc(sizeof(struct privilege_set_t)); + role_set->item = strdup(nick); + role_set->privilege = strdup(role); + + xmpp_id_handler_add(conn, _room_role_set_result_handler, id, role_set); + + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + +static void +_iq_room_role_list(const char * const room, char *role) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_room_role_list_iq(ctx, room, role); + + char *id = xmpp_stanza_get_id(iq); + xmpp_id_handler_add(conn, _room_role_list_result_handler, id, strdup(role)); + + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + static void _iq_send_ping(const char * const target) { @@ -841,7 +879,7 @@ static int _room_affiliation_set_result_handler(xmpp_conn_t * const conn, xmpp_s const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); const char *type = xmpp_stanza_get_type(stanza); const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); - struct affiliation_set_t *affiliation_set = (struct affiliation_set_t *)userdata; + struct privilege_set_t *affiliation_set = (struct privilege_set_t *)userdata; if (id != NULL) { log_debug("IQ affiliation set handler fired, id: %s.", id); @@ -852,19 +890,49 @@ static int _room_affiliation_set_result_handler(xmpp_conn_t * const conn, xmpp_s // handle error responses if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { char *error_message = stanza_get_error_message(stanza); - handle_room_affiliation_set_error(from, affiliation_set->jid, affiliation_set->affiliation, error_message); + handle_room_affiliation_set_error(from, affiliation_set->item, affiliation_set->privilege, error_message); free(error_message); } else { - handle_room_affiliation_set(from, affiliation_set->jid, affiliation_set->affiliation); + handle_room_affiliation_set(from, affiliation_set->item, affiliation_set->privilege); } - free(affiliation_set->jid); - free(affiliation_set->affiliation); + free(affiliation_set->item); + free(affiliation_set->privilege); free(affiliation_set); return 0; } +static int _room_role_set_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); + const char *type = xmpp_stanza_get_type(stanza); + const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + struct privilege_set_t *role_set = (struct privilege_set_t *)userdata; + + if (id != NULL) { + log_debug("IQ role set handler fired, id: %s.", id); + } else { + log_debug("IQ role set handler fired."); + } + + // handle error responses + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + char *error_message = stanza_get_error_message(stanza); + handle_room_role_set_error(from, role_set->item, role_set->privilege, error_message); + free(error_message); + } else { + handle_room_role_set(from, role_set->item, role_set->privilege); + } + + free(role_set->item); + free(role_set->privilege); + free(role_set); + + return 0; +} + static int _room_affiliation_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { @@ -910,6 +978,51 @@ _room_affiliation_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * return 0; } +static int +_room_role_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) +{ + const char *id = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_ID); + const char *type = xmpp_stanza_get_type(stanza); + const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + char *role = (char *)userdata; + + if (id != NULL) { + log_debug("IQ role list result handler fired, id: %s.", id); + } else { + log_debug("IQ role list result handler fired."); + } + + // handle error responses + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + char *error_message = stanza_get_error_message(stanza); + handle_room_role_list_result_error(from, role, error_message); + free(error_message); + free(role); + return 0; + } + GSList *nicks = NULL; + + xmpp_stanza_t *query = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_ADMIN); + if (query) { + xmpp_stanza_t *child = xmpp_stanza_get_children(query); + while (child) { + char *name = xmpp_stanza_get_name(child); + if (g_strcmp0(name, "item") == 0) { + char *nick = xmpp_stanza_get_attribute(child, STANZA_ATTR_NICK); + if (nick) { + nicks = g_slist_insert_sorted(nicks, nick, (GCompareFunc)g_strcmp0); + } + } + child = xmpp_stanza_get_next(child); + } + } + + handle_room_role_list(from, role, nicks); + free(role); + + return 0; +} + static int _room_config_submit_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) @@ -1141,5 +1254,7 @@ iq_init_module(void) iq_room_info_request = _iq_room_info_request; iq_room_affiliation_set = _iq_room_affiliation_set; iq_room_affiliation_list = _iq_room_affiliation_list; + iq_room_role_set = _iq_room_role_set; iq_room_kick_occupant = _iq_room_kick_occupant; + iq_room_role_list = _iq_room_role_list; } \ No newline at end of file -- cgit 1.4.1-2-gfad0