NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
muttlib.h File Reference

Some miscellaneous functions. More...

#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include "attach/lib.h"
+ Include dependency graph for muttlib.h:

Go to the source code of this file.

Macros

#define mutt_adv_mktemp(buf)
 
#define mutt_adv_mktemp_draft(buf)
 

Functions

void mutt_adv_mktemp_cfg (struct Buffer *buf, const char *cfg)
 Create a temporary file.
 
void expand_path (struct Buffer *buf, bool regex)
 Create the canonical path.
 
void pretty_mailbox (struct Buffer *s)
 Shorten a mailbox path using '~' or '='.
 
void buf_sanitize_filename (struct Buffer *buf, const char *path, short slash)
 Replace unsafe characters in a filename.
 
void generate_save_path (struct Buffer *dest, const struct Address *a)
 Make a safe filename from an email address.
 
int mutt_check_overwrite (const char *attname, const char *path, struct Buffer *fname, enum SaveAttach *opt, char **directory)
 Ask the user if overwriting is necessary.
 
void mutt_encode_path (struct Buffer *buf, const char *src)
 Convert a path to 'us-ascii'.
 
char * mutt_gecos_name (char *dest, size_t destlen, struct passwd *pw)
 Lookup a user's real name in /etc/passwd.
 
void mutt_get_parent_path (const char *path, char *buf, size_t buflen)
 Find the parent of a path (or mailbox)
 
bool mutt_is_text_part (const struct Body *b)
 Is this part of an email in plain text?
 
bool mutt_needs_mailcap (struct Body *b)
 Does this type need a mailcap entry do display.
 
FILE * mutt_open_read (const char *path, pid_t *thepid)
 Run a command to read from.
 
int mutt_save_confirm (const char *s, struct stat *st)
 Ask the user to save.
 
void mutt_sleep (short s)
 Sleep for a while.
 
int mutt_str_pretty_size (struct Buffer *buf, size_t num)
 Display an abbreviated size, like 3.4K.
 

Detailed Description

Some miscellaneous functions.

Authors
  • Richard Russon
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file muttlib.h.

Macro Definition Documentation

◆ mutt_adv_mktemp

#define mutt_adv_mktemp ( buf)
Value:
mutt_adv_mktemp_cfg(buf, "tmp_dir")
void mutt_adv_mktemp_cfg(struct Buffer *buf, const char *cfg)
Create a temporary file.
Definition muttlib.c:84

Definition at line 39 of file muttlib.h.

◆ mutt_adv_mktemp_draft

#define mutt_adv_mktemp_draft ( buf)
Value:
mutt_adv_mktemp_cfg(buf, "tmp_draft_dir")

Definition at line 40 of file muttlib.h.

Function Documentation

◆ mutt_adv_mktemp_cfg()

void mutt_adv_mktemp_cfg ( struct Buffer * buf,
const char * cfg )

Create a temporary file.

Parameters
bufBuffer for the name
cfgConfig option for the temp directory

Accept a "suggestion" for file name. If that file exists, then construct one with unique name but keep any extension. This might fail, I guess.

Definition at line 84 of file muttlib.c.

85{
86 if (buf_is_empty(buf))
87 {
88 buf_mktemp_full(buf, "neomutt", NULL, __FILE__, __LINE__, cfg);
89 }
90 else
91 {
92 struct Buffer *prefix = buf_pool_get();
93 buf_strcpy(prefix, buf->data);
94 mutt_file_sanitize_filename(prefix->data, true);
95 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, cfg);
96 buf_printf(buf, "%s/%s", NONULL(c_tmp_dir), buf_string(prefix));
97
98 struct stat st = { 0 };
99 if ((lstat(buf_string(buf), &st) == -1) && (errno == ENOENT))
100 goto out;
101
102 char *suffix = strchr(prefix->data, '.');
103 if (suffix)
104 {
105 *suffix = '\0';
106 suffix++;
107 }
108 buf_mktemp_full(buf, prefix->data, suffix, __FILE__, __LINE__, cfg);
109
110 out:
111 buf_pool_release(&prefix);
112 }
113}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition helpers.c:168
void mutt_file_sanitize_filename(char *path, bool slash)
Replace unsafe characters in a filename.
Definition file.c:582
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define NONULL(x)
Definition string2.h:44
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
void buf_mktemp_full(struct Buffer *buf, const char *prefix, const char *suffix, const char *src, int line, const char *cfg)
Create a temporary file.
Definition tmp.c:52
+ Here is the call graph for this function:

◆ expand_path()

void expand_path ( struct Buffer * buf,
bool regex )

