about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-03-17 22:21:05 +0000
committerJames Booth <boothj5@gmail.com>2013-03-17 22:21:05 +0000
commit8d2e0656b497d2e72ed9df15303f3ba692bdfd42 (patch)
tree3835bd0a1788c5441196ac6e23c50e7182c26657 /src/xmpp
parente22970b304b8045be1599e6f5127d2d8d19265fe (diff)
downloadprofani-tty-8d2e0656b497d2e72ed9df15303f3ba692bdfd42.tar.gz
Refactor show and status stanza handling
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/presence.c41
-rw-r--r--src/xmpp/stanza.c26
-rw-r--r--src/xmpp/stanza.h3
3 files changed, 34 insertions, 36 deletions
diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c
index 366aa64c..55fc9189 100644
--- a/src/xmpp/presence.c
+++ b/src/xmpp/presence.c
@@ -333,14 +333,7 @@ _unavailable_handler(xmpp_conn_t * const conn,
     Jid *my_jid = jid_create(jid);
     Jid *from_jid = jid_create(from);
 
-    char *status_str;
-    xmpp_stanza_t *status =
-        xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
-
-    if (status != NULL)
-        status_str = xmpp_stanza_get_text(status);
-    else
-        status_str = NULL;
+    char *status_str = stanza_get_status(stanza, NULL);
 
     if (strcmp(my_jid->barejid, from_jid->barejid) !=0) {
         if (from_jid->resourcepart != NULL) {
@@ -387,7 +380,8 @@ _available_handler(xmpp_conn_t * const conn,
     Jid *my_jid = jid_create(jid);
     Jid *from_jid = jid_create(from);
 
-    char *show_str, *status_str;
+    char *show_str = stanza_get_show(stanza, "online");
+    char *status_str = stanza_get_status(stanza, NULL);
     char *caps_key = _get_caps_key(stanza);
     int idle_seconds = stanza_get_idle_time(stanza);
     GDateTime *last_activity = NULL;
@@ -398,21 +392,6 @@ _available_handler(xmpp_conn_t * const conn,
         g_date_time_unref(now);
     }
 
-    xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
-
-    if (show != NULL)
-        show_str = xmpp_stanza_get_text(show);
-    else
-        show_str = "online";
-
-    xmpp_stanza_t *status =
-        xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
-
-    if (status != NULL)
-        status_str = xmpp_stanza_get_text(status);
-    else
-        status_str = NULL;
-
     // get priority
     int priority = 0;
     xmpp_stanza_t *priority_stanza =
@@ -601,12 +580,7 @@ _room_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
 
         log_debug("Room presence received from %s", from_jid->fulljid);
 
-        xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
-        if (status != NULL) {
-            status_str = xmpp_stanza_get_text(status);
-        } else {
-            status_str = NULL;
-        }
+        status_str = stanza_get_status(stanza, NULL);
 
         if ((type != NULL) && (strcmp(type, STANZA_TYPE_UNAVAILABLE) == 0)) {
 
@@ -618,12 +592,7 @@ _room_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
                 prof_handle_room_member_offline(room, nick, "offline", status_str);
             }
         } else {
-            xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
-            if (show != NULL) {
-                show_str = xmpp_stanza_get_text(show);
-            } else {
-                show_str = "online";
-            }
+            show_str = stanza_get_show(stanza, "online");
             if (!muc_get_roster_received(room)) {
                 muc_add_to_roster(room, nick, show_str, status_str, caps_key);
             } else {
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index 7b6778e3..47f809ae 100644
--- a/src/xmpp/stanza.c
+++ b/src/xmpp/stanza.c
@@ -280,6 +280,32 @@ stanza_get_delay(xmpp_stanza_t * const stanza, GTimeVal *tv_stamp)
     return FALSE;
 }
 
+char *
+stanza_get_status(xmpp_stanza_t *stanza, char *def)
+{
+    xmpp_stanza_t *status =
+        xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
+
+    if (status != NULL) {
+        return xmpp_stanza_get_text(status);
+    } else {
+        return def;
+    }
+}
+
+char *
+stanza_get_show(xmpp_stanza_t *stanza, char *def)
+{
+    xmpp_stanza_t *show =
+        xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
+
+    if (show != NULL) {
+        return xmpp_stanza_get_text(show);
+    } else {
+        return def;
+    }
+}
+
 gboolean
 stanza_is_muc_presence(xmpp_stanza_t * const stanza)
 {
diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h
index e92d3186..cee1de88 100644
--- a/src/xmpp/stanza.h
+++ b/src/xmpp/stanza.h
@@ -167,4 +167,7 @@ xmpp_stanza_t * stanza_create_software_version_iq(xmpp_ctx_t *ctx, const char *
 xmpp_stanza_t * stanza_create_disco_items_iq(xmpp_ctx_t *ctx, const char * const id,
     const char * const jid);
 
+char * stanza_get_status(xmpp_stanza_t *stanza, char *def);
+char * stanza_get_show(xmpp_stanza_t *stanza, char *def);
+
 #endif