From 9f5e8e7d1edd7f836765c2d751f68dd1e0bee960 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 3 Feb 2012 00:00:15 +0000 Subject: [PATCH] Added libstrophe examples --- .gitignore | 5 ++ Makefile | 18 ++++++ active.c | 128 ++++++++++++++++++++++++++++++++++++++ basic.c | 82 ++++++++++++++++++++++++ bot.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++ roster.c | 133 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 545 insertions(+) create mode 100644 Makefile create mode 100644 active.c create mode 100644 basic.c create mode 100644 bot.c create mode 100644 roster.c diff --git a/.gitignore b/.gitignore index 4441bd40..304f96b5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,8 @@ # *.[oa] # *~ profanity +active +roster +basic +bot +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..3bf1898f --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CC = gcc +CFLAGS = -O3 -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-variable +LIBS = -lxml2 -lssl -lresolv -lncurses -lstrophe + +profanity: clean + $(CC) profanity.c $(LIBS) -o profanity + $(CC) roster.c $(LIBS) -o roster + $(CC) active.c $(LIBS) -o active + $(CC) basic.c $(LIBS) -o basic + $(CC) bot.c $(LIBS) -o bot + +.PHONY: clean +clean: + rm -f profanity + rm -f roster + rm -f active + rm -f basic + rm -f bot diff --git a/active.c b/active.c new file mode 100644 index 00000000..79492b8b --- /dev/null +++ b/active.c @@ -0,0 +1,128 @@ +/* active.c +** libstrophe XMPP client library -- basic usage example +** +** Copyright (C) 2005-2009 Collecta, Inc. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +/* This example demonstrates basic handler functions by printing out +** active resources on a jabberd 2.x server. This program requires +** an admin account on a jabberd 2.x account in order to run. +*/ + +#include +#include + +#include + +int handle_reply(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, + void * const userdata) +{ + xmpp_stanza_t *query, *item; + char *type; + + 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("Active Sessions:\n"); + for (item = xmpp_stanza_get_children(query); item; + item = xmpp_stanza_get_next(item)) + printf("\t %s\n", xmpp_stanza_get_attribute(item, "jid")); + printf("END OF LIST\n"); + } + + /* disconnect */ + xmpp_disconnect(conn); + + return 0; +} + +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) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + xmpp_stanza_t *iq, *query; + + if (status == XMPP_CONN_CONNECT) { + fprintf(stderr, "DEBUG: connected\n"); + + /* 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, "active1"); + xmpp_stanza_set_attribute(iq, "to", "xxxxxxxxx.com"); + + query = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(query, "query"); + xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS); + xmpp_stanza_set_attribute(query, "node", "sessions"); + + 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, "active1", 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 main(int argc, char **argv) +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + + if (argc != 3) { + fprintf(stderr, "Usage: active \n\n"); + return 1; + } + + /* initialize lib */ + xmpp_initialize(); + + /* create a context */ + ctx = xmpp_ctx_new(NULL, NULL); + + /* create a connection */ + conn = xmpp_conn_new(ctx); + + /* setup authentication information */ + xmpp_conn_set_jid(conn, argv[1]); + xmpp_conn_set_pass(conn, argv[2]); + + /* initiate connection */ + xmpp_connect_client(conn, NULL, 0, conn_handler, ctx); + + /* start the event loop */ + xmpp_run(ctx); + + /* release our connection and context */ + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + + /* shutdown lib */ + xmpp_shutdown(); + + return 0; +} diff --git a/basic.c b/basic.c new file mode 100644 index 00000000..726dea55 --- /dev/null +++ b/basic.c @@ -0,0 +1,82 @@ +/* basic.c +** libstrophe XMPP client library -- basic usage example +** +** Copyright (C) 2005-2009 Collecta, Inc. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +#include + +#include + + +/* define a handler for connection events */ +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) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + + if (status == XMPP_CONN_CONNECT) { + fprintf(stderr, "DEBUG: connected\n"); + xmpp_disconnect(conn); + } + else { + fprintf(stderr, "DEBUG: disconnected\n"); + xmpp_stop(ctx); + } +} + +int main(int argc, char **argv) +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + xmpp_log_t *log; + char *jid, *pass; + + /* take a jid and password on the command line */ + if (argc != 3) { + fprintf(stderr, "Usage: basic \n\n"); + return 1; + } + + jid = argv[1]; + pass = argv[2]; + + /* init library */ + xmpp_initialize(); + + /* create a context */ + log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */ + ctx = xmpp_ctx_new(NULL, log); + + /* create a connection */ + conn = xmpp_conn_new(ctx); + + /* setup authentication information */ + xmpp_conn_set_jid(conn, jid); + xmpp_conn_set_pass(conn, pass); + + /* initiate connection */ + xmpp_connect_client(conn, NULL, 0, conn_handler, ctx); + + /* enter the event loop - + our connect handler will trigger an exit */ + xmpp_run(ctx); + + /* release our connection and context */ + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + + /* final shutdown of the library */ + xmpp_shutdown(); + + return 0; +} diff --git a/bot.c b/bot.c new file mode 100644 index 00000000..1084af45 --- /dev/null +++ b/bot.c @@ -0,0 +1,179 @@ +/* bot.c +** libstrophe XMPP client library -- basic usage example +** +** Copyright (C) 2005-2009 Collecta, Inc. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +/* simple bot example +** +** This example was provided by Matthew Wild . +** +** This bot responds to basic messages and iq version requests. +*/ + +#include +#include +#include + +#include + + +int version_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) +{ + xmpp_stanza_t *reply, *query, *name, *version, *text; + char *ns; + xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata; + printf("Received version request from %s\n", xmpp_stanza_get_attribute(stanza, "from")); + + reply = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(reply, "iq"); + xmpp_stanza_set_type(reply, "result"); + xmpp_stanza_set_id(reply, xmpp_stanza_get_id(stanza)); + xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from")); + + query = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(query, "query"); + ns = xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza)); + if (ns) { + xmpp_stanza_set_ns(query, ns); + } + + name = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(name, "name"); + xmpp_stanza_add_child(query, name); + + text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(text, "libstrophe example bot"); + xmpp_stanza_add_child(name, text); + + version = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(version, "version"); + xmpp_stanza_add_child(query, version); + + text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(text, "1.0"); + xmpp_stanza_add_child(version, text); + + xmpp_stanza_add_child(reply, query); + + xmpp_send(conn, reply); + xmpp_stanza_release(reply); + return 1; +} + + +int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) +{ + xmpp_stanza_t *reply, *body, *text; + char *intext, *replytext; + xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata; + + if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1; + if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1; + + intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body")); + + printf("Incoming message from %s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext); + + reply = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(reply, "message"); + xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza)?xmpp_stanza_get_type(stanza):"chat"); + xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from")); + + body = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(body, "body"); + + replytext = malloc(strlen(" to you too!") + strlen(intext) + 1); + strcpy(replytext, intext); + strcat(replytext, " to you too!"); + + text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(text, replytext); + xmpp_stanza_add_child(body, text); + xmpp_stanza_add_child(reply, body); + + xmpp_send(conn, reply); + xmpp_stanza_release(reply); + free(replytext); + return 1; +} + +/* define a handler for connection events */ +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) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + + if (status == XMPP_CONN_CONNECT) { + xmpp_stanza_t* pres; + fprintf(stderr, "DEBUG: connected\n"); + xmpp_handler_add(conn,version_handler, "jabber:iq:version", "iq", NULL, ctx); + xmpp_handler_add(conn,message_handler, NULL, "message", NULL, ctx); + + /* Send initial so that we appear online to contacts */ + pres = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(pres, "presence"); + xmpp_send(conn, pres); + xmpp_stanza_release(pres); + } + else { + fprintf(stderr, "DEBUG: disconnected\n"); + xmpp_stop(ctx); + } +} + +int main(int argc, char **argv) +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + xmpp_log_t *log; + char *jid, *pass; + + /* take a jid and password on the command line */ + if (argc != 3) { + fprintf(stderr, "Usage: bot \n\n"); + return 1; + } + + jid = argv[1]; + pass = argv[2]; + + /* init library */ + xmpp_initialize(); + + /* create a context */ + log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */ + ctx = xmpp_ctx_new(NULL, log); + + /* create a connection */ + conn = xmpp_conn_new(ctx); + + /* setup authentication information */ + xmpp_conn_set_jid(conn, jid); + xmpp_conn_set_pass(conn, pass); + + /* initiate connection */ + xmpp_connect_client(conn, NULL, 0, conn_handler, ctx); + + /* enter the event loop - + our connect handler will trigger an exit */ + xmpp_run(ctx); + + /* release our connection and context */ + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + + /* final shutdown of the library */ + xmpp_shutdown(); + + return 0; +} diff --git a/roster.c b/roster.c new file mode 100644 index 00000000..a6c5b08b --- /dev/null +++ b/roster.c @@ -0,0 +1,133 @@ +/* roster.c +** libstrophe XMPP client library -- handler example +** +** Copyright (C) 2005-2009 Collecta, Inc. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +/* This example demonstrates basic handler functions by printing out +** the user's roster. +*/ + +#include +#include + +#include + +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; +} + +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) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + xmpp_stanza_t *iq, *query; + + if (status == XMPP_CONN_CONNECT) { + fprintf(stderr, "DEBUG: connected\n"); + + /* 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 main(int argc, char **argv) +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + + if (argc != 3) { + fprintf(stderr, "Usage: roster \n\n"); + return 1; + } + + /* initialize lib */ + xmpp_initialize(); + + /* create a context */ + ctx = xmpp_ctx_new(NULL, NULL); + + /* create a connection */ + conn = xmpp_conn_new(ctx); + + /* setup authentication information */ + xmpp_conn_set_jid(conn, argv[1]); + xmpp_conn_set_pass(conn, argv[2]); + + /* initiate connection */ + xmpp_connect_client(conn, NULL, 0, conn_handler, ctx); + + /* start the event loop */ + xmpp_run(ctx); + + /* release our connection and context */ + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + + /* shutdown lib */ + xmpp_shutdown(); + + return 0; +}