Example #1
0
/* XML stuff to extract the data we need from the Rubrica file */
static GSList *
extract_cards(xmlNodePtr card)
{
    GSList *addrlist = NULL;

    while (card) {
	if (!xmlStrcmp(card->name, CXMLCHARP("Card"))) {
	    LibBalsaAddress *address = libbalsa_address_new();
	    xmlNodePtr children;

	    address->full_name =
		xml_node_get_attr(card, CXMLCHARP("name"));
	    children = card->children;
	    while (children) {
		if (!xmlStrcmp(children->name, CXMLCHARP("Data")))
		    extract_data(children->children, &address->first_name,
				 &address->last_name, &address->nick_name);
		else if (!xmlStrcmp(children->name, CXMLCHARP("Work")))
		    extract_work(children->children,
				 &address->organization);
		else if (!xmlStrcmp(children->name, CXMLCHARP("Net")))
		    extract_net(children->children,
				&address->address_list);

		children = children->next;
	    }

	    if (address->address_list)
		addrlist = g_slist_prepend(addrlist, address);
	    else
		g_object_unref(address);
	}

	card = card->next;
    }

    return addrlist;
}
Example #2
0
/* libbalsa_address_book_ldap_get_address:
 * loads a single address from connection specified by LDAPMessage.
 */
static LibBalsaAddress* 
libbalsa_address_book_ldap_get_address(LibBalsaAddressBook * ab,
				       LDAPMessage * e)
{
    LibBalsaAddressBookLdap *ldap_ab;
    gchar *email = NULL, *cn = NULL, *org = NULL, *uid = NULL;
    gchar *first = NULL, *last = NULL;
    LibBalsaAddress *address = NULL;
    char *attr;
    struct berval **vals;
    BerElement *ber = NULL;
    int i;

    ldap_ab = LIBBALSA_ADDRESS_BOOK_LDAP(ab);

    for (attr = ldap_first_attribute(ldap_ab->directory, e, &ber);
	 attr != NULL; attr=ldap_next_attribute(ldap_ab->directory, e, ber)) {
	/*
	 * For each attribute, get the attribute name and values.
	 */
	if ((vals=ldap_get_values_len(ldap_ab->directory, e, attr)) != NULL) {
	    for (i = 0; vals[i] != NULL; i++) {
		if ((g_ascii_strcasecmp(attr, "sn") == 0) && (!last))
		    last = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "cn") == 0) && (!cn))
		    cn = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "givenName") == 0) && (!first))
		    first = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "o") == 0) && (!org))
		    org = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "uid") == 0) && (!uid))
		    uid = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
		if ((g_ascii_strcasecmp(attr, "mail") == 0) && (!email))
		    email = g_strndup(vals[i]->bv_val, vals[i]->bv_len);
	    }
	    ldap_value_free_len(vals);
	}
        ldap_memfree(attr);
    }
    /*
     * Record will have e-mail (searched)
     */
    if(email == NULL) email = g_strdup("none");
    g_return_val_if_fail(email != NULL, NULL);

    address = libbalsa_address_new();
    if (cn)
	address->full_name = cn;
    else {
	address->full_name = create_name(first, last);
        if(!address->full_name)
            address->full_name = g_strdup(_("No-Name"));
    }
    address->first_name = first;
    address->last_name = last;
    address->nick_name = uid;
    address->organization = org;
    address->address_list = g_list_prepend(address->address_list, email);

    return address;
}
Example #3
0
LibBalsaAddress *
rfc6350_parse_from_stream(GDataInputStream *stream,
						  gboolean		   *eos,
						  GError		   **error)
{
	gchar *line;
	LibBalsaAddress *result = NULL;

	line = g_data_input_stream_read_line_utf8(stream, NULL, NULL, error);
	if (line == NULL) {
		if (*error == NULL) {
			*eos = TRUE;
		}
	} else if (g_ascii_strcasecmp(line, "BEGIN:VCARD") != 0) {
		g_set_error(error, RFC6350_ERROR_QUARK, RFC6350_ERROR_BEGIN, _("malformed card, BEGIN:VCARD expected"));
		g_free(line);
	} else {
		gboolean parse_done = FALSE;

		result = libbalsa_address_new();
		while (result && (line != NULL) && !parse_done) {
			if (g_ascii_strcasecmp(line, "END:VCARD") == 0) {
				parse_done = TRUE;
				g_free(line);
			} else {
				gchar *nextline;

				/* perform unfolding (RFC 6350, sect. 3.2. "Line Delimiting and Folding") */
				nextline = g_data_input_stream_read_line_utf8(stream, NULL, NULL, error);
				while ((nextline) != NULL && ((nextline[0] == ' ') || (nextline[0] == '\t'))) {
					gchar *unfold;

					unfold = g_strconcat(line, &nextline[1], NULL);
					g_free(line);
					g_free(nextline);
					line = unfold;
					nextline = g_data_input_stream_read_line_utf8(stream, NULL, NULL, error);
				}

				/* evaluate unfolded line, drop address on error */
				if (!rfc6350_eval_line(line, result, error)) {
					g_object_unref(result);
					result = NULL;
				}

				/* process next line */
				g_free(line);
				line = nextline;
			}
		}

		if (!parse_done) {
			g_set_error(error, RFC6350_ERROR_QUARK, RFC6350_ERROR_END, _("malformed card, END:VCARD missing"));
			g_object_unref(result);
			result = NULL;
		}
	}

	/* ignore items without an Email address, fill empty full name if necessary */
	if (result != NULL) {
		if (result->address_list == NULL) {
			g_object_unref(result);
			result = NULL;
		} else if (result->full_name == NULL) {
			result->full_name = g_strdup(_("No-Name"));
		}
	}

	return result;
}