NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
dlg_alias.c File Reference

Address book. More...

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "lib.h"
#include "menu/lib.h"
#include "pattern/lib.h"
#include "send/lib.h"
#include "alias.h"
#include "format_flags.h"
#include "functions.h"
#include "gui.h"
#include "keymap.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "opcodes.h"
+ Include dependency graph for dlg_alias.c:

Go to the source code of this file.

Functions

static const char * alias_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Format a string for the alias list - Implements format_t -. More...
 
static void alias_make_entry (struct Menu *menu, char *buf, size_t buflen, int line)
 Format a menu item for the alias list - Implements Menu::make_entry() -. More...
 
static int alias_tag (struct Menu *menu, int sel, int act)
 Tag some aliases - Implements Menu::tag() -. More...
 
static int alias_alias_observer (struct NotifyCallback *nc)
 Notification that an Alias has changed - Implements observer_t -. More...
 
static int alias_window_observer (struct NotifyCallback *nc)
 Notification that a Window has changed - Implements observer_t -. More...
 
static struct MuttWindowalias_dialog_new (struct AliasMenuData *mdata)
 Create an Alias Selection Dialog. More...
 
static bool dlg_select_alias (struct Buffer *buf, struct AliasMenuData *mdata)
 Display a menu of Aliases. More...
 
int alias_complete (char *buf, size_t buflen, struct ConfigSubset *sub)
 Alias completion routine. More...
 
void alias_dialog (struct Mailbox *m, struct ConfigSubset *sub)
 Open the aliases dialog. More...
 

Variables

static const struct Mapping AliasHelp []
 Help Bar for the Alias dialog (address book) More...
 

Detailed Description

Address book.

Authors
  • Michael R. Elkins
  • Pietro Cerutti
  • Richard Russon

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 dlg_alias.c.

Function Documentation

◆ alias_dialog_new()

static struct MuttWindow * alias_dialog_new ( struct AliasMenuData mdata)
static

Create an Alias Selection Dialog.

Parameters
mdataMenu data holding Aliases
Return values
ptrNew Dialog

Definition at line 279 of file dlg_alias.c.

280{
282
283 struct Menu *menu = dlg->wdata;
284
286 menu->custom_search = true;
287 menu->tag = alias_tag;
288 menu->max = alias_array_count_visible(&mdata->ava);
289 menu->mdata = mdata;
290 menu->mdata_free = NULL; // Menu doesn't own the data
291
292 struct MuttWindow *win_menu = menu->win;
293
294 // Override the Simple Dialog's recalc()
295 win_menu->recalc = alias_recalc;
296
297 struct MuttWindow *sbar = window_find_child(dlg, WT_STATUS_BAR);
298 alias_set_title(sbar, mdata->title, mdata->limit);
299
300 // NT_COLOR is handled by the SimpleDialog
304
305 return dlg;
306}
int alias_array_count_visible(struct AliasViewArray *ava)
Count number of visible Aliases.
Definition: array.c:95
static const struct Mapping AliasHelp[]
Help Bar for the Alias dialog (address book)
Definition: dlg_alias.c:99
static void alias_make_entry(struct Menu *menu, char *buf, size_t buflen, int line)
Format a menu item for the alias list - Implements Menu::make_entry() -.
Definition: dlg_alias.c:172
static int alias_tag(struct Menu *menu, int sel, int act)
Tag some aliases - Implements Menu::tag() -.
Definition: dlg_alias.c:187
static int alias_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: dlg_alias.c:250
static int alias_alias_observer(struct NotifyCallback *nc)
Notification that an Alias has changed - Implements observer_t -.
Definition: dlg_alias.c:203
int alias_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: gui.c:43
int alias_recalc(struct MuttWindow *win)
Recalculate the display of the Alias Window - Implements MuttWindow::recalc() -.
Definition: gui.c:89
struct MuttWindow * simple_dialog_new(enum MenuType mtype, enum WindowType wtype, const struct Mapping *help_data)
Create a simple index Dialog.
Definition: simple.c:129
void alias_set_title(struct MuttWindow *sbar, char *menu_name, char *limit)
Create a title string for the Menu.
Definition: gui.c:69
bool notify_observer_add(struct Notify *notify, enum NotifyType type, observer_t callback, void *global_data)
Add an observer to an object.
Definition: notify.c:189
struct MuttWindow * window_find_child(struct MuttWindow *win, enum WindowType type)
Recursively find a child Window of a given type.
Definition: mutt_window.c:521
@ WT_DLG_ALIAS
Alias Dialog, dlg_select_alias()
Definition: mutt_window.h:77
@ WT_STATUS_BAR
Status Bar containing extra info about the Index/Pager/etc.
Definition: mutt_window.h:102
@ NT_WINDOW
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:55
@ NT_CONFIG
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:43
@ NT_ALIAS
Alias has changed, NotifyAlias, EventAlias.
Definition: notify_type.h:37
char * limit
Limit being used.
Definition: gui.h:58
char * title
Title for the status bar.
Definition: gui.h:60
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void(* make_entry)(struct Menu *menu, char *buf, size_t buflen, int line)
Definition: lib.h:97
void(* mdata_free)(struct Menu *menu, void **ptr)
Definition: lib.h:152
int(* tag)(struct Menu *menu, int sel, int act)
Definition: lib.h:122
void * mdata
Private data.
Definition: lib.h:138
int max
Number of entries in the menu.
Definition: lib.h:72
bool custom_search
The menu implements its own non-Menusearch()-compatible search, trickle OP_SEARCH*.
Definition: lib.h:85
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
int(* recalc)(struct MuttWindow *win)
Definition: mutt_window.h:170
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
@ MENU_ALIAS
Select an email address by its alias.
Definition: type.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dlg_select_alias()

