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"
11 #include "socket_bundle.h"
16 #include <libxml/globals.h>
17 #include <libxml/xmlerror.h>
18 #include <libxml/parser.h>
19 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
20 #include <libxml/tree.h>
21 #include <libxml/debugXML.h>
22 #include <libxml/xmlmemory.h>
24 #ifndef TRANSPORT_SESSION_H
25 #define TRANSPORT_SESSION_H
27 #define CONNECTING_1 1 /* just starting the connection to Jabber */
28 #define CONNECTING_2 2 /* First <stream> packet sent and <stream> packet received from server */
30 /* Note. these are growing buffers, so all that's necessary is a sane starting point */
31 #define JABBER_BODY_BUFSIZE 4096
32 #define JABBER_SUBJECT_BUFSIZE 64
33 #define JABBER_THREAD_BUFSIZE 64
34 #define JABBER_JID_BUFSIZE 64
35 #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 );
41 void grab_incoming(void* blob, socket_manager* mgr, int sockid, char* data, int parent);
43 // ---------------------------------------------------------------------------------
44 // Callback for handling the startElement event. Much of the jabber logic occurs
45 // in this and the characterHandler callbacks.
46 // Here we check for the various top level jabber elements: body, iq, etc.
47 // ---------------------------------------------------------------------------------
48 void startElementHandler(
49 void *session, const xmlChar *name, const xmlChar **atts);
51 // ---------------------------------------------------------------------------------
52 // Callback for handling the endElement event. Updates the Jabber state machine
53 // to let us know the element is over.
54 // ---------------------------------------------------------------------------------
55 void endElementHandler( void *session, const xmlChar *name);
57 // ---------------------------------------------------------------------------------
58 // This is where we extract XML text content. In particular, this is useful for
59 // extracting Jabber message bodies.
60 // ---------------------------------------------------------------------------------
61 void characterHandler(
62 void *session, const xmlChar *ch, int len);
64 void parseWarningHandler( void *session, const char* msg, ... );
65 void parseErrorHandler( void *session, const char* msg, ... );
67 // ---------------------------------------------------------------------------------
68 // Tells the SAX parser which functions will be used as event callbacks
69 // ---------------------------------------------------------------------------------
70 static xmlSAXHandler SAXHandlerStruct = {
71 NULL, /* internalSubset */
72 NULL, /* isStandalone */
73 NULL, /* hasInternalSubset */
74 NULL, /* hasExternalSubset */
75 NULL, /* resolveEntity */
77 NULL, /* entityDecl */
78 NULL, /* notationDecl */
79 NULL, /* attributeDecl */
80 NULL, /* elementDecl */
81 NULL, /* unparsedEntityDecl */
82 NULL, /* setDocumentLocator */
83 NULL, /* startDocument */
84 NULL, /* endDocument */
85 startElementHandler, /* startElement */
86 endElementHandler, /* endElement */
88 characterHandler, /* characters */
89 NULL, /* ignorableWhitespace */
90 NULL, /* processingInstruction */
92 parseWarningHandler, /* xmlParserWarning */
93 parseErrorHandler, /* xmlParserError */
94 NULL, /* xmlParserFatalError : unused */
95 NULL, /* getParameterEntity */
96 NULL, /* cdataBlock; */
97 NULL, /* externalSubset; */
100 NULL, /* startElementNs */
101 NULL, /* endElementNs */
102 NULL /* xmlStructuredErrorFunc */
105 // ---------------------------------------------------------------------------------
106 // Our SAX handler pointer.
107 // ---------------------------------------------------------------------------------
108 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
110 // ---------------------------------------------------------------------------------
111 // Jabber state machine. This is how we know where we are in the Jabber
113 // ---------------------------------------------------------------------------------
114 struct jabber_state_machine_struct {
122 int in_message_error;
127 typedef struct jabber_state_machine_struct jabber_machine;
130 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
132 // ---------------------------------------------------------------------------------
133 // Transport session. This maintains all the various parts of a session
134 // ---------------------------------------------------------------------------------
135 struct transport_session_struct {
137 /* our socket connection */
138 //transport_socket* sock_obj;
139 socket_manager* sock_mgr;
141 /* our Jabber state machine */
142 jabber_machine* state_machine;
143 /* our SAX push parser context */
144 xmlParserCtxtPtr parser_ctxt;
146 /* our text buffers for holding text data */
147 growing_buffer* body_buffer;
148 growing_buffer* subject_buffer;
149 growing_buffer* thread_buffer;
150 growing_buffer* from_buffer;
151 growing_buffer* recipient_buffer;
152 growing_buffer* status_buffer;
153 growing_buffer* message_error_type;
154 growing_buffer* session_id;
155 int message_error_code;
157 /* for OILS extenstions */
158 growing_buffer* router_to_buffer;
159 growing_buffer* router_from_buffer;
160 growing_buffer* router_class_buffer;
161 growing_buffer* router_command_buffer;
162 int router_broadcast;
164 /* this can be anything. It will show up in the
165 callbacks for your convenience. Otherwise, it's
174 int component; /* true if we're a component */
176 /* the Jabber message callback */
177 void (*message_callback) ( void* user_data, transport_message* msg );
178 //void (iq_callback) ( void* user_data, transport_iq_message* iq );
180 typedef struct transport_session_struct transport_session;
183 // ------------------------------------------------------------------
184 // Allocates and initializes the necessary transport session
186 // If port > 0, then this session uses TCP connection. Otherwise,
187 // if unix_path != NULL, it uses a UNIX domain socket.
188 // ------------------------------------------------------------------
189 transport_session* init_transport( char* server, int port,
190 char* unix_path, void* user_data, int component );
192 // ------------------------------------------------------------------
193 // Returns the value of the given XML attribute
194 // The xmlChar** construct is commonly returned from SAX event
195 // handlers. Pass that in with the name of the attribute you want
197 // ------------------------------------------------------------------
198 char* get_xml_attr( const xmlChar** atts, char* attr_name );
200 // ------------------------------------------------------------------
201 // Waits at most 'timeout' seconds for data to arrive from the
202 // TCP handler. A timeout of -1 means to wait indefinitely.
203 // ------------------------------------------------------------------
204 int session_wait( transport_session* session, int timeout );
206 // ---------------------------------------------------------------------------------
207 // Sends the given Jabber message
208 // ---------------------------------------------------------------------------------
209 int session_send_msg( transport_session* session, transport_message* msg );
211 // ---------------------------------------------------------------------------------
212 // Returns 1 if this session is connected to the jabber server. 0 otherwise
213 // ---------------------------------------------------------------------------------
214 int session_connected( transport_session* );
216 // ------------------------------------------------------------------
217 // Deallocates session memory
218 // ------------------------------------------------------------------
219 int session_free( transport_session* session );
221 // ------------------------------------------------------------------
222 // Connects to the Jabber server. Waits at most connect_timeout
223 // seconds before failing
224 // ------------------------------------------------------------------
225 int session_connect( transport_session* session,
226 const char* username, const char* password,
227 const char* resource, int connect_timeout,
228 enum TRANSPORT_AUTH_TYPE auth_type );
230 int session_disconnect( transport_session* session );
232 int reset_session_buffers( transport_session* session );