From: Michael D. Lowis Date: Fri, 4 Mar 2016 02:33:15 +0000 (-0500) Subject: Print the roster and quit X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=f74f0ecbdde696b7a06f1df3bddcda3ea3cd8b87;p=archive%2Facc.git Print the roster and quit --- diff --git a/Makefile b/Makefile index ce98623..4fdd75d 100644 --- 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 $@ $< diff --git a/source/main.c b/source/main.c index 3b3b853..71a2875 100644 --- a/source/main.c +++ b/source/main.c @@ -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; +}