static bool dlg_select_alias ( struct Buffer buf,
struct AliasMenuData mdata 
)
static

Display a menu of Aliases.

Parameters
bufBuffer for expanded aliases
mdataMenu data holding Aliases
Return values
trueSelection was made

Definition at line 314 of file dlg_alias.c.

315{
316 if (ARRAY_EMPTY(&mdata->ava))
317 {
318 mutt_warning(_("You have no aliases"));
319 return false;
320 }
321
322 mdata->query = buf;
323 mdata->title = mutt_str_dup(_("Aliases"));
324
325 struct MuttWindow *dlg = alias_dialog_new(mdata);
326 struct Menu *menu = dlg->wdata;
327 struct MuttWindow *win_sbar = window_find_child(dlg, WT_STATUS_BAR);
328 mdata->menu = menu;
329 mdata->sbar = win_sbar;
330
331 alias_array_sort(&mdata->ava, mdata->sub);
332
333 struct AliasView *avp = NULL;
334 ARRAY_FOREACH(avp, &mdata->ava)
335 {
336 avp->num = ARRAY_FOREACH_IDX;
337 }
338
339 // ---------------------------------------------------------------------------
340 // Event Loop
341 int rc = 0;
342 int op = OP_NULL;
343 do
344 {
345 menu_tagging_dispatcher(menu->win, op);
346 window_redraw(NULL);
347
348 op = km_dokey(MENU_ALIAS);
349 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
350 if (op < 0)
351 continue;
352 if (op == OP_NULL)
353 {
355 continue;
356 }
358
359 rc = alias_function_dispatcher(dlg, op);
360 if (rc == FR_UNKNOWN)
361 rc = menu_function_dispatcher(menu->win, op);
362 if (rc == FR_UNKNOWN)
363 rc = global_function_dispatcher(NULL, op);
364 } while ((rc != FR_DONE) && (rc != FR_CONTINUE));
365 // ---------------------------------------------------------------------------
366
367 simple_dialog_free(&dlg);
368 window_redraw(NULL);
369 return (rc == FR_CONTINUE); // Was a selection made?
370}
void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub)
Sort and reindex an AliasViewArray.
Definition: sort.c:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition: array.h:211
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition: array.h:73
@ FR_DONE
Exit the Dialog.
Definition: dispatcher.h:35
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
@ FR_CONTINUE
Remain in the Dialog.
Definition: dispatcher.h:34
static struct MuttWindow * alias_dialog_new(struct AliasMenuData *mdata)
Create an Alias Selection Dialog.
Definition: dlg_alias.c:279
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int global_function_dispatcher(struct MuttWindow *win, int op)
Perform a Global function - Implements function_dispatcher_t -.
Definition: global.c:164
int menu_function_dispatcher(struct MuttWindow *win, int op)
Perform a Menu function - Implements function_dispatcher_t -.
Definition: functions.c:320
int alias_function_dispatcher(struct MuttWindow *win, int op)
Perform a Alias function - Implements function_dispatcher_t -.
Definition: functions.c:333
#define mutt_warning(...)
Definition: logging.h:85
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
void simple_dialog_free(struct MuttWindow **ptr)
Destroy a simple index Dialog.
Definition: simple.c:166
int km_dokey(enum MenuType mtype)
Determine what a keypress should do.
Definition: keymap.c:796
void km_error_key(enum MenuType mtype)
Handle an unbound key sequence.
Definition: keymap.c:1062
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:603
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:46
struct AliasViewArray ava
All Aliases/Queries.
Definition: gui.h:53
struct MuttWindow * sbar
Status Bar.
Definition: gui.h:59
struct Menu * menu
Menu.
Definition: gui.h:56
struct Buffer * query
Query string.
Definition: gui.h:57
struct ConfigSubset * sub
Config items.
Definition: gui.h:55
GUI data wrapping an Alias.
Definition: gui.h:36
int num
Index number in list.
Definition: gui.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_complete()

