]> git.mdlowis.com Git - archive/acc.git/commitdiff
Print the roster and quit
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 4 Mar 2016 02:33:15 +0000 (21:33 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 4 Mar 2016 02:33:15 +0000 (21:33 -0500)
Makefile
source/main.c

index ce98623d7a7f7375de14acfbdf0e73b40d2e7c10..4fdd75dd0e545d2564cf0f07310d963cad919e27 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,7 +13,7 @@ LIBS     = -lexpat -lssl -lresolv
 CPPFLAGS = -DVERSION="$(VERSION)"
 CFLAGS   = -std=gnu11 ${INCS} ${CPPFLAGS}
 LDFLAGS  = ${LIBS}
-INCS     = -Iinclude/ -Isource/ -I/usr/include/
+INCS     = -Iinclude/ -Isource/ -I/usr/include/ -I/opt/local/include/
 
 # commands
 COMPILE = ${CC} ${CFLAGS} -c -o $@ $<
index 3b3b853841d89ff030bfde1fb714550fd48f5ae7..71a287549bfda8c14911da30c94a90632694ae4c 100644 (file)
@@ -7,21 +7,19 @@
 #include "ini.h"
 #include "strophe.h"
 
-char* ARGV0     = NULL;
-char* User      = NULL;
-char* Pass      = NULL;
-char* Resource  = NULL;
-char* Alias     = NULL;
-char* Server    = NULL;
-char* FileProxy = NULL;
-int   Port      = 5222;
-long  Flags     = 0;
+char* ARGV0  = NULL;
+char* User   = NULL;
+char* Pass   = NULL;
+char* Server = NULL;
+int   Port   = 5222;
+long  Flags  = 0;
 
 static void usage(void);
 static void load_config(void);
 void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
                   const int error, xmpp_stream_error_t * const stream_error,
                   void * const userdata);
+int handle_reply(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata);
 
 int main(int argc, char** argv)
 {
@@ -41,7 +39,7 @@ int main(int argc, char** argv)
     xmpp_conn_set_flags(conn, Flags);
     xmpp_conn_set_jid(conn,  User);
     xmpp_conn_set_pass(conn, Pass);
-    xmpp_connect_client(conn, Server, 0, conn_handler, ctx);
+    xmpp_connect_client(conn, Server, Port, conn_handler, ctx);
     /* enter the event loop our connect handler will trigger an exit */
     xmpp_run(ctx);
     /* gracefully shut everything down */
@@ -62,21 +60,14 @@ static void load_config(void)
     inientry_t entry  = { 0 };
     inifile_t cfgfile = { .file = fopen("./config.ini","r") };
     while (iniparse(&cfgfile, &entry)) {
-        //printf("[%s] '%s' = '%s'\n", entry.section, entry.name, entry.value);
         if (inimatch("user",&entry))
             User = estrdup(entry.value);
         else if (inimatch("pass",&entry))
             Pass = estrdup(entry.value);
-        else if (inimatch("resource",&entry))
-            Resource = estrdup(entry.value);
-        else if (inimatch("alias",&entry))
-            Alias = estrdup(entry.value);
         else if (inimatch("port",&entry))
             Port = strtoul(entry.value,NULL,0);
         else if (inimatch("server",&entry))
             Server = estrdup(entry.value);
-        else if (inimatch("fileproxy",&entry))
-            FileProxy = estrdup(entry.value);
     }
 }
 
@@ -85,6 +76,7 @@ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
                   void * const userdata)
 {
     xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
+    xmpp_stanza_t *iq, *query;
     int secured;
 
     if (status == XMPP_CONN_CONNECT) {
@@ -92,11 +84,62 @@ void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status,
         secured = xmpp_conn_is_secured(conn);
         fprintf(stderr, "DEBUG: connection is %s.\n",
                 secured ? "secured" : "NOT secured");
-        xmpp_disconnect(conn);
-    }
-    else {
+
+        /* create iq stanza for request */
+        iq = xmpp_stanza_new(ctx);
+        xmpp_stanza_set_name(iq, "iq");
+        xmpp_stanza_set_type(iq, "get");
+        xmpp_stanza_set_id(iq, "roster1");
+
+        query = xmpp_stanza_new(ctx);
+        xmpp_stanza_set_name(query, "query");
+        xmpp_stanza_set_ns(query, XMPP_NS_ROSTER);
+
+        xmpp_stanza_add_child(iq, query);
+
+        /* we can release the stanza since it belongs to iq now */
+        xmpp_stanza_release(query);
+
+        /* set up reply handler */
+        xmpp_id_handler_add(conn, handle_reply, "roster1", ctx);
+
+        /* send out the stanza */
+        xmpp_send(conn, iq);
+
+        /* release the stanza */
+        xmpp_stanza_release(iq);
+    } else {
         fprintf(stderr, "DEBUG: disconnected\n");
         xmpp_stop(ctx);
     }
 }
 
+int handle_reply(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
+{
+    xmpp_stanza_t *query, *item;
+    char *type, *name;
+
+    type = xmpp_stanza_get_type(stanza);
+    if (strcmp(type, "error") == 0) {
+        fprintf(stderr, "ERROR: query failed\n");
+    } else {
+        query = xmpp_stanza_get_child_by_name(stanza, "query");
+        printf("Roster:\n");
+        for (item = xmpp_stanza_get_children(query); item; item = xmpp_stanza_get_next(item))
+            if ((name = xmpp_stanza_get_attribute(item, "name")))
+                printf("\t %s (%s) sub=%s\n",
+                       name,
+                       xmpp_stanza_get_attribute(item, "jid"),
+                       xmpp_stanza_get_attribute(item, "subscription"));
+            else
+                printf("\t %s sub=%s\n",
+                       xmpp_stanza_get_attribute(item, "jid"),
+                       xmpp_stanza_get_attribute(item, "subscription"));
+        printf("END OF LIST\n");
+    }
+
+    /* disconnect */
+    xmpp_disconnect(conn);
+
+    return 0;
+}