aboutsummaryrefslogtreecommitdiff
path: root/notify.c
blob: 6f294458566ec521441203ca8fd1200b03320e2d (plain)
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
100
101
102
103
104
105
106
107
108
#include "notify.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/stat.h>
#include <fcntl.h>

#define LOG_MODULE "notify"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "config.h"
#include "spawn.h"
#include "xmalloc.h"

void
notify_notify(const struct terminal *term, const char *title, const char *body)
{
    LOG_DBG("notify: title=\"%s\", msg=\"%s\"", title, msg);

    if (term->kbd_focus) {
        /* No notifications while we’re focused */
        return;
    }

    if (title == NULL || body == NULL)
        return;

    if (term->conf->notify.argv == NULL)
        return;

    size_t argv_size = 0;
    for (; term->conf->notify.argv[argv_size] != NULL; argv_size++)
        ;

#define append(s, n)                                        \
    do {                                                    \
        expanded = xrealloc(expanded, len + (n) + 1);       \
        memcpy(&expanded[len], s, n);                       \
        len += n;                                           \
        expanded[len] = '\0';                               \
    } while (0)

    char **argv = malloc((argv_size + 1) * sizeof(argv[0]));

    /* Expand ${title} and ${body} */
    for (size_t i = 0; i < argv_size; i++) {
        size_t len = 0;
        char *expanded = NULL;

        char *start = NULL;
        char *last_end = term->conf->notify.argv[i];

        while ((start = strstr(last_end, "${")) != NULL) {
            /* Append everything from the last template's end to this
             * one's beginning */
            append(last_end, start - last_end);

            /* Find end of template */
            start += 2;
            char *end = strstr(start, "}");

            if (end == NULL) {
                /* Ensure final append() copies the unclosed '${' */
                last_end = start - 2;
                LOG_WARN("notify: unclosed template: %s", last_end);
                break;
            }

            /* Expand template */
            if (strncmp(start, "title", end - start) == 0)
                append(title, strlen(title));
            else if (strncmp(start, "body", end - start) == 0)
                append(body, strlen(body));
            else {
                /* Unrecognized template - append it as-is */
                start -= 2;
                append(start, end + 1 - start);
                LOG_WARN("notify: unrecognized template: %.*s",
                         (int)(end + 1 - start), start);
            }

            last_end = end + 1;;
        }

        append(last_end, term->conf->notify.argv[i] + strlen(term->conf->notify.argv[i]) - last_end);
        argv[i] = expanded;
    }
    argv[argv_size] = NULL;

#undef append

    LOG_DBG("notify command:");
    for (size_t i = 0; i < argv_size; i++)
        LOG_DBG("  argv[%zu] = \"%s\"", i, argv[i]);

    /* Redirect stdin to /dev/null, but ignore failure to open */
    int devnull = open("/dev/null", O_RDONLY);
    spawn(term->reaper, NULL, argv, devnull, -1, -1);

    if (devnull >= 0)
        close(devnull);

    for (size_t i = 0; i < argv_size; i++)
        free(argv[i]);
    free(argv);
}