Create the canonical path.

Parameters
bufBuffer with path
regexIf true, escape regex special characters
Note
The path is expanded in-place

Definition at line 122 of file muttlib.c.

123{
124 const char *s = NULL;
125 const char *tail = "";
126
127 bool recurse = false;
128
129 struct Buffer *p = buf_pool_get();
130 struct Buffer *q = buf_pool_get();
131 struct Buffer *tmp = buf_pool_get();
132
133 do
134 {
135 recurse = false;
136 s = buf_string(buf);
137
138 switch (*s)
139 {
140 case '~':
141 {
142 if ((s[1] == '/') || (s[1] == '\0'))
143 {
145 tail = s + 1;
146 }
147 else
148 {
149 char *t = strchr(s + 1, '/');
150 if (t)
151 *t = '\0';
152
153 struct passwd *pw = getpwnam(s + 1);
154 if (pw)
155 {
156 buf_strcpy(p, pw->pw_dir);
157 if (t)
158 {
159 *t = '/';
160 tail = t;
161 }
162 else
163 {
164 tail = "";
165 }
166 }
167 else
168 {
169 /* user not found! */
170 if (t)
171 *t = '/';
172 buf_reset(p);
173 tail = s;
174 }
175 }
176 break;
177 }
178
179 case '=':
180 case '+':
181 {
182 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
183 enum MailboxType mb_type = mx_path_probe(c_folder);
184
185 /* if folder = {host} or imap[s]://host/: don't append slash */
186 if ((mb_type == MUTT_IMAP) && ((c_folder[strlen(c_folder) - 1] == '}') ||
187 (c_folder[strlen(c_folder) - 1] == '/')))
188 {
189 buf_strcpy(p, c_folder);
190 }
191 else if (mb_type == MUTT_NOTMUCH)
192 {
193 buf_strcpy(p, c_folder);
194 }
195 else if (c_folder && (c_folder[strlen(c_folder) - 1] == '/'))
196 {
197 buf_strcpy(p, c_folder);
198 }
199 else
200 {
201 buf_printf(p, "%s/", NONULL(c_folder));
202 }
203
204 tail = s + 1;
205 break;
206 }
207
208 /* elm compatibility, @ expands alias to user name */
209
210 case '@':
211 {
212 struct AddressList *al = alias_lookup(s + 1);
213 if (al && !TAILQ_EMPTY(al))
214 {
215 struct Email *e = email_new();
216 e->env = mutt_env_new();
217 mutt_addrlist_copy(&e->env->from, al, false);
218 mutt_addrlist_copy(&e->env->to, al, false);
219
221 mutt_default_save(p, e);
222
223 email_free(&e);
224 /* Avoid infinite recursion if the resulting folder starts with '@' */
225 if (*p->data != '@')
226 recurse = true;
227
228 tail = "";
229 }
230 break;
231 }
232
233 case '>':
234 {
235 const char *const c_mbox = cs_subset_string(NeoMutt->sub, "mbox");
236 buf_strcpy(p, c_mbox);
237 tail = s + 1;
238 break;
239 }
240
241 case '<':
242 {
243 const char *const c_record = cs_subset_string(NeoMutt->sub, "record");
244 buf_strcpy(p, c_record);
245 tail = s + 1;
246 break;
247 }
248
249 case '!':
250 {
251 if (s[1] == '!')
252 {
254 tail = s + 2;
255 }
256 else
257 {
258 const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file");
259 buf_strcpy(p, c_spool_file);
260 tail = s + 1;
261 }
262 break;
263 }
264
265 case '-':
266 {
268 tail = s + 1;
269 break;
270 }
271
272 case '^':
273 {
275 tail = s + 1;
276 break;
277 }
278
279 default:
280 {
281 buf_reset(p);
282 tail = s;
283 }
284 }
285
286 if (regex && *(buf_string(p)) && !recurse)
287 {
289 buf_printf(tmp, "%s%s", buf_string(q), tail);
290 }
291 else
292 {
293 buf_printf(tmp, "%s%s", buf_string(p), tail);
294 }
295
296 buf_copy(buf, tmp);
297 } while (recurse);
298
301 buf_pool_release(&tmp);
302
303 /* Rewrite IMAP path in canonical form - aids in string comparisons of
304 * folders. May possibly fail, in which case buf should be the same. */
305 if (imap_path_probe(buf_string(buf), NULL) == MUTT_IMAP)
306 imap_path_canon(buf);
307}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition address.c:774
struct AddressList * alias_lookup(const char *name)
Find an Alias.
Definition alias.c:273
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition buffer.c:601
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition buffer.c:337
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
MailboxType
Supported mailbox formats.
Definition mailbox.h:40
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition mailbox.h:50
@ MUTT_IMAP
'IMAP' Mailbox type
Definition mailbox.h:49
struct Email * email_new(void)
Create a new Email.
Definition email.c:77
void email_free(struct Email **ptr)
Free an Email.
Definition email.c:46
struct Envelope * mutt_env_new(void)
Create a new Envelope.
Definition envelope.c:45
int mutt_file_sanitize_regex(struct Buffer *dest, const char *src)
Escape any regex-magic characters in a string.
Definition file.c:624
char * LastFolder
Previously selected mailbox.
Definition globals.c:39
char * CurrentFolder
Currently selected mailbox.
Definition globals.c:38
int imap_path_canon(struct Buffer *path)
Canonicalise a Mailbox path - Implements MxOps::path_canon() -.
Definition imap.c:2560
enum MailboxType imap_path_probe(const char *path, const struct stat *st)
Is this an IMAP Mailbox?
Definition imap.c:2546
void mutt_default_save(struct Buffer *path, struct Email *e)
Find the default save path for an email.
Definition exec.c:214
#define PATH_MAX
Definition mutt.h:49
enum MailboxType mx_path_probe(const char *path)
Find a mailbox that understands a path.
Definition mx.c:1323
#define TAILQ_EMPTY(head)
Definition queue.h:778
The envelope/body of an email.
Definition email.h:39
struct Envelope * env
Envelope information.
Definition email.h:68
struct AddressList to
Email's 'To' list.
Definition envelope.h:60
struct AddressList from
Email's 'From' list.
Definition envelope.h:59
char * home_dir
User's home directory.
Definition neomutt.h:56
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pretty_mailbox()

