Added id handler

This commit is contained in:
James Booth
2015-05-16 21:55:51 +01:00
parent 50cd28a977
commit ede3ccab9d
10 changed files with 88 additions and 11 deletions

View File

@@ -46,3 +46,10 @@ stbbr_auth_passwd(char *password)
prime_required_passwd(password);
return 1;
}
int
stbbr_for(char *id, char *stanza)
{
prime_for(id, stanza);
return 1;
}

View File

@@ -16,6 +16,7 @@ static XMPPStanza *curr_stanza;
static stream_start_func stream_start_cb = NULL;
static auth_func auth_cb = NULL;
static id_func id_cb = NULL;
static void
start_element(void *data, const char *element, const char **attributes)
@@ -44,18 +45,20 @@ end_element(void *data, const char *element)
{
depth--;
if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) {
auth_cb(curr_stanza, xmppclient);
}
if (depth > 0) {
stanza_add_child(curr_stanza->parent, curr_stanza);
curr_stanza = curr_stanza->parent;
} else {
stanza_add(curr_stanza);
}
if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) {
auth_cb(curr_stanza, xmppclient);
} else {
const char *id = stanza_get_id(curr_stanza);
if (id) {
id_cb(id, xmppclient);
}
}
if (depth == 0) {
do_reset = 1;
}
}
@@ -71,11 +74,12 @@ handle_data(void *data, const char *content, int length)
}
void
parser_init(XMPPClient *client, stream_start_func startcb, auth_func authcb)
parser_init(XMPPClient *client, stream_start_func startcb, auth_func authcb, id_func idcb)
{
xmppclient = client;
stream_start_cb = startcb;
auth_cb = authcb;
id_cb = idcb;
parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, start_element, end_element);
@@ -99,7 +103,7 @@ parser_reset(void)
{
if (do_reset == 1) {
parser_close();
parser_init(xmppclient, stream_start_cb, auth_cb);
parser_init(xmppclient, stream_start_cb, auth_cb, id_cb);
do_reset = 0;
}
}

View File

@@ -6,8 +6,9 @@
typedef void (*stream_start_func)(XMPPClient *client);
typedef void (*auth_func)(XMPPStanza *stanza, XMPPClient *client);
typedef void (*id_func)(const char *id, XMPPClient *client);
void parser_init(XMPPClient *client, stream_start_func startcb, auth_func authcb);
void parser_init(XMPPClient *client, stream_start_func startcb, auth_func authcb, id_func);
int parser_feed(char *chunk, int len);
void parser_close(void);
void parser_reset(void);

View File

@@ -1,6 +1,10 @@
#include <stdlib.h>
#include <glib.h>
#include <string.h>
static char *required_passwd = NULL;
static GHashTable *idstubs;
void
prime_required_passwd(char *password)
@@ -13,3 +17,23 @@ prime_get_passwd(void)
{
return required_passwd;
}
void
prime_for(char *id, char *stanza)
{
if (!idstubs) {
idstubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
}
g_hash_table_insert(idstubs, strdup(id), strdup(stanza));
}
char*
prime_get_for(const char *id)
{
if (idstubs) {
return g_hash_table_lookup(idstubs, id);
}
return NULL;
}

View File

@@ -4,4 +4,7 @@
void prime_required_passwd(char *password);
char* prime_get_passwd(void);
int prime_for(char *id, char *stanza);
char* prime_get_for(const char *id);
#endif

View File

@@ -166,6 +166,18 @@ auth_callback(XMPPStanza *stanza, XMPPClient *client)
log_print("RECV: ");
}
void
id_callback(const char *id, XMPPClient *client)
{
char *stanza_ret = prime_get_for(id);
if (stanza_ret) {
log_print_chars("\n");
log_println("--> ID callback fired for '%s'", id);
send_to(client, stanza_ret);
log_print("RECV: ");
}
}
int
server_run(int port)
{
@@ -224,7 +236,7 @@ server_run(int port)
}
client = xmppclient_new(client_addr, client_socket);
parser_init(client, stream_start_callback, auth_callback);
parser_init(client, stream_start_callback, auth_callback, id_callback);
log_print("RECV: ");
int res = listen_for_xmlstart(client);
if (res == -1) {

View File

@@ -2,5 +2,4 @@
#define __H_SERVER
int server_run(int port);
#endif

View File

@@ -139,3 +139,27 @@ stanza_get_child_by_name(XMPPStanza *stanza, char *name)
return NULL;
}
const char *
stanza_get_id(XMPPStanza *stanza)
{
if (!stanza) {
return NULL;
}
if (!stanza->attrs) {
return NULL;
}
GList *curr_attr = stanza->attrs;
while (curr_attr) {
XMPPAttr *attr = curr_attr->data;
if (g_strcmp0(attr->name, "id") == 0) {
return attr->value;
}
curr_attr = g_list_next(curr_attr);
}
return NULL;
}

View File

@@ -19,6 +19,8 @@ void stanza_show(XMPPStanza *stanza);
void stanza_show_all(void);
void stanza_add(XMPPStanza *stanza);
void stanza_add_child(XMPPStanza *parent, XMPPStanza *child);
const char* stanza_get_id(XMPPStanza *stanza);
XMPPStanza* stanza_get_child_by_ns(XMPPStanza *stanza, char *ns);
XMPPStanza* stanza_get_child_by_name(XMPPStanza *stanza, char *name);

View File

@@ -5,5 +5,6 @@ int stbbr_main(int port);
int stbbr_start(int port);
int stbbr_auth_passwd(char *password);
void stbbr_for(char *id, char *stanza);
#endif