summaryrefslogtreecommitdiffstats
path: root/journal-notify.c
blob: 0f54090e17a253774de80779a20779b948c01211 (about) (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * (C) 2014 by Christian Hesse <mail@eworm.de>
 *
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>

#include <systemd/sd-journal.h>

#include <libnotify/notify.h>

#include "version.h"

const char * program = NULL;

#define OPTSTRING	"ehi:m:nr:v"
#define DEFAULTICON	"dialog-information"

int notify(const char * summary, const char * body, const char * icon) {
	NotifyNotification * notification;
	int rc = -1;

	notification =
#if NOTIFY_CHECK_VERSION(0, 7, 0)
		notify_notification_new(summary, body, icon);
#else
		notify_notification_new(summary, body, icon, NULL);
#endif

	if (notification == NULL)
		return rc;

	if (notify_notification_show(notification, NULL) == FALSE)
		goto out;

	rc = 0;

out:
	g_object_unref(G_OBJECT(notification));

	return rc;
}

int main(int argc, char **argv) {
	int i, rc = EXIT_FAILURE;
	uint8_t verbose = 0;

	uint8_t have_regex = 0;
	regex_t regex;
	int regex_flags = REG_NOSUB;

	sd_journal * journal;
	const void * data;
	size_t length;

	char * summary, * message;
	const char * icon = DEFAULTICON;

	program = argv[0];

	/* get command line options - part I
	 * just get -h (help), -e and -n (regex options) here */
	while ((i = getopt(argc, argv, OPTSTRING)) != -1) {
		switch (i) {
			case 'e':
				regex_flags |= REG_EXTENDED;
				break;
			case 'h':
				fprintf(stderr, "usage: %s [-e] [-h] [-i ICON] [-m MATCH] [-n] [-r REGEX] [-vv]\n", program);
				return EXIT_SUCCESS;
			case 'n':
				regex_flags |= REG_ICASE;
				break;
			case 'v':
				verbose++;
				break;
		}
	}

	/* say hello */
	if (verbose > 0)
		printf("%s v%s (compiled: " __DATE__ ", " __TIME__ ")\n", program, VERSION);

	/* reinitialize getopt() by resetting optind to 0 */
	optind = 0;

	/* open journal */
	if ((rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY + SD_JOURNAL_SYSTEM)) < 0) {
		fprintf(stderr, "Failed to open journal: %s\n", strerror(-rc));
		goto out10;
	}

	/* get command line options - part II*/
	while ((i = getopt(argc, argv, OPTSTRING)) != -1) {
		switch (i) {
			case 'i':
				icon = optarg;
				break;
			case 'm':
				if (verbose > 1)
					printf("Adding match '%s'...\n", optarg);

				if ((rc = sd_journal_add_match(journal, optarg, 0)) < 0) {
					fprintf(stderr, "Failed to add match '%s': %s\n", optarg, strerror(-rc));
					goto out20;
				}

				break;
			case 'r':
				if (verbose > 1)
					printf("Adding regular expression '%s'...\n", optarg);

				if (have_regex > 0) {
					fprintf(stderr, "Only one regex allowed!\n");
					rc = EXIT_FAILURE;
					goto out20;
				}

				if ((rc = regcomp(&regex, optarg, regex_flags)) != 0) {
					fprintf(stderr, "Could not compile regex\n");
					goto out20;
				}
				have_regex++;

				break;
		}
	}

	/* seek to the end of the journal */
	if ((rc = sd_journal_seek_tail(journal)) < 0) {
		fprintf(stderr, "Failed to seek to the tail: %s\n", strerror(-rc));
		goto out30;
	}

	/* we are behind the last entry, so use previous one */
	if ((rc = sd_journal_previous(journal)) < 0) {
		fprintf(stderr, "Failed to iterate to previous entry: %s\n", strerror(-rc));
		goto out30;
	}

	if (notify_init(program) == FALSE) {
		fprintf(stderr, "Failed to initialize notify.\n");
		rc = EXIT_FAILURE;
		goto out30;
	}

	while (1) {
		if ((rc = sd_journal_next(journal)) < 0) {
			fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-rc));
			goto out40;
		} else if (rc == 0) {
			if ((rc = sd_journal_wait(journal, (uint64_t) -1)) < 0) {
				fprintf(stderr, "Failed to wait for changes: %s\n", strerror(-rc));
				goto out40;
			}
			continue;
		}

		/* get MESSAGE field */
		if ((rc = sd_journal_get_data(journal, "MESSAGE", &data, &length)) < 0) {
			fprintf(stderr, "Failed to read message field: %s\n", strerror(-rc));
			continue;
		}
		message = g_markup_escape_text(data + 8, length - 8);

		/* get SYSLOG_IDENTIFIER field */
		if ((rc = sd_journal_get_data(journal, "SYSLOG_IDENTIFIER", &data, &length)) < 0) {
			fprintf(stderr, "Failed to read syslog identifier field: %s\n", strerror(-rc));
			continue;
		}
		summary = g_markup_escape_text(data + 18, length - 18);

		/* show notification */
		if (have_regex == 0 || regexec(&regex, message, 0, NULL, 0) == 0) {
			for (i = 0; i < 3; i++) {
				if (verbose > 0)
					printf("Showing notification: %s: %s\n", summary, message);

				if ((rc = notify(summary, message, icon)) == 0)
					break;

				fprintf(stderr, "Failed to show notification, reinitializing libnotify.\n");
				notify_uninit();
				usleep(500 * 1000);
				if (notify_init(program) == FALSE) {
					fprintf(stderr, "Failed to initialize notify.\n");
					rc = EXIT_FAILURE;
				}
			}
			if (rc != 0)
				goto out40;
		}

		free(summary);
		free(message);
	}

	rc = EXIT_SUCCESS;

out40:
	notify_uninit();

out30:
	if (have_regex > 0)
		regfree(&regex);

out20:
	sd_journal_close(journal);

out10:
	return rc;
}

// vim: set syntax=c: