mirror of
https://github.com/strophe/libstrophe.git
synced 2026-07-18 20:06:21 +00:00
Having debugging on by default in the example seems to help people trying out the library, and with the default_logger() call it's not too much extra complication. We should also write an example/complex.c that exercises all the callback options, a custom allocator and so on.
90 lines
2.3 KiB
C
90 lines
2.3 KiB
C
/* basic.c
|
|
** libstrophe XMPP client library -- basic usage example
|
|
**
|
|
** Copyright (C) 2005 OGG, LCC. All rights reserved.
|
|
**
|
|
** 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 <stdio.h>
|
|
|
|
#include <strophe.h>
|
|
|
|
|
|
/* 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;
|
|
char *server;
|
|
|
|
/* take a jid and password on the command line,
|
|
with optional server to connect to */
|
|
if ((argc < 3) || (argc > 4)) {
|
|
fprintf(stderr, "Usage: basic <jid> <pass> [<server>]\n\n");
|
|
return 1;
|
|
}
|
|
|
|
jid = argv[1];
|
|
pass = argv[2];
|
|
server = NULL;
|
|
/* Normally we pass NULL for the connection domain, in which case
|
|
the library derives the target host from the jid, but we can
|
|
override this for testing. */
|
|
if (argc >= 4) server = argv[3];
|
|
|
|
/* 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, server, 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;
|
|
}
|