NeoMutt  2023-03-22
Teaching an old dog new tricks
DOXYGEN
compose.c
Go to the documentation of this file.
1
67#include "config.h"
68#include <stdbool.h>
69#include <stdint.h>
70#include <stdio.h>
71#include "private.h"
72#include "mutt/lib.h"
73#include "config/lib.h"
74#include "email/lib.h"
75#include "core/lib.h"
76#include "gui/lib.h"
77#include "lib.h"
78#include "attach/lib.h"
79#include "envelope/lib.h"
80#include "index/lib.h"
81#include "menu/lib.h"
82#include "ncrypt/lib.h"
83#include "attach_data.h"
84#include "cbar.h"
85#include "functions.h"
86#include "globals.h" // IWYU pragma: keep
87#include "hook.h"
88#include "keymap.h"
89#include "mutt_logging.h"
90#include "opcodes.h"
91#include "shared_data.h"
92
94static const struct Mapping ComposeHelp[] = {
95 // clang-format off
96 { N_("Send"), OP_COMPOSE_SEND_MESSAGE },
97 { N_("Abort"), OP_EXIT },
98 /* L10N: compose menu help line entry */
99 { N_("To"), OP_ENVELOPE_EDIT_TO },
100 /* L10N: compose menu help line entry */
101 { N_("CC"), OP_ENVELOPE_EDIT_CC },
102 /* L10N: compose menu help line entry */
103 { N_("Subj"), OP_ENVELOPE_EDIT_SUBJECT },
104 { N_("Attach file"), OP_ATTACHMENT_ATTACH_FILE },
105 { N_("Descrip"), OP_ATTACHMENT_EDIT_DESCRIPTION },
106 { N_("Help"), OP_HELP },
107 { NULL, 0 },
108 // clang-format on
109};
110
111#ifdef USE_NNTP
113static const struct Mapping ComposeNewsHelp[] = {
114 // clang-format off
115 { N_("Send"), OP_COMPOSE_SEND_MESSAGE },
116 { N_("Abort"), OP_EXIT },
117 { N_("Newsgroups"), OP_ENVELOPE_EDIT_NEWSGROUPS },
118 { N_("Subj"), OP_ENVELOPE_EDIT_SUBJECT },
119 { N_("Attach file"), OP_ATTACHMENT_ATTACH_FILE },
120 { N_("Descrip"), OP_ATTACHMENT_EDIT_DESCRIPTION },
121 { N_("Help"), OP_HELP },
122 { NULL, 0 },
123 // clang-format on
124};
125#endif
126
131{
132 if (nc->event_type != NT_CONFIG)
133 return 0;
134 if (!nc->global_data || !nc->event_data)
135 return -1;
136
137 struct EventConfig *ev_c = nc->event_data;
138 struct MuttWindow *dlg = nc->global_data;
139
140 if (!mutt_str_equal(ev_c->name, "status_on_top"))
141 return 0;
142
144 mutt_debug(LL_DEBUG5, "config done, request WA_REFLOW\n");
145 return 0;
146}
147
152{
153 if (nc->event_type != NT_ENVELOPE)
154 return 0;
155 if (!nc->global_data || !nc->event_data)
156 return -1;
157
158 struct ComposeSharedData *shared = nc->global_data;
159
161 shared->fcc_set = true;
162
164 return 0;
165}
166
171{
172 if (nc->event_type != NT_WINDOW)
173 return 0;
174 if (!nc->global_data || !nc->event_data)
175 return -1;
177 return 0;
178
179 struct MuttWindow *dlg = nc->global_data;
180 struct EventWindow *ev_w = nc->event_data;
181 if (ev_w->win != dlg)
182 return 0;
183
186 mutt_debug(LL_DEBUG5, "window delete done\n");
187
188 return 0;
189}
190
198static void gen_attach_list(struct AttachCtx *actx, struct Body *m, int parent_type, int level)
199{
200 for (; m; m = m->next)
201 {
202 struct AttachPtr *ap = mutt_aptr_new();
203 mutt_actx_add_attach(actx, ap);
204 ap->body = m;
205 m->aptr = ap;
207 ap->level = level;
208 if ((m->type == TYPE_MULTIPART) && m->parts &&
210 {
211 gen_attach_list(actx, m->parts, m->type, level + 1);
212 }
213 }
214}
215
222void update_menu(struct AttachCtx *actx, struct Menu *menu, bool init)
223{
224 if (init)
225 {
226 gen_attach_list(actx, actx->email->body, -1, 0);
227 mutt_attach_init(actx);
228
229 struct ComposeAttachData *adata = menu->mdata;
230 adata->actx = actx;
231 }
232
234
235 menu->max = actx->vcount;
236 if (menu->max)
237 {
238 int index = menu_get_index(menu);
239 if (index >= menu->max)
241 }
242 else
244
246}
247
255static struct MuttWindow *compose_dlg_init(struct ConfigSubset *sub,
256 struct Email *e, struct Buffer *fcc)
257{
259 shared->sub = sub;
260 shared->email = e;
261
265 dlg->wdata = shared;
267
268 struct MuttWindow *win_env = env_window_new(e, fcc, sub);
269 struct MuttWindow *win_attach = attach_new(dlg, shared);
270 struct MuttWindow *win_cbar = cbar_new(shared);
271 struct MuttWindow *win_abar = sbar_new();
272 sbar_set_title(win_abar, _("-- Attachments"));
273
274 const bool c_status_on_top = cs_subset_bool(sub, "status_on_top");
275 if (c_status_on_top)
276 {
277 mutt_window_add_child(dlg, win_cbar);
278 mutt_window_add_child(dlg, win_env);
279 mutt_window_add_child(dlg, win_abar);
280 mutt_window_add_child(dlg, win_attach);
281 }
282 else
283 {
284 mutt_window_add_child(dlg, win_env);
285 mutt_window_add_child(dlg, win_abar);
286 mutt_window_add_child(dlg, win_attach);
287 mutt_window_add_child(dlg, win_cbar);
288 }
289
290 dlg->help_data = ComposeHelp;
291 dlg->help_menu = MENU_COMPOSE;
292 dlg->focus = win_attach;
293
294 return dlg;
295}
296
307int mutt_compose_menu(struct Email *e, struct Buffer *fcc, uint8_t flags,
308 struct ConfigSubset *sub)
309{
310 struct MuttWindow *dlg = compose_dlg_init(sub, e, fcc);
311 struct ComposeSharedData *shared = dlg->wdata;
312 shared->mailbox = get_current_mailbox();
313 shared->email = e;
314 shared->sub = sub;
315 shared->fcc = fcc;
316 shared->fcc_set = false;
317 shared->flags = flags;
318 shared->rc = -1;
319
323 dialog_push(dlg);
324
325#ifdef USE_NNTP
326 if (OptNewsSend)
328 else
329#endif
330 dlg->help_data = ComposeHelp;
331 dlg->help_menu = MENU_COMPOSE;
332
333 struct Menu *menu = shared->adata->menu;
334 update_menu(shared->adata->actx, menu, true);
336
337 struct MuttWindow *win_env = window_find_child(dlg, WT_CUSTOM);
338
339 // ---------------------------------------------------------------------------
340 // Event Loop
341 int rc = 0;
342 int op = OP_NULL;
343 do
344 {
345#ifdef USE_NNTP
346 OptNews = false; /* for any case */
347#endif
348 menu_tagging_dispatcher(menu->win, op);
349 window_redraw(NULL);
350
352 mutt_debug(LL_DEBUG1, "Got op %s (%d)\n", opcodes_get_name(op), op);
353 if (op < 0)
354 continue;
355 if (op == OP_NULL)
356 {
358 continue;
359 }
361
362 rc = compose_function_dispatcher(dlg, op);
363 if (rc == FR_UNKNOWN)
364 rc = env_function_dispatcher(win_env, op);
365 if (rc == FR_UNKNOWN)
366 rc = menu_function_dispatcher(menu->win, op);
367 if (rc == FR_UNKNOWN)
368 rc = global_function_dispatcher(NULL, op);
369 } while (rc != FR_DONE);
370 // ---------------------------------------------------------------------------
371
372#ifdef USE_AUTOCRYPT
373 /* This is a fail-safe to make sure the bit isn't somehow turned
374 * on. The user could have disabled the option after setting SEC_AUTOCRYPT,
375 * or perhaps resuming or replying to an autocrypt message. */
376 const bool c_autocrypt = cs_subset_bool(sub, "autocrypt");
377 if (!c_autocrypt)
378 e->security &= ~SEC_AUTOCRYPT;
379#endif
380
381 if (shared->adata->actx->idxlen)
382 e->body = shared->adata->actx->idx[0]->body;
383 else
384 e->body = NULL;
385
386 rc = shared->rc;
387
388 dialog_pop();
389 mutt_window_free(&dlg);
390
391 return rc;
392}
void mutt_actx_add_attach(struct AttachCtx *actx, struct AttachPtr *attach)
Add an Attachment to an Attachment Context.
Definition: attach.c:65
struct AttachPtr * mutt_aptr_new(void)
Create a new Attachment Pointer.
Definition: attach.c:40
GUI display the mailboxes in a side panel.
Compose Attach Data.
struct MuttWindow * cbar_new(struct ComposeSharedData *shared)
Create the Compose Bar (status)
Definition: cbar.c:305
Compose Bar.
struct MuttWindow * attach_new(struct MuttWindow *parent, struct ComposeSharedData *shared)
Create the Attachments Menu.
Definition: attach.c:236
struct ComposeSharedData * compose_shared_data_new(void)
Free the compose shared data.
Definition: shared_data.c:47
static void gen_attach_list(struct AttachCtx *actx, struct Body *m, int parent_type, int level)
Generate the attachment list for the compose screen.
Definition: compose.c:198
void update_menu(struct AttachCtx *actx, struct Menu *menu, bool init)
Redraw the compose window.
Definition: compose.c:222
static const struct Mapping ComposeHelp[]
Help Bar for the Compose dialog.
Definition: compose.c:94
static const struct Mapping ComposeNewsHelp[]
Help Bar for the News Compose dialog.
Definition: compose.c:113
int mutt_compose_menu(struct Email *e, struct Buffer *fcc, uint8_t flags, struct ConfigSubset *sub)
Allow the user to edit the message envelope.
Definition: compose.c:307
static struct MuttWindow * compose_dlg_init(struct ConfigSubset *sub, struct Email *e, struct Buffer *fcc)
Allocate the Windows for Compose.
Definition: compose.c:255
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
SecurityFlags mutt_is_multipart_encrypted(struct Body *b)
Does the message have encrypted parts?
Definition: crypt.c:437
void dialog_push(struct MuttWindow *dlg)
Display a Window to the user.
Definition: dialog.c:103
void dialog_pop(void)
Hide a Window from the user.
Definition: dialog.c:137
@ FR_DONE
Exit the Dialog.
Definition: dispatcher.h:35
@ FR_UNKNOWN
Unknown function.
Definition: dispatcher.h:33
Structs that make up an email.
@ NT_EMAIL_CHANGE
Email has changed.
Definition: email.h:150
Envelope-editing Window.
struct MuttWindow * env_window_new(struct Email *e, struct Buffer *fcc, struct ConfigSubset *sub)
Create the Envelope Window.
Definition: window.c:990
@ NT_ENVELOPE_FCC
"Fcc:" header has changed
Definition: envelope.h:107
bool OptNews
(pseudo) used to change reader mode
Definition: globals.c:78
bool OptNewsSend
(pseudo) used to change behavior when posting
Definition: globals.c:79
int menu_tagging_dispatcher(struct MuttWindow *win, int op)
Perform tagging operations on the Menu - Implements function_dispatcher_t -.
Definition: tagging.c:223
int env_function_dispatcher(struct MuttWindow *win, int op)
Perform an Envelope function - Implements function_dispatcher_t -.
Definition: functions.c:559
int compose_function_dispatcher(struct MuttWindow *win, int op)
Perform a Compose function - Implements function_dispatcher_t -.
Definition: functions.c:1970
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
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
static int compose_email_observer(struct NotifyCallback *nc)
Notification that an Email has changed - Implements observer_t -.
Definition: compose.c:151
static int compose_config_observer(struct NotifyCallback *nc)
Notification that a Config Variable has changed - Implements observer_t -.
Definition: compose.c:130
static int compose_window_observer(struct NotifyCallback *nc)
Notification that a Window has changed - Implements observer_t -.
Definition: compose.c:170
void compose_shared_data_free(struct MuttWindow *win, void **ptr)
Create the compose shared data - Implements MuttWindow::wdata_free() -.
Definition: shared_data.c:36
Convenience wrapper for the gui headers.
void mutt_message_hook(struct Mailbox *m, struct Email *e, HookFlags type)
Perform a message hook.
Definition: hook.c:656
Parse and execute user-defined hooks.
#define MUTT_SEND2_HOOK
send2-hook: when changing fields in the compose menu
Definition: hook.h:49
GUI manage the main index (list of emails)
Data shared between Index, Pager and Sidebar.
struct Mailbox * get_current_mailbox(void)
Get the current Mailbox.
Definition: index.c:618
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
Manage keymappings.
@ LL_DEBUG5
Log at debug level 5.
Definition: logging.h:44
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
GUI present the user with a selectable list.
#define MENU_REDRAW_INDEX
Redraw the index.
Definition: lib.h:57
void menu_queue_redraw(struct Menu *menu, MenuRedrawFlags redraw)
Queue a request for a redraw.
Definition: menu.c:178
int menu_get_index(struct Menu *menu)
Get the current selection in the Menu.
Definition: menu.c:154
MenuRedrawFlags menu_set_index(struct Menu *menu, int index)
Set the current selection in the Menu.
Definition: menu.c:168
@ TYPE_MULTIPART
Type: 'multipart/*'.
Definition: mime.h:37
Convenience wrapper for the library headers.
#define N_(a)
Definition: message.h:32
#define _(a)
Definition: message.h:28
bool notify_observer_remove(struct Notify *notify, const observer_t callback, const void *global_data)
Remove an observer from an object.
Definition: notify.c:228
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
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
NeoMutt Logging.
void window_redraw(struct MuttWindow *win)
Reflow, recalc and repaint a tree of Windows.
Definition: mutt_window.c:603
bool window_status_on_top(struct MuttWindow *panel, struct ConfigSubset *sub)
Organise windows according to config variable.
Definition: mutt_window.c:758
void mutt_window_free(struct MuttWindow **ptr)
Free a Window and its children.
Definition: mutt_window.c:200
void mutt_window_add_child(struct MuttWindow *parent, struct MuttWindow *child)
Add a child to Window.
Definition: mutt_window.c:438
struct MuttWindow * mutt_window_new(enum WindowType type, enum MuttWindowOrientation orient, enum MuttWindowSize size, int cols, int rows)
Create a new Window.
Definition: mutt_window.c:180
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_CUSTOM
Window with a custom drawing function.
Definition: mutt_window.h:95
@ WT_DLG_COMPOSE
Compose Dialog, mutt_compose_menu()
Definition: mutt_window.h:82
@ MUTT_WIN_ORIENT_VERTICAL
Window uses all available vertical space.
Definition: mutt_window.h:38
@ NT_WINDOW_DELETE
Window is about to be deleted.
Definition: mutt_window.h:205
#define MUTT_WIN_SIZE_UNLIMITED
Use as much space as possible.
Definition: mutt_window.h:52
@ MUTT_WIN_SIZE_MAXIMISE
Window wants as much space as possible.
Definition: mutt_window.h:48
API for encryption/signing of emails.
#define APPLICATION_PGP
Use PGP to encrypt/sign.
Definition: lib.h:90
#define WithCrypto
Definition: lib.h:116
@ 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_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
@ NT_ENVELOPE
Envelope has changed, NotifyEnvelope.
Definition: notify_type.h:45
@ NT_ALL
Register for all notifications.
Definition: notify_type.h:35
const char * opcodes_get_name(int op)
Get the name of an opcode.
Definition: opcodes.c:46
All user-callable functions.
void mutt_attach_init(struct AttachCtx *actx)
Create a new Attachment context.
Definition: recvattach.c:1162
void mutt_update_tree(struct AttachCtx *actx)
Refresh the list of attachments.
Definition: recvattach.c:112
struct MuttWindow * sbar_new(void)
Add the Simple Bar (status)
Definition: sbar.c:200
void sbar_set_title(struct MuttWindow *win, const char *title)
Set the title for the Simple Bar.
Definition: sbar.c:224
Sidebar functions.
GUI display the mailboxes in a side panel.
Key value store.
A set of attachments.
Definition: attach.h:51
short vcount
The number of virtual attachments.
Definition: attach.h:60
struct Email * email
Used by recvattach for updating.
Definition: attach.h:52
struct AttachPtr ** idx
Array of attachments.
Definition: attach.h:55
short idxlen
Number of attachmentes.
Definition: attach.h:56
An email to which things will be attached.
Definition: attach.h:35
struct Body * body
Attachment.
Definition: attach.h:36
int level
Nesting depth of attachment.
Definition: attach.h:40
int parent_type
Type of parent attachment, e.g. TYPE_MULTIPART.
Definition: attach.h:38
The body of an email.
Definition: body.h:36
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:72
struct AttachPtr * aptr
Menu information, used in recvattach.c.
Definition: body.h:74
struct Body * next
next attachment in the list
Definition: body.h:71
unsigned int type
content-type primary type, ContentType
Definition: body.h:40
String manipulation buffer.
Definition: buffer.h:34
Data to fill the Compose Attach Window.
Definition: attach_data.h:33
struct Menu * menu
Menu displaying the attachments.
Definition: attach_data.h:35
struct AttachCtx * actx
Set of attachments.
Definition: attach_data.h:34
Shared Compose Data.
Definition: shared_data.h:33
struct ConfigSubset * sub
Config set to use.
Definition: shared_data.h:34
struct Mailbox * mailbox
Current Mailbox.
Definition: shared_data.h:35
int flags
Flags, e.g. MUTT_COMPOSE_NOFREEHEADER.
Definition: shared_data.h:41
bool fcc_set
User has edited the Fcc: field.
Definition: shared_data.h:42
int rc
Return code to leave compose.
Definition: shared_data.h:43
struct ComposeAttachData * adata
Attachments.
Definition: shared_data.h:37
struct Email * email
Email being composed.
Definition: shared_data.h:36
struct Buffer * fcc
Buffer to save FCC.
Definition: shared_data.h:40
A set of inherited config items.
Definition: subset.h:47
The envelope/body of an email.
Definition: email.h:37
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/lib.h pgplib....
Definition: email.h:41
struct Body * body
List of MIME parts.
Definition: email.h:67
struct Notify * notify
Notifications: NotifyEmail, EventEmail.
Definition: email.h:71
A config-change event.
Definition: subset.h:70
const char * name
Name of config item that changed.
Definition: subset.h:72
An Event that happened to a Window.
Definition: mutt_window.h:215
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:216
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Definition: lib.h:70
struct MuttWindow * win
Window holding the Menu.
Definition: lib.h:77
void * mdata
Private data.
Definition: lib.h:138
int max
Number of entries in the menu.
Definition: lib.h:72
const struct Mapping * help_data
Data for the Help Bar.
Definition: mutt_window.h:142
struct MuttWindow * focus
Focused Window.
Definition: mutt_window.h:140
void * wdata
Private data.
Definition: mutt_window.h:145
struct Notify * notify
Notifications: NotifyWindow, EventWindow.
Definition: mutt_window.h:138
void(* wdata_free)(struct MuttWindow *win, void **ptr)
Definition: mutt_window.h:159
int help_menu
Menu for key bindings, e.g. MENU_PAGER.
Definition: mutt_window.h:141
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct Notify * notify
Notifications handler.
Definition: neomutt.h:38
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Data passed to a notification function.
Definition: observer.h:34
void * event_data
Data from notify_send()
Definition: observer.h:38
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:36
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:37
void * global_data
Data from notify_observer_add()
Definition: observer.h:39
@ MENU_COMPOSE
Compose an email.
Definition: type.h:42