From: Thadeu Lima de Souza Cascardo Date: Mon, 3 Nov 2008 10:42:13 +0000 (-0200) Subject: Separated main hook from main program X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fchat.git;a=commitdiff_plain;h=2dd07de4ac61aea2ce976ea0841d597d8801b139 Separated main hook from main program --- diff --git a/Makefile b/Makefile index fe90dbe..6a87a8b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ OBJECTS = sort_udns.o tcp_connect.o iksemel_extra.o tictactoe.o \ - xmpp.o features.o sasl.o bind.o disco.o + xmpp.o features.o sasl.o bind.o disco.o hook.o CC = gcc CFLAGS = -g -Wall `pkg-config --cflags iksemel libgsasl` LIBS = -ludns `pkg-config --libs iksemel libgsasl` diff --git a/hook.c b/hook.c new file mode 100644 index 0000000..766d6a1 --- /dev/null +++ b/hook.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2008 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#define _GNU_SOURCE +#include +#include +#include "iksemel_extra.h" +#include "xmpp.h" +#include "sasl.h" +#include "bind.h" +#include "disco.h" + +int +hc_xmpp_hook (void *data, int type, iks *stanza) +{ + if (!iks_strcmp (iks_name (stanza), "iq")) + { + char *ns = iks_find_attrib (iks_child (stanza), "xmlns"); + if (!iks_strcmp (ns, HC_XMPP_NS_BIND)) + { + hc_xmpp_bind_result (data, stanza); + if (hc_xmpp_status (data) == HC_XMPP_BOUND && + hc_xmpp_is_session_supported (data)) + hc_xmpp_session (data); + } + else if (!iks_strcmp (ns, HC_XMPP_NS_SESSION)) + { + hc_xmpp_session_result (data, stanza); + if (hc_xmpp_status (data) == HC_XMPP_SESSION) + hc_xmpp_send_disco_info (data, hc_xmpp_server (data)); + } + else if (!iks_strcmp (ns, HC_XMPP_NS_DISCO_INFO)) + { + hc_xmpp_recv_disco (data, stanza); + } + } + else if (!iks_strcmp (iks_name (stanza), "stream:features")) + { + hc_xmpp_features (data, stanza); + if (hc_xmpp_is_sasl_supported (data) & !hc_xmpp_is_sasl_enabled (data)) + { + hc_xmpp_sasl_authenticate (data); + } + if (hc_xmpp_is_bind_supported (data)) + { + hc_xmpp_bind (data); + } + } + else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL)) + { + hc_xmpp_sasl_iterate (data, stanza); + if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED) + { + hc_xmpp_send_stream (data); + } + } + return IKS_OK; +} + +void +hc_xmpp_send_stream (hc_xmpp_t *xmpp) +{ + char *buffer = NULL; + asprintf (&buffer, "", hc_xmpp_server (xmpp)); + hc_xmpp_send_buffer (xmpp, buffer, 0); + free (buffer); +} diff --git a/tictactoe.c b/tictactoe.c index ed74e49..af9de7c 100644 --- a/tictactoe.c +++ b/tictactoe.c @@ -26,81 +26,8 @@ #include "tcp_connect.h" #include "iksemel_extra.h" #include "xmpp.h" -#include "sasl.h" -#include "bind.h" #include "disco.h" -static void write_stream (hc_xmpp_t *); - -static void -send_message_test (hc_xmpp_t *xmpp) -{ - hc_xmpp_send_disco_info (xmpp, hc_xmpp_server (xmpp)); -} - -static int -myhook (void *data, int type, iks *stanza) -{ - if (!iks_strcmp (iks_name (stanza), "iq")) - { - char *ns = iks_find_attrib (iks_child (stanza), "xmlns"); - if (!iks_strcmp (ns, HC_XMPP_NS_BIND)) - { - hc_xmpp_bind_result (data, stanza); - if (hc_xmpp_status (data) == HC_XMPP_BOUND && - hc_xmpp_is_session_supported (data)) - hc_xmpp_session (data); - } - else if (!iks_strcmp (ns, HC_XMPP_NS_SESSION)) - { - hc_xmpp_session_result (data, stanza); - if (hc_xmpp_status (data) == HC_XMPP_SESSION) - send_message_test (data); - } - else if (!iks_strcmp (ns, HC_XMPP_NS_DISCO_INFO)) - { - hc_xmpp_recv_disco (data, stanza); - } - } - else if (!iks_strcmp (iks_name (stanza), "stream:features")) - { - hc_xmpp_features (data, stanza); - if (hc_xmpp_is_sasl_supported (data) & !hc_xmpp_is_sasl_enabled (data)) - { - hc_xmpp_sasl_authenticate (data); - } - if (hc_xmpp_is_bind_supported (data)) - { - hc_xmpp_bind (data); - } - } - else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL)) - { - hc_xmpp_sasl_iterate (data, stanza); - if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED) - { - write_stream (data); - fprintf (stdout, "Authenticated\n"); - } - } - else - { - fprintf (stderr, "Other: %s\n", iks_string (iks_stack (stanza), stanza)); - } - return IKS_OK; -} - -static void -write_stream (hc_xmpp_t *xmpp) -{ - char *buffer = NULL; - asprintf (&buffer, "", hc_xmpp_server (xmpp)); - hc_xmpp_send_buffer (xmpp, buffer, 0); - free (buffer); -} - static void loop (hc_xmpp_t *xmpp) { @@ -116,8 +43,8 @@ main (int argc, char **argv) char *password = "pubsub"; hc_xmpp_t *xmpp; dns_init (NULL, 1); - xmpp = hc_xmpp_new (myhook, server, user, password); - write_stream (xmpp); + xmpp = hc_xmpp_new (hc_xmpp_hook, server, user, password); + hc_xmpp_send_stream (xmpp); loop (xmpp); return 0; } diff --git a/xmpp.h b/xmpp.h index c76bb06..f26cf73 100644 --- a/xmpp.h +++ b/xmpp.h @@ -57,5 +57,7 @@ void hc_xmpp_send_iks (hc_xmpp_t *, iks *); void hc_xmpp_read_and_parse (hc_xmpp_t *); void hc_xmpp_features (hc_xmpp_t *, iks *); int hc_xmpp_status (hc_xmpp_t *); +int hc_xmpp_hook (void *, int, iks *); +void hc_xmpp_send_stream (hc_xmpp_t *); #endif