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