int alias_complete ( char *  buf,
size_t  buflen,
struct ConfigSubset sub 
)

Alias completion routine.

Parameters
bufPartial Alias to complete
buflenLength of buffer
subConfig items
Return values
1Success
0Error

Given a partial alias, this routine attempts to fill in the alias from the alias list as much as possible. if given empty search string or found nothing, present all aliases

Definition at line 384 of file dlg_alias.c.

385{
386 struct Alias *np = NULL;
387 char bestname[8192] = { 0 };
388
389 struct AliasMenuData mdata = { ARRAY_HEAD_INITIALIZER, NULL, sub };
390 mdata.limit = mutt_str_dup(buf);
391
392 if (buf[0] != '\0')
393 {
394 TAILQ_FOREACH(np, &Aliases, entries)
395 {
396 if (np->name && mutt_strn_equal(np->name, buf, strlen(buf)))
397 {
398 if (bestname[0] == '\0') /* init */
399 {
400 mutt_str_copy(bestname, np->name,
401 MIN(mutt_str_len(np->name) + 1, sizeof(bestname)));
402 }
403 else
404 {
405 int i;
406 for (i = 0; np->name[i] && (np->name[i] == bestname[i]); i++)
407 ; // do nothing
408
409 bestname[i] = '\0';
410 }
411 }
412 }
413
414 if (bestname[0] == '\0')
415 {
416 // Create a View Array of all the Aliases
417 FREE(&mdata.limit);
418 TAILQ_FOREACH(np, &Aliases, entries)
419 {
421 }
422 }
423 else
424 {
425 /* fake the pattern for menu title */
426 char *mtitle = NULL;
427 mutt_str_asprintf(&mtitle, "~f ^%s", buf);
428 FREE(&mdata.limit);
429 mdata.limit = mtitle;
430
431 if (!mutt_str_equal(bestname, buf))
432 {
433 /* we are adding something to the completion */
434 mutt_str_copy(buf, bestname, mutt_str_len(bestname) + 1);
435 FREE(&mdata.limit);
436 return 1;
437 }
438
439 /* build alias list and show it */
440 TAILQ_FOREACH(np, &Aliases, entries)
441 {
442 int aasize = alias_array_alias_add(&mdata.ava, np);
443
444 struct AliasView *av = ARRAY_GET(&mdata.ava, aasize - 1);
445
446 if (np->name && !mutt_strn_equal(np->name, buf, strlen(buf)))
447 {
448 av->is_visible = false;
449 }
450 }
451 }
452 }
453
454 if (ARRAY_EMPTY(&mdata.ava))
455 {
456 TAILQ_FOREACH(np, &Aliases, entries)
457 {
458 alias_array_alias_add(&mdata.ava, np);
459 }
460
461 mutt_pattern_alias_func(NULL, &mdata, NULL);
462 }
463
464 if (!dlg_select_alias(NULL, &mdata))
465 goto done;
466
467 buf[0] = '\0';
468
469 // Extract the selected aliases
470 struct Buffer *tmpbuf = mutt_buffer_pool_get();
471 struct AliasView *avp = NULL;
472 ARRAY_FOREACH(avp, &mdata.ava)
473 {
474 if (!avp->is_tagged)
475 continue;
476
477 mutt_addrlist_write(&avp->alias->addr, tmpbuf, true);
478 }
479 mutt_str_copy(buf, mutt_buffer_string(tmpbuf), buflen);
481
482done:
483 // Process any deleted aliases
484 ARRAY_FOREACH(avp, &mdata.ava)
485 {
486 if (!avp->is_deleted)
487 continue;
488
489 TAILQ_REMOVE(&Aliases, avp->alias, entries);
490 alias_free(&avp->alias);
491 }
492
493 ARRAY_FREE(&mdata.ava);
494 FREE(&mdata.limit);
495 FREE(&mdata.title);
496
497 return 0;
498}
size_t mutt_addrlist_write(const struct AddressList *al, struct Buffer *buf, bool display)
Write an Address to a buffer.
Definition: address.c:1184
void alias_free(struct Alias **ptr)
Free an Alias.
Definition: alias.c:641
struct AliasList Aliases
List of all the user's email aliases.
Definition: alias.c:58
int alias_array_alias_add(struct AliasViewArray *ava, struct Alias *alias)
Add an Alias to the AliasViewArray.
Definition: array.c:47
#define ARRAY_FREE(head)
Release all memory.
Definition: array.h:203
#define ARRAY_GET(head, idx)
Return the element at index.
Definition: array.h:108
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition: array.h:57
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:78
static bool dlg_select_alias(struct Buffer *buf, struct AliasMenuData *mdata)
Display a menu of Aliases.
Definition: dlg_alias.c:314
#define FREE(x)
Definition: memory.h:43
#define MIN(a, b)
Definition: memory.h:31
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:1031
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
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:496
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:567
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:652
int mutt_pattern_alias_func(char *prompt, struct AliasMenuData *mdata, struct Menu *menu)
Perform some Pattern matching for Alias.
Definition: pattern.c:242
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
AliasView array wrapper with Pattern information -.
Definition: gui.h:52
bool is_visible
Is visible?
Definition: gui.h:43
struct Alias * alias
Alias.
Definition: gui.h:44
bool is_deleted
Is it deleted?
Definition: gui.h:42
bool is_tagged
Is it tagged?
Definition: gui.h:41
A shortcut for an email address or addresses.
Definition: alias.h:34
char * name
Short name.
Definition: alias.h:35
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:36
String manipulation buffer.
Definition: buffer.h:34
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_dialog()

