not just the addresses of the 'email' field.
muttq_print_item(FILE *file, int item)
{
abook_list *emails, *e;
+ char *tmp = db_email_get(item);
- emails = csv_to_abook_list(db_email_get(item));
+ emails = csv_to_abook_list(tmp);
+ free(tmp);
for(e = emails; e; e = e->next) {
fprintf(file, "%s\t%s\t%s\n", e->data, db_name_get(item),
get_first_email(email, item);
- ret = *db_email_get(item) ?
+ ret = *email ?
strdup_printf("%s <%s>", name, email) :
xstrdup(name);
return database[i];
}
+/* Fetch addresses from all fields of FIELD_EMAILS type */
+/* Memory has to be freed by the caller */
+char *
+db_email_get(int item)
+{
+ int i;
+ char *res;
+ abook_field_list *cur;
+ abook_list *emails = NULL;
+
+ for(cur = fields_list, i = 0; cur; cur = cur->next, i++)
+ if(cur->field->type == FIELD_EMAILS && *database[item][i])
+ abook_list_append(&emails, database[item][i]);
+
+ res = abook_list_to_csv(emails);
+ abook_list_free(&emails);
+ return res ? res : xstrdup("");
+}
+
#define db_fget(item, i) real_db_field_get(item, i, 1)
#define db_fget_byid(item, i) real_db_field_get(item, i, 0)
#define db_name_get(item) db_fget(item, NAME)
-#define db_email_get(item) db_fget(item, EMAIL)
+char *db_email_get(int item); /* memory has to be freed by the caller */
/*
* database field write
list_item db_item_get(int i);
-/*
- * Various macros
- */
-
-#define have_multiple_emails(item) \
- strchr(db_email_get(item), ',')
-
#endif /* _DATABASE_H */
void
get_first_email(char *str, int item)
{
- char *tmp;
+ char *tmp, *emails = db_email_get(item);
- if(!db_email_get(item)) {
+ if(!*emails) {
*str = 0;
return;
}
- strncpy(str, db_email_get(item), MAX_EMAIL_LEN);
+ strncpy(str, emails, MAX_EMAIL_LEN);
+ free(emails);
if( (tmp = strchr(str, ',')) )
*tmp = 0;
else
str[MAX_EMAIL_LEN - 1] = 0;
}
+/* This only rolls emails from the 'email' field, not emails from any
+ * field of type FIELD_EMAILS.
+ * TODO: expand to ask for which field to roll if several are present? */
static void
roll_emails(int item, enum rotate_dir dir)
{
- abook_list *emails = csv_to_abook_list(db_email_get(item));
+ abook_list *emails = csv_to_abook_list(db_fget(item, EMAIL));
if(!emails)
return;
- free(db_email_get(item));
+ free(db_fget(item, EMAIL));
abook_list_rotate(&emails, dir);
db_fput(item, EMAIL, abook_list_to_csv(emails));
abook_list_free(&emails);
get_first_email(email, item);
- if(*db_email_get(item))
+ if(*email)
snprintf(header, EDITW_COLS, "%s <%s>",
db_name_get(item),
email);
static int
html_export_database(FILE *out, struct db_enumerator e)
{
- char tmp[MAX_EMAILSTR_LEN];
+ char tmp[MAX_EMAILSTR_LEN], *emails;
int extra_column;
if(list_is_empty())
else
fprintf(out, "<tr>\n<td>%s</td>\n", db_name_get(e.item));
- fprintf(out, "<td>%s</td>\n", db_email_get(e.item));
+ emails = db_email_get(e.item);
+ fprintf(out, "<td>%s</td>\n", emails);
+ free(emails);
if(extra_column >= 0)
fprintf(out, "<td>%s</td>\n",
safe_str(db_fget_byid(e.item, extra_column)));
static int
pine_export_database(FILE *out, struct db_enumerator e)
{
+ char *emails;
+
db_enumerate_items(e) {
- fprintf(out, have_multiple_emails(e.item) ?
+ emails = db_email_get(e.item);
+ fprintf(out, strchr(emails, ',') /* multiple addresses? */ ?
"%s\t%s\t(%s)\t\t%s\n" : "%s\t%s\t%s\t\t%s\n",
safe_str(db_fget(e.item, NICK)),
safe_str(db_name_get(e.item)),
- safe_str(db_email_get(e.item)),
+ emails,
safe_str(db_fget(e.item, NOTES))
);
+ free(emails);
}
return 0;
gcrd_export_database(FILE *out, struct db_enumerator e)
{
int j;
- char *name;
+ char *name, *tmp;
abook_list *emails, *em;
db_enumerate_items(e) {
fprintf(out, "TEL;CELL:%s\r\n",
db_fget(e.item, MOBILEPHONE));
- if(*db_email_get(e.item)) {
- emails = csv_to_abook_list(db_email_get(e.item));
+ tmp = db_email_get(e.item);
+ if(*tmp) {
+ emails = csv_to_abook_list(tmp);
for(em = emails; em; em = em->next)
fprintf(out, "EMAIL;INTERNET:%s\r\n", em->data);
abook_list_free(&emails);
}
+ free(tmp);
if(db_fget(e.item, NOTES))
fprintf(out, "NOTE:%s\r\n",
{
abook_list *emails, *em;
int j;
- char *realname = get_real_name(), *str = NULL;
+ char *realname = get_real_name(), *str = NULL, *tmp;
char *style = opt_get_str(STR_ADDRESS_STYLE);
fprintf(out,
fprintf(out, "\n(%s)", db_fget(e.item, NICK));
fprintf(out, "\n");
- if(*db_email_get(e.item)) {
- emails = csv_to_abook_list(db_email_get(e.item));
+ tmp = db_email_get(e.item);
+ if(*tmp) {
+ emails = csv_to_abook_list(tmp);
fprintf(out, "\n");
for(em = emails; em; em = em->next)
abook_list_free(&emails);
}
+ free(tmp);
/* Print address */
if(db_fget(e.item, ADDRESS)) {
if(!safe_strcmp(style, "us")) /* US like */
fprintf(out, "# This is a generated file made by abook for the Spruce e-mail client.\n\n");
db_enumerate_items(e) {
- if(strcmp(safe_str(db_email_get(e.item)), "")) {
- get_first_email(email, e.item);
+ get_first_email(email, e.item);
+ if(strcmp(email, "")) {
fprintf(out, "# Address %d\nName: %s\nEmail: %s\nMemo: %s\n\n",
e.item,
db_name_get(e.item),
static int
wl_export_database(FILE *out, struct db_enumerator e)
{
- abook_list *emails;
+ char email[MAX_EMAIL_LEN];
fprintf(out, "# Wanderlust address book written by %s\n\n", PACKAGE);
db_enumerate_items(e) {
- if((emails = csv_to_abook_list(db_email_get(e.item))) != NULL) {
+ get_first_email(email, e.item);
+ if(*email) {
fprintf(out,
"%s\t\"%s\"\t\"%s\"\n",
- emails->data,
+ email,
safe_str(db_fget(e.item, NICK)),
safe_str(db_name_get(e.item))
);
}
- abook_list_free(&emails);
}
fprintf (out, "\n# End of address book file.\n");
print_list_line(int i, int line, int highlight)
{
int extra = extra_column;
- char tmp[MAX_EMAILSTR_LEN];
+ char tmp[MAX_EMAILSTR_LEN], *emails;
int real_emaillen = (extra_column > 0 || extra_alternative > 0) ?
EMAILLEN : COLS - EMAILPOS;
mvwaddnstr(list, line, NAMEPOS, db_name_get(i),
bytes2width(db_name_get(i), NAMELEN));
- if(opt_get_bool(BOOL_SHOW_ALL_EMAILS))
- mvwaddnstr(list, line, EMAILPOS, db_email_get(i),
- bytes2width(db_email_get(i), real_emaillen));
- else {
+ if(opt_get_bool(BOOL_SHOW_ALL_EMAILS)) {
+ emails = db_email_get(i);
+ mvwaddnstr(list, line, EMAILPOS, emails,
+ bytes2width(emails, real_emaillen));
+ free(emails);
+ } else {
get_first_email(tmp, i);
mvwaddnstr(list, line, EMAILPOS, tmp,
bytes2width(tmp, real_emaillen));