Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
System notification via event notification system
Introduces `devel_new_mail_command` config option. When used instead of `new_mail_command`, Neomutt's event notification system will be used for new mail notifications. This is part of a bigger refactoring of the legacy notification approach. The initial implementation uses only very limited set of expandos.
- Loading branch information
Showing
14 changed files
with
295 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @file | ||
* New mail notification observer. | ||
* | ||
* @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/>. | ||
*/ | ||
|
||
#ifndef MUTT_NEW_MAIL_H | ||
#define MUTT_NEW_MAIL_H | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include "mutt/notify.h" | ||
#include "format_flags.h" | ||
|
||
typedef int(Execute)(const char *cmd); | ||
|
||
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); | ||
int handle_new_mail_event(const char *cmd, struct NotifyCallback *nc, Execute *execute); | ||
int new_mail_observer(struct NotifyCallback *nc); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @file | ||
* Test code for new_mail_format_str() | ||
* | ||
* @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/>. | ||
*/ | ||
|
||
#include "mutt/string2.h" | ||
#define TEST_NO_MAIN | ||
#include "config.h" | ||
#include "acutest.h" | ||
#include "mutt/lib.h" | ||
#include "core/lib.h" | ||
|
||
void test_new_mail_format_str(void) | ||
{ | ||
// 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); | ||
|
||
char buf[64]; | ||
size_t col = 0; | ||
int cols = 64; | ||
struct Mailbox *mailbox = mailbox_new(); | ||
mailbox->name = mutt_str_dup("MailBox"); | ||
mailbox->pathbuf = mutt_buffer_make(16); | ||
mutt_buffer_strcpy(&mailbox->pathbuf, "/path"); | ||
intptr_t data = (intptr_t) mailbox; | ||
|
||
new_mail_format_str((char *) buf, 64, col, cols, 'n', NULL, NULL, NULL, NULL, data, 0); | ||
TEST_CHECK(mutt_str_equal(buf, "MailBox")); | ||
TEST_MSG("Check failed: %s != MailBox", buf); | ||
|
||
new_mail_format_str((char *) buf, 64, col, cols, 'f', NULL, NULL, NULL, NULL, data, 0); | ||
TEST_CHECK(mutt_str_equal(buf, "/path")); | ||
TEST_MSG("Check failed: %s != /path", buf); | ||
|
||
mailbox_free(&mailbox); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @file | ||
* Test code for new_mail_observer() | ||
* | ||
* @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/>. | ||
*/ | ||
|
||
#include "mutt/notify.h" | ||
#include "mutt/observer.h" | ||
#include "mutt/string2.h" | ||
#define TEST_NO_MAIN | ||
#include "config.h" | ||
#include "acutest.h" | ||
#include "mutt/lib.h" | ||
#include "core/lib.h" | ||
|
||
static char *message = NULL; | ||
|
||
int dummy_execute_cmd(const char *cmd) | ||
{ | ||
message = mutt_str_dup(cmd); | ||
return 0; | ||
} | ||
|
||
int dummy_new_mail_observer(struct NotifyCallback *nc) | ||
{ | ||
return handle_new_mail_event("New messages", nc, dummy_execute_cmd); | ||
} | ||
|
||
void test_new_mail_observer(void) | ||
{ | ||
struct Notify *notify = notify_new(); | ||
notify_observer_add(notify, NT_MAILBOX, dummy_new_mail_observer, NULL); | ||
struct Mailbox *mailbox = mailbox_new(); | ||
mailbox->name = mutt_str_dup("Mailbox"); | ||
struct EventMailbox event; | ||
event.mailbox = mailbox; | ||
|
||
notify_send(notify, NT_MAILBOX, NT_MAILBOX_NEW_MAIL, NULL); | ||
TEST_CHECK(message == NULL); | ||
|
||
notify_send(notify, NT_MAILBOX, NT_MAILBOX_NEW_MAIL, &event); | ||
TEST_CHECK(message != NULL); | ||
TEST_CHECK(mutt_str_equal(message, "New messages")); | ||
TEST_MSG("Check failed: \"%s\" != \"New messages\"", message); | ||
|
||
notify_free(¬ify); | ||
mailbox_free(&mailbox); | ||
FREE(&message); | ||
} |
Oops, something went wrong.