stropheruby 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/ext/common.h ADDED
@@ -0,0 +1,287 @@
1
+ /* common.h
2
+ ** strophe XMPP client library -- internal common structures
3
+ **
4
+ ** Copyright (C) 2005-2008 OGG, LLC. All rights reserved.
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
+ * Internally used functions and structures.
17
+ */
18
+
19
+ #ifndef __LIBSTROPHE_COMMON_H__
20
+ #define __LIBSTROPHE_COMMON_H__
21
+
22
+ #include <stdio.h>
23
+ #include <stdarg.h>
24
+ #ifndef _WIN32
25
+ #include <stdint.h>
26
+ #endif
27
+
28
+
29
+ #include "strophe.h"
30
+ #include "sock.h"
31
+ #include "tls.h"
32
+ #include "hash.h"
33
+ #include "util.h"
34
+
35
+ #include "expat.h"
36
+
37
+ /** run-time context **/
38
+
39
+ typedef enum {
40
+ XMPP_LOOP_NOTSTARTED,
41
+ XMPP_LOOP_RUNNING,
42
+ XMPP_LOOP_QUIT
43
+ } xmpp_loop_status_t;
44
+
45
+ typedef struct _xmpp_connlist_t {
46
+ xmpp_conn_t *conn;
47
+ struct _xmpp_connlist_t *next;
48
+ } xmpp_connlist_t;
49
+
50
+ struct _xmpp_ctx_t {
51
+ const xmpp_mem_t *mem;
52
+ const xmpp_log_t *log;
53
+
54
+ xmpp_loop_status_t loop_status;
55
+ xmpp_connlist_t *connlist;
56
+ };
57
+
58
+
59
+ /* convenience functions for accessing the context */
60
+ void *xmpp_alloc(const xmpp_ctx_t * const ctx, const size_t size);
61
+ void *xmpp_realloc(const xmpp_ctx_t * const ctx, void *p,
62
+ const size_t size);
63
+ char *xmpp_strdup(const xmpp_ctx_t * const ctx, const char * const s);
64
+
65
+ void xmpp_log(const xmpp_ctx_t * const ctx,
66
+ const xmpp_log_level_t level,
67
+ const char * const area,
68
+ const char * const fmt,
69
+ va_list ap);
70
+
71
+ /* wrappers for xmpp_log at specific levels */
72
+ void xmpp_error(const xmpp_ctx_t * const ctx,
73
+ const char * const area,
74
+ const char * const fmt,
75
+ ...);
76
+ void xmpp_warn(const xmpp_ctx_t * const ctx,
77
+ const char * const area,
78
+ const char * const fmt,
79
+ ...);
80
+ void xmpp_info(const xmpp_ctx_t * const ctx,
81
+ const char * const area,
82
+ const char * const fmt,
83
+ ...);
84
+ void xmpp_debug(const xmpp_ctx_t * const ctx,
85
+ const char * const area,
86
+ const char * const fmt,
87
+ ...);
88
+
89
+ /** jid */
90
+ /* these return new strings that must be xmpp_free()'d */
91
+ char *xmpp_jid_new(xmpp_ctx_t *ctx, const char *node,
92
+ const char *domain,
93
+ const char *resource);
94
+ char *xmpp_jid_bare(xmpp_ctx_t *ctx, const char *jid);
95
+ char *xmpp_jid_node(xmpp_ctx_t *ctx, const char *jid);
96
+ char *xmpp_jid_domain(xmpp_ctx_t *ctx, const char *jid);
97
+ char *xmpp_jid_resource(xmpp_ctx_t *ctx, const char *jid);
98
+
99
+
100
+ /** connection **/
101
+
102
+ /* opaque connection object */
103
+ typedef enum {
104
+ XMPP_STATE_DISCONNECTED,
105
+ XMPP_STATE_CONNECTING,
106
+ XMPP_STATE_CONNECTED
107
+ } xmpp_conn_state_t;
108
+
109
+ typedef struct _xmpp_send_queue_t xmpp_send_queue_t;
110
+ struct _xmpp_send_queue_t {
111
+ char *data;
112
+ size_t len;
113
+ size_t written;
114
+
115
+ xmpp_send_queue_t *next;
116
+ };
117
+
118
+ typedef struct _xmpp_handlist_t xmpp_handlist_t;
119
+ struct _xmpp_handlist_t {
120
+ /* common members */
121
+ int user_handler;
122
+ void *handler;
123
+ void *userdata;
124
+ int enabled; /* handlers are added disabled and enabled after the
125
+ * handler chain is processed to prevent stanzas from
126
+ * getting processed by newly added handlers */
127
+ xmpp_handlist_t *next;
128
+
129
+ union {
130
+ /* timed handlers */
131
+ struct {
132
+ unsigned long period;
133
+ uint64_t last_stamp;
134
+ };
135
+ /* id handlers */
136
+ struct {
137
+ char *id;
138
+ };
139
+ /* normal handlers */
140
+ struct {
141
+ char *ns;
142
+ char *name;
143
+ char *type;
144
+ };
145
+ };
146
+ };
147
+
148
+ #define SASL_MASK_PLAIN 0x01
149
+ #define SASL_MASK_DIGESTMD5 0x02
150
+ #define SASL_MASK_ANONYMOUS 0x04
151
+
152
+ typedef void (*xmpp_open_handler)(xmpp_conn_t * const conn);
153
+
154
+ struct _xmpp_conn_t {
155
+ unsigned int ref;
156
+ xmpp_ctx_t *ctx;
157
+ xmpp_conn_type_t type;
158
+
159
+ xmpp_conn_state_t state;
160
+ uint64_t timeout_stamp;
161
+ int error;
162
+ xmpp_stream_error_t *stream_error;
163
+ sock_t sock;
164
+ tls_t *tls;
165
+
166
+ int tls_support;
167
+ int tls_failed; /* set when tls fails, so we don't try again */
168
+ int sasl_support; /* if true, field is a bitfield of supported
169
+ mechanisms */
170
+
171
+ /* if server returns <bind/> or <session/> we must do them */
172
+ int bind_required;
173
+ int session_required;
174
+
175
+ char *lang;
176
+ char *domain;
177
+ char *connectdomain;
178
+ char *connectport;
179
+ char *jid;
180
+ char *pass;
181
+ char *stream_id;
182
+
183
+ /* send queue and parameters */
184
+ int blocking_send;
185
+ int send_queue_max;
186
+ int send_queue_len;
187
+ xmpp_send_queue_t *send_queue_head;
188
+ xmpp_send_queue_t *send_queue_tail;
189
+
190
+ /* xml parser */
191
+ int reset_parser;
192
+ XML_Parser parser;
193
+ int depth;
194
+ xmpp_stanza_t *stanza;
195
+
196
+ /* timeouts */
197
+ unsigned int connect_timeout;
198
+
199
+ /* event handlers */
200
+
201
+ /* stream open handler */
202
+ xmpp_open_handler open_handler;
203
+
204
+ /* user handlers only get called after authentication */
205
+ int authenticated;
206
+
207
+ /* connection events handler */
208
+ xmpp_conn_handler conn_handler;
209
+ void *userdata;
210
+
211
+ /* other handlers */
212
+ xmpp_handlist_t *timed_handlers;
213
+ hash_t *id_handlers;
214
+ xmpp_handlist_t *handlers;
215
+ };
216
+
217
+ void conn_disconnect(xmpp_conn_t * const conn);
218
+ void conn_disconnect_clean(xmpp_conn_t * const conn);
219
+ void conn_open_stream(xmpp_conn_t * const conn);
220
+
221
+
222
+ typedef enum {
223
+ XMPP_STANZA_UNKNOWN,
224
+ XMPP_STANZA_TEXT,
225
+ XMPP_STANZA_TAG
226
+ } xmpp_stanza_type_t;
227
+
228
+ struct _xmpp_stanza_t {
229
+ int ref;
230
+ xmpp_ctx_t *ctx;
231
+
232
+ xmpp_stanza_type_t type;
233
+
234
+ xmpp_stanza_t *prev;
235
+ xmpp_stanza_t *next;
236
+ xmpp_stanza_t *children;
237
+ xmpp_stanza_t *parent;
238
+
239
+ char *data;
240
+
241
+ hash_t *attributes;
242
+ };
243
+
244
+ int xmpp_stanza_set_attributes(xmpp_stanza_t * const stanza,
245
+ const char * const * const attr);
246
+
247
+ /* parser functions */
248
+ void parser_handle_start(void *userdata,
249
+ const XML_Char *name,
250
+ const XML_Char **attr);
251
+ void parser_handle_character(void *userdata, const XML_Char *s, int len);
252
+ void parser_handle_end(void *userdata, const XML_Char *name);
253
+ void parser_prepare_reset(xmpp_conn_t * const conn,
254
+ xmpp_open_handler handler);
255
+ int parser_reset(xmpp_conn_t * const conn);
256
+
257
+ /* handler management */
258
+ void handler_fire_stanza(xmpp_conn_t * const conn,
259
+ xmpp_stanza_t * const stanza);
260
+ uint64_t handler_fire_timed(xmpp_ctx_t * const ctx);
261
+ void handler_reset_timed(xmpp_conn_t *conn, int user_only);
262
+ void handler_add_timed(xmpp_conn_t * const conn,
263
+ xmpp_timed_handler handler,
264
+ const unsigned long period,
265
+ void * const userdata);
266
+ void handler_add_id(xmpp_conn_t * const conn,
267
+ xmpp_handler handler,
268
+ const char * const id,
269
+ void * const userdata);
270
+ void handler_add(xmpp_conn_t * const conn,
271
+ xmpp_handler handler,
272
+ const char * const ns,
273
+ const char * const name,
274
+ const char * const type,
275
+ void * const userdata);
276
+
277
+ /* utility functions */
278
+ void disconnect_mem_error(xmpp_conn_t * const conn);
279
+
280
+ /* auth functions */
281
+ void auth_handle_open(xmpp_conn_t * const conn);
282
+
283
+ /* replacement snprintf and vsnprintf */
284
+ int xmpp_snprintf (char *str, size_t count, const char *fmt, ...);
285
+ int xmpp_vsnprintf (char *str, size_t count, const char *fmt, va_list arg);
286
+
287
+ #endif /* __LIBSTROPHE_COMMON_H__ */