void pretty_mailbox ( struct Buffer * buf)

Shorten a mailbox path using '~' or '='.

Parameters
bufBuffer containing Mailbox name

Collapse the pathname using ~ or = when possible

Definition at line 428 of file muttlib.c.

429{
430 if (!buf || !buf->data)
431 return;
432
433 /* Ensure buffer is large enough for path manipulation operations.
434 * The function will modify the string in-place, potentially shortening it.
435 * buf_alloc() also guarantees buf->data is not NULL. */
436 buf_alloc(buf, PATH_MAX);
437
438 char *p = buf->data;
439 char *q = buf->data;
440 size_t len;
441 enum UrlScheme scheme;
442 char tmp[PATH_MAX] = { 0 };
443
444 scheme = url_check_scheme(buf->data);
445
446 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
447 if ((scheme == U_IMAP) || (scheme == U_IMAPS))
448 {
449 imap_pretty_mailbox(buf->data, buf->dsize, c_folder);
450 goto done;
451 }
452
453 if (scheme == U_NOTMUCH)
454 goto done;
455
456 /* if buf is an url, only collapse path component */
457 if (scheme != U_UNKNOWN)
458 {
459 p = strchr(buf->data, ':') + 1;
460 if (mutt_strn_equal(p, "//", 2))
461 q = strchr(p + 2, '/');
462 if (!q)
463 q = strchr(p, '\0');
464 p = q;
465 }
466
467 /* cleanup path */
468 if (strstr(p, "//") || strstr(p, "/./"))
469 {
470 /* first attempt to collapse the pathname, this is more
471 * lightweight than realpath() and doesn't resolve links */
472 while (*p)
473 {
474 if ((p[0] == '/') && (p[1] == '/'))
475 {
476 *q++ = '/';
477 p += 2;
478 }
479 else if ((p[0] == '/') && (p[1] == '.') && (p[2] == '/'))
480 {
481 *q++ = '/';
482 p += 3;
483 }
484 else
485 {
486 *q++ = *p++;
487 }
488 }
489 *q = '\0';
490 }
491 else if (strstr(p, "..") && ((scheme == U_UNKNOWN) || (scheme == U_FILE)) &&
492 realpath(p, tmp))
493 {
494 mutt_str_copy(p, tmp, buf->dsize - (p - buf->data));
495 }
496
497 if ((len = mutt_str_startswith(buf->data, c_folder)) && (buf->data[len] == '/'))
498 {
499 buf->data[0] = '=';
500 memmove(buf->data + 1, buf->data + len, mutt_str_len(buf->data + len) + 1);
501 }
502 else if ((len = mutt_str_startswith(buf->data, NeoMutt->home_dir)) &&
503 (buf->data[len] == '/'))
504 {
505 buf->data[0] = '~';
506 memmove(buf->data + 1, buf->data + len, mutt_str_len(buf->data + len) + 1);
507 }
508
509done:
510 buf_fix_dptr(buf);
511}
void buf_fix_dptr(struct Buffer *buf)
Move the dptr to end of the Buffer.
Definition buffer.c:182
void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
Prettify an IMAP mailbox name.
Definition util.c:586
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition string.c:429
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:586
size_t dsize
Length of data.
Definition buffer.h:39
enum UrlScheme url_check_scheme(const char *str)
Check the protocol of a URL.
Definition url.c:226
UrlScheme
All recognised Url types.
Definition url.h:34
@ U_NOTMUCH
Url is notmuch://.
Definition url.h:46
@ U_UNKNOWN
Url wasn't recognised.
Definition url.h:35
@ U_FILE
Url is file://.
Definition url.h:36
@ U_IMAP
Url is imap://.
Definition url.h:39
@ U_IMAPS
Url is imaps://.
Definition url.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ buf_sanitize_filename()

