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