2 ** libstrophe XMPP client library -- basic usage example
4 ** Copyright (C) 2005-2009 Collecta, Inc.
6 ** This software is provided AS-IS with no warranty, either express
9 ** This program is dual licensed under the MIT and GPLv3 licenses.
16 // hardcoded TCP keepalive timeout and interval
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 {
24 xmpp_ctx_t
* ctx
= cast(xmpp_ctx_t
*)userdata
;
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
);
32 stderr
.writeln("DEBUG: disconnected");
35 } catch (Exception e
) {}
39 int main (string
[] args
) {
43 char* jid
, pass
, host
;
45 int tcp_keepalive
= 0;
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;
56 if (args
.length
-i
< 2 || args
.length
-i
> 3) {
58 "Usage: basic [options] <jid> <pass> [<host>]\n\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");
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.
80 scope(exit
) xmpp_shutdown(); // final shutdown of the library
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