From d65e160bcd5e5dd29a85ad06bfa5d7c9c99336fa Mon Sep 17 00:00:00 2001 From: Jakub Adam Date: Sun, 3 Apr 2011 19:31:14 +0200 Subject: [PATCH] core: moved repeating code to sipmsg_parse_warning() --- src/core/sip-transport.c | 14 ++++---------- src/core/sipe-im.c | 22 ++-------------------- src/core/sipe-media.c | 3 +-- src/core/sipmsg.c | 32 ++++++++++++++++++++++++++++++++ src/core/sipmsg.h | 11 +++++++++++ 5 files changed, 50 insertions(+), 32 deletions(-) diff --git a/src/core/sip-transport.c b/src/core/sip-transport.c index 784f582a..c2119ff9 100644 --- a/src/core/sip-transport.c +++ b/src/core/sip-transport.c @@ -1190,18 +1190,12 @@ static gboolean process_register_response(struct sipe_core_private *sipe_private break; case 403: { - const gchar *diagnostics = sipmsg_find_header(msg, "Warning"); - gchar **reason = NULL; + gchar *reason; gchar *warning; - if (diagnostics != NULL) { - /* Example header: - Warning: 310 lcs.microsoft.com "You are currently not using the recommended version of the client" - */ - reason = g_strsplit(diagnostics, "\"", 0); - } + sipmsg_parse_warning(msg, &reason); warning = g_strdup_printf(_("You have been rejected by the server: %s"), - (reason && reason[1]) ? reason[1] : _("no reason given")); - g_strfreev(reason); + reason ? reason : _("no reason given")); + g_free(reason); sipe_backend_connection_error(SIPE_CORE_PUBLIC, SIPE_CONNECTION_ERROR_INVALID_SETTINGS, diff --git a/src/core/sipe-im.c b/src/core/sipe-im.c index c3253b18..ed9a6dc6 100644 --- a/src/core/sipe-im.c +++ b/src/core/sipe-im.c @@ -179,19 +179,10 @@ static gboolean process_invite_response(struct sipe_core_private *sipe_private, if (msg->response != 200) { gchar *alias = get_buddy_alias(sipe_private, with); - const char *warn_hdr = sipmsg_find_header(msg, "Warning"); - int warning = -1; + int warning = sipmsg_parse_warning(msg, NULL); SIPE_DEBUG_INFO_NOFORMAT("process_invite_response: INVITE response not 200"); - if (warn_hdr) { - gchar **parts = g_strsplit(warn_hdr, " ", 2); - if (parts[0]) { - warning = atoi(parts[0]); - } - g_strfreev(parts); - } - /* cancel file transfer as rejected by server */ if (msg->response == 606 && /* Not acceptable all. */ warning == 309 && /* Message contents not allowed by policy */ @@ -479,19 +470,10 @@ process_message_response(struct sipe_core_private *sipe_private, message = g_hash_table_lookup(session->unconfirmed_messages, key); if (msg->response >= 400) { - const char *warn_hdr = sipmsg_find_header(msg, "Warning"); - int warning = -1; + int warning = sipmsg_parse_warning(msg, NULL); SIPE_DEBUG_INFO_NOFORMAT("process_message_response: MESSAGE response >= 400"); - if (warn_hdr) { - gchar **parts = g_strsplit(warn_hdr, " ", 2); - if (parts[0]) { - warning = atoi(parts[0]); - } - g_strfreev(parts); - } - /* cancel file transfer as rejected by server */ if (msg->response == 606 && /* Not acceptable all. */ warning == 309 && /* Message contents not allowed by policy */ diff --git a/src/core/sipe-media.c b/src/core/sipe-media.c index 3e601381..9222b37e 100644 --- a/src/core/sipe-media.c +++ b/src/core/sipe-media.c @@ -1049,10 +1049,9 @@ process_invite_call_response(struct sipe_core_private *sipe_private, switch (msg->response) { case 480: { - const gchar *warn = sipmsg_find_header(msg, "Warning"); title = _("User unavailable"); - if (warn && g_str_has_prefix(warn, "391 lcs.microsoft.com")) { + if (sipmsg_parse_warning(msg, NULL) == 391) { g_string_append_printf(desc, _("%s does not want to be disturbed"), with); } else g_string_append_printf(desc, _("User %s is not available"), with); diff --git a/src/core/sipmsg.c b/src/core/sipmsg.c index b3d4701d..3e790e2a 100644 --- a/src/core/sipmsg.c +++ b/src/core/sipmsg.c @@ -662,6 +662,38 @@ sipmsg_get_ms_diagnostics_reason(struct sipmsg *msg) return NULL; } +int +sipmsg_parse_warning(struct sipmsg *msg, gchar **reason) +{ + /* + * Example header: + * Warning: 310 lcs.microsoft.com "You are currently not using the recommended version of the client" + */ + const gchar *hdr = sipmsg_find_header(msg, "Warning"); + int code = -1; + + if (reason) + *reason = NULL; + + if (hdr) { + gchar **parts = g_strsplit(hdr, " ", 3); + + if (parts[0]) { + code = atoi(parts[0]); + + if (reason && parts[1] && parts[2]) { + size_t len = strlen(parts[2]); + if (len > 2 && parts[2][0] == '"' && parts[2][len - 1] == '"') + *reason = g_strndup(parts[2] + 1, len - 2); + } + } + + g_strfreev(parts); + } + + return code; +} + //------------------------------------------------------------------------------------------ diff --git a/src/core/sipmsg.h b/src/core/sipmsg.h index 144458b0..72de894b 100644 --- a/src/core/sipmsg.h +++ b/src/core/sipmsg.h @@ -126,3 +126,14 @@ void sipe_parse_html(const char *html, char **attributes, char **message); * @return reason string. Must be g_free()'d after use. */ gchar *sipmsg_get_ms_diagnostics_reason(struct sipmsg *msg); + +/** + * Parses Warning header of SIP message, if present. + * + * @param msg (in) SIP message + * @param reason (out) parsed warning text or NULL if missing. Must be g_free()'d + * after use. + * + * @return warning code or -1 if warning header is not present in message. + */ +int sipmsg_parse_warning(struct sipmsg *msg, gchar **reason); -- 2.11.4.GIT