From: Kevin McCarthy Date: Sun, 6 Oct 2019 08:32:42 +0000 (+0800) Subject: Convert save-hook and fcc-hook to use buffer pool internally X-Git-Tag: 2019-10-25~15^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5155496be4ff42d3d8b23ebce2b721381937de34;p=neomutt Convert save-hook and fcc-hook to use buffer pool internally The hook parameters still need to be converted, but I'm working towards it slowly. Co-authored-by: Richard Russon --- diff --git a/hook.c b/hook.c index 87f10d3b1..3cfcb6611 100644 --- a/hook.c +++ b/hook.c @@ -685,9 +685,10 @@ void mutt_default_save(char *path, size_t pathlen, struct Email *e) addr = NULL; if (addr) { - char tmp[PATH_MAX]; - mutt_safe_path(tmp, sizeof(tmp), addr); - snprintf(path, pathlen, "=%s", tmp); + struct Buffer *tmp = mutt_buffer_pool_get(); + mutt_safe_path(tmp, addr); + snprintf(path, pathlen, "=%s", mutt_b2s(tmp)); + mutt_buffer_pool_release(&tmp); } } @@ -707,9 +708,10 @@ void mutt_select_fcc(char *path, size_t pathlen, struct Email *e) if ((C_SaveName || C_ForceName) && (to || cc || bcc)) { const struct Address *addr = to ? to : (cc ? cc : bcc); - char buf[PATH_MAX]; - mutt_safe_path(buf, sizeof(buf), addr); - mutt_path_concat(path, NONULL(C_Folder), buf, pathlen); + struct Buffer *buf = mutt_buffer_pool_get(); + mutt_safe_path(buf, addr); + mutt_path_concat(path, NONULL(C_Folder), mutt_b2s(buf), pathlen); + mutt_buffer_pool_release(&buf); if (!C_ForceName && (mx_access(path, W_OK) != 0)) mutt_str_strfcpy(path, C_Record, pathlen); } diff --git a/muttlib.c b/muttlib.c index e59caf7fa..e500aa0ce 100644 --- a/muttlib.c +++ b/muttlib.c @@ -810,18 +810,42 @@ void mutt_save_path(char *buf, size_t buflen, const struct Address *addr) *buf = '\0'; } +/** + * mutt_buffer_save_path - Make a safe filename from an email address + * @param dest Buffer for the result + * @param a Address to use + */ +void mutt_buffer_save_path(struct Buffer *dest, const struct Address *a) +{ + if (a && a->mailbox) + { + mutt_buffer_strcpy(dest, a->mailbox); + if (!C_SaveAddress) + { + char *p = strpbrk(dest->data, "%@"); + if (p) + { + *p = '\0'; + mutt_buffer_fix_dptr(dest); + } + } + mutt_str_strlower(dest->data); + } + else + mutt_buffer_reset(dest); +} + /** * mutt_safe_path - Make a safe filename from an email address - * @param buf Buffer for the result - * @param buflen Length of buffer - * @param a Address to use + * @param dest Buffer for the result + * @param a Address to use * * The filename will be stripped of '/', space, etc to make it safe. */ -void mutt_safe_path(char *buf, size_t buflen, const struct Address *a) +void mutt_safe_path(struct Buffer *dest, const struct Address *a) { - mutt_save_path(buf, buflen, a); - for (char *p = buf; *p; p++) + mutt_buffer_save_path(dest, a); + for (char *p = dest->data; *p; p++) if ((*p == '/') || IS_SPACE(*p) || !IsPrint((unsigned char) *p)) *p = '_'; } diff --git a/muttlib.h b/muttlib.h index 7bd151cdc..9628421dd 100644 --- a/muttlib.h +++ b/muttlib.h @@ -49,6 +49,7 @@ void mutt_buffer_expand_path(struct Buffer *buf); void mutt_buffer_expand_path_regex(struct Buffer *buf, bool regex); void mutt_buffer_pretty_mailbox(struct Buffer *s); void mutt_buffer_sanitize_filename (struct Buffer *buf, const char *path, short slash); +void mutt_buffer_save_path(struct Buffer *dest, const struct Address *a); int mutt_check_overwrite(const char *attname, const char *path, struct Buffer *fname, enum SaveAttach *opt, char **directory); void mutt_encode_path(char *dest, size_t dlen, const char *src); void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t *callback, unsigned long data, MuttFormatFlags flags); @@ -68,7 +69,7 @@ uint32_t mutt_rand32(void); uint64_t mutt_rand64(void); void mutt_rand_base32(void *out, size_t len); int mutt_randbuf(void *out, size_t len); -void mutt_safe_path(char *s, size_t l, const struct Address *a); +void mutt_safe_path(struct Buffer *dest, const struct Address *a); int mutt_save_confirm(const char *s, struct stat *st); void mutt_save_path(char *d, size_t dsize, const struct Address *a); void mutt_sleep(short s);