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