Documentation files
[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 "jabber.h"
24
25 static void jabber_connect (net_hook_t* hook)
26 {
27 }
28
29 static void jabber_close (net_hook_t* hook)
30 {
31 }
32
33 static void jabber_write (net_hook_t* hook)
34 {
35 }
36
37 static void jabber_read (net_hook_t* hook, gchar* buffer, size_t len)
38 {
39   iks_parse (hook->data, buffer, len, FALSE);
40 }
41
42 void jabber_error (net_hook_t* hook)
43 {
44   g_message ("Error in jabber client connection.");
45 }
46
47 GString* jabber_new_start (iks* node)
48 {
49   GString* buffer;
50   iks* attrib;
51   buffer = g_string_sized_new (256);
52   g_string_append (buffer, iks_string (iks_stack (node), node));
53   g_string_erase (buffer, buffer->len - 2, 1);
54   return buffer;
55 }
56
57 void jabber_connect_server (net_hook_t* hook, char* server, iks* node)
58 {
59   net_hook_t* server_hook;
60   GString* buffer;
61   g_message ("Received new stream.");
62   if (hook->peer == NULL)
63     {
64       server_hook = jabber_server_hook_new (hook, server);
65       hook->peer = server_hook->conn;
66       g_message ("Trying to connect to server %s.", server);
67       gnet_conn_connect (hook->peer);
68       gnet_conn_read (hook->peer);
69     }
70   buffer = jabber_new_start (node);
71   gnet_conn_write (hook->peer, buffer->str, buffer->len);
72   g_string_free (buffer, TRUE);
73 }
74
75 void jabber_send (net_hook_t* hook, iks* node)
76 {
77   GString* buffer;
78   buffer = g_string_new (iks_string (iks_stack (node), node));
79   gnet_conn_write (hook->peer, buffer->str, buffer->len);
80   g_string_free (buffer, TRUE);
81 }
82
83 int jabber_parser (gpointer data, int type, iks* node)
84 {
85   net_hook_t* hook;
86   char* server;
87   hook = (net_hook_t*) data;
88   switch (type)
89     {
90     case IKS_NODE_START:
91       server = iks_find_attrib (node, "to");
92       if (server == NULL)
93         {
94           jabber_error (hook);
95         }
96       jabber_connect_server (hook, server, node);
97       iks_delete (node);
98       break;
99     case IKS_NODE_NORMAL:
100       jabber_send (hook, node);
101       iks_delete (node);
102       break;
103     case IKS_NODE_STOP:
104       gnet_conn_write (hook->peer, "</stream:stream>", 16);
105       break;
106     case IKS_NODE_ERROR:
107       break;
108     }
109   return IKS_OK;
110 }
111
112 net_hook_t* jabber_hook_new (GConn* conn)
113 {
114   net_hook_t* hook;
115   hook = g_slice_new (net_hook_t);
116   hook->conn = conn;
117   hook->peer = NULL;
118   hook->server = FALSE;
119   hook->connect = jabber_connect;
120   hook->close = jabber_close;
121   hook->write = jabber_write;
122   hook->read = jabber_read;
123   hook->data = iks_stream_new ("jabber:client", hook, jabber_parser);
124   gnet_conn_set_callback (hook->conn, nethook_event, hook);
125   return hook;
126 }
127
128 void jabber_destroy (net_hook_t* hook)
129 {
130   g_slice_free (net_hook_t, hook);
131 }