NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
notify.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include "mutt/lib.h"
32 #include "config/lib.h"
33 #include "email/lib.h"
34 #include "core/lib.h"
35 #include "gui/lib.h"
36 #include "lib.h"
37 #include "context.h"
38 #include "mutt_commands.h"
39 
40 extern const struct Mapping ColorFields[];
41 extern const struct Mapping ComposeColorFields[];
42 
43 static const char *get_event_type(enum NotifyType type)
44 {
45  switch (type)
46  {
47  case NT_ACCOUNT:
48  return "account";
49  case NT_COLOR:
50  return "color";
51  case NT_COMMAND:
52  return "command";
53  case NT_CONFIG:
54  return "config";
55  case NT_CONTEXT:
56  return "context";
57  case NT_EMAIL:
58  return "email";
59  case NT_GLOBAL:
60  return "global";
61  case NT_MAILBOX:
62  return "mailbox";
63  case NT_WINDOW:
64  return "window";
65  default:
66  return "UNKNOWN";
67  }
68 }
69 
70 const char *get_mailbox_type(enum MailboxType type)
71 {
72  switch (type)
73  {
74  case MUTT_COMPRESSED:
75  return "compressed";
76  case MUTT_IMAP:
77  return "imap";
78  case MUTT_MAILDIR:
79  return "maildir";
80  case MUTT_MBOX:
81  return "mbox";
82  case MUTT_MH:
83  return "mh";
84  case MUTT_MMDF:
85  return "mmdf";
86  case MUTT_NNTP:
87  return "nntp";
88  case MUTT_NOTMUCH:
89  return "notmuch";
90  case MUTT_POP:
91  return "pop";
92  default:
93  return "UNKNOWN";
94  }
95 }
96 
97 static const char *get_global_event(int id)
98 {
99  switch (id)
100  {
101  case NT_GLOBAL_SHUTDOWN:
102  return "shutdown";
103  case NT_GLOBAL_STARTUP:
104  return "startup";
105  case NT_GLOBAL_TIMEOUT:
106  return "timeout";
107  default:
108  return "UNKNOWN";
109  }
110 }
111 
112 static const char *get_config_type(int id)
113 {
114  switch (id)
115  {
116  case NT_CONFIG_SET:
117  return "set";
118  case NT_CONFIG_RESET:
119  return "reset";
120  default:
121  return "UNKNOWN";
122  }
123 }
124 
125 static const char *get_mailbox_event(int id)
126 {
127  switch (id)
128  {
129  case NT_MAILBOX_ADD:
130  return "add";
131  case NT_MAILBOX_DELETE:
132  return "delete";
133  case NT_MAILBOX_INVALID:
134  return "invalid";
135  case NT_MAILBOX_RESORT:
136  return "resort";
137  case NT_MAILBOX_UPDATE:
138  return "update";
139  case NT_MAILBOX_UNTAG:
140  return "untag";
141  default:
142  return "UNKNOWN";
143  }
144 }
145 
146 static const char *get_context(int id)
147 {
148  switch (id)
149  {
150  case NT_CONTEXT_DELETE:
151  return "delete";
152  case NT_CONTEXT_ADD:
153  return "add";
154  default:
155  return "UNKNOWN";
156  }
157 }
158 
159 static void notify_dump_account(struct NotifyCallback *nc)
160 {
161  struct EventAccount *ev_a = nc->event_data;
162  struct Account *a = ev_a->account;
163 
164  mutt_debug(LL_DEBUG1, " Account: %p (%s) %s\n", a,
165  get_mailbox_type(a->type), NONULL(a->name));
166 }
167 
168 static void notify_dump_color(struct NotifyCallback *nc)
169 {
170  struct EventColor *ev_c = nc->event_data;
171 
172  const char *color = NULL;
173  const char *scope = "";
174 
175  if (ev_c->color == MT_COLOR_MAX)
176  color = "ALL";
177 
178  if (!color)
179  color = mutt_map_get_name(ev_c->color, ColorFields);
180 
181  if (!color)
182  {
184  scope = "compose ";
185  }
186 
187  if (!color)
188  color = "UNKNOWN";
189 
190  mutt_debug(LL_DEBUG1, " Color: %s %s%s (%d)\n",
191  (nc->event_subtype == NT_COLOR_SET) ? "set" : "reset", scope,
192  color, ev_c->color);
193 }
194 
195 static void notify_dump_command(struct NotifyCallback *nc)
196 {
197  struct Command *cmd = nc->event_data;
198 
199  if (cmd->data < 4096)
200  mutt_debug(LL_DEBUG1, " Command: %s, data: %ld\n", cmd->name, cmd->data);
201  else
202  mutt_debug(LL_DEBUG1, " Command: %s, data: %p\n", cmd->name, (void *) cmd->data);
203 }
204 
205 static void notify_dump_config(struct NotifyCallback *nc)
206 {
207  struct EventConfig *ev_c = nc->event_data;
208 
209  struct Buffer value = mutt_buffer_make(128);
210  cs_he_string_get(ev_c->sub->cs, ev_c->he, &value);
211  mutt_debug(LL_DEBUG1, " Config: %s %s = %s\n", get_config_type(nc->event_subtype),
212  ev_c->name, mutt_buffer_string(&value));
213  mutt_buffer_dealloc(&value);
214 }
215 
216 static void notify_dump_context(struct NotifyCallback *nc)
217 {
218  struct EventContext *ev_c = nc->event_data;
219 
220  const char *path = "NONE";
221  if (ev_c->ctx && ev_c->ctx->mailbox)
222  path = mailbox_path(ev_c->ctx->mailbox);
223 
224  mutt_debug(LL_DEBUG1, " Context: %s %s\n", get_context(nc->event_subtype), path);
225 }
226 
227 static void notify_dump_email(struct NotifyCallback *nc)
228 {
229  struct EventEmail *ev_e = nc->event_data;
230 
231  mutt_debug(LL_DEBUG1, " Email: %d\n", ev_e->num_emails);
232  for (size_t i = 0; i < ev_e->num_emails; i++)
233  {
234  mutt_debug(LL_DEBUG1, " : %p\n", ev_e->emails[i]);
235  }
236 }
237 
238 static void notify_dump_global(struct NotifyCallback *nc)
239 {
240  mutt_debug(LL_DEBUG1, " Global: %s\n", get_global_event(nc->event_subtype));
241 }
242 
243 static void notify_dump_mailbox(struct NotifyCallback *nc)
244 {
245  struct EventMailbox *ev_m = nc->event_data;
246 
247  struct Mailbox *m = ev_m->mailbox;
248  const char *path = m ? mailbox_path(m) : "";
249  mutt_debug(LL_DEBUG1, " Mailbox: %s %s\n", get_mailbox_event(nc->event_subtype), path);
250 }
251 
253 {
254  struct EventWindow *ev_w = nc->event_data;
255  const struct MuttWindow *win = ev_w->win;
256  WindowNotifyFlags flags = ev_w->flags;
257 
258  struct Buffer buf = mutt_buffer_make(128);
259 
260  mutt_buffer_add_printf(&buf, "[%s] ", mutt_window_win_name(win));
261 
262  if (flags & WN_VISIBLE)
263  mutt_buffer_addstr(&buf, "visible ");
264  if (flags & WN_HIDDEN)
265  mutt_buffer_addstr(&buf, "hidden ");
266 
267  if (flags & WN_MOVED)
268  {
269  mutt_buffer_add_printf(&buf, "moved (C%d,R%d)->(C%d,R%d) ",
270  win->old.col_offset, win->old.row_offset,
271  win->state.col_offset, win->state.row_offset);
272  }
273 
274  if (flags & WN_TALLER)
275  mutt_buffer_add_printf(&buf, "taller [%d->%d] ", win->old.rows, win->state.rows);
276  if (flags & WN_SHORTER)
277  mutt_buffer_add_printf(&buf, "shorter [%d->%d] ", win->old.rows, win->state.rows);
278  if (flags & WN_WIDER)
279  mutt_buffer_add_printf(&buf, "wider [%d->%d] ", win->old.cols, win->state.cols);
280  if (flags & WN_NARROWER)
281  mutt_buffer_add_printf(&buf, "narrower [%d->%d] ", win->old.cols, win->state.cols);
282 
283  mutt_debug(LL_DEBUG1, " Window: %s\n", mutt_buffer_string(&buf));
284 
285  mutt_buffer_dealloc(&buf);
286 }
287 
289 {
290  struct EventWindow *ev_w = nc->event_data;
291  struct MuttWindow *win = ev_w->win;
292 
293  struct Buffer buf = mutt_buffer_make(128);
294 
295  mutt_buffer_addstr(&buf, "Focus: ");
296 
297  if (win)
298  {
299  struct MuttWindow *dlg = dialog_find(win);
300  if (dlg && (dlg != win))
301  mutt_buffer_add_printf(&buf, "%s:", mutt_window_win_name(dlg));
302 
303  mutt_buffer_add_printf(&buf, "%s ", mutt_window_win_name(win));
304 
305  mutt_buffer_add_printf(&buf, "(C%d,R%d) [%dx%d]", win->state.col_offset,
306  win->state.row_offset, win->state.cols, win->state.rows);
307  }
308  else
309  {
310  mutt_buffer_addstr(&buf, "NONE");
311  }
312 
313  mutt_debug(LL_DEBUG1, " Window: %s\n", mutt_buffer_string(&buf));
314 
315  mutt_buffer_dealloc(&buf);
316 }
317 
319 {
320  mutt_debug(LL_DEBUG1, "\033[1;31mNotification:\033[0m %s\n", get_event_type(nc->event_type));
321 
322  switch (nc->event_type)
323  {
324  case NT_ACCOUNT:
326  break;
327  case NT_COLOR:
328  notify_dump_color(nc);
329  break;
330  case NT_COMMAND:
332  break;
333  case NT_CONFIG:
334  notify_dump_config(nc);
335  break;
336  case NT_CONTEXT:
338  break;
339  case NT_EMAIL:
340  notify_dump_email(nc);
341  break;
342  case NT_GLOBAL:
343  notify_dump_global(nc);
344  break;
345  case NT_MAILBOX:
347  break;
348  case NT_WINDOW:
349  if (nc->event_subtype == NT_WINDOW_STATE)
351  else if (nc->event_subtype == NT_WINDOW_FOCUS)
353  break;
354  default:
355  mutt_debug(LL_DEBUG1, " Event Type: %d\n", nc->event_type);
356  mutt_debug(LL_DEBUG1, " Event Sub-type: %d\n", nc->event_subtype);
357  mutt_debug(LL_DEBUG1, " Event Data: %p\n", nc->event_data);
358  break;
359  }
360 
361  mutt_debug(LL_DEBUG1, " Global Data: %p\n", nc->global_data);
362 
363  mutt_debug(LL_DEBUG5, "debug done\n");
364  return 0;
365 }
static void notify_dump_window_state(struct NotifyCallback *nc)
Definition: notify.c:252
static const char * get_event_type(enum NotifyType type)
Definition: notify.c:43
Convenience wrapper for the gui headers.
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:215
Mailbox is about to be deleted.
Definition: mailbox.h:174
An Event that happened to an Email.
Definition: email.h:155
#define WN_NARROWER
Window became narrower.
Definition: mutt_window.h:190
static void notify_dump_email(struct NotifyCallback *nc)
Definition: notify.c:227
#define NONULL(x)
Definition: string2.h:37
struct Context * ctx
The Context this Event relates to.
Definition: context.h:70
char * name
Name of Account.
Definition: account.h:39
intptr_t data
Data or flags to pass to the command.
Definition: mutt_commands.h:64
Clear the &#39;last-tagged&#39; pointer.
Definition: mailbox.h:184
Data passed to a notification function.
Definition: observer.h:39
struct MuttWindow * win
Window that changed.
Definition: mutt_window.h:217
Update internal tables.
Definition: mailbox.h:183
&#39;NNTP&#39; (Usenet) Mailbox type
Definition: mailbox.h:52
Structs that make up an email.
An Event that happened to a Colour.
Definition: color.h:122
An Event that happened to a Window.
Definition: mutt_window.h:215
struct Mailbox * mailbox
The Mailbox this Event relates to.
Definition: mailbox.h:192
The "currently-open" mailbox.
static const char * get_global_event(int id)
Definition: notify.c:97
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:61
A group of associated Mailboxes.
Definition: account.h:36
A config-change event.
Definition: subset.h:69
String manipulation buffer.
Definition: buffer.h:33
static void notify_dump_window_focus(struct NotifyCallback *nc)
Definition: notify.c:288
#define WN_WIDER
Window became wider.
Definition: mutt_window.h:189
static const char * get_mailbox_event(int id)
Definition: notify.c:125
const char * name
Name of the command.
Definition: mutt_commands.h:49
#define WN_HIDDEN
Window became hidden.
Definition: mutt_window.h:193
Window focus has changed.
Definition: mutt_window.h:209
short col_offset
Absolute on-screen column.
Definition: mutt_window.h:62
A Command has been executed, Command.
Definition: notify_type.h:40
Email list was changed.
Definition: mailbox.h:180
enum MailboxType type
Type of Mailboxes this Account contains.
Definition: account.h:38
An Event that happened to a Mailbox.
Definition: mailbox.h:190
struct Email ** emails
Definition: email.h:158
WindowNotifyFlags flags
Attributes of Window that changed.
Definition: mutt_window.h:218
MuttWindow has changed, NotifyWindow, EventWindow.
Definition: notify_type.h:53
static void notify_dump_color(struct NotifyCallback *nc)
Definition: notify.c:168
An Event that happened to an Account.
Definition: account.h:78
Convenience wrapper for the config headers.
static void notify_dump_config(struct NotifyCallback *nc)
Definition: notify.c:205
NotifyType
Notification Types.
Definition: notify_type.h:31
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:43
enum NotifyType event_type
Send: Event type, e.g. NT_ACCOUNT.
Definition: observer.h:42
int num_emails
Definition: email.h:157
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
Mailbox has been added.
Definition: mailbox.h:173
struct HashElem * he
Config item that changed.
Definition: subset.h:73
static void notify_dump_context(struct NotifyCallback *nc)
Definition: notify.c:216
struct Mailbox * mailbox
Definition: context.h:49
Convenience wrapper for the core headers.
const struct Mapping ComposeColorFields[]
Definition: color.c:203
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:294
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:203
int debug_all_observer(struct NotifyCallback *nc)
Definition: notify.c:318
void * global_data
Data from notify_observer_add()
Definition: observer.h:45
&#39;Maildir&#39; Mailbox type
Definition: mailbox.h:51
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
Window state has changed, e.g. WN_VISIBLE.
Definition: mutt_window.h:207
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:60
Color has been set.
Definition: color.h:136
Email list needs resorting.
Definition: mailbox.h:181
Colour has changed, NotifyColor, EventColor.
Definition: notify_type.h:39
static const char * get_config_type(int id)
Definition: notify.c:112
The Context has been opened.
Definition: context.h:60
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:127
#define WN_TALLER
Window became taller.
Definition: mutt_window.h:187
&#39;POP3&#39; Mailbox type
Definition: mailbox.h:55
short row_offset
Absolute on-screen row.
Definition: mutt_window.h:63
A mailbox.
Definition: mailbox.h:81
static void notify_dump_command(struct NotifyCallback *nc)
Definition: notify.c:195
short rows
Number of rows, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:61
uint8_t WindowNotifyFlags
Changes to a MuttWindow.
Definition: mutt_window.h:185
The Context is about to be destroyed.
Definition: context.h:61
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:34
#define WN_SHORTER
Window became shorter.
Definition: mutt_window.h:188
&#39;mmdf&#39; Mailbox type
Definition: mailbox.h:49
Not object-related, NotifyGlobal.
Definition: notify_type.h:45
Definitions of NeoMutt commands.
enum ColorId color
Definition: color.h:124
struct WindowState old
Previous state of the Window.
Definition: mutt_window.h:128
Compressed file Mailbox type.
Definition: mailbox.h:56
&#39;MH&#39; Mailbox type
Definition: mailbox.h:50
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Config item has been set.
Definition: subset.h:62
void * event_data
Data from notify_send()
Definition: observer.h:44
&#39;Notmuch&#39; (virtual) Mailbox type
Definition: mailbox.h:54
&#39;mbox&#39; Mailbox type
Definition: mailbox.h:48
const char * get_mailbox_type(enum MailboxType type)
Definition: notify.c:70
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:42
Context has changed, NotifyContext, EventContext.
Definition: notify_type.h:43
MailboxType
Supported mailbox formats.
Definition: mailbox.h:43
NeoMutt is initialised.
Definition: neomutt.h:50
Log at debug level 1.
Definition: logging.h:40
#define WN_VISIBLE
Window became visible.
Definition: mutt_window.h:192
NeoMutt is about to close.
Definition: neomutt.h:51
A timer has elapsed.
Definition: neomutt.h:52
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:48
Mapping between user-readable string and a constant.
Definition: mapping.h:31
Config item has been reset to initial, or parent, value.
Definition: subset.h:63
static const char * get_context(int id)
Definition: notify.c:146
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: set.c:618
An Event that happened to an Context.
Definition: context.h:68
Log at debug level 5.
Definition: logging.h:44
Convenience wrapper for the library headers.
const char * mutt_window_win_name(const struct MuttWindow *win)
Get the name of a Window.
Definition: mutt_window.c:731
static void notify_dump_global(struct NotifyCallback *nc)
Definition: notify.c:238
&#39;IMAP&#39; Mailbox type
Definition: mailbox.h:53
const struct ConfigSubset * sub
Config Subset.
Definition: subset.h:71
struct Account * account
The Account this Event relates to.
Definition: account.h:80
struct MuttWindow * dialog_find(struct MuttWindow *win)
Find the parent Dialog of a Window.
Definition: dialog.c:85
const char * name
Name of config item that changed.
Definition: subset.h:72
#define WN_MOVED
Window moved.
Definition: mutt_window.h:191
const struct Mapping ColorFields[]
Definition: color.c:156
static void notify_dump_mailbox(struct NotifyCallback *nc)
Definition: notify.c:243
static void notify_dump_account(struct NotifyCallback *nc)
Definition: notify.c:159