void buf_sanitize_filename ( struct Buffer * buf,
const char * path,
short slash )

Replace unsafe characters in a filename.

Parameters
bufBuffer for the result
pathFilename to make safe
slashReplace '/' characters too

Definition at line 913 of file muttlib.c.

914{
915 if (!buf || !path)
916 return;
917
918 buf_reset(buf);
919
920 for (; *path; path++)
921 {
922 if ((slash && (*path == '/')) || !strchr(FilenameSafeChars, *path))
923 buf_addch(buf, '_');
924 else
925 buf_addch(buf, *path);
926 }
927}
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
const char FilenameSafeChars[]
Set of characters <=0x7F that are safe to use in filenames.
Definition file.c:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generate_save_path()

void generate_save_path ( struct Buffer * dest,
const struct Address * a )

Make a safe filename from an email address.

Parameters
destBuffer for the result
aAddress to use

The filename will be stripped of unsafe characters (/, space, etc) to make it safe.

Definition at line 608 of file muttlib.c.

609{
610 if (a && a->mailbox)
611 {
612 buf_copy(dest, a->mailbox);
613 const bool c_save_address = cs_subset_bool(NeoMutt->sub, "save_address");
614 if (!c_save_address)
615 {
616 char *p = strpbrk(dest->data, "%@");
617 if (p)
618 {
619 *p = '\0';
620 buf_fix_dptr(dest);
621 }
622 }
623 mutt_str_lower(dest->data);
624 /* Sanitize filename by replacing unsafe characters */
625 for (char *p = dest->data; *p; p++)
626 if ((*p == '/') || mutt_isspace(*p) || !IsPrint((unsigned char) *p))
627 *p = '_';
628 }
629 else
630 {
631 buf_reset(dest);
632 }
633}
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition ctype.c:96
#define IsPrint(ch)
Definition mbyte.h:39
char * mutt_str_lower(char *str)
Convert all characters in the string to lowercase.
Definition string.c:317
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_check_overwrite()

int mutt_check_overwrite ( const char * attname,
const char * path,
struct Buffer * fname,
enum SaveAttach * opt,
char ** directory )

Ask the user if overwriting is necessary.

Parameters
[in]attnameAttachment name
[in]pathPath to save the file
[out]fnameBuffer for filename
[out]optSave option, see SaveAttach
[out]directoryDirectory to save under (OPTIONAL)
Return values
0Success
-1Abort
1Error

Definition at line 524 of file muttlib.c.