void alias_dialog ( struct Mailbox m,
struct ConfigSubset sub 
)

Open the aliases dialog.

Parameters
mMailbox
subConfig item

Definition at line 505 of file dlg_alias.c.

506{
507 struct Alias *np = NULL;
508
509 struct AliasMenuData mdata = { ARRAY_HEAD_INITIALIZER, NULL, sub };
510
511 // Create a View Array of all the Aliases
512 TAILQ_FOREACH(np, &Aliases, entries)
513 {
515 }
516
517 if (!dlg_select_alias(NULL, &mdata))
518 goto done;
519
520 // Prepare the "To:" field of a new email
521 struct Email *e = email_new();
522 e->env = mutt_env_new();
523
524 struct AliasView *avp = NULL;
525 ARRAY_FOREACH(avp, &mdata.ava)
526 {
527 if (!avp->is_tagged)
528 continue;
529
530 struct AddressList al_copy = TAILQ_HEAD_INITIALIZER(al_copy);
531 if (alias_to_addrlist(&al_copy, avp->alias))
532 {
533 mutt_addrlist_copy(&e->env->to, &al_copy, false);
534 mutt_addrlist_clear(&al_copy);
535 }
536 }
537
538 mutt_send_message(SEND_REVIEW_TO, e, NULL, m, NULL, sub);
539
540done:
541 // Process any deleted aliases
542 ARRAY_FOREACH(avp, &mdata.ava)
543 {
544 if (avp->is_deleted)
545 {
546 TAILQ_REMOVE(&Aliases, avp->alias, entries);
547 alias_free(&avp->alias);
548 }
549 }
550
551 ARRAY_FREE(&mdata.ava);
552 FREE(&mdata.limit);
553 FREE(&mdata.title);
554}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:737
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1435
bool alias_to_addrlist(struct AddressList *al, struct Alias *alias)
Turn an Alias into an AddressList.
Definition: dlg_query.c:118
struct Email * email_new(void)
Create a new Email.
Definition: email.c:78
struct Envelope * mutt_env_new(void)
Create a new Envelope.
Definition: envelope.c:43
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
int mutt_send_message(SendFlags flags, struct Email *e_templ, const char *tempfile, struct Mailbox *m, struct EmailList *el, struct ConfigSubset *sub)
Send an email.
Definition: send.c:2129
#define SEND_REVIEW_TO
Allow the user to edit the To field.
Definition: send.h:54
The envelope/body of an email.
Definition: email.h:37
struct Envelope * env
Envelope information.
Definition: email.h:66
struct AddressList to
Email's 'To' list.
Definition: envelope.h:60
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ AliasHelp

const struct Mapping AliasHelp[]
static
Initial value:
= {
{ N_("Exit"), OP_EXIT },
{ N_("Del"), OP_DELETE },
{ N_("Undel"), OP_UNDELETE },
{ N_("Sort"), OP_SORT },
{ N_("Rev-Sort"), OP_SORT_REVERSE },
{ N_("Select"), OP_GENERIC_SELECT_ENTRY },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
}
#define N_(a)
Definition: message.h:32

Help Bar for the Alias dialog (address book)

Definition at line 99 of file dlg_alias.c.