Added support for a message hook and simple message sending
[cascardo/chat.git] / hook.c
1 /*
2  *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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 along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "iksemel_extra.h"
24 #include "xmpp.h"
25 #include "sasl.h"
26 #include "bind.h"
27 #include "disco.h"
28 #include "xmpp_internal.h"
29
30 void
31 hc_xmpp_hook_bind (hc_xmpp_t *xmpp, iks *stanza)
32 {
33   hc_xmpp_bind_result (xmpp, stanza);
34   if (hc_xmpp_status (xmpp) == HC_XMPP_BOUND &&
35       hc_xmpp_is_session_supported (xmpp))
36     hc_xmpp_session (xmpp);
37 }
38
39 void
40 hc_xmpp_hook_session (hc_xmpp_t *xmpp, iks *stanza)
41 {
42   hc_xmpp_session_result (xmpp, stanza);
43   if (hc_xmpp_status (xmpp) == HC_XMPP_SESSION)
44     hc_xmpp_send_disco_info (xmpp, hc_xmpp_server (xmpp));
45 }
46
47 void
48 hc_xmpp_hook_disco (hc_xmpp_t *xmpp, iks *stanza)
49 {
50   hc_xmpp_recv_disco (xmpp, stanza);
51 }
52
53 int
54 hc_xmpp_hook (void *data, int type, iks *stanza)
55 {
56   hc_xmpp_t *xmpp = (hc_xmpp_t *) data;
57   if (!iks_strcmp (iks_name (stanza), "message"))
58     {
59       hc_xmpp_recv_message (xmpp, stanza);
60     }
61   else if (!iks_strcmp (iks_name (stanza), "iq"))
62     {
63       char *ns = iks_find_attrib (iks_child (stanza), "xmlns");
64       hc_xmpp_hook_t hook;
65       if (g_hash_table_lookup_extended (xmpp->nshooks, ns,
66                                         NULL, (gpointer *) &hook))
67         hook (xmpp, stanza);
68     }
69   else if (!iks_strcmp (iks_name (stanza), "stream:features"))
70     {
71       hc_xmpp_features (data, stanza);
72       if (hc_xmpp_is_sasl_supported (data) & !hc_xmpp_is_sasl_enabled (data))
73         {
74           hc_xmpp_sasl_authenticate (data);
75         }
76       if (hc_xmpp_is_bind_supported (data))
77         {
78           hc_xmpp_bind (data);
79         }
80     }
81   else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL))
82     {
83       hc_xmpp_sasl_iterate (data, stanza);
84       if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED)
85         {
86           hc_xmpp_send_stream (data);
87         }
88     }
89   return IKS_OK;
90 }
91
92 void
93 hc_xmpp_send_stream (hc_xmpp_t *xmpp)
94 {
95   char *buffer = NULL;
96   asprintf (&buffer, "<stream:stream xmlns='jabber:client' "
97                      "xmlns:stream='http://etherx.jabber.org/streams' "
98                      "version='1.0' to='%s'>", hc_xmpp_server (xmpp));
99   hc_xmpp_send_buffer (xmpp, buffer, 0);
100   free (buffer);
101 }