526{
527 struct stat st = { 0 };
528
529 buf_strcpy(fname, path);
530 if (access(buf_string(fname), F_OK) != 0)
531 return 0;
532 if (stat(buf_string(fname), &st) != 0)
533 return -1;
534 if (S_ISDIR(st.st_mode))
535 {
536 enum QuadOption ans = MUTT_NO;
537 if (directory)
538 {
539 switch (mw_multi_choice
540 /* L10N: Means "The path you specified as the destination file is a directory."
541 See the msgid "Save to file: " (alias.c, recvattach.c)
542 These three letters correspond to the choices in the string. */
543 (_("File is a directory, save under it: (y)es, (n)o, (a)ll?"), _("yna")))
544 {
545 case 3: /* all */
546 mutt_str_replace(directory, buf_string(fname));
547 break;
548 case 1: /* yes */
549 FREE(directory);
550 break;
551 case -1: /* abort */
552 FREE(directory);
553 return -1;
554 case 2: /* no */
555 FREE(directory);
556 return 1;
557 }
558 }
559 /* L10N: Means "The path you specified as the destination file is a directory."
560 See the msgid "Save to file: " (alias.c, recvattach.c) */
561 else if ((ans = query_yesorno(_("File is a directory, save under it?"), MUTT_YES)) != MUTT_YES)
562 return (ans == MUTT_NO) ? 1 : -1;
563
564 struct Buffer *tmp = buf_pool_get();
565 buf_strcpy(tmp, mutt_path_basename(NONULL(attname)));
566 struct FileCompletionData cdata = { false, NULL, NULL, NULL, NULL };
567 if ((mw_get_field(_("File under directory: "), tmp, MUTT_COMP_CLEAR,
568 HC_FILE, &CompleteFileOps, &cdata) != 0) ||
569 buf_is_empty(tmp))
570 {
571 buf_pool_release(&tmp);
572 return (-1);
573 }
574 buf_concat_path(fname, path, buf_string(tmp));
575 buf_pool_release(&tmp);
576 }
577
578 if ((*opt == MUTT_SAVE_NO_FLAGS) && (access(buf_string(fname), F_OK) == 0))
579 {
580 char buf[4096] = { 0 };
581 snprintf(buf, sizeof(buf), "%s - %s", buf_string(fname),
582 // L10N: Options for: File %s exists, (o)verwrite, (a)ppend, or (c)ancel?
583 _("File exists, (o)verwrite, (a)ppend, or (c)ancel?"));
584 switch (mw_multi_choice(buf, _("oac")))
585 {
586 case -1: /* abort */
587 return -1;
588 case 3: /* cancel */
589 return 1;
590
591 case 2: /* append */
592 *opt = MUTT_SAVE_APPEND;
593 break;
594 case 1: /* overwrite */
595 break;
596 }
597 }
598 return 0;
599}
const struct CompleteOps CompleteFileOps
Auto-Completion of Files.
Definition complete.c:152
size_t buf_concat_path(struct Buffer *buf, const char *dir, const char *fname)
Join a directory name and a filename.
Definition buffer.c:509
#define MUTT_COMP_CLEAR
Clear input if printable character is pressed.
Definition wdata.h:43
int mw_get_field(const char *prompt, struct Buffer *buf, CompletionFlags complete, enum HistoryClass hclass, const struct CompleteOps *comp_api, void *cdata)
Ask the user for a string -.
Definition window.c:463
int mw_multi_choice(const char *prompt, const char *letters)
Offer the user a multiple choice question -.
Definition question.c:62
@ HC_FILE
Files.
Definition lib.h:58
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define _(a)
Definition message.h:28
const char * mutt_path_basename(const char *path)
Find the last component for a pathname.
Definition path.c:282
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
@ MUTT_SAVE_APPEND
Append to existing file.
Definition mutt_attach.h:59
@ MUTT_SAVE_NO_FLAGS
Overwrite existing file (the default)
Definition mutt_attach.h:58
QuadOption
Possible values for a quad-option.
Definition quad.h:36
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:329
Input for the file completion function.
Definition curs_lib.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_encode_path()

void mutt_encode_path ( struct Buffer * buf,
const char * src )

Convert a path to 'us-ascii'.

Parameters
bufBuffer for the result
srcPath to convert (OPTIONAL)

If src is NULL, the path in buf will be converted in-place.

Definition at line 803 of file muttlib.c.

804{
805 char *p = mutt_str_dup(src);
806 int rc = mutt_ch_convert_string(&p, cc_charset(), "us-ascii", MUTT_ICONV_NO_FLAGS);
807 size_t len = buf_strcpy(buf, (rc == 0) ? NONULL(p) : NONULL(src));
808
809 /* convert the path to POSIX "Portable Filename Character Set" */
810 for (size_t i = 0; i < len; i++)
811 {
812 if (!mutt_isalnum(buf_at(buf, i)) && !strchr("/.-_", buf_at(buf, i)))
813 {
814 buf->data[i] = '_';
815 }
816 }
817 FREE(&p);
818}
char buf_at(const struct Buffer *buf, size_t offset)
Return the character at the given offset.
Definition buffer.c:668
const char * cc_charset(void)
Get the cached value of $charset.
bool mutt_isalnum(int arg)
Wrapper for isalnum(3)
Definition ctype.c:40
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition charset.c:817
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition charset.h:66
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_gecos_name()

char * mutt_gecos_name ( char * dest,
size_t destlen,
struct passwd * pw )

Lookup a user's real name in /etc/passwd.

Parameters
destBuffer for the result
destlenLength of buffer
pwPasswd entry
Return values
ptrResult buffer on success

