Chop Chop, Jabber
[OpenSRF.git] / src / jserver / jserver-c.c
1 #include "jserver-c.h"
2
3 /* ------------------------------------------------
4         some pre-packaged Jabber XML
5         ------------------------------------------------ */
6 static const char* xml_parse_error = "<stream:stream xmlns:stream='http://etherx.jabber.org/streams'" 
7         "version='1.0'><stream:error xmlns:stream='http://etherx.jabber.org/streams'>"
8         "<xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>"
9         "<text xmlns='urn:ietf:params:xml:ns:xmpp-streams'>syntax error</text></stream:error></stream:stream>";
10
11 static const char* xml_login_ok = "<iq xmlns='jabber:client' id='asdfjkl' type='result'/>";
12
13
14 jserver* jserver_init() {
15         jserver* js                                     = safe_malloc(sizeof(jserver));
16         js->mgr                                         = safe_malloc(sizeof(socket_manager));
17         js->mgr->data_received  = &jserver_handle_request;
18         js->mgr->blob                           = js;
19         js->mgr->on_socket_closed       = &jserver_socket_closed;
20         js->client                                      = NULL;
21
22         return js;
23 }
24
25 void jserver_free(jserver* js) {
26         if(js == NULL) return;
27         jclient_node* node; 
28         while(js->client) {
29                 node = js->client->next;
30                 _jserver_remove_client(js, js->client->addr);
31                 js->client = node;
32         }
33         socket_manager_free(js->mgr);
34         free(js);
35 }
36
37 void jserver_socket_closed(void* blob, int sock_id) {
38         jserver* js = (jserver*) blob;
39         if(js == NULL) return;
40         jclient_node* node = jserver_find_client_id(js, sock_id);
41         if(node) {
42                 info_handler("Removing client %d - %s remote "
43                                 "site closed socket", sock_id, node->addr);
44                 _jserver_remove_client(js, node->addr);
45         }
46
47 }
48
49 /* opens the inet and unix sockets that we're listening on */
50 int jserver_connect(jserver* js, int port, char* unix_path) {
51         if(js == NULL || js->mgr == NULL) return -1;
52         int status = 0;
53
54         if(port > 0) {
55                 status = socket_open_tcp_server(js->mgr, port);
56                 if(status == -1) return status;
57         }
58
59         if(unix_path != NULL) {
60                 status = socket_open_unix_server(js->mgr, unix_path);
61                 if(status == -1) return status;
62         }
63
64         return 0;
65 }
66
67 void _free_jclient_node(jclient_node* node) {
68         if(node == NULL) return;
69         free(node->addr);
70         jserver_session_free(node->session);
71         free(node);
72 }
73
74 /* allocates a new client node */
75 jclient_node* _new_jclient_node(int id) {
76         jclient_node* node = safe_malloc(sizeof(jclient_node));
77         node->id = id;
78         node->addr = NULL;
79         node->session = jserver_session_init();
80         node->session->blob = node;
81         node->session->on_msg_complete = &jserver_client_handle_msg; 
82         node->session->on_from_discovered = &jserver_client_from_found;
83         node->session->on_login_init = &jserver_client_login_init;
84         node->session->on_login_ok = &jserver_client_login_ok;
85         node->session->on_client_finish = &jserver_client_finish;
86         return node;
87 }
88
89 /* client has sent the end of it's session doc, we may now disconnect */
90 void jserver_client_finish(void* blob) {
91         jclient_node* node = (jclient_node*) blob;
92         if(node == NULL) return;
93         jserver_send_id(node->id, "</stream:stream>");
94         _jserver_remove_client(node->parent, node->addr);
95
96 }
97
98 void jserver_client_from_found(void* blob, char* from) {
99         jclient_node* node = (jclient_node*) blob;
100         if(node == NULL || from == NULL) return;
101
102         /* prevent duplicate login - kick off original */
103         _jserver_remove_client(node->parent, from);
104         info_handler("logged in: %s", from);
105         node->addr = strdup(from);
106 }
107
108 void jserver_client_login_init(void* blob, char* reply) {
109         debug_handler("here");
110         jclient_node* node = (jclient_node*) blob;
111         if(node == NULL || reply == NULL) return;
112         debug_handler("jserver handling login init");
113         jserver_send_id(node->id, reply);
114 }
115
116 void jserver_client_login_ok(void* blob) {
117         jclient_node* node = (jclient_node*) blob;
118         if(node == NULL) return;
119         info_handler("Client logging in ok => %d", node->id);
120         jserver_send_id(node->id, xml_login_ok);
121 }
122
123 void jserver_client_handle_msg( 
124         void* blob, char* xml, char* from, char* to ) {
125
126         jclient_node* node = (jclient_node*) blob;
127         if(node == NULL || xml == NULL || to == NULL) return;
128         int from_id = 0;
129
130         jclient_node* from_node = jserver_find_client(node->parent, from);
131         if(from_node)
132                 from_id = from_node->id;
133
134
135         debug_handler("Client %d received from %s message : %s", 
136                         node->id, from, xml );
137
138         jserver_send(node->parent, from_id, to, xml);
139 }
140
141 /* allocates a new client node and adds it to the set */
142 jclient_node* _jserver_add_client(jserver* js, int id) {
143         if(js == NULL) return NULL;
144         jclient_node* node = _new_jclient_node(id);
145         node->next = js->client;
146         js->client = node;
147         node->parent = js;
148         return node;
149 }
150
151
152 /* removes and frees a client node */
153 void _jserver_remove_client(jserver* js, char* addr) {
154         if(js == NULL || js->client == NULL || addr == NULL) return;
155
156         jclient_node* node = js->client;
157
158         if(node->addr && !strcmp(node->addr,addr)) {
159                 js->client = node->next;
160                 socket_disconnect(js->mgr, node->id);
161                 _free_jclient_node(node);
162                 return;
163         }
164
165         debug_handler("Searching for jclient to remove");
166         jclient_node* tail_node = node;
167         node = node->next;
168
169         while(node) {
170                 if(node->addr && !strcmp(node->addr,addr)) {
171                         tail_node->next = node->next;
172                         socket_disconnect(js->mgr, node->id);
173                         _free_jclient_node(node);
174                         return;
175                 }
176                 tail_node = node;
177                 node = node->next;
178         }
179 }
180
181 /* finds a client node by addr */
182 jclient_node* jserver_find_client(jserver* js, char* addr) {
183         if(js == NULL || addr == NULL) return NULL;
184         jclient_node* node = js->client;
185         while(node) {
186                 if(node->addr && !strcmp(node->addr, addr)) 
187                         return node;
188                 node = node->next;
189         }
190         return NULL;
191 }
192
193 jclient_node* jserver_find_client_id(jserver* js, int id) {
194         if(js == NULL) return NULL;
195         jclient_node* node = js->client;
196         while(node) {
197                 if(node->id == id) 
198                         return node;
199                 node = node->next;
200         }
201         return NULL;
202 }
203
204 /* sends msg to client at 'to_addr' */
205 int jserver_send(jserver* js, int from_id, char* to_addr, const char* msg_xml) {
206         debug_handler("sending message to %s : %s", to_addr, msg_xml);
207         if(to_addr == NULL || msg_xml == NULL) return -1;
208
209         jclient_node* node = jserver_find_client(js, to_addr);
210
211         if(node == NULL) {
212                 info_handler("message to non-existent client %s", to_addr);
213                 if(from_id > 0) {
214                         jclient_node* from = jserver_find_client_id(js, from_id);
215
216                         if(from) {
217                                 info_handler("replying with error...");
218                                 char buf[2048];
219                                 memset(buf, 0, 2048);
220                                 snprintf(buf, 2047, "<message xmlns='jabber:client' type='error' from='%s' "
221                                         "to='%s'><error type='cancel' code='404'><item-not-found "
222                                         "xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>"
223                                         "<body>NOT ADDING BODY</body></message>", to_addr, from->addr );
224                                 jserver_send_id(from_id, buf);
225                         }
226                 }
227                 return -1;
228         }
229
230         return jserver_send_id(node->id, msg_xml);
231 }
232
233 int jserver_send_id(int client_id, const char* msg_xml) {
234         if(msg_xml == NULL || client_id < 1) return -1;
235         return socket_send(client_id, msg_xml );
236 }
237
238 /* waits for any incoming data */
239 int jserver_wait(jserver* js) {
240         if(js == NULL) return -1;
241         while(1) {
242                 if(socket_wait_all(js->mgr, -1) < 0)
243                         warning_handler(
244                                 "jserver_wait(): socket_wait_all() returned error");
245
246         }
247 }
248
249
250 int _jserver_push_client_data(jclient_node* node, char* data) {
251         if(node == NULL || data == NULL) return -1;
252         return jserver_session_push_data( node->session, data);
253 }
254
255 void jserver_handle_request(void* js_blob, 
256         socket_manager* mgr, int sock_id, char* data, int parent_id ) {
257
258         jserver* js = (jserver*) js_blob;
259
260         debug_handler("jsever received data from socket %d (parent %d)", sock_id, parent_id );
261
262         jclient_node* node = jserver_find_client_id(js, sock_id);
263         if(!node) {
264                 debug_handler("We have a new client connection, adding to list");
265                 node = _jserver_add_client(js, sock_id);
266         }
267
268         if(_jserver_push_client_data(node, data) == -1) {
269                 warning_handler("Client sent bad data, disconnecting...");
270                 jserver_send_id(node->id, xml_parse_error);
271                 _jserver_remove_client(js, node->addr);         
272
273         } else {
274                 debug_handler("Client data successfully parsed");
275         }
276
277 }
278
279
280
281