updated make process to copy over new web stuff and not old web stuff
[Evergreen.git] / OpenSRF / src / libtransport / transport_session.h
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, 
4 // logic ensues.
5 // ---------------------------------------------------------------------------------
6 #include "transport_message.h"
7
8 #include "utils.h"
9 #include "logging.h"
10 #include "socket_bundle.h"
11
12 #include "sha.h"
13
14 #include <string.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>
22
23 #ifndef TRANSPORT_SESSION_H
24 #define TRANSPORT_SESSION_H
25
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 */
28
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 
35
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 );
40 void grab_incoming(void* blob, socket_manager* mgr, int sockid, char* data, int parent);
41
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);
49
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);
55
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);
62
63 void  parseWarningHandler( void *session, const char* msg, ... );
64 void  parseErrorHandler( void *session, const char* msg, ... );
65
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 */
75    NULL,                                                        /* getEntity */
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 */
86    NULL,                                                        /* reference */
87         characterHandler,                       /* characters */
88    NULL,                                                        /* ignorableWhitespace */
89    NULL,                                                        /* processingInstruction */
90    NULL,                                                        /* comment */
91    parseWarningHandler,         /* xmlParserWarning */
92    parseErrorHandler,           /* xmlParserError */
93    NULL,                                                        /* xmlParserFatalError : unused */
94    NULL,                                                        /* getParameterEntity */
95    NULL,                                                        /* cdataBlock; */
96    NULL,                                                        /* externalSubset; */
97    1,
98    NULL,
99    NULL,                                                        /* startElementNs */
100    NULL,                                                        /* endElementNs */
101         NULL                                                    /* xmlStructuredErrorFunc */
102 };
103
104 // ---------------------------------------------------------------------------------
105 // Our SAX handler pointer.
106 // ---------------------------------------------------------------------------------
107 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
108
109 // ---------------------------------------------------------------------------------
110 // Jabber state machine.  This is how we know where we are in the Jabber
111 // conversation.
112 // ---------------------------------------------------------------------------------
113 struct jabber_state_machine_struct {
114         int connected;
115         int connecting;
116         int in_message;
117         int in_message_body;
118         int in_thread;
119         int in_subject;
120         int in_error;
121         int in_message_error;
122         int in_iq;
123         int in_presence;
124         int in_status;
125 };
126 typedef struct jabber_state_machine_struct jabber_machine;
127
128
129 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
130
131 // ---------------------------------------------------------------------------------
132 // Transport session.  This maintains all the various parts of a session
133 // ---------------------------------------------------------------------------------
134 struct transport_session_struct {
135
136         /* our socket connection */
137         //transport_socket* sock_obj;
138         socket_manager* sock_mgr;
139
140         /* our Jabber state machine */
141         jabber_machine* state_machine;
142         /* our SAX push parser context */
143         xmlParserCtxtPtr parser_ctxt;
144
145         /* our text buffers for holding text data */
146         growing_buffer* body_buffer;
147         growing_buffer* subject_buffer;
148         growing_buffer* thread_buffer;
149         growing_buffer* from_buffer;
150         growing_buffer* recipient_buffer;
151         growing_buffer* status_buffer;
152         growing_buffer* message_error_type;
153         growing_buffer* session_id;
154         int message_error_code;
155
156         /* for OILS extenstions */
157         growing_buffer* router_to_buffer;
158         growing_buffer* router_from_buffer;
159         growing_buffer* router_class_buffer;
160         growing_buffer* router_command_buffer;
161         int router_broadcast;
162
163         /* this can be anything.  It will show up in the 
164                 callbacks for your convenience. Otherwise, it's
165                 left untouched.  */
166         void* user_data;
167
168         char* server;
169         char* unix_path;
170         int     port;
171         int sock_id;
172
173         int component; /* true if we're a component */
174
175         /* the Jabber message callback */
176         void (*message_callback) ( void* user_data, transport_message* msg );
177         //void (iq_callback) ( void* user_data, transport_iq_message* iq );
178 };
179 typedef struct transport_session_struct transport_session;
180
181
182 // ------------------------------------------------------------------
183 // Allocates and initializes the necessary transport session
184 // data structures.
185 // If port > 0, then this session uses  TCP connection.  Otherwise,
186 // if unix_path != NULL, it uses a UNIX domain socket.
187 // ------------------------------------------------------------------
188 transport_session* init_transport( char* server, int port, 
189         char* unix_path, void* user_data, int component );
190
191 // ------------------------------------------------------------------
192 // Returns the value of the given XML attribute
193 // The xmlChar** construct is commonly returned from SAX event
194 // handlers.  Pass that in with the name of the attribute you want
195 // to retrieve.
196 // ------------------------------------------------------------------
197 char* get_xml_attr( const xmlChar** atts, char* attr_name );
198
199 // ------------------------------------------------------------------
200 // Waits  at most 'timeout' seconds  for data to arrive from the 
201 // TCP handler. A timeout of -1 means to wait indefinitely.
202 // ------------------------------------------------------------------
203 int session_wait( transport_session* session, int timeout );
204
205 // ---------------------------------------------------------------------------------
206 // Sends the given Jabber message
207 // ---------------------------------------------------------------------------------
208 int session_send_msg( transport_session* session, transport_message* msg );
209
210 // ---------------------------------------------------------------------------------
211 // Returns 1 if this session is connected to the jabber server. 0 otherwise
212 // ---------------------------------------------------------------------------------
213 int session_connected( transport_session* );
214
215 // ------------------------------------------------------------------
216 // Deallocates session memory
217 // ------------------------------------------------------------------
218 int session_free( transport_session* session );
219
220 // ------------------------------------------------------------------
221 // Connects to the Jabber server.  Waits at most connect_timeout
222 // seconds before failing
223 // ------------------------------------------------------------------
224 int session_connect( transport_session* session, 
225                 const char* username, const char* password, 
226                 const char* resource, int connect_timeout, 
227                 enum TRANSPORT_AUTH_TYPE auth_type );
228
229 int session_disconnect( transport_session* session );
230
231 int reset_session_buffers( transport_session* session );
232
233 #endif