Extract the real name from /etc/passwd's GECOS field. When set, honor the regular expression in $gecos_mask, otherwise assume that the GECOS field is a comma-separated list. Replace "&" by a capitalized version of the user's login name.

Definition at line 321 of file muttlib.c.

322{
323 regmatch_t pat_match[1] = { 0 };
324 char *p = NULL;
325
326 if (!pw || !pw->pw_gecos)
327 return NULL;
328
329 memset(dest, 0, destlen);
330
331 const struct Regex *c_gecos_mask = cs_subset_regex(NeoMutt->sub, "gecos_mask");
332 if (mutt_regex_capture(c_gecos_mask, pw->pw_gecos, 1, pat_match))
333 {
334 mutt_str_copy(dest, pw->pw_gecos + pat_match[0].rm_so,
335 MIN(pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen));
336 }
337 else if ((p = strchr(pw->pw_gecos, ',')))
338 {
339 mutt_str_copy(dest, pw->pw_gecos, MIN(destlen, p - pw->pw_gecos + 1));
340 }
341 else
342 {
343 mutt_str_copy(dest, pw->pw_gecos, destlen);
344 }
345
346 size_t pwnl = strlen(pw->pw_name);
347
348 for (int idx = 0; dest[idx]; idx++)
349 {
350 if (dest[idx] == '&')
351 {
352 memmove(&dest[idx + pwnl], &dest[idx + 1],
353 MAX((ssize_t) (destlen - idx - pwnl - 1), 0));
354 memcpy(&dest[idx], pw->pw_name, MIN(destlen - idx - 1, pwnl));
355 dest[idx] = mutt_toupper(dest[idx]);
356 }
357 }
358
359 return dest;
360}
const struct Regex * cs_subset_regex(const struct ConfigSubset *sub, const char *name)
Get a regex config item by name.
Definition helpers.c:217
int mutt_toupper(int arg)
Wrapper for toupper(3)
Definition ctype.c:140
#define MIN(a, b)
Return the minimum of two values.
Definition memory.h:40
#define MAX(a, b)
Return the maximum of two values.
Definition memory.h:38
bool mutt_regex_capture(const struct Regex *regex, const char *str, size_t nmatch, regmatch_t matches[])
Match a regex against a string, with provided options.
Definition regex.c:597
Cached regular expression.
Definition regex3.h:85
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_get_parent_path()

void mutt_get_parent_path ( const char * path,
char * buf,
size_t buflen )

Find the parent of a path (or mailbox)

Parameters
pathPath to use
bufBuffer for the result
buflenLength of buffer

Definition at line 867 of file muttlib.c.

868{
869 enum MailboxType mb_type = mx_path_probe(path);
870
871 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
872 if (mb_type == MUTT_IMAP)
873 {
874 imap_get_parent_path(path, buf, buflen);
875 }
876 else if (mb_type == MUTT_NOTMUCH)
877 {
878 mutt_str_copy(buf, c_folder, buflen);
879 }
880 else
881 {
882 mutt_str_copy(buf, path, buflen);
883 int n = mutt_str_len(buf);
884 if (n == 0)
885 return;
886
887 /* remove any final trailing '/' */
888 if (buf[n - 1] == '/')
889 buf[n - 1] = '\0';
890
891 /* Remove everything until the next slash */
892 for (n--; ((n >= 0) && (buf[n] != '/')); n--)
893 ; // do nothing
894
895 if (n > 0)
896 {
897 buf[n] = '\0';
898 }
899 else
900 {
901 buf[0] = '/';
902 buf[1] = '\0';
903 }
904 }
905}
void imap_get_parent_path(const char *path, char *buf, size_t buflen)
Get the path of the parent folder.
Definition util.c:165
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_is_text_part()

bool mutt_is_text_part ( const struct Body * b)

Is this part of an email in plain text?

Parameters
bPart of an email
Return values
truePart is in plain text

Definition at line 396 of file muttlib.c.

397{
398 int t = b->type;
399 char *s = b->subtype;
400
402 return false;
403
404 if (t == TYPE_TEXT)
405 return true;
406
407 if (t == TYPE_MESSAGE)
408 {
409 if (mutt_istr_equal("delivery-status", s))
410 return true;
411 }
412
413 if (((WithCrypto & APPLICATION_PGP) != 0) && (t == TYPE_APPLICATION))
414 {
415 if (mutt_istr_equal("pgp-keys", s))
416 return true;
417 }
418
419 return false;
420}
SecurityFlags mutt_is_application_pgp(const struct Body *b)
Does the message use PGP?
Definition crypt.c:548
@ TYPE_MESSAGE
Type: 'message/*'.
Definition mime.h:35
@ TYPE_APPLICATION
Type: 'application/*'.
Definition mime.h:33
@ TYPE_TEXT
Type: 'text/*'.
Definition mime.h:38
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition lib.h:98
#define WithCrypto
Definition lib.h:124
char * subtype
content-type subtype
Definition body.h:61
unsigned int type
content-type primary type, ContentType
Definition body.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_needs_mailcap()

