Server side connection parses xml using a new written handler
[cascardo/rnetproxy.git] / jabber.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 **  
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **  
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **  
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 **  
18 */
19
20 #include <gnet.h>
21 #include <glib.h>
22 #include <iksemel.h>
23 #include "iksemel_extra.h"
24 #include "jabber.h"
25
26 static void jabber_connect (net_hook_t* hook)
27 {
28 }
29
30 static void jabber_close (net_hook_t* hook)
31 {
32 }
33
34 static void jabber_write (net_hook_t* hook)
35 {
36 }
37
38 static void jabber_read (net_hook_t* hook, gchar* buffer, size_t len)
39 {
40   iks_parse (hook->data, buffer, len, FALSE);
41 }
42
43 void jabber_error (net_hook_t* hook)
44 {
45   g_message ("Error in jabber client connection.");
46 }
47
48 GString* jabber_new_start (iks* node)
49 {
50   GString* buffer;
51   iks* attrib;
52   buffer = g_string_sized_new (256);
53   g_string_append (buffer, iks_string (iks_stack (node), node));
54   g_string_erase (buffer, buffer->len - 2, 1);
55   return buffer;
56 }
57
58 void jabber_connect_server (net_hook_t* hook, char* server, iks* node)
59 {
60   net_hook_t* server_hook;
61   GString* buffer;
62   g_message ("Received new stream.");
63   if (hook->peer == NULL)
64     {
65       server_hook = jabber_server_hook_new (hook, server);
66       hook->peer = server_hook->conn;
67       g_message ("Trying to connect to server %s.", server);
68       gnet_conn_connect (hook->peer);
69       gnet_conn_read (hook->peer);
70     }
71   buffer = jabber_new_start (node);
72   gnet_conn_write (hook->peer, buffer->str, buffer->len);
73   g_string_free (buffer, TRUE);
74 }
75
76 void jabber_send (net_hook_t* hook, iks* node)
77 {
78   GString* buffer;
79   buffer = g_string_new (iks_string (iks_stack (node), node));
80   gnet_conn_write (hook->peer, buffer->str, buffer->len);
81   g_string_free (buffer, TRUE);
82 }
83
84 int jabber_parser (gpointer data, int type, iks* node)
85 {
86   net_hook_t* hook;
87   char* server;
88   hook = (net_hook_t*) data;
89   switch (type)
90     {
91     case IKS_NODE_START:
92       server = iks_find_attrib (node, "to");
93       if (server == NULL)
94         {
95           jabber_error (hook);
96         }
97       jabber_connect_server (hook, server, node);
98       iks_delete (node);
99       break;
100     case IKS_NODE_NORMAL:
101       jabber_send (hook, node);
102       iks_delete (node);
103       break;
104     case IKS_NODE_STOP:
105       gnet_conn_write (hook->peer, "</stream:stream>", 16);
106       break;
107     case IKS_NODE_ERROR:
108       break;
109     }
110   return IKS_OK;
111 }
112
113 net_hook_t* jabber_hook_new (GConn* conn)
114 {
115   net_hook_t* hook;
116   hook = g_slice_new (net_hook_t);
117   hook->conn = conn;
118   hook->peer = NULL;
119   hook->server = FALSE;
120   hook->connect = jabber_connect;
121   hook->close = jabber_close;
122   hook->write = jabber_write;
123   hook->read = jabber_read;
124   hook->data = iks_extra_stream_new (hook, jabber_parser);
125   gnet_conn_set_callback (hook->conn, nethook_event, hook);
126   return hook;
127 }
128
129 void jabber_destroy (net_hook_t* hook)
130 {
131   g_slice_free (net_hook_t, hook);
132 }