about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-10-06 21:42:09 +0100
committerJames Booth <boothj5@gmail.com>2014-10-06 21:42:09 +0100
commit965f048b18f7f7456bcb59dca841784aad3bfa11 (patch)
tree810e4e479e91e9db57a9d760afd09cd73f56298b /src/xmpp
parent2aeaad230e0095216c6a226ab4da8b03b3198e81 (diff)
downloadprofani-tty-965f048b18f7f7456bcb59dca841784aad3bfa11.tar.gz
Implemented setting and listing roles
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/iq.c137
-rw-r--r--src/xmpp/stanza.c68
-rw-r--r--src/xmpp/stanza.h4
-rw-r--r--src/xmpp/xmpp.h3
4 files changed, 201 insertions, 11 deletions
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);
 
@@ -335,6 +339,40 @@ _iq_room_affiliation_set(const char * const room, const char * const jid, char *
 }
 
 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)
 {
     xmpp_conn_t * const conn = connection_get_conn();
@@ -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)
 {
@@ -911,6 +979,51 @@ _room_affiliation_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t *
 }
 
 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
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index 07e96659..03b81bf3 100644
--- a/src/xmpp/stanza.c
+++ b/src/xmpp/stanza.c
@@ -579,6 +579,33 @@ stanza_create_room_affiliation_list_iq(xmpp_ctx_t *ctx, const char * const room,
 }
 
 xmpp_stanza_t *
+stanza_create_room_role_list_iq(xmpp_ctx_t *ctx, const char * const room, const char * const role)
+{
+    xmpp_stanza_t *iq = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(iq, STANZA_NAME_IQ);
+    xmpp_stanza_set_type(iq, STANZA_TYPE_GET);
+    xmpp_stanza_set_attribute(iq, STANZA_ATTR_TO, room);
+    char *id = create_unique_id("role_get");
+    xmpp_stanza_set_id(iq, id);
+    free(id);
+
+    xmpp_stanza_t *query = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
+    xmpp_stanza_set_ns(query, STANZA_NS_MUC_ADMIN);
+
+    xmpp_stanza_t *item = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(item, STANZA_NAME_ITEM);
+    xmpp_stanza_set_attribute(item, "role", role);
+
+    xmpp_stanza_add_child(query, item);
+    xmpp_stanza_release(item);
+    xmpp_stanza_add_child(iq, query);
+    xmpp_stanza_release(query);
+
+    return iq;
+}
+
+xmpp_stanza_t *
 stanza_create_room_affiliation_set_iq(xmpp_ctx_t *ctx, const char * const room, const char * const jid,
     const char * const affiliation, const char * const reason)
 {
@@ -620,6 +647,47 @@ stanza_create_room_affiliation_set_iq(xmpp_ctx_t *ctx, const char * const room,
 }
 
 xmpp_stanza_t *
+stanza_create_room_role_set_iq(xmpp_ctx_t * const ctx, const char * const room, const char * const nick,
+    const char * const role, const char * const reason)
+{
+    xmpp_stanza_t *iq = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(iq, STANZA_NAME_IQ);
+    xmpp_stanza_set_type(iq, STANZA_TYPE_SET);
+    xmpp_stanza_set_attribute(iq, STANZA_ATTR_TO, room);
+    char *id = create_unique_id("role_set");
+    xmpp_stanza_set_id(iq, id);
+    free(id);
+
+    xmpp_stanza_t *query = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
+    xmpp_stanza_set_ns(query, STANZA_NS_MUC_ADMIN);
+
+    xmpp_stanza_t *item = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(item, STANZA_NAME_ITEM);
+    xmpp_stanza_set_attribute(item, "role", role);
+    xmpp_stanza_set_attribute(item, STANZA_ATTR_NICK, nick);
+
+    if (reason) {
+        xmpp_stanza_t *reason_st = xmpp_stanza_new(ctx);
+        xmpp_stanza_set_name(reason_st, STANZA_NAME_REASON);
+        xmpp_stanza_t *reason_text = xmpp_stanza_new(ctx);
+        xmpp_stanza_set_text(reason_text, reason);
+        xmpp_stanza_add_child(reason_st, reason_text);
+        xmpp_stanza_release(reason_text);
+
+        xmpp_stanza_add_child(item, reason_st);
+        xmpp_stanza_release(reason_st);
+    }
+
+    xmpp_stanza_add_child(query, item);
+    xmpp_stanza_release(item);
+    xmpp_stanza_add_child(iq, query);
+    xmpp_stanza_release(query);
+
+    return iq;
+}
+
+xmpp_stanza_t *
 stanza_create_room_kick_iq(xmpp_ctx_t * const ctx, const char * const room, const char * const nick,
     const char * const reason)
 {
diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h
index ae673848..56aea01b 100644
--- a/src/xmpp/stanza.h
+++ b/src/xmpp/stanza.h
@@ -210,6 +210,10 @@ xmpp_stanza_t* stanza_create_room_affiliation_list_iq(xmpp_ctx_t *ctx, const cha
     const char * const affiliation);
 xmpp_stanza_t* stanza_create_room_affiliation_set_iq(xmpp_ctx_t *ctx, const char * const room, const char * const jid,
     const char * const affiliation, const char * const reason);
+xmpp_stanza_t* stanza_create_room_role_set_iq(xmpp_ctx_t * const ctx, const char * const room, const char * const jid,
+    const char * const role, const char * const reason);
+xmpp_stanza_t* stanza_create_room_role_list_iq(xmpp_ctx_t *ctx, const char * const room, const char * const role);
+
 xmpp_stanza_t* stanza_create_room_subject_message(xmpp_ctx_t *ctx, const char * const room, const char * const subject);
 xmpp_stanza_t* stanza_create_room_kick_iq(xmpp_ctx_t * const ctx, const char * const room, const char * const nick,
     const char * const reason);
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index fc75a7ec..798cb339 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -198,6 +198,9 @@ void (*iq_room_affiliation_list)(const char * const room, char *affiliation);
 void (*iq_room_affiliation_set)(const char * const room, const char * const jid, char *affiliation,
     const char * const reason);
 void (*iq_room_kick_occupant)(const char * const room, const char * const nick, const char * const reason);
+void (*iq_room_role_set)(const char * const room, const char * const nick, char *role,
+    const char * const reason);
+void (*iq_room_role_list)(const char * const room, char *role);
 
 // caps functions
 Capabilities* (*caps_lookup)(const char * const jid);