2 * Copyright (C) 2017 Andy Spencer <andy753421@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #define _POSIX_C_SOURCE 200112L
31 static int reminder = 15*60;
32 static char *command = NULL;
39 static void date_str(char *str, size_t size, date_t *date)
41 snprintf(str, size, "%04d-%02d-%02d %02d:%02d:%02d",
42 date->year+1, date->month+1, date->day,
43 date->hour, date->min, date->sec);
46 static void notify(event_t *event)
48 static char start_str[32];
49 static char end_str[32];
51 date_t start = event->start;
52 date_t end = event->end;
53 wday_t wday = day_of_week(start.year, start.month, start.day);
56 date_str(start_str, sizeof(start_str), &event->start);
57 date_str(end_str, sizeof(end_str), &event->end);
59 setenv("EVENT_NAME", event->name ?: "", 1);
60 setenv("EVENT_DESC", event->desc ?: "", 1);
61 setenv("EVENT_LOC", event->loc ?: "", 1);
62 setenv("EVENT_START", start_str, 1);
63 setenv("EVENT_END", end_str, 1);
67 unsetenv("EVENT_NAME");
68 unsetenv("EVENT_DESC");
69 unsetenv("EVENT_LOC");
70 unsetenv("EVENT_START");
71 unsetenv("EVENT_END");
73 printf("%s, %s %d, %d, %02d:%02d-%02d:%02d\n",
75 month_to_string(start.month),
78 start.hour, start.min,
82 printf(" Name: %s\n", event->name);
84 printf(" Location: %s\n", event->loc);
86 printf(" Details: %s\n", event->desc);
94 debug("Polling: %02dh %02dm %02ds",
95 NOW.hour, NOW.min, NOW.sec);
101 /* Print notifications */
102 for (event_t *cur = EVENTS; cur; cur = cur->next) {
103 if (compare(&cur->start, &old) < 0)
106 int old_until = get_secs(&old, &cur->start);
107 int new_until = get_secs(&new, &cur->start);
109 if (old_until > reminder && new_until <= reminder) {
110 debug("Notify: %s", cur->name);
113 if (new_until > reminder) {
114 delay = new_until - reminder;
124 void daemon_init(void)
130 void daemon_main(void)
141 void daemon_exit(void)
146 void daemon_config(const char *group, const char *name, const char *key, const char *value)
148 if (match(group, "daemon")) {
149 if (match(key, "reminder")) {
150 reminder = get_number(value);
151 } else if (match(key, "notify")) {
152 command = get_string(value);
158 void daemon_test(void)
160 static event_t event0 = {
161 .name = "test event",
162 .desc = "this event is random and does not exist",
164 static event_t event1 = {
165 .name = "test event",
166 .desc = "this event is random and does not exist",
168 static todo_t todo = {
170 .desc = "this todo is random and does not exist",
175 EVENTS->next = &event1;
184 event0.start = ((date_t){NOW.year, NOW.month, NOW.day, 12, 0});
185 event0.end = ((date_t){NOW.year, NOW.month, NOW.day, 13, 0});
186 event1.start = ((date_t){NOW.year, NOW.month, NOW.day, 14, 0});
187 event1.end = ((date_t){NOW.year, NOW.month, NOW.day, 15, 0});
188 todo.due = ((date_t){NOW.year, NOW.month, NOW.day, 16, 0});
192 daemon_config("daemon", NULL, "reminder", "15");
193 daemon_config("daemon", NULL, "notify", "lackey-notify");
196 while (NOW.day == EVENTS->start.day) {
198 delay = MIN(delay, rand() % (60*60));
199 NOW = get_date(get_stamp(NOW)+delay);