1 // ---------------------------------------------------------------------------------
2 // Manages the Jabber session. Data is taken from the TCP object and pushed into
3 // a SAX push parser as it arrives. When key Jabber documetn elements are met,
5 // ---------------------------------------------------------------------------------
6 #include "opensrf/transport_socket.h"
7 #include "opensrf/transport_message.h"
8 #include "opensrf/generic_utils.h"
11 #include <libxml/globals.h>
12 #include <libxml/xmlerror.h>
13 #include <libxml/parser.h>
14 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
15 #include <libxml/tree.h>
16 #include <libxml/debugXML.h>
17 #include <libxml/xmlmemory.h>
19 #ifndef TRANSPORT_SESSION_H
20 #define TRANSPORT_SESSION_H
22 #define CONNECTING_1 1 /* just starting the connection to Jabber */
23 #define CONNECTING_2 2 /* First <stream> packet sent and <stream> packet received from server */
25 /* Note. these are growing buffers, so all that's necessary is a sane starting point */
26 #define JABBER_BODY_BUFSIZE 4096
27 #define JABBER_SUBJECT_BUFSIZE 64
28 #define JABBER_THREAD_BUFSIZE 64
29 #define JABBER_JID_BUFSIZE 64
30 #define JABBER_STATUS_BUFSIZE 16
32 // ---------------------------------------------------------------------------------
33 // Takes data from the socket handler and pushes it directly into the push parser
34 // ---------------------------------------------------------------------------------
35 void grab_incoming( void * session, char* data );
37 // ---------------------------------------------------------------------------------
38 // Callback for handling the startElement event. Much of the jabber logic occurs
39 // in this and the characterHandler callbacks.
40 // Here we check for the various top level jabber elements: body, iq, etc.
41 // ---------------------------------------------------------------------------------
42 void startElementHandler(
43 void *session, const xmlChar *name, const xmlChar **atts);
45 // ---------------------------------------------------------------------------------
46 // Callback for handling the endElement event. Updates the Jabber state machine
47 // to let us know the element is over.
48 // ---------------------------------------------------------------------------------
49 void endElementHandler( void *session, const xmlChar *name);
51 // ---------------------------------------------------------------------------------
52 // This is where we extract XML text content. In particular, this is useful for
53 // extracting Jabber message bodies.
54 // ---------------------------------------------------------------------------------
55 void characterHandler(
56 void *session, const xmlChar *ch, int len);
58 void parseWarningHandler( void *session, const char* msg, ... );
59 void parseErrorHandler( void *session, const char* msg, ... );
61 // ---------------------------------------------------------------------------------
62 // Tells the SAX parser which functions will be used as event callbacks
63 // ---------------------------------------------------------------------------------
64 static xmlSAXHandler SAXHandlerStruct = {
65 NULL, /* internalSubset */
66 NULL, /* isStandalone */
67 NULL, /* hasInternalSubset */
68 NULL, /* hasExternalSubset */
69 NULL, /* resolveEntity */
71 NULL, /* entityDecl */
72 NULL, /* notationDecl */
73 NULL, /* attributeDecl */
74 NULL, /* elementDecl */
75 NULL, /* unparsedEntityDecl */
76 NULL, /* setDocumentLocator */
77 NULL, /* startDocument */
78 NULL, /* endDocument */
79 startElementHandler, /* startElement */
80 endElementHandler, /* endElement */
82 characterHandler, /* characters */
83 NULL, /* ignorableWhitespace */
84 NULL, /* processingInstruction */
86 parseWarningHandler, /* xmlParserWarning */
87 parseErrorHandler, /* xmlParserError */
88 NULL, /* xmlParserFatalError : unused */
89 NULL, /* getParameterEntity */
90 NULL, /* cdataBlock; */
91 NULL, /* externalSubset; */
94 NULL, /* startElementNs */
95 NULL, /* endElementNs */
96 NULL /* xmlStructuredErrorFunc */
99 // ---------------------------------------------------------------------------------
100 // Our SAX handler pointer.
101 // ---------------------------------------------------------------------------------
102 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
104 // ---------------------------------------------------------------------------------
105 // Jabber state machine. This is how we know where we are in the Jabber
107 // ---------------------------------------------------------------------------------
108 struct jabber_state_machine_struct {
116 int in_message_error;
121 typedef struct jabber_state_machine_struct jabber_machine;
123 // ---------------------------------------------------------------------------------
124 // Transport session. This maintains all the various parts of a session
125 // ---------------------------------------------------------------------------------
126 struct transport_session_struct {
128 /* our socket connection */
129 transport_socket* sock_obj;
130 /* our Jabber state machine */
131 jabber_machine* state_machine;
132 /* our SAX push parser context */
133 xmlParserCtxtPtr parser_ctxt;
135 /* our text buffers for holding text data */
136 growing_buffer* body_buffer;
137 growing_buffer* subject_buffer;
138 growing_buffer* thread_buffer;
139 growing_buffer* from_buffer;
140 growing_buffer* recipient_buffer;
141 growing_buffer* status_buffer;
142 growing_buffer* message_error_type;
143 int message_error_code;
145 /* for OILS extenstions */
146 growing_buffer* router_to_buffer;
147 growing_buffer* router_from_buffer;
148 growing_buffer* router_class_buffer;
149 growing_buffer* router_command_buffer;
150 int router_broadcast;
152 /* this can be anything. It will show up in the
153 callbacks for your convenience. We will *not*
154 deallocate whatever this is when we're done. That's
159 /* the Jabber message callback */
160 void (*message_callback) ( void* user_data, transport_message* msg );
161 //void (iq_callback) ( void* user_data, transport_iq_message* iq );
163 typedef struct transport_session_struct transport_session;
166 // ------------------------------------------------------------------
167 // Allocates and initializes the necessary transport session
169 // ------------------------------------------------------------------
170 transport_session* init_transport( char* server, int port, void* user_data );
172 // ------------------------------------------------------------------
173 // Returns the value of the given XML attribute
174 // The xmlChar** construct is commonly returned from SAX event
175 // handlers. Pass that in with the name of the attribute you want
177 // ------------------------------------------------------------------
178 char* get_xml_attr( const xmlChar** atts, char* attr_name );
180 // ------------------------------------------------------------------
181 // Waits at most 'timeout' seconds for data to arrive from the
182 // TCP handler. A timeout of -1 means to wait indefinitely.
183 // ------------------------------------------------------------------
184 int session_wait( transport_session* session, int timeout );
186 // ---------------------------------------------------------------------------------
187 // Sends the given Jabber message
188 // ---------------------------------------------------------------------------------
189 int session_send_msg( transport_session* session, transport_message* msg );
191 // ---------------------------------------------------------------------------------
192 // Returns 1 if this session is connected to the jabber server. 0 otherwise
193 // ---------------------------------------------------------------------------------
194 int session_connected( transport_session* );
196 // ------------------------------------------------------------------
197 // Deallocates session memory
198 // ------------------------------------------------------------------
199 int session_free( transport_session* session );
201 // ------------------------------------------------------------------
202 // Connects to the Jabber server. Waits at most connect_timeout
203 // seconds before failing
204 // ------------------------------------------------------------------
205 int session_connect( transport_session* session,
206 const char* username, const char* password, const char* resource, int connect_timeout );
208 int session_disconnect( transport_session* session );
210 int reset_session_buffers( transport_session* session );