egra: agg mini code cleanups
[iv.d.git] / libstrophe / samples / basic.d
blob1ea6fc5cb2a58975fa6871f1a6b0099da3ad874f
1 /* basic.c
2 ** libstrophe XMPP client library -- basic usage example
3 **
4 ** Copyright (C) 2005-2009 Collecta, Inc.
5 **
6 ** This software is provided AS-IS with no warranty, either express
7 ** or implied.
8 **
9 ** This program is dual licensed under the MIT and GPLv3 licenses.
11 import iv.libstrophe;
13 import iv.vfs.io;
16 // hardcoded TCP keepalive timeout and interval
17 enum KA_TIMEOUT = 60;
18 enum KA_INTERVAL = 1;
21 // define a handler for connection events
22 extern(C) void conn_handler (xmpp_conn_t* conn, xmpp_conn_event_t status, int error, xmpp_stream_error_t* stream_error, void* userdata) nothrow {
23 try {
24 xmpp_ctx_t* ctx = cast(xmpp_ctx_t*)userdata;
25 int secured;
26 if (status == XMPP_CONN_CONNECT) {
27 stderr.writeln("DEBUG: connected");
28 secured = xmpp_conn_is_secured(conn);
29 stderr.writefln("DEBUG: connection is %ssecured.", (secured ? "" : "NOT "));
30 xmpp_disconnect(conn);
31 } else {
32 stderr.writeln("DEBUG: disconnected");
33 xmpp_stop(ctx);
35 } catch (Exception e) {}
39 int main (string[] args) {
40 xmpp_ctx_t* ctx;
41 xmpp_conn_t* conn;
42 xmpp_log_t* log;
43 char* jid, pass, host;
44 xmpp_long flags = 0;
45 int tcp_keepalive = 0;
46 int i;
48 // take a jid and password on the command line
49 for (i = 1; i < args.length; ++i) {
50 if (args[i] == "--disable-tls") flags |= XMPP_CONN_FLAG_DISABLE_TLS;
51 else if (args[i] == "--mandatory-tls") flags |= XMPP_CONN_FLAG_MANDATORY_TLS;
52 else if (args[i] == "--legacy-ssl") flags |= XMPP_CONN_FLAG_LEGACY_SSL;
53 else if (args[i] == "--tcp-keepalive") tcp_keepalive = 1;
54 else break;
56 if (args.length-i < 2 || args.length-i > 3) {
57 stderr.writeln(
58 "Usage: basic [options] <jid> <pass> [<host>]\n\n",
59 "Options:\n",
60 " --disable-tls Disable TLS.\n",
61 " --mandatory-tls Deny plaintext connection.\n",
62 " --legacy-ssl Use old style SSL.\n",
63 " --tcp-keepalive Configure TCP keepalive.\n\n",
64 "Note: --disable-tls conflicts with --mandatory-tls or --legacy-ssl");
65 return 1;
69 jid = args[i].xmpp_toStrz;
70 pass = args[i+1].xmpp_toStrz;
71 if (i+2 < args.length) host = args[i+2].xmpp_toStrz;
74 * Note, this example doesn't handle errors. Applications should check
75 * return values of non-void functions.
78 // init library
79 xmpp_initialize();
80 scope(exit) xmpp_shutdown(); // final shutdown of the library
82 // create a context
83 log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); // pass null instead to silence output
84 ctx = xmpp_ctx_new(null, log);
85 scope(exit) xmpp_ctx_free(ctx);
87 // create a connection
88 conn = xmpp_conn_new(ctx);
89 scope(exit) xmpp_conn_release(conn);
91 // configure connection properties (optional)
92 xmpp_conn_set_flags(conn, flags);
93 // configure TCP keepalive (optional)
94 if (tcp_keepalive) xmpp_conn_set_keepalive(conn, KA_TIMEOUT, KA_INTERVAL);
96 // setup authentication information
97 xmpp_conn_set_jid(conn, jid);
98 xmpp_conn_set_pass(conn, pass);
100 // initiate connection
101 xmpp_connect_client(conn, host, 0, &conn_handler, ctx);
103 // enter the event loop -- our connect handler will trigger an exit
104 xmpp_run(ctx);
106 return 0;