bool mutt_needs_mailcap ( struct Body * b)

Does this type need a mailcap entry do display.

Parameters
bAttachment body to be displayed
Return values
trueNeoMutt requires a mailcap entry to display
falseotherwise

Definition at line 368 of file muttlib.c.

369{
370 switch (b->type)
371 {
372 case TYPE_TEXT:
373 if (mutt_istr_equal("plain", b->subtype))
374 return false;
375 break;
376 case TYPE_APPLICATION:
378 return false;
380 return false;
381 break;
382
383 case TYPE_MULTIPART:
384 case TYPE_MESSAGE:
385 return false;
386 }
387
388 return true;
389}
SecurityFlags mutt_is_application_smime(struct Body *b)
Does the message use S/MIME?
Definition crypt.c:609
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition mime.h:37
#define APPLICATION_SMIME
Use SMIME to encrypt/sign.
Definition lib.h:99
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_open_read()

FILE * mutt_open_read ( const char * path,
pid_t * thepid )

Run a command to read from.

Parameters
[in]pathPath to command
[out]thepidPID of the command
Return values
ptrFile containing output of command

This function allows the user to specify a command to read stdout from in place of a normal file. If the last character in the string is a pipe (|), then we assume it is a command to run instead of a normal file.

Definition at line 645 of file muttlib.c.

646{
647 FILE *fp = NULL;
648 struct stat st = { 0 };
649
650 size_t len = mutt_str_len(path);
651 if (len == 0)
652 {
653 return NULL;
654 }
655
656 if (path[len - 1] == '|')
657 {
658 /* read from a pipe */
659
660 char *p = mutt_str_dup(path);
661
662 p[len - 1] = 0;
663 mutt_endwin();
664 *thepid = filter_create(p, NULL, &fp, NULL, NeoMutt->env);
665 FREE(&p);
666 }
667 else
668 {
669 if (stat(path, &st) < 0)
670 return NULL;
671 if (S_ISDIR(st.st_mode))
672 {
673 errno = EINVAL;
674 return NULL;
675 }
676 fp = mutt_file_fopen(path, "r");
677 *thepid = -1;
678 }
679 return fp;
680}
void mutt_endwin(void)
Shutdown curses.
Definition curs_lib.c:151
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
pid_t filter_create(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, char **envlist)
Set up filter program.
Definition filter.c:217
char ** env
Private copy of the environment variables.
Definition neomutt.h:58
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_save_confirm()

int mutt_save_confirm ( const char * s,
struct stat * st )

Ask the user to save.

Parameters
sSave location
stTimestamp
Return values
0OK to proceed
-1to abort
1to retry

Definition at line 690 of file muttlib.c.

