about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-02-10 11:19:36 +0000
committerJames Booth <boothj5@gmail.com>2013-02-10 11:19:36 +0000
commitec78914044978fee1119c5a905c6a8fdce03a476 (patch)
treebd731ac50f84d1219699a0609d744ae0dd9bf4f0 /src
parent84a4ab95459107d50625b3487ad0ffbee78ffe31 (diff)
downloadprofani-tty-ec78914044978fee1119c5a905c6a8fdce03a476.tar.gz
Only create contacts resource when online
Diffstat (limited to 'src')
-rw-r--r--src/contact.c49
-rw-r--r--src/contact_list.c3
-rw-r--r--src/contact_list.h1
-rw-r--r--src/resource.c1
-rw-r--r--src/xmpp/iq.c3
5 files changed, 40 insertions, 17 deletions
diff --git a/src/contact.c b/src/contact.c
index dd0205ee..801e4539 100644
--- a/src/contact.c
+++ b/src/contact.c
@@ -64,8 +64,10 @@ p_contact_new(const char * const barejid, const char * const name,
     contact->resources = g_hash_table_new_full(g_str_hash, g_str_equal, free,
         (GDestroyNotify)resource_destroy);
     // TODO, priority, last activity
-    Resource *resource = resource_new("default", presence, status, 0, caps_str);
-    g_hash_table_insert(contact->resources, strdup(resource->name), resource);
+    if (g_strcmp0(presence, "offline") != 0) {
+        Resource *resource = resource_new("default", presence, status, 0, caps_str);
+        g_hash_table_insert(contact->resources, strdup(resource->name), resource);
+    }
 
     return contact;
 }
@@ -127,15 +129,23 @@ p_contact_name(const PContact contact)
 const char *
 p_contact_presence(const PContact contact)
 {
-    Resource *resource = g_hash_table_lookup(contact->resources, "default");
-    return resource->show;
+    if (g_hash_table_size(contact->resources) == 0) {
+        return "offline";
+    } else {
+        Resource *resource = g_hash_table_lookup(contact->resources, "default");
+        return resource->show;
+    }
 }
 
 const char *
 p_contact_status(const PContact contact)
 {
-    Resource *resource = g_hash_table_lookup(contact->resources, "default");
-    return resource->status;
+    if (g_hash_table_size(contact->resources) == 0) {
+        return NULL;
+    } else {
+        Resource *resource = g_hash_table_lookup(contact->resources, "default");
+        return resource->status;
+    }
 }
 
 const char *
@@ -159,17 +169,32 @@ p_contact_last_activity(const PContact contact)
 const char *
 p_contact_caps_str(const PContact contact)
 {
-    Resource *resource = g_hash_table_lookup(contact->resources, "default");
-    return resource->caps_str;
+    if (g_hash_table_size(contact->resources) == 0) {
+        return NULL;
+    } else {
+        Resource *resource = g_hash_table_lookup(contact->resources, "default");
+        return resource->caps_str;
+    }
 }
 
 void
 p_contact_set_presence(const PContact contact, const char * const presence)
 {
-    Resource *resource = g_hash_table_lookup(contact->resources, "default");
-    FREE_SET_NULL(resource->show);
-    if (presence != NULL) {
-        resource->show = strdup(presence);
+    if (g_strcmp0(presence, "offline") == 0) {
+        g_hash_table_remove(contact->resources, "default");
+    } else {
+        if (g_hash_table_size(contact->resources) == 0) {
+            Resource *resource = resource_new("default", presence, NULL, 0, NULL);
+            g_hash_table_insert(contact->resources, strdup(resource->name), resource);
+        } else {
+            Resource *resource = g_hash_table_lookup(contact->resources, "default");
+            if (presence != NULL) {
+                FREE_SET_NULL(resource->show);
+                resource->show = strdup(presence);
+            } else {
+                resource->show = NULL;
+            }
+        }
     }
 }
 
diff --git a/src/contact_list.c b/src/contact_list.c
index cac593ee..73333156 100644
--- a/src/contact_list.c
+++ b/src/contact_list.c
@@ -64,14 +64,13 @@ contact_list_reset_search_attempts(void)
 
 gboolean
 contact_list_add(const char * const barejid, const char * const name,
-    const char * const presence, const char * const status,
     const char * const subscription, gboolean pending_out)
 {
     gboolean added = FALSE;
     PContact contact = g_hash_table_lookup(contacts, barejid);
 
     if (contact == NULL) {
-        contact = p_contact_new(barejid, name, presence, status, subscription,
+        contact = p_contact_new(barejid, name, "offline", NULL, subscription,
             pending_out, NULL);
         g_hash_table_insert(contacts, strdup(barejid), contact);
         autocomplete_add(ac, strdup(barejid));
diff --git a/src/contact_list.h b/src/contact_list.h
index c1ccf152..37511e67 100644
--- a/src/contact_list.h
+++ b/src/contact_list.h
@@ -33,7 +33,6 @@ void contact_list_free(void);
 void contact_list_reset_search_attempts(void);
 void contact_list_remove(const char * const barejid);
 gboolean contact_list_add(const char * const barejid, const char * const name,
-    const char * const presence, const char * const status,
     const char * const subscription, gboolean pending_out);
 gboolean contact_list_update_contact(const char * const barejid, const char * const presence,
     const char * const status, GDateTime *last_activity, const char * const caps_str);
diff --git a/src/resource.c b/src/resource.c
index e8443d58..74cb5755 100644
--- a/src/resource.c
+++ b/src/resource.c
@@ -30,6 +30,7 @@
 Resource * resource_new(const char * const name, const char * const show,
     const char * const status, const int priority, const char * const caps_str)
 {
+    assert(g_strcmp0(show, "offline") != 0);
     assert(name != NULL);
     Resource *new_resource = malloc(sizeof(struct resource_t));
     new_resource->name = strdup(name);
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index 9954da1e..6d9e9e6f 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -144,8 +144,7 @@ _iq_handle_roster_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
                 pending_out = TRUE;
             }
 
-            gboolean added = contact_list_add(barejid, name, "offline", NULL, sub,
-                pending_out);
+            gboolean added = contact_list_add(barejid, name, sub, pending_out);
 
             if (!added) {
                 log_warning("Attempt to add contact twice: %s", barejid);