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