示例#1
0
void
roster_send_remove_from_group(const char * const group, PContact contact)
{
    GSList *groups = p_contact_groups(contact);
    GSList *new_groups = NULL;
    while (groups) {
        if (strcmp(groups->data, group) != 0) {
            new_groups = g_slist_append(new_groups, strdup(groups->data));
        }
        groups = g_slist_next(groups);
    }

    xmpp_conn_t * const conn = connection_get_conn();
    xmpp_ctx_t * const ctx = connection_get_ctx();

    // add an id handler to handle the response
    char *unique_id = create_unique_id(NULL);
    GroupData *data = malloc(sizeof(GroupData));
    data->group = strdup(group);
    if (p_contact_name(contact)) {
        data->name = strdup(p_contact_name(contact));
    } else {
        data->name = strdup(p_contact_barejid(contact));
    }

    xmpp_id_handler_add(conn, _group_remove_handler, unique_id, data);
    xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact),
        p_contact_name(contact), new_groups);
    xmpp_send(conn, iq);
    xmpp_stanza_release(iq);
    free(unique_id);
}
示例#2
0
GSList*
roster_get_nogroup(roster_ord_t order, gboolean include_offline)
{
    assert(roster != NULL);

    GSList *result = NULL;
    GHashTableIter iter;
    gpointer key;
    gpointer value;

    GCompareFunc cmp_func;
    if (order == ROSTER_ORD_PRESENCE) {
        cmp_func = (GCompareFunc) _compare_presence;
    } else {
        cmp_func = (GCompareFunc) _compare_name;
    }

    g_hash_table_iter_init(&iter, roster->contacts);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        PContact contact = value;
        const char *presence = p_contact_presence(contact);
        if (!include_offline && (g_strcmp0(presence, "offline") == 0)) {
            continue;
        }

        GSList *groups = p_contact_groups(value);
        if (groups == NULL) {
            result = g_slist_insert_sorted(result, value, cmp_func);
        }
    }

    // return all contact structs
    return result;
}
示例#3
0
void
roster_send_add_to_group(const char *const group, PContact contact)
{
    GSList *groups = p_contact_groups(contact);
    GSList *new_groups = NULL;
    while (groups) {
        new_groups = g_slist_append(new_groups, strdup(groups->data));
        groups = g_slist_next(groups);
    }

    new_groups = g_slist_append(new_groups, strdup(group));
    // add an id handler to handle the response
    char *unique_id = create_unique_id(NULL);
    GroupData *data = malloc(sizeof(GroupData));
    data->group = strdup(group);
    if (p_contact_name(contact)) {
        data->name = strdup(p_contact_name(contact));
    } else {
        data->name = strdup(p_contact_barejid(contact));
    }

    xmpp_ctx_t * const ctx = connection_get_ctx();
    iq_id_handler_add(unique_id, _group_add_id_handler, (ProfIdFreeCallback)_free_group_data, data);
    xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact),
        p_contact_name(contact), new_groups);
    iq_send_stanza(iq);
    xmpp_stanza_release(iq);
    free(unique_id);
}
示例#4
0
void
roster_remove(const char *const name, const char *const barejid)
{
    assert(roster != NULL);

    autocomplete_remove(roster->barejid_ac, barejid);
    autocomplete_remove(roster->name_ac, name);
    g_hash_table_remove(roster->name_to_barejid, name);

    // remove each fulljid
    PContact contact = roster_get_contact(barejid);
    if (contact) {
        GList *resources = p_contact_get_available_resources(contact);
        while (resources) {
            GString *fulljid = g_string_new(strdup(barejid));
            g_string_append(fulljid, "/");
            g_string_append(fulljid, resources->data);
            autocomplete_remove(roster->fulljid_ac, fulljid->str);
            g_string_free(fulljid, TRUE);
            resources = g_list_next(resources);
        }
        g_list_free(resources);

        GSList *groups = p_contact_groups(contact);
        GSList *curr = groups;
        while (curr) {
            gchar *group = curr->data;
            if (g_hash_table_contains(roster->group_count, group)) {
                int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, group));
                count--;
                if (count < 1) {
                    g_hash_table_remove(roster->group_count, group);
                    autocomplete_remove(roster->groups_ac, group);
                } else {
                    g_hash_table_insert(roster->group_count, strdup(group), GINT_TO_POINTER(count));
                }
            }
            curr = g_slist_next(curr);
        }
    }

    // remove the contact
    g_hash_table_remove(roster->contacts, barejid);
}
示例#5
0
GSList*
roster_get_nogroup(void)
{
    GSList *result = NULL;
    GHashTableIter iter;
    gpointer key;
    gpointer value;

    g_hash_table_iter_init(&iter, contacts);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        GSList *groups = p_contact_groups(value);
        if (groups == NULL) {
            result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts);
        }
    }

    // return all contact structs
    return result;
}
示例#6
0
GSList*
roster_get_group(const char *const group, roster_ord_t order)
{
    assert(roster != NULL);

    GSList *result = NULL;
    GHashTableIter iter;
    gpointer key;
    gpointer value;

    GCompareFunc cmp_func;
    if (order == ROSTER_ORD_PRESENCE) {
        cmp_func = (GCompareFunc) _compare_presence;
    } else {
        cmp_func = (GCompareFunc) _compare_name;
    }

    g_hash_table_iter_init(&iter, roster->contacts);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        GSList *groups = p_contact_groups(value);
        if (group == NULL) {
            if (groups == NULL) {
                result = g_slist_insert_sorted(result, value, cmp_func);
            }
        } else {
            while (groups) {
                if (strcmp(groups->data, group) == 0) {
                    result = g_slist_insert_sorted(result, value, cmp_func);
                    break;
                }
                groups = g_slist_next(groups);
            }
        }
    }

    // return all contact structs
    return result;
}
示例#7
0
GSList *
roster_get_group(const char * const group)
{
    GSList *result = NULL;
    GHashTableIter iter;
    gpointer key;
    gpointer value;

    g_hash_table_iter_init(&iter, contacts);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        GSList *groups = p_contact_groups(value);
        while (groups != NULL) {
            if (strcmp(groups->data, group) == 0) {
                result = g_slist_insert_sorted(result, value, (GCompareFunc)_compare_contacts);
                break;
            }
            groups = g_slist_next(groups);
        }
    }

    // resturn all contact structs
    return result;
}
示例#8
0
void
roster_update(const char *const barejid, const char *const name, GSList *groups, const char *const subscription,
    gboolean pending_out)
{
    assert(roster != NULL);

    PContact contact = roster_get_contact(barejid);
    assert(contact != NULL);

    p_contact_set_subscription(contact, subscription);
    p_contact_set_pending_out(contact, pending_out);

    const char * const new_name = name;
    const char * current_name = NULL;
    if (p_contact_name(contact)) {
        current_name = strdup(p_contact_name(contact));
    }

    p_contact_set_name(contact, new_name);
    _replace_name(current_name, new_name, barejid);

    GSList *curr_new_group = groups;
    while (curr_new_group) {
        char *new_group = curr_new_group->data;

        // contact added to group
        if (!p_contact_in_group(contact, new_group)) {

            // group doesn't yet exist
            if (!g_hash_table_contains(roster->group_count, new_group)) {
                g_hash_table_insert(roster->group_count, strdup(new_group), GINT_TO_POINTER(1));
                autocomplete_add(roster->groups_ac, curr_new_group->data);

            // increment count
            } else {
                int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, new_group));
                g_hash_table_insert(roster->group_count, strdup(new_group), GINT_TO_POINTER(count + 1));
            }
        }
        curr_new_group = g_slist_next(curr_new_group);
    }

    GSList *old_groups = p_contact_groups(contact);
    GSList *curr_old_group = old_groups;
    while (curr_old_group) {
        char *old_group = curr_old_group->data;
        // removed from group
        if (!g_slist_find_custom(groups, old_group, (GCompareFunc)g_strcmp0)) {
            if (g_hash_table_contains(roster->group_count, old_group)) {
                int count = GPOINTER_TO_INT(g_hash_table_lookup(roster->group_count, old_group));
                count--;
                if (count < 1) {
                    g_hash_table_remove(roster->group_count, old_group);
                    autocomplete_remove(roster->groups_ac, old_group);
                } else {
                    g_hash_table_insert(roster->group_count, strdup(old_group), GINT_TO_POINTER(count));
                }
            }
        }

        curr_old_group = g_slist_next(curr_old_group);
    }

    p_contact_set_groups(contact, groups);
}
示例#9
0
void
_show_roster_contacts(GSList *list, gboolean show_groups)
{
    GSList *curr = list;
    while(curr) {

        PContact contact = curr->data;
        GString *title = g_string_new("  ");
        title = g_string_append(title, p_contact_barejid(contact));
        if (p_contact_name(contact) != NULL) {
            title = g_string_append(title, " (");
            title = g_string_append(title, p_contact_name(contact));
            title = g_string_append(title, ")");
        }

        const char *presence = p_contact_presence(contact);
        win_print_time(console, '-');
        if (p_contact_subscribed(contact)) {
            win_presence_colour_on(console, presence);
            wprintw(console->win, "%s\n", title->str);
            win_presence_colour_off(console, presence);
        } else {
            win_presence_colour_on(console, "offline");
            wprintw(console->win, "%s\n", title->str);
            win_presence_colour_off(console, "offline");
        }

        g_string_free(title, TRUE);

        win_print_time(console, '-');
        wprintw(console->win, "    Subscription : ");
        GString *sub = g_string_new("");
        sub = g_string_append(sub, p_contact_subscription(contact));
        if (p_contact_pending_out(contact)) {
            sub = g_string_append(sub, ", request sent");
        }
        if (presence_sub_request_exists(p_contact_barejid(contact))) {
            sub = g_string_append(sub, ", request received");
        }
        if (p_contact_subscribed(contact)) {
            wattron(console->win, COLOUR_SUBSCRIBED);
        } else {
            wattron(console->win, COLOUR_UNSUBSCRIBED);
        }
        wprintw(console->win, "%s\n", sub->str);
        if (p_contact_subscribed(contact)) {
            wattroff(console->win, COLOUR_SUBSCRIBED);
        } else {
            wattroff(console->win, COLOUR_UNSUBSCRIBED);
        }

        g_string_free(sub, TRUE);

        if (show_groups) {
            GSList *groups = p_contact_groups(contact);
            if (groups != NULL) {
                GString *groups_str = g_string_new("    Groups : ");
                while (groups != NULL) {
                    g_string_append(groups_str, groups->data);
                    if (g_slist_next(groups) != NULL) {
                        g_string_append(groups_str, ", ");
                    }
                    groups = g_slist_next(groups);
                }

                cons_show(groups_str->str);
                g_string_free(groups_str, TRUE);
            }
        }

        curr = g_slist_next(curr);
    }

}