-
-
Notifications
You must be signed in to change notification settings - Fork 304
/
new_mail.c
99 lines (90 loc) · 3.02 KB
/
new_mail.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @file
* New mail notification observer implementation.
*
* @authors
* Copyright (C) 2022 Michal Siedlaczek <michal@siedlaczek.me>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page new_mail Notify about new incoming mail
*
* Use an external command to send a system notification when new mail arrives.
*/
#include "config/lib.h"
#include "new_mail.h"
#include "mutt_mailbox.h"
#include "muttlib.h"
#include "protos.h"
/**
* new_mail_format_str - Format a string for the new mail notification.
* @param[out] buf Buffer in which to save string
* @param[in] buflen Buffer length
* @param[in] col Starting column
* @param[in] cols Number of screen columns
* @param[in] src Printf-like format string
* @param[in] prec Field precision, e.g. "-3.4"
* ############# TODO #############
*
* | Expando | Description
* | :------ | :-------------------------------------------------------
* | \%n | Folder name
* | \%f | Folder path
*/
const char *new_mail_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)
{
struct Mailbox *mailbox = (struct Mailbox *) data;
switch (op)
{
case 'n':
snprintf(buf, buflen, "%s", NONULL(mailbox->name));
break;
case 'f':
snprintf(buf, buflen, "%s", NONULL(mailbox_path(mailbox)));
break;
}
return src;
}
int handle_new_mail_event(const char *cmd, struct NotifyCallback *nc, Execute *execute)
{
if (nc->event_subtype != NT_MAILBOX_NEW_MAIL)
return 0;
struct EventMailbox *ev_m = nc->event_data;
if (!ev_m || !ev_m->mailbox)
return 0;
struct Mailbox *mailbox = ev_m->mailbox;
char expanded_cmd[1024];
mutt_expando_format(expanded_cmd, 1024, 0, 1024, cmd, new_mail_format_str,
(intptr_t) mailbox, MUTT_FORMAT_NO_FLAGS);
execute(expanded_cmd);
return 0;
}
int execute_cmd(const char *cmd)
{
if (mutt_system(cmd) != 0)
mutt_error(_("Error running \"%s\""), cmd);
return 0;
}
int new_mail_observer(struct NotifyCallback *nc)
{
const char *c_devel_new_mail_command = cs_subset_string(NeoMutt->sub, "devel_new_mail_command");
if (!c_devel_new_mail_command)
return 0;
return handle_new_mail_event(c_devel_new_mail_command, nc, execute_cmd);
}