6370d9ec19ef3ce796a1a05d7f775efa1176a5a0
[public/netxms.git] / include / strophe.h
1 /* strophe.h
2 ** strophe XMPP client library C API
3 **
4 ** Copyright (C) 2005-2009 Collecta, Inc.
5 **
6 ** This software is provided AS-IS with no warranty, either express or
7 ** implied.
8 **
9 ** This software is distributed under license and may not be copied,
10 ** modified or distributed except as expressly authorized under the
11 ** terms of the license contained in the file LICENSE.txt in this
12 ** distribution.
13 */
14
15 /** @file
16 * Strophe public C API definitions.
17 */
18
19 #ifndef __LIBSTROPHE_STROPHE_H__
20 #define __LIBSTROPHE_STROPHE_H__
21
22 #ifdef _WIN32
23 #ifdef LIBSTROPHE_EXPORTS
24 #define LIBSTROPHE_EXPORTABLE __declspec(dllexport)
25 #else
26 #define LIBSTROPHE_EXPORTABLE __declspec(dllimport)
27 #endif
28 #else
29 #define LIBSTROPHE_EXPORTABLE
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 #include <stdio.h>
37
38 /* namespace defines */
39 /** @def XMPP_NS_CLIENT
40 * Namespace definition for 'jabber:client'.
41 */
42 #define XMPP_NS_CLIENT "jabber:client"
43 /** @def XMPP_NS_COMPONENT
44 * Namespace definition for 'jabber:component:accept'.
45 */
46 #define XMPP_NS_COMPONENT "jabber:component:accept"
47 /** @def XMPP_NS_STREAMS
48 * Namespace definition for 'http://etherx.jabber.org/streams'.
49 */
50 #define XMPP_NS_STREAMS "http://etherx.jabber.org/streams"
51 /** @def XMPP_NS_STREAMS_IETF
52 * Namespace definition for 'urn:ietf:params:xml:ns:xmpp-streams'.
53 */
54 #define XMPP_NS_STREAMS_IETF "urn:ietf:params:xml:ns:xmpp-streams"
55 /** @def XMPP_NS_TLS
56 * Namespace definition for 'url:ietf:params:xml:ns:xmpp-tls'.
57 */
58 #define XMPP_NS_TLS "urn:ietf:params:xml:ns:xmpp-tls"
59 /** @def XMPP_NS_SASL
60 * Namespace definition for 'urn:ietf:params:xml:ns:xmpp-sasl'.
61 */
62 #define XMPP_NS_SASL "urn:ietf:params:xml:ns:xmpp-sasl"
63 /** @def XMPP_NS_BIND
64 * Namespace definition for 'urn:ietf:params:xml:ns:xmpp-bind'.
65 */
66 #define XMPP_NS_BIND "urn:ietf:params:xml:ns:xmpp-bind"
67 /** @def XMPP_NS_SESSION
68 * Namespace definition for 'urn:ietf:params:xml:ns:xmpp-session'.
69 */
70 #define XMPP_NS_SESSION "urn:ietf:params:xml:ns:xmpp-session"
71 /** @def XMPP_NS_AUTH
72 * Namespace definition for 'jabber:iq:auth'.
73 */
74 #define XMPP_NS_AUTH "jabber:iq:auth"
75 /** @def XMPP_NS_DISCO_INFO
76 * Namespace definition for 'http://jabber.org/protocol/disco#info'.
77 */
78 #define XMPP_NS_DISCO_INFO "http://jabber.org/protocol/disco#info"
79 /** @def XMPP_NS_DISCO_ITEMS
80 * Namespace definition for 'http://jabber.org/protocol/disco#items'.
81 */
82 #define XMPP_NS_DISCO_ITEMS "http://jabber.org/protocol/disco#items"
83 /** @def XMPP_NS_ROSTER
84 * Namespace definition for 'jabber:iq:roster'.
85 */
86 #define XMPP_NS_ROSTER "jabber:iq:roster"
87
88 /* error defines */
89 /** @def XMPP_EOK
90 * Success error code.
91 */
92 #define XMPP_EOK 0
93 /** @def XMPP_EMEM
94 * Memory related failure error code.
95 *
96 * This is returned on allocation errors and signals that the host may
97 * be out of memory.
98 */
99 #define XMPP_EMEM -1
100 /** @def XMPP_EINVOP
101 * Invalid operation error code.
102 *
103 * This error code is returned when the operation was invalid and signals
104 * that the Strophe API is being used incorrectly.
105 */
106 #define XMPP_EINVOP -2
107 /** @def XMPP_EINT
108 * Internal failure error code.
109 */
110 #define XMPP_EINT -3
111
112 /* initialization and shutdown */
113 void LIBSTROPHE_EXPORTABLE xmpp_initialize();
114 void LIBSTROPHE_EXPORTABLE xmpp_shutdown();
115
116 /* version */
117 int LIBSTROPHE_EXPORTABLE xmpp_version_check(int major, int minor);
118
119 /* run-time contexts */
120
121 /* user-replaceable memory allocator */
122 typedef struct _xmpp_mem_t xmpp_mem_t;
123
124 /* user-replaceable log object */
125 typedef struct _xmpp_log_t xmpp_log_t;
126
127 /* opaque run time context containing the above hooks */
128 typedef struct _xmpp_ctx_t xmpp_ctx_t;
129
130 xmpp_ctx_t LIBSTROPHE_EXPORTABLE *xmpp_ctx_new(const xmpp_mem_t * const mem,
131 const xmpp_log_t * const log);
132 void LIBSTROPHE_EXPORTABLE xmpp_ctx_free(xmpp_ctx_t * const ctx);
133
134 struct _xmpp_mem_t {
135 void *(*alloc)(const size_t size, void * const userdata);
136 void (*free)(void *p, void * const userdata);
137 void *(*realloc)(void *p, const size_t size, void * const userdata);
138 void *userdata;
139 };
140
141 typedef enum {
142 XMPP_LEVEL_DEBUG,
143 XMPP_LEVEL_INFO,
144 XMPP_LEVEL_WARN,
145 XMPP_LEVEL_ERROR
146 } xmpp_log_level_t;
147
148 typedef enum {
149 XMPP_UNKNOWN,
150 XMPP_CLIENT,
151 XMPP_COMPONENT
152 } xmpp_conn_type_t;
153
154 typedef void (*xmpp_log_handler)(void * const userdata,
155 const xmpp_log_level_t level,
156 const char * const area,
157 const char * const msg);
158
159 struct _xmpp_log_t {
160 xmpp_log_handler handler;
161 void *userdata;
162 /* mutex_t lock; */
163 };
164
165 /* return a default logger filtering at a given level */
166 xmpp_log_t LIBSTROPHE_EXPORTABLE *xmpp_get_default_logger(xmpp_log_level_t level);
167
168 /* connection */
169
170 /* opaque connection object */
171 typedef struct _xmpp_conn_t xmpp_conn_t;
172 typedef struct _xmpp_stanza_t xmpp_stanza_t;
173
174 /* connect callback */
175 typedef enum {
176 XMPP_CONN_CONNECT,
177 XMPP_CONN_DISCONNECT,
178 XMPP_CONN_FAIL
179 } xmpp_conn_event_t;
180
181 typedef enum {
182 XMPP_SE_BAD_FORMAT,
183 XMPP_SE_BAD_NS_PREFIX,
184 XMPP_SE_CONFLICT,
185 XMPP_SE_CONN_TIMEOUT,
186 XMPP_SE_HOST_GONE,
187 XMPP_SE_HOST_UNKNOWN,
188 XMPP_SE_IMPROPER_ADDR,
189 XMPP_SE_INTERNAL_SERVER_ERROR,
190 XMPP_SE_INVALID_FROM,
191 XMPP_SE_INVALID_ID,
192 XMPP_SE_INVALID_NS,
193 XMPP_SE_INVALID_XML,
194 XMPP_SE_NOT_AUTHORIZED,
195 XMPP_SE_POLICY_VIOLATION,
196 XMPP_SE_REMOTE_CONN_FAILED,
197 XMPP_SE_RESOURCE_CONSTRAINT,
198 XMPP_SE_RESTRICTED_XML,
199 XMPP_SE_SEE_OTHER_HOST,
200 XMPP_SE_SYSTEM_SHUTDOWN,
201 XMPP_SE_UNDEFINED_CONDITION,
202 XMPP_SE_UNSUPPORTED_ENCODING,
203 XMPP_SE_UNSUPPORTED_STANZA_TYPE,
204 XMPP_SE_UNSUPPORTED_VERSION,
205 XMPP_SE_XML_NOT_WELL_FORMED
206 } xmpp_error_type_t;
207
208 typedef struct {
209 xmpp_error_type_t type;
210 char *text;
211 xmpp_stanza_t *stanza;
212 } xmpp_stream_error_t;
213
214 typedef void (*xmpp_conn_handler)(xmpp_conn_t * const conn,
215 const xmpp_conn_event_t event,
216 const int error,
217 xmpp_stream_error_t * const stream_error,
218 void * const userdata);
219
220 xmpp_conn_t LIBSTROPHE_EXPORTABLE *xmpp_conn_new(xmpp_ctx_t * const ctx);
221 xmpp_conn_t LIBSTROPHE_EXPORTABLE *xmpp_conn_clone(xmpp_conn_t * const conn);
222 int LIBSTROPHE_EXPORTABLE xmpp_conn_release(xmpp_conn_t * const conn);
223
224 const char LIBSTROPHE_EXPORTABLE *xmpp_conn_get_jid(const xmpp_conn_t * const conn);
225 const char LIBSTROPHE_EXPORTABLE *xmpp_conn_get_bound_jid(const xmpp_conn_t * const conn);
226 void LIBSTROPHE_EXPORTABLE xmpp_conn_set_jid(xmpp_conn_t * const conn, const char * const jid);
227 const char LIBSTROPHE_EXPORTABLE *xmpp_conn_get_pass(const xmpp_conn_t * const conn);
228 void LIBSTROPHE_EXPORTABLE xmpp_conn_set_pass(xmpp_conn_t * const conn, const char * const pass);
229 xmpp_ctx_t LIBSTROPHE_EXPORTABLE *xmpp_conn_get_context(xmpp_conn_t * const conn);
230 void LIBSTROPHE_EXPORTABLE xmpp_conn_disable_tls(xmpp_conn_t * const conn);
231
232 int LIBSTROPHE_EXPORTABLE xmpp_connect_client(xmpp_conn_t * const conn,
233 const char * const altdomain,
234 unsigned short altport,
235 xmpp_conn_handler callback,
236 void * const userdata);
237
238 /*
239 int xmpp_connect_component(conn, name)
240 */
241 void LIBSTROPHE_EXPORTABLE xmpp_disconnect(xmpp_conn_t * const conn);
242
243 void LIBSTROPHE_EXPORTABLE xmpp_send(xmpp_conn_t * const conn,
244 xmpp_stanza_t * const stanza);
245
246 void LIBSTROPHE_EXPORTABLE xmpp_send_raw_string(xmpp_conn_t * const conn,
247 const char * const fmt, ...);
248 void LIBSTROPHE_EXPORTABLE xmpp_send_raw(xmpp_conn_t * const conn,
249 const char * const data, const size_t len);
250
251
252 /* handlers */
253
254 /* if the handle returns false it is removed */
255 typedef int (*xmpp_timed_handler)(xmpp_conn_t * const conn,
256 void * const userdata);
257
258 void LIBSTROPHE_EXPORTABLE xmpp_timed_handler_add(xmpp_conn_t * const conn,
259 xmpp_timed_handler handler,
260 const unsigned long period,
261 void * const userdata);
262 void LIBSTROPHE_EXPORTABLE xmpp_timed_handler_delete(xmpp_conn_t * const conn,
263 xmpp_timed_handler handler);
264
265
266 /* if the handler returns false it is removed */
267 typedef int (*xmpp_handler)(xmpp_conn_t * const conn,
268 xmpp_stanza_t * const stanza,
269 void * const userdata);
270
271 void LIBSTROPHE_EXPORTABLE xmpp_handler_add(xmpp_conn_t * const conn,
272 xmpp_handler handler,
273 const char * const ns,
274 const char * const name,
275 const char * const type,
276 void * const userdata);
277 void LIBSTROPHE_EXPORTABLE xmpp_handler_delete(xmpp_conn_t * const conn,
278 xmpp_handler handler);
279
280 void LIBSTROPHE_EXPORTABLE xmpp_id_handler_add(xmpp_conn_t * const conn,
281 xmpp_handler handler,
282 const char * const id,
283 void * const userdata);
284 void LIBSTROPHE_EXPORTABLE xmpp_id_handler_delete(xmpp_conn_t * const conn,
285 xmpp_handler handler,
286 const char * const id);
287
288 /*
289 void xmpp_register_stanza_handler(conn, stanza, xmlns, type, handler)
290 */
291
292 /** stanzas **/
293
294 /** allocate an initialize a blank stanza */
295 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_new(xmpp_ctx_t *ctx);
296
297 /** clone a stanza */
298 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_clone(xmpp_stanza_t * const stanza);
299
300 /** copies a stanza and all children */
301 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_copy(const xmpp_stanza_t * const stanza);
302
303 /** free a stanza object and it's contents */
304 int LIBSTROPHE_EXPORTABLE xmpp_stanza_release(xmpp_stanza_t * const stanza);
305
306 /** free some blocks returned by other APIs, for example the
307 buffer you get from xmpp_stanza_to_text **/
308 void LIBSTROPHE_EXPORTABLE xmpp_free(const xmpp_ctx_t * const ctx, void *p);
309
310 int LIBSTROPHE_EXPORTABLE xmpp_stanza_is_text(xmpp_stanza_t * const stanza);
311 int LIBSTROPHE_EXPORTABLE xmpp_stanza_is_tag(xmpp_stanza_t * const stanza);
312
313 /** marshall a stanza into text for transmission or display **/
314 int LIBSTROPHE_EXPORTABLE xmpp_stanza_to_text(xmpp_stanza_t *stanza,
315 char ** const buf, size_t * const buflen);
316
317 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_children(xmpp_stanza_t * const stanza);
318 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_child_by_name(xmpp_stanza_t * const stanza,
319 const char * const name);
320 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_child_by_ns(xmpp_stanza_t * const stanza,
321 const char * const ns);
322 xmpp_stanza_t LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_next(xmpp_stanza_t * const stanza);
323 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_attribute(xmpp_stanza_t * const stanza,
324 const char * const name);
325 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_ns(xmpp_stanza_t * const stanza);
326 /* concatenate all child text nodes. this function
327 * returns a string that must be freed by the caller */
328
329 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_text(xmpp_stanza_t * const stanza);
330 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_text_ptr(xmpp_stanza_t * const stanza);
331 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_name(xmpp_stanza_t * const stanza);
332
333 int LIBSTROPHE_EXPORTABLE xmpp_stanza_add_child(xmpp_stanza_t *stanza, xmpp_stanza_t *child);
334 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_ns(xmpp_stanza_t * const stanza, const char * const ns);
335 /* set_attribute adds/replaces attributes */
336 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_attribute(xmpp_stanza_t * const stanza,
337 const char * const key,
338 const char * const value);
339 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_name(xmpp_stanza_t *stanza,
340 const char * const name);
341 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_text(xmpp_stanza_t *stanza,
342 const char * const text);
343 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_text_with_size(xmpp_stanza_t *stanza,
344 const char * const text,
345 const size_t size);
346
347 /* common stanza helpers */
348 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_type(xmpp_stanza_t * const stanza);
349 char LIBSTROPHE_EXPORTABLE *xmpp_stanza_get_id(xmpp_stanza_t * const stanza);
350 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_id(xmpp_stanza_t * const stanza,
351 const char * const id);
352 int LIBSTROPHE_EXPORTABLE xmpp_stanza_set_type(xmpp_stanza_t * const stanza,
353 const char * const type);
354
355 /* unimplemented
356 int xmpp_stanza_set_to();
357 int xmpp_stanza_set_from();
358 */
359
360 /* allocate and initialize a stanza in reply to another */
361 /* unimplemented
362 xmpp_stanza_t *xmpp_stanza_reply(const xmpp_stanza_t *stanza);
363 */
364
365 /* stanza subclasses */
366 /* unimplemented
367 void xmpp_message_new();
368 void xmpp_message_get_body();
369 void xmpp_message_set_body();
370
371 void xmpp_iq_new();
372 void xmpp_presence_new();
373 */
374
375 /** event loop **/
376 void LIBSTROPHE_EXPORTABLE xmpp_run_once(xmpp_ctx_t *ctx, const unsigned long timeout);
377 void LIBSTROPHE_EXPORTABLE xmpp_run(xmpp_ctx_t *ctx);
378 void LIBSTROPHE_EXPORTABLE xmpp_stop(xmpp_ctx_t *ctx);
379
380 #ifdef __cplusplus
381 }
382 #endif
383
384 #endif /* __LIBSTROPHE_STROPHE_H__ */