forked from flamontagne/stropheruby
/
strophe_ruby.c
739 lines (620 loc) · 26.3 KB
/
strophe_ruby.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include <ruby.h>
#include "strophe.h"
#include "common.h"
VALUE mStropheRuby;
VALUE mErrorTypes;
VALUE mLogging;
VALUE mConnectionEvents;
VALUE cConnection;
VALUE cContext;
VALUE cStreamError;
VALUE cEventLoop;
VALUE client_conn_handler;
VALUE cStanza;
VALUE cStreamError;
/* release the stanza. Called automatically by the GC */
static void t_xmpp_stanza_release(void *stanza) {
//if(stanza != NULL)
// xmpp_stanza_release(stanza);
}
/* Initialize the strophe library */
VALUE t_xmpp_initialize(VALUE self) {
xmpp_initialize();
return Qtrue;
}
/* shutdown the library */
VALUE t_xmpp_shutdown(VALUE self) {
xmpp_shutdown();
return Qnil;
}
/* check Strophe version */
VALUE t_xmpp_version_check(VALUE self, VALUE major, VALUE minor) {
int res = xmpp_version_check(FIX2INT(major), FIX2INT(minor));
return INT2FIX(res);
}
/* parse the stream one time */
VALUE t_xmpp_run_once(VALUE self, VALUE rb_ctx, VALUE timeout) {
xmpp_ctx_t *ctx;
Data_Get_Struct(rb_ctx,xmpp_ctx_t,ctx);
int i = xmpp_run_once(ctx, NUM2INT(timeout));
return INT2FIX(i);
}
/* parse the stream continuously (by calling xmpp_run_once in a while loop) */
VALUE t_xmpp_run(VALUE self, VALUE rb_ctx) {
xmpp_ctx_t *ctx;
Data_Get_Struct(rb_ctx,xmpp_ctx_t,ctx);
xmpp_run(ctx);
return Qtrue;
}
/* Set a flag to indicate to our event loop that it must exit */
VALUE t_xmpp_stop(VALUE self, VALUE rb_ctx) {
xmpp_ctx_t *ctx;
Data_Get_Struct(rb_ctx, xmpp_ctx_t, ctx);
xmpp_stop(ctx);
return Qtrue;
}
/* free the context object (because it causes segmentation error once in a while) */
static VALUE t_xmpp_ctx_free(VALUE self) {
xmpp_ctx_t *ctx;
Data_Get_Struct(self,xmpp_ctx_t,ctx);
xmpp_ctx_free(ctx);
}
/* Get the status of the control loop
TODO: Define ruby constants for the loop statuses. Currently we have to know them by heart (0 = NOTSTARTED, 1 = RUNNING, 2 = QUIT)
*/
static VALUE t_xmpp_get_loop_status(VALUE self) {
xmpp_ctx_t *ctx;
Data_Get_Struct(self, xmpp_ctx_t, ctx);
return INT2FIX(ctx->loop_status);
}
/* Set the loop status. Don't call this method if you want to exit the control loop. Call xmpp_stop instead. This method
will set the loop status at QUIT */
static VALUE t_xmpp_set_loop_status(VALUE self, VALUE rb_loop_status) {
xmpp_ctx_t *ctx;
Data_Get_Struct(self, xmpp_ctx_t, ctx);
ctx->loop_status=FIX2INT(rb_loop_status);
return rb_loop_status;
}
/* Initialize a run time context */
VALUE t_xmpp_ctx_new(VALUE class, VALUE log_level) {
xmpp_log_t *log;
xmpp_log_level_t level;
level=FIX2INT(log_level);
log = xmpp_get_default_logger((xmpp_log_level_t)level);
xmpp_ctx_t *ctx = xmpp_ctx_new(NULL, log);
VALUE tdata = Data_Wrap_Struct(class, 0, free, ctx);
VALUE argv[1];
argv[0] = log_level;
rb_obj_call_init(tdata,1,argv);
return tdata;
}
/* Ruby initialize for the context. Hmm... do we really need this? */
static VALUE t_xmpp_ctx_init(VALUE self, VALUE log_level) {
rb_iv_set(self, "@log_level", log_level);
return self;
}
/* Release the connection object. (Currently not called at all... because it causes segmentation error once in a while) */
static VALUE t_xmpp_conn_release(VALUE self) {
xmpp_conn_t *conn;
Data_Get_Struct(self,xmpp_conn_t,conn);
xmpp_conn_release(conn);
}
/* Initialize a connection object. We register instance variables that will hold the various callbacks
You should not manipulate instance variables. Use add_handler instead.
Maybe put this into xmpp_conn_new?
*/
static VALUE t_xmpp_conn_init(VALUE self, VALUE ctx) {
rb_iv_set(self, "@ctx", ctx);
rb_iv_set(self, "@presence_handlers", rb_ary_new());
rb_iv_set(self, "@message_handlers", rb_ary_new());
rb_iv_set(self, "@iq_handlers", rb_ary_new());
rb_iv_set(self, "@id_handlers", rb_ary_new());
rb_gv_set("rb_conn",self);
return self;
}
/* create a connection object then call the initialize method for it*/
VALUE t_xmpp_conn_new(VALUE class, VALUE rb_ctx) {
//Get the context in a format that C can understand
xmpp_ctx_t *ctx;
Data_Get_Struct(rb_ctx, xmpp_ctx_t, ctx);
xmpp_conn_t *conn = xmpp_conn_new(ctx);
VALUE tdata = Data_Wrap_Struct(class, 0, free, conn);
VALUE argv[1];
argv[0] = rb_ctx;
//call ruby "initialize"
rb_obj_call_init(tdata, 1, argv);
return tdata;
}
/* Clone a connection */
static VALUE t_xmpp_conn_clone(VALUE self) {
xmpp_conn_t *conn = xmpp_conn_clone((xmpp_conn_t *)self);
return Data_Wrap_Struct(cConnection, 0, t_xmpp_conn_release, conn);
}
/* Get the jid */
static VALUE t_xmpp_conn_get_jid(VALUE self) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
return rb_str_new2(xmpp_conn_get_jid(conn));
}
/* Set the jid */
static VALUE t_xmpp_conn_set_jid(VALUE self, VALUE jid) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
xmpp_conn_set_jid(conn, StringValuePtr(jid));
return jid;
}
/* get the password */
static VALUE t_xmpp_conn_get_pass(VALUE self) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
return rb_str_new2(xmpp_conn_get_pass(conn));
}
/* set the password */
static VALUE t_xmpp_conn_set_pass(VALUE self, VALUE pass) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
xmpp_conn_set_pass(conn, StringValuePtr(pass));
return pass;
}
/* get the connect_timeout */
static VALUE t_xmpp_conn_get_connect_timeout(VALUE self)
{
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
return INT2FIX(xmpp_conn_get_connect_timeout(conn));
}
/* set the connect_timeout */
static VALUE t_xmpp_conn_set_connect_timeout(VALUE self, VALUE timeout)
{
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
xmpp_conn_set_connect_timeout(conn, FIX2INT(timeout));
return timeout;
}
/* Parent handler for the connection... we call yield to invoke the client callback*/
static 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) {
//yield code block for connection
if (RTEST(client_conn_handler)) {
VALUE args[3];
args[0] = INT2FIX(status);
args[1] = INT2FIX(error);
args[2] = (NULL == stream_error) ? Qnil : Data_Wrap_Struct(cStreamError, 0, NULL, stream_error);
rb_funcall2(client_conn_handler, rb_intern("call"), 3, args);
}
}
/*Get the type of a StreamError. */
static VALUE t_xmpp_stream_error_get_type(VALUE self) {
xmpp_stream_error_t *stream_error;
Data_Get_Struct(self, xmpp_stream_error_t, stream_error);
return INT2FIX(stream_error->type);
}
/*Get the text of a StreamError. */
static VALUE t_xmpp_stream_error_get_text(VALUE self) {
xmpp_stream_error_t *stream_error;
Data_Get_Struct(self, xmpp_stream_error_t, stream_error);
return (NULL != stream_error->text) ? rb_str_new2(stream_error->text) : Qnil;
}
/*Get the stanza of a StreamError. */
static VALUE t_xmpp_stream_error_get_stanza(VALUE self) {
xmpp_stream_error_t *stream_error;
Data_Get_Struct(self, xmpp_stream_error_t, stream_error);
return (NULL != stream_error->stanza) ? Data_Wrap_Struct(cStanza, 0, NULL, stream_error->stanza) : Qnil;
}
/*this is called in a loop (rb_iterate). We invoke the block passed by the user*/
static VALUE _call_handler(VALUE obj, VALUE stanza) {
rb_funcall(obj, rb_intern("call"),1, stanza);
return Qnil;
}
/* Called when a message is received in the stream. From there we invoke all code blocks for stanzas of type 'message'*/
int _message_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata) {
VALUE arr;
VALUE rb_conn;
rb_conn = rb_gv_get("rb_conn");
VALUE rb_stanza = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, stanza);
arr=rb_iv_get(rb_conn,"@message_handlers");
rb_iterate(rb_each,arr,_call_handler, rb_stanza);
return 1;
}
/* Called when a presence is received in the stream. From there we invoke all code blocks for stanzas of type 'presence'*/
int _presence_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata) {
VALUE arr;
VALUE rb_conn;
rb_conn = rb_gv_get("rb_conn");
VALUE rb_stanza = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, stanza);
arr=rb_iv_get(rb_conn,"@presence_handlers");
rb_iterate(rb_each,arr,_call_handler, rb_stanza);
return 1;
}
/* Called when a iq is received in the stream. From there we invoke all code blocks for stanzas of type 'iq'*/
int _iq_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata) {
VALUE arr;
VALUE rb_conn;
rb_conn = rb_gv_get("rb_conn");
VALUE rb_stanza = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, stanza);
arr=rb_iv_get(rb_conn,"@iq_handlers");
rb_iterate(rb_each,arr,_call_handler, rb_stanza);
return 1;
}
/* Called when a id stanza is received TODO: Test this!*/
int _id_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata) {
VALUE arr;
VALUE rb_conn;
rb_conn = rb_gv_get("rb_conn");
VALUE rb_stanza = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, stanza);
arr=rb_iv_get(rb_conn,"@id_handlers");
rb_iterate(rb_each,arr,_call_handler, rb_stanza);
return 1;
}
/* Add an handler for events in the stream (message, presence or iqs), We store the block we just received in the correct instance variable
to invoke it later*/
static VALUE t_xmpp_handler_add(VALUE self,VALUE rb_name) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
char *name = StringValuePtr(rb_name);
VALUE arr;
xmpp_handler handler;
if(strcmp(name,"message") == 0) {
arr = rb_iv_get(self, "@message_handlers");
handler = _message_handler;
} else {
if(strcmp(name,"presence") == 0) {
arr = rb_iv_get(self, "@presence_handlers");
handler = _presence_handler;
} else {
arr = rb_iv_get(self, "@iq_handlers");
handler = _iq_handler;
}
}
xmpp_handler_add(conn, handler, NULL, name, NULL, conn->ctx);
rb_ary_push(arr, rb_block_proc());
return Qnil;
}
/* Add an handler for ID stanzas. TODO:Test this!*/
static VALUE t_xmpp_id_handler_add(VALUE self, VALUE rb_id) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
char *id = StringValuePtr(rb_id);
VALUE arr;
arr = rb_iv_get(self, "@id_handlers");
rb_ary_push(arr, rb_block_proc());
xmpp_id_handler_add(conn, _id_handler, StringValuePtr(id), conn->ctx);
return Qnil;
}
/* Connect and authenticate. We store the block in the client_conn_handler variable to invoke it later*/
static VALUE t_xmpp_connect_client(VALUE self) {
xmpp_conn_t *conn;
xmpp_ctx_t *ctx;
Data_Get_Struct(rb_iv_get(self, "@ctx"), xmpp_ctx_t, ctx);
Data_Get_Struct(self, xmpp_conn_t, conn);
/*The user might have passed a block... however we don't want to invoke it right now.
We store it to invoke it later in _xmpp_conn_handler */
if (rb_block_given_p())
client_conn_handler = rb_block_proc();
int result = xmpp_connect_client(conn, NULL, 0, _conn_handler, ctx);
return INT2FIX(result);
}
/* Disconnect from the stream. Is it needed? Not too sure about it. Normally if you just call xmpp_stop you should be fine*/
static VALUE t_xmpp_disconnect(VALUE self) {
xmpp_conn_t *conn;
Data_Get_Struct(self, xmpp_conn_t, conn);
xmpp_disconnect(conn);
conn_disconnect(conn);
return Qtrue;
}
/* Send a stanza in the stream */
static VALUE t_xmpp_send(VALUE self, VALUE rb_stanza) {
xmpp_conn_t *conn;
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_conn_t, conn);
Data_Get_Struct(rb_stanza, xmpp_stanza_t, stanza);
xmpp_send(conn,stanza);
return Qtrue;
}
/* Send a stanza in the stream */
static VALUE t_xmpp_send_raw_string(VALUE self, VALUE rb_text) {
xmpp_conn_t *conn;
char *text;
Data_Get_Struct(self, xmpp_conn_t, conn);
text = StringValuePtr(rb_text);
xmpp_send_raw_string(conn, "%s", text);
return Qtrue;
}
/* Create a new stanza */
VALUE t_xmpp_stanza_new(VALUE class) {
//Get the context in a format that C can understand
xmpp_ctx_t *ctx;
VALUE rb_conn = rb_gv_get("rb_conn");
Data_Get_Struct(rb_iv_get(rb_conn,"@ctx"), xmpp_ctx_t, ctx);
xmpp_stanza_t *stanza = xmpp_stanza_new(ctx);
VALUE tdata = Data_Wrap_Struct(class, 0, t_xmpp_stanza_release, stanza);
}
/*Clone a stanza. TODO: Test this!*/
static VALUE t_xmpp_stanza_clone(VALUE self) {
xmpp_stanza_t *stanza;
xmpp_stanza_t *new_stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
new_stanza = xmpp_stanza_clone(stanza);
VALUE tdata = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, new_stanza);
return tdata;
}
/*Copy a stanza. TODO: Test this!*/
static VALUE t_xmpp_stanza_copy(VALUE self) {
xmpp_stanza_t *stanza;
xmpp_stanza_t *new_stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
new_stanza = xmpp_stanza_copy(stanza);
VALUE tdata = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, new_stanza);
return tdata;
}
/*Get the children of the stanza. For example, message stanzas have a "body" child that contains another stanza that contains the actual
message. If you have the top level stanza you can do something like : body_stanza = message_stanza.children */
static VALUE t_xmpp_stanza_get_children(VALUE self) {
xmpp_stanza_t *stanza;
xmpp_stanza_t *children;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
children = xmpp_stanza_get_children(stanza);
if(children) {
VALUE tdata = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, children);
return tdata;
}
else
return Qnil;
}
/*Get the child of a stanza by its name. eg. body_stanza = message_stanza.child_by_name("body")*/
static VALUE t_xmpp_stanza_get_child_by_name(VALUE self, VALUE rb_name) {
xmpp_stanza_t *stanza;
xmpp_stanza_t *child;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *name = StringValuePtr(rb_name);
child = xmpp_stanza_get_child_by_name(stanza, name);
if(child) {
VALUE rb_child = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, child);
return rb_child;
} else {
return Qnil;
}
}
/*TODO: Test this!*/
static VALUE t_xmpp_stanza_get_next(VALUE self) {
xmpp_stanza_t *stanza;
xmpp_stanza_t *next;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
next = xmpp_stanza_get_next(stanza);
VALUE tdata = Data_Wrap_Struct(cStanza, 0, t_xmpp_stanza_release, next);
return tdata;
}
/*Get the attribute of a stanza. eg. message_stanza.child_by_name("body").text*/
static VALUE t_xmpp_stanza_get_attribute(VALUE self, VALUE rb_attribute) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *val = xmpp_stanza_get_attribute(stanza,StringValuePtr(rb_attribute));
return rb_str_new2(val);
}
/*Get the namespace of a stanza TODO: Test this!*/
static VALUE t_xmpp_stanza_get_ns(VALUE self) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *ns = xmpp_stanza_get_ns(stanza);
return rb_str_new2(ns);
}
/*Get the text of a stanza. */
static VALUE t_xmpp_stanza_get_text(VALUE self) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *text = xmpp_stanza_get_text(stanza);
if(text)
return rb_str_new2(text);
else
return rb_str_new2("");
}
/*Get the name of a stanza (message, presence, iq) */
static VALUE t_xmpp_stanza_get_name(VALUE self) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *name = xmpp_stanza_get_name(stanza);
return rb_str_new2(name);
}
/*Get the type of a stanza. For example, if the name is 'message', type can be 'chat', 'normal' and so on */
static VALUE t_xmpp_stanza_get_type(VALUE self) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *type = xmpp_stanza_get_type(stanza);
if(type)
return rb_str_new2(type);
else
return Qnil;
}
/*Get the id of a stanza. TODO:Test this!*/
static VALUE t_xmpp_stanza_get_id(VALUE self) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *id = xmpp_stanza_get_id(stanza);
return rb_str_new2(id);
}
/*Set the value of a stanza attribute (eg. stanza.set_attribute("to","johnsmith@example.com") */
static VALUE t_xmpp_stanza_set_attribute(VALUE self, VALUE rb_attribute, VALUE rb_val) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *attribute = StringValuePtr(rb_attribute);
char *val = StringValuePtr(rb_val);
xmpp_stanza_set_attribute(stanza, attribute, val);
return Qtrue;
}
/*Set the namespace of a stanza. TODO:Test this!*/
static VALUE t_xmpp_stanza_set_ns(VALUE self, VALUE rb_ns) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *ns = StringValuePtr(rb_ns);
xmpp_stanza_set_ns(stanza, ns);
return Qtrue;
}
/*Convert Stanza to String */
static VALUE t_xmpp_stanza_to_text(VALUE self) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char* buffer;
size_t len;
xmpp_stanza_to_text(stanza, &buffer, &len);
if (NULL == buffer)
return rb_str_new(0, 0);
else
return rb_str_buf_new2(buffer);
}
/*Set the text of a stanza */
static VALUE t_xmpp_stanza_set_text(VALUE self, VALUE rb_text) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *text = StringValuePtr(rb_text);
xmpp_stanza_set_text(stanza, text);
return rb_text;
}
/*Set the name of a stanza (message, presence, iq) */
static VALUE t_xmpp_stanza_set_name(VALUE self, VALUE rb_name) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *name = StringValuePtr(rb_name);
xmpp_stanza_set_name(stanza, name);
return Qtrue;
}
/*Set the type of a stanza. For example if the name is 'message', the type can be 'chat', 'normal' and so on*/
static VALUE t_xmpp_stanza_set_type(VALUE self, VALUE rb_type) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *type = StringValuePtr(rb_type);
xmpp_stanza_set_type(stanza, type);
return Qtrue;
}
/*Set the id of a stanza. TODO:Test this!*/
static VALUE t_xmpp_stanza_set_id(VALUE self, VALUE rb_id) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
char *id = StringValuePtr(rb_id);
xmpp_stanza_set_id(stanza, id);
return Qtrue;
}
/*Add a child element to a stanza (hint: message stanzas have a body element...) */
static VALUE t_xmpp_stanza_add_child(VALUE self, VALUE rb_child) {
xmpp_stanza_t *stanza;
Data_Get_Struct(self, xmpp_stanza_t, stanza);
xmpp_stanza_t *child;
Data_Get_Struct(rb_child, xmpp_stanza_t, child);
int res = xmpp_stanza_add_child(stanza,child);
return INT2FIX(res);
}
void Init_strophe_ruby() {
/*Main module that contains everything*/
mStropheRuby = rb_define_module("StropheRuby");
/*Wrap the stream_error_t structure into a ruby class named StreamError*/
cStreamError = rb_define_class_under(mStropheRuby, "StreamError", rb_cObject);
cEventLoop = rb_define_class_under(mStropheRuby, "EventLoop", rb_cObject);
rb_define_singleton_method(cEventLoop, "run_once", t_xmpp_run_once, 2);
rb_define_singleton_method(cEventLoop, "prepare", t_xmpp_initialize, 0);
rb_define_singleton_method(cEventLoop, "run", t_xmpp_run, 1);
rb_define_singleton_method(cEventLoop, "stop", t_xmpp_stop, 1);
rb_define_singleton_method(cEventLoop, "shutdown", t_xmpp_shutdown, 0);
rb_define_singleton_method(cEventLoop, "version", t_xmpp_version_check, 2);
rb_define_const(cEventLoop, "LOOP_RUNNING", INT2FIX(XMPP_LOOP_RUNNING));
rb_define_const(cEventLoop, "LOOP_NOTSTARTED", INT2FIX(XMPP_LOOP_NOTSTARTED));
rb_define_const(cEventLoop, "LOOP_QUIT", INT2FIX(XMPP_LOOP_QUIT));
/*Logs*/
mLogging = rb_define_module_under(mStropheRuby, "Logging");
rb_define_const(mLogging, "DEBUG", INT2FIX(XMPP_LEVEL_DEBUG));
rb_define_const(mLogging, "INFO", INT2FIX(XMPP_LEVEL_INFO));
rb_define_const(mLogging, "WARN", INT2FIX(XMPP_LEVEL_WARN));
rb_define_const(mLogging, "ERROR", INT2FIX(XMPP_LEVEL_ERROR));
/*Connection Events*/
mConnectionEvents = rb_define_module_under(mStropheRuby, "ConnectionEvents");
rb_define_const(mConnectionEvents, "CONNECT", INT2FIX(XMPP_CONN_CONNECT));
rb_define_const(mConnectionEvents, "DISCONNECT", INT2FIX(XMPP_CONN_DISCONNECT));
rb_define_const(mConnectionEvents, "FAIL", INT2FIX(XMPP_CONN_FAIL));
/*Error types*/
mErrorTypes = rb_define_module_under(mStropheRuby, "ErrorTypes");
rb_define_const(mErrorTypes, "BAD_FORMAT", INT2FIX(XMPP_SE_BAD_FORMAT));
rb_define_const(mErrorTypes, "BAD_NS_PREFIX", INT2FIX(XMPP_SE_BAD_NS_PREFIX));
rb_define_const(mErrorTypes, "CONFLICT", INT2FIX(XMPP_SE_CONFLICT));
rb_define_const(mErrorTypes, "CONN_TIMEOUT", INT2FIX(XMPP_SE_CONN_TIMEOUT));
rb_define_const(mErrorTypes, "HOST_GONE", INT2FIX(XMPP_SE_HOST_GONE));
rb_define_const(mErrorTypes, "HOST_UNKNOWN", INT2FIX(XMPP_SE_HOST_UNKNOWN));
rb_define_const(mErrorTypes, "IMPROPER_ADDR", INT2FIX(XMPP_SE_IMPROPER_ADDR));
rb_define_const(mErrorTypes, "INTERNAL_SERVER_ERROR", INT2FIX(XMPP_SE_INTERNAL_SERVER_ERROR));
rb_define_const(mErrorTypes, "INVALID_FROM", INT2FIX(XMPP_SE_INVALID_FROM));
rb_define_const(mErrorTypes, "INVALID_ID", INT2FIX(XMPP_SE_INVALID_ID));
rb_define_const(mErrorTypes, "INVALID_NS", INT2FIX(XMPP_SE_INVALID_NS));
rb_define_const(mErrorTypes, "INVALID_XML", INT2FIX(XMPP_SE_INVALID_XML));
rb_define_const(mErrorTypes, "NOT_AUTHORIZED", INT2FIX(XMPP_SE_NOT_AUTHORIZED));
rb_define_const(mErrorTypes, "POLICY_VIOLATION", INT2FIX(XMPP_SE_POLICY_VIOLATION));
rb_define_const(mErrorTypes, "REMOTE_CONN_FAILED", INT2FIX(XMPP_SE_REMOTE_CONN_FAILED));
rb_define_const(mErrorTypes, "RESOURCE_CONSTRAINT", INT2FIX(XMPP_SE_RESOURCE_CONSTRAINT));
rb_define_const(mErrorTypes, "RESTRICTED_XML", INT2FIX(XMPP_SE_RESTRICTED_XML));
rb_define_const(mErrorTypes, "SEE_OTHER_HOST", INT2FIX(XMPP_SE_SEE_OTHER_HOST));
rb_define_const(mErrorTypes, "SYSTEM_SHUTDOWN", INT2FIX(XMPP_SE_SYSTEM_SHUTDOWN));
rb_define_const(mErrorTypes, "UNDEFINED_CONDITION", INT2FIX(XMPP_SE_UNDEFINED_CONDITION));
rb_define_const(mErrorTypes, "UNSUPPORTED_ENCODING", INT2FIX(XMPP_SE_UNSUPPORTED_ENCODING));
rb_define_const(mErrorTypes, "UNSUPPORTED_STANZA_TYPE", INT2FIX(XMPP_SE_UNSUPPORTED_STANZA_TYPE));
rb_define_const(mErrorTypes, "UNSUPPORTED_VERSION", INT2FIX(XMPP_SE_UNSUPPORTED_VERSION));
rb_define_const(mErrorTypes, "XML_NOT_WELL_FORMED", INT2FIX(XMPP_SE_XML_NOT_WELL_FORMED));
/*Context*/
cContext = rb_define_class_under(mStropheRuby, "Context", rb_cObject);
rb_define_singleton_method(cContext, "new", t_xmpp_ctx_new, 1);
rb_define_method(cContext, "initialize", t_xmpp_ctx_init, 1);
rb_define_method(cContext, "free", t_xmpp_ctx_free, 0);
rb_define_method(cContext, "loop_status", t_xmpp_get_loop_status, 0);
rb_define_method(cContext, "loop_status=", t_xmpp_set_loop_status, 1);
/*Connection*/
cConnection = rb_define_class_under(mStropheRuby, "Connection", rb_cObject);
rb_define_singleton_method(cConnection, "new", t_xmpp_conn_new, 1);
rb_define_method(cConnection, "initialize", t_xmpp_conn_init, 1);
rb_define_method(cConnection, "clone", t_xmpp_conn_clone, 1);
rb_define_method(cConnection, "release", t_xmpp_conn_release, 0);
rb_define_method(cConnection, "jid", t_xmpp_conn_get_jid,0);
rb_define_method(cConnection, "jid=", t_xmpp_conn_set_jid,1);
rb_define_method(cConnection, "password", t_xmpp_conn_get_pass,0);
rb_define_method(cConnection, "password=", t_xmpp_conn_set_pass,1);
rb_define_method(cConnection, "connect_timeout", t_xmpp_conn_get_connect_timeout,0);
rb_define_method(cConnection, "connect_timeout=", t_xmpp_conn_set_connect_timeout,1);
rb_define_method(cConnection, "connect", t_xmpp_connect_client,0);
rb_define_method(cConnection, "disconnect", t_xmpp_disconnect, 0);
rb_define_method(cConnection, "send", t_xmpp_send, 1);
rb_define_method(cConnection, "send_raw_string", t_xmpp_send_raw_string, 1);
/*Handlers*/
rb_define_method(cConnection, "add_handler", t_xmpp_handler_add, 1);
rb_define_method(cConnection, "add_id_handler", t_xmpp_id_handler_add, 3);
/*StreamError*/
cStreamError = rb_define_class_under(mStropheRuby, "StreamError", rb_cObject);
rb_define_method(cStreamError, "error_type", t_xmpp_stream_error_get_type, 0);
rb_define_method(cStreamError, "text", t_xmpp_stream_error_get_text, 0);
rb_define_method(cStreamError, "stanza", t_xmpp_stream_error_get_stanza, 0);
/*Stanza*/
cStanza = rb_define_class_under(mStropheRuby, "Stanza", rb_cObject);
rb_define_singleton_method(cStanza, "new", t_xmpp_stanza_new, 0);
rb_define_method(cStanza, "clone", t_xmpp_stanza_clone, 0);
rb_define_method(cStanza, "copy", t_xmpp_stanza_copy, 0);
//rb_define_method(cStanza, "release", t_xmpp_stanza_release, 0);
rb_define_method(cStanza, "children", t_xmpp_stanza_get_children, 0);
rb_define_method(cStanza, "child_by_name", t_xmpp_stanza_get_child_by_name, 1);
rb_define_method(cStanza, "next", t_xmpp_stanza_get_next, 0);
rb_define_method(cStanza, "attribute", t_xmpp_stanza_get_attribute, 1);
rb_define_method(cStanza, "ns", t_xmpp_stanza_get_ns, 0);
rb_define_method(cStanza, "text", t_xmpp_stanza_get_text, 0);
rb_define_method(cStanza, "name", t_xmpp_stanza_get_name, 0);
rb_define_method(cStanza, "add_child", t_xmpp_stanza_add_child, 1);
rb_define_method(cStanza, "ns=", t_xmpp_stanza_set_ns, 1);
rb_define_method(cStanza, "to_s", t_xmpp_stanza_to_text, 0);
rb_define_method(cStanza, "set_attribute", t_xmpp_stanza_set_attribute, 2);
rb_define_method(cStanza, "name=", t_xmpp_stanza_set_name, 1);
rb_define_method(cStanza, "text=", t_xmpp_stanza_set_text, 1);
rb_define_method(cStanza, "type", t_xmpp_stanza_get_type, 0);
rb_define_method(cStanza, "id", t_xmpp_stanza_get_id, 0);
rb_define_method(cStanza, "id=", t_xmpp_stanza_set_id, 1);
rb_define_method(cStanza, "type=", t_xmpp_stanza_set_type, 1);
}