2
0
mirror of https://github.com/msantos/xmppipe synced 2024-11-04 06:00:22 +00:00

reorg: version handler

This commit is contained in:
Michael Santos 2019-01-25 10:28:00 -05:00
parent 895ad40ee2
commit 9a98b8b9bf
3 changed files with 88 additions and 72 deletions

86
src/version.c Normal file
View File

@ -0,0 +1,86 @@
/* Copyright (c) 2015-2019, Michael Santos <michael.santos@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "xmppipe.h"
int
handle_version(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_stanza_t *reply = NULL;
xmpp_stanza_t *query = NULL;
xmpp_stanza_t *name = NULL;
xmpp_stanza_t *version = NULL;
xmpp_stanza_t *text = NULL;
xmpp_stanza_t *child = NULL;
const char *ns = NULL;
const char *id = NULL;
const char *from = NULL;
xmppipe_state_t *state = userdata;
xmpp_ctx_t *ctx = state->ctx;
reply = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(reply, "iq");
xmppipe_stanza_set_type(reply, "result");
id = xmpp_stanza_get_attribute(stanza, "id");
if (id == NULL)
return 1;
xmppipe_stanza_set_id(reply, id);
from = xmpp_stanza_get_attribute(stanza, "from");
if (from == NULL)
return 1;
xmppipe_stanza_set_attribute(reply, "to", from);
query = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(query, "query");
child = xmpp_stanza_get_children(stanza);
if (child == NULL) {
(void)xmpp_stanza_release(query);
return 1;
}
ns = xmpp_stanza_get_ns(child);
if (ns)
xmppipe_stanza_set_ns(query, ns);
name = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(name, "name");
xmppipe_stanza_add_child(query, name);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, "xmppipe");
xmppipe_stanza_add_child(name, text);
version = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(version, "version");
xmppipe_stanza_add_child(query, version);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, XMPPIPE_VERSION);
xmppipe_stanza_add_child(version, text);
xmppipe_stanza_add_child(reply, query);
xmppipe_send(state, reply);
(void)xmpp_stanza_release(reply);
return 1;
}

View File

@ -33,7 +33,6 @@ int handle_disco_items(xmpp_conn_t * const, xmpp_stanza_t * const,
void * const);
int handle_disco_info(xmpp_conn_t * const, xmpp_stanza_t * const,
void * const);
int handle_version(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_presence(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_presence_error(xmpp_conn_t * const, xmpp_stanza_t * const,
void * const);
@ -835,77 +834,6 @@ handle_disco_info(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
return 0;
}
return 1;
}
int
handle_version(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_stanza_t *reply = NULL;
xmpp_stanza_t *query = NULL;
xmpp_stanza_t *name = NULL;
xmpp_stanza_t *version = NULL;
xmpp_stanza_t *text = NULL;
xmpp_stanza_t *child = NULL;
const char *ns = NULL;
const char *id = NULL;
const char *from = NULL;
xmppipe_state_t *state = userdata;
xmpp_ctx_t *ctx = state->ctx;
reply = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(reply, "iq");
xmppipe_stanza_set_type(reply, "result");
id = xmpp_stanza_get_attribute(stanza, "id");
if (id == NULL)
return 1;
xmppipe_stanza_set_id(reply, id);
from = xmpp_stanza_get_attribute(stanza, "from");
if (from == NULL)
return 1;
xmppipe_stanza_set_attribute(reply, "to", from);
query = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(query, "query");
child = xmpp_stanza_get_children(stanza);
if (child == NULL) {
(void)xmpp_stanza_release(query);
return 1;
}
ns = xmpp_stanza_get_ns(child);
if (ns)
xmppipe_stanza_set_ns(query, ns);
name = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(name, "name");
xmppipe_stanza_add_child(query, name);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, "xmppipe");
xmppipe_stanza_add_child(name, text);
version = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_name(version, "version");
xmppipe_stanza_add_child(query, version);
text = xmppipe_stanza_new(ctx);
xmppipe_stanza_set_text(text, XMPPIPE_VERSION);
xmppipe_stanza_add_child(version, text);
xmppipe_stanza_add_child(reply, query);
xmppipe_send(state, reply);
(void)xmpp_stanza_release(reply);
return 1;
}

View File

@ -101,7 +101,9 @@ typedef struct {
int format; /* input format: stdin, colon */
} xmppipe_state_t;
/* handlers */
int handle_message(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
int handle_version(xmpp_conn_t * const, xmpp_stanza_t * const, void * const);
void xmppipe_send_stanza(xmppipe_state_t *, char *, size_t);
void xmppipe_send_stanza_fmt(xmppipe_state_t *state, char *buf, size_t len);