From 46b8a21cfb49d64dbb190b38192934df25de0fb8 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Sat, 20 Oct 2012 09:19:59 +0300 Subject: [PATCH] jabber: rewrite error handling --- src/jabber.c | 78 ++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 52 deletions(-) diff --git a/src/jabber.c b/src/jabber.c index 70162cc4..d7809b03 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -42,18 +42,6 @@ static struct _jabber_conn_t { int tls_disabled; } jabber_conn; -typedef struct _err_code_tbl_t { - int code; - char *condition; -} err_code_tbl_t; - -static err_code_tbl_t err_code_tbl[] = { - {404, "recipient-unavailable"}, - {503, "service-unavailable"}, - {0, NULL}, // termination line -}; - -static char *_get_xmpp_err_by_code(const char * const err_code); static log_level_t _get_log_level(xmpp_log_level_t xmpp_level); static xmpp_log_level_t _get_xmpp_log_level(); static void _xmpp_file_logger(void * const userdata, @@ -267,6 +255,32 @@ static int _message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { + char *type; + char *from; + + type = xmpp_stanza_get_attribute(stanza, "type"); + from = xmpp_stanza_get_attribute(stanza, "from"); + + if (strcmp(type, "error") == 0) { + char *err_msg = NULL; + xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, "error"); + if (error == NULL) { + log_debug("error message without received"); + return 1; + } else { + xmpp_stanza_t *err_cond = xmpp_stanza_get_children(error); + if (err_cond == NULL) { + log_debug("error message without received"); + return 1; + } else { + err_msg = xmpp_stanza_get_name(err_cond); + } + // TODO: process 'type' attribute from [RFC6120, 8.3.2] + } + prof_handle_error_message(from, err_msg); + return 1; + } + xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body"); // if no message, check for chatstates @@ -277,7 +291,6 @@ _message_handler(xmpp_conn_t * const conn, // active } else if (xmpp_stanza_get_child_by_name(stanza, "composing") != NULL) { // composing - char *from = xmpp_stanza_get_attribute(stanza, "from"); prof_handle_typing(from); } } @@ -286,27 +299,7 @@ _message_handler(xmpp_conn_t * const conn, } // message body recieved - char *type = xmpp_stanza_get_attribute(stanza, "type"); - if(strcmp(type, "error") == 0) { - // error message received - char *from = xmpp_stanza_get_attribute(stanza, "from"); - char *err_msg = NULL; - xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, "error"); - if (error) { - xmpp_stanza_t *err_cond = xmpp_stanza_get_children(error); - if (err_cond) { - err_msg = xmpp_stanza_get_name(err_cond); - } else { - char *err_code = xmpp_stanza_get_attribute(error, "code"); - err_msg = _get_xmpp_err_by_code(err_code); - } - } - prof_handle_error_message(from, err_msg); - return 1; - } - char *message = xmpp_stanza_get_text(body); - char *from = xmpp_stanza_get_attribute(stanza, "from"); prof_handle_incoming_message(from, message); return 1; @@ -463,25 +456,6 @@ _presence_handler(xmpp_conn_t * const conn, return 1; } -static char * -_get_xmpp_err_by_code(const char * const err_code) -{ - int code; - int i = 0; - - if (err_code == NULL) - return NULL; - - code = atoi(err_code); - while (err_code_tbl[i].code != 0) { - if (err_code_tbl[i].code == code) { - return err_code_tbl[i].condition; - } - } - - return NULL; -} - static log_level_t _get_log_level(xmpp_log_level_t xmpp_level) {