Chop Chop, Jabber
[OpenSRF.git] / src / jserver / jserver-c_session.h
1 #include "utils.h"
2 #include "logging.h"
3
4 #include <string.h>
5 #include <stdio.h>
6
7 #include <libxml/globals.h>
8 #include <libxml/xmlerror.h>
9 #include <libxml/parser.h>
10 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
11 #include <libxml/tree.h>
12 #include <libxml/debugXML.h>
13 #include <libxml/xmlmemory.h>
14
15
16 /* session states */
17 #define JABBER_STATE_CONNECTED                  2
18 #define JABBER_STATE_CONNECTING                 4 
19 #define JABBER_STATE_IN_MESSAGE                 8       
20
21
22 struct jserver_session_struct {
23
24         /* our connection state */
25         unsigned int state;
26
27         /* incoming XML is parsed with the SAX parser */
28         xmlParserCtxtPtr parser_ctxt;
29
30         /* incoming message are shoved into this DOM doc after they are parsed */
31         xmlDocPtr current_msg;
32
33         /* we have to grab off the from and to for routing */
34         char* current_to;
35         char* current_from;
36
37         char* current_domain;
38         char* current_resource;
39         char* current_username;
40
41         int in_iq;
42         int in_uname;
43         int in_resource;
44
45         void* blob; /* callback blob - can be anything that needs passing around */
46         void (*on_msg_complete) (void* blob, char* msg_xml, char* from, char* to );
47
48         /* happens after someone logs in and we've pieced together the from address */
49         void (*on_from_discovered) (void* blob, char* from );
50         void (*on_login_init) (void* blob, char* reply );
51         void (*on_login_ok) (void* blob);
52         void (*on_client_finish) (void* blob);
53
54 };
55 typedef struct jserver_session_struct jserver_session;
56
57
58 jserver_session* jserver_session_init();
59 void jserver_session_free(jserver_session* session);
60 char* sax_xml_attr( const xmlChar** atts, char* attr_name );
61 int jserver_session_push_data(jserver_session* session, char* data);
62
63 void dom_add_attrs(xmlNodePtr node, const xmlChar** atts);
64 char* _xml_to_string( xmlDocPtr doc ); 
65
66
67 // ---------------------------------------------------------------------------------
68 // Our SAX handlers 
69 // ---------------------------------------------------------------------------------
70 void sax_start_element( 
71                 void *session, const xmlChar *name, const xmlChar **atts);
72
73 void sax_end_element( void* blob, const xmlChar *name);
74
75 void sax_start_doc(void* blob);
76 //void sax_end_doc(void* blob);
77
78 void sax_character( void* blob, const xmlChar *ch, int len);
79
80 void  sax_warning( void* blob, const char* msg, ... );
81
82 static xmlSAXHandler sax_handler_struct = {
83    NULL,                                                /* internalSubset */
84    NULL,                                                /* isStandalone */
85    NULL,                                                /* hasInternalSubset */
86    NULL,                                                /* hasExternalSubset */
87    NULL,                                                /* resolveEntity */
88    NULL,                                                /* getEntity */
89    NULL,                                                /* entityDecl */
90    NULL,                                                /* notationDecl */
91    NULL,                                                /* attributeDecl */
92    NULL,                                                /* elementDecl */
93    NULL,                                                /* unparsedEntityDecl */
94    NULL,                                                /* setDocumentLocator */
95    sax_start_doc,                       /* startDocument */
96    NULL,                                                /* endDocument */
97         sax_start_element,      /* startElement */
98         sax_end_element,                /* endElement */
99    NULL,                                                /* reference */
100         sax_character,                  /* characters */
101    NULL,                                                /* ignorableWhitespace */
102    NULL,                                                /* processingInstruction */
103    NULL,                                                /* comment */
104    sax_warning,                 /* xmlParserWarning */
105    sax_warning,                 /* xmlParserError */
106    NULL,                                                /* xmlParserFatalError : unused */
107    NULL,                                                /* getParameterEntity */
108    NULL,                                                /* cdataBlock; */
109    NULL,                                                /* externalSubset; */
110    1,
111    NULL,
112    NULL,                                                /* startElementNs */
113    NULL,                                                /* endElementNs */
114         NULL                                            /* xmlStructuredErrorFunc */
115 };
116
117 static const xmlSAXHandlerPtr sax_handler = &sax_handler_struct;