691{
692 int rc = 0;
693
694 enum MailboxType type = mx_path_probe(s);
695
696 if (type == MUTT_POP)
697 {
698 mutt_error(_("Can't save message to POP mailbox"));
699 return 1;
700 }
701
702 if ((type != MUTT_MAILBOX_ERROR) && (type != MUTT_UNKNOWN) && (mx_access(s, W_OK) == 0))
703 {
704 const bool c_confirm_append = cs_subset_bool(NeoMutt->sub, "confirm_append");
705 if (c_confirm_append)
706 {
707 struct Buffer *tmp = buf_pool_get();
708 buf_printf(tmp, _("Append messages to %s?"), s);
710 NeoMutt->sub, "confirm_append");
711 if (ans == MUTT_NO)
712 rc = 1;
713 else if (ans == MUTT_ABORT)
714 rc = -1;
715 buf_pool_release(&tmp);
716 }
717 }
718
719 if (type == MUTT_NNTP)
720 {
721 mutt_error(_("Can't save message to news server"));
722 return 0;
723 }
724
725 if (stat(s, st) != -1)
726 {
727 if (type == MUTT_MAILBOX_ERROR)
728 {
729 mutt_error(_("%s is not a mailbox"), s);
730 return 1;
731 }
732 }
733 else if (type != MUTT_IMAP)
734 {
735 st->st_mtime = 0;
736 st->st_atime = 0;
737
738 /* pathname does not exist */
739 if (errno == ENOENT)
740 {
741 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
742 if (c_confirm_create)
743 {
744 struct Buffer *tmp = buf_pool_get();
745 buf_printf(tmp, _("Create %s?"), s);
747 NeoMutt->sub, "confirm_create");
748 if (ans == MUTT_NO)
749 rc = 1;
750 else if (ans == MUTT_ABORT)
751 rc = -1;
752 buf_pool_release(&tmp);
753 }
754
755 /* user confirmed with MUTT_YES or set `$confirm_create` */
756 if (rc == 0)
757 {
758 /* create dir recursively */
759 char *tmp_path = mutt_path_dirname(s);
760 if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1)
761 {
762 /* report failure & abort */
763 mutt_perror("%s", s);
764 FREE(&tmp_path);
765 return 1;
766 }
767 FREE(&tmp_path);
768 }
769 }
770 else
771 {
772 mutt_perror("%s", s);
773 return 1;
774 }
775 }
776
777 msgwin_clear_text(NULL);
778 return rc;
779}
@ MUTT_MAILBOX_ERROR
Error occurred examining Mailbox.
Definition mailbox.h:42
@ MUTT_POP
'POP3' Mailbox type
Definition mailbox.h:51
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition mailbox.h:48
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition mailbox.h:43
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition file.c:844
#define mutt_error(...)
Definition logging2.h:94
#define mutt_perror(...)
Definition logging2.h:95
void msgwin_clear_text(struct MuttWindow *win)
Clear the text in the Message Window.
Definition msgwin.c:518
char * mutt_path_dirname(const char *path)
Return a path up to, but not including, the final '/'.
Definition path.c:312
int mx_access(const char *path, int flags)
Wrapper for access, checks permissions on a given mailbox.
Definition mx.c:167
@ MUTT_ABORT
User aborted the question (with Ctrl-G)
Definition quad.h:37
enum QuadOption query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition question.c:357
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_sleep()

void mutt_sleep ( short s)

Sleep for a while.

Parameters
sNumber of seconds to sleep

If the user config '$sleep_time' is larger, sleep that long instead.

Definition at line 787 of file muttlib.c.

788{
789 const short c_sleep_time = cs_subset_number(NeoMutt->sub, "sleep_time");
790 if (c_sleep_time > s)
791 sleep(c_sleep_time);
792 else if (s)
793 sleep(s);
794}
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition helpers.c:143
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_str_pretty_size()

int mutt_str_pretty_size ( struct Buffer * buf,
size_t num )

Display an abbreviated size, like 3.4K.

Parameters
[out]bufBuffer for the result
[in]numNumber to abbreviate
Return values
numBytes written to buf

Definition at line 935 of file muttlib.c.

936{
937 if (!buf)
938 return 0;
939
940 const bool c_size_show_bytes = cs_subset_bool(NeoMutt->sub, "size_show_bytes");
941 const bool c_size_show_fractions = cs_subset_bool(NeoMutt->sub, "size_show_fractions");
942 const bool c_size_show_mb = cs_subset_bool(NeoMutt->sub, "size_show_mb");
943 const bool c_size_units_on_left = cs_subset_bool(NeoMutt->sub, "size_units_on_left");
944
945 if (c_size_show_bytes && (num < 1024))
946 {
947 return buf_add_printf(buf, "%d", (int) num);
948 }
949
950 if (num == 0)
951 {
952 return buf_addstr(buf, c_size_units_on_left ? "K0" : "0K");
953 }
954
955 if (c_size_show_fractions && (num < 10189)) /* 0.1K - 9.9K */
956 {
957 return buf_add_printf(buf, c_size_units_on_left ? "K%3.1f" : "%3.1fK",
958 (num < 103) ? 0.1 : (num / 1024.0));
959 }
960
961 if (!c_size_show_mb || (num < 1023949)) /* 10K - 999K */
962 {
963 /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
964 return buf_add_printf(buf, c_size_units_on_left ? ("K%zu") : ("%zuK"), (num + 51) / 1024);
965 }
966
967 if (c_size_show_fractions && (num < 10433332)) /* 1.0M - 9.9M */
968 {
969 return buf_add_printf(buf, c_size_units_on_left ? "M%3.1f" : "%3.1fM", num / 1048576.0);
970 }
971
972 /* 10M+ -- (10433332 + 52428) / 1048576 = 10 */
973 return buf_add_printf(buf, c_size_units_on_left ? ("M%zu") : ("%zuM"),
974 (num + 52428) / 1048576);
975}
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
+ Here is the call graph for this function:
+ Here is the caller graph for this function: