Debian Codegraph @

tools/notify-send.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2004 Christian Hammond.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 #include "config.h"
22
23 #include <libnotify/notify.h>
24 #include <locale.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <glib-unix.h>
30 #include <glib/gprintf.h>
31
32 #define N_(x) (x)
33 #define GETTEXT_PACKAGE NULL
34
35 static NotifyUrgency urgency = NOTIFY_URGENCY_NORMAL;
36 static GMainLoop *loop = NULL;
37
38 static gboolean
39 g_option_arg_urgency_cb (const char *option_name,
40 const char *value,
41 gpointer data,
42 GError **error)
43 {
44 if (value != NULL) {
45 if (!strcasecmp (value, "low"))
46 urgency = NOTIFY_URGENCY_LOW;
47 else if (!strcasecmp (value, "normal"))
48 urgency = NOTIFY_URGENCY_NORMAL;
49 else if (!strcasecmp (value, "critical"))
50 urgency = NOTIFY_URGENCY_CRITICAL;
51 else {
52 *error = g_error_new (G_OPTION_ERROR,
53 G_OPTION_ERROR_BAD_VALUE,
54 N_("Unknown urgency %s specified. "
55 "Known urgency levels: low, "
56 "normal, critical."), value);
57
58 return FALSE;
59 }
60 }
61
62 return TRUE;
63 }
64
65 static gboolean
66 notify_notification_set_hint_variant (NotifyNotification *notification,
67 const char *type,
68 const char *key,
69 const char *value,
70 GError **error)
71 {
72 static gboolean conv_error = FALSE;
73 if (!strcasecmp (type, "string")) {
74 notify_notification_set_hint_string (notification,
75 key,
76 value);
77 } else if (!strcasecmp (type, "int")) {
78 if (!g_ascii_isdigit (*value))
79 conv_error = TRUE;
80 else {
81 gint h_int = (gint) g_ascii_strtoull (value, NULL, 10);
82 notify_notification_set_hint_int32 (notification,
83 key,
84 h_int);
85 }
86 } else if (!strcasecmp (type, "double")) {
87 if (!g_ascii_isdigit (*value))
88 conv_error = TRUE;
89 else {
90 gdouble h_double = g_strtod (value, NULL);
91 notify_notification_set_hint_double (notification,
92 key, h_double);
93 }
94 } else if (!strcasecmp (type, "byte")) {
95 int base = g_str_has_prefix (value, "0x") ? 16 : 10;
96 gint h_byte = (gint) g_ascii_strtoull (value, NULL, base);
97
98 if (h_byte < 0 || h_byte > 0xFF)
99 conv_error = TRUE;
100 else {
101 notify_notification_set_hint_byte (notification,
102 key,
103 (guchar) h_byte);
104 }
105 } else if (g_ascii_strcasecmp (type, "boolean") == 0) {
106 gboolean h_boolean = FALSE;
107
108 if (g_ascii_strcasecmp (value, "true") == 0) {
109 h_boolean = TRUE;
110 } else if (g_ascii_isdigit (*value)) {
111 h_boolean = !!g_ascii_strtoull (value, NULL, 10);
112 }
113
114 notify_notification_set_hint (notification, key,
115 g_variant_new_boolean (h_boolean));
116 } else if (g_ascii_strcasecmp (type, "variant") == 0) {
117 GVariant *variant = g_variant_parse (NULL, value, NULL, NULL, NULL);
118
119 if (variant != NULL) {
120 notify_notification_set_hint (notification, key, variant);
121 } else {
122 conv_error = TRUE;
123 }
124
125 } else {
126 *error = g_error_new (G_OPTION_ERROR,
127 G_OPTION_ERROR_BAD_VALUE,
128 N_("Invalid hint type \"%s\". Valid types "
129 "are int, double, string and byte."),
130 type);
131 return FALSE;
132 }
133
134 if (conv_error) {
135 *error = g_error_new (G_OPTION_ERROR,
136 G_OPTION_ERROR_BAD_VALUE,
137 N_("Value \"%s\" of hint \"%s\" could not be "
138 "parsed as type \"%s\"."), value, key,
139 type);
140 return FALSE;
141 }
142
143 return TRUE;
144 }
145
146 static void
147 handle_closed (NotifyNotification *notify,
148 gpointer user_data)
149 {
150 g_main_loop_quit (loop);
151 }
152
153 static gboolean
154 on_sigint (gpointer data)
155 {
156 NotifyNotification *notification = data;
157
158 g_printerr ("%s\n", N_("Wait cancelled, closing notification"));
159
160 notify_notification_close (notification, NULL);
161 g_main_loop_quit (loop);
162
163 return FALSE;
164 }
165
166 typedef struct _ActionData {
167 int selected_action_fd;
168 int activation_token_fd;
169 } ActionData;
170
171 static void
172 handle_action (NotifyNotification *notify,
173 char *action,
174 gpointer user_data)
175 {
176 ActionData *action_data = user_data;
177 g_autoptr(GAppLaunchContext) launch_context = NULL;
178
179 launch_context = notify_notification_get_activation_app_launch_context (notify);
180
181 g_printf ("%s\n", action);
182
183 if (action_data->selected_action_fd >= 0) {
184 write (action_data->selected_action_fd, action, strlen (action));
185 write (action_data->selected_action_fd, "\n", 1);
186 fsync (action_data->selected_action_fd);
187 }
188
189 if (launch_context) {
190 g_autofree char *activation_token = NULL;
191
192 activation_token =
193 g_app_launch_context_get_startup_notify_id (launch_context,
194 NULL, NULL);
195 g_debug ("Activation Token: %s", activation_token);
196
197 if (action_data->activation_token_fd) {
198 write (action_data->activation_token_fd,
199 activation_token, strlen (activation_token));
200 }
201 }
202
203 if (action_data->activation_token_fd) {
204 write (action_data->activation_token_fd, "\n", 1);
205 fsync (action_data->activation_token_fd);
206 }
207
208 notify_notification_close (notify, NULL);
209 }
210
211 static gboolean
212 on_wait_timeout (gpointer data)
213 {
214 g_printerr ("%s\n", N_("Wait timeout expired"));
215 g_main_loop_quit (loop);
216
217 return FALSE;
218 }
219
220 static gboolean
221 server_has_capability (const gchar *capability)
222 {
223 GList *server_caps = notify_get_server_caps ();
224 gboolean supported;
225
226 supported = !!g_list_find_custom (server_caps,
227 capability,
228 (GCompareFunc) g_ascii_strcasecmp);
229
230 g_list_foreach (server_caps, (GFunc) g_free, NULL);
231 g_list_free (server_caps);
232
233 return supported;
234 }
235
236 /* The XDG Desktop Notifications Specification requires valid UTF-8 for certain
237 * strings. Given the stability/security implications by accepting console
238 * input, we will insist upon valid UTF-8 being provided for these strings, and
239 * print an error message otherwise.
240 */
241 static void
242 validate_utf8_or_die (const char *str, const char *param)
243 {
244 if (! g_utf8_validate (str, -1, NULL))
245 {
246 /* Translators: the parameter is the 'Component' as defined by the
247 * XDG notification specification (eg, 'Summary', 'Body', etc.) -
248 * these are separately translated.
249 */
250 g_printerr (N_("Invalid UTF-8 provided for parameter: %s\n"), param);
251 exit (1);
252 }
253 }
254
255 int
256 main (int argc, char *argv[])
257 {
258 static const char *summary = NULL;
259 char *body;
260 static const char *type = NULL;
261 static char *app_name = NULL;
262 static char *app_icon = NULL;
263 static char *icon_str = NULL;
264 static char **n_text = NULL;
265 static char **hints = NULL;
266 static char **actions = NULL;
267 static char *server_name = NULL;
268 static char *server_vendor = NULL;
269 static char *server_version = NULL;
270 static char *server_spec_version = NULL;
271 static int id_fd = -1;
272 static int selected_action_fd = -1;
273 static int activation_token_fd = -1;
274 static gboolean print_id = FALSE;
275 static gint notification_id = 0;
276 static gboolean do_version = FALSE;
277 static gboolean hint_error = FALSE, show_error = FALSE;
278 static gboolean transient = FALSE;
279 static gboolean wait = FALSE;
280 static int expire_timeout = NOTIFY_EXPIRES_DEFAULT;
281 GOptionContext *opt_ctx;
282 NotifyNotification *notify;
283 GError *error = NULL;
284 gboolean retval;
285
286 static const GOptionEntry entries[] = {
287 {"urgency", 'u', 0, G_OPTION_ARG_CALLBACK,
288 g_option_arg_urgency_cb,
289 N_("Specifies the urgency level (low, normal, critical)."),
290 N_("LEVEL")},
291 {"expire-time", 't', 0, G_OPTION_ARG_INT, &expire_timeout,
292 N_
293 ("Specifies the timeout in milliseconds at which to expire the "
294 "notification."), N_("TIME")},
295 {"app-name", 'a', 0, G_OPTION_ARG_STRING, &app_name,
296 N_("Specifies the app name for the notification"), N_("APP_NAME")},
297 {"icon", 'i', 0, G_OPTION_ARG_FILENAME, &icon_str,
298 N_("Specifies an icon filename or stock icon to display."),
299 N_("ICON")},
300 {"app-icon", 'n', 0, G_OPTION_ARG_FILENAME, &app_icon,
301 N_("Specifies an application icon filename or app icon name. The server may or may not display it."),
302 N_("ICON")},
303 {"category", 'c', 0, G_OPTION_ARG_FILENAME, &type,
304 N_("Specifies the notification category."),
305 N_("TYPE[,TYPE...]")},
306 {"transient", 'e', 0, G_OPTION_ARG_NONE, &transient,
307 N_("Create a transient notification"),
308 NULL},
309 {"hint", 'h', 0, G_OPTION_ARG_FILENAME_ARRAY, &hints,
310 N_
311 ("Specifies basic extra data to pass. Valid types are boolean, int, double, string, byte and variant."),
312 N_("TYPE:NAME:VALUE")},
313 {"print-id", 'p', 0, G_OPTION_ARG_NONE, &print_id,
314 N_ ("Print the notification ID."), NULL},
315 {"id-fd", 0, 0, G_OPTION_ARG_INT, &id_fd,
316 N_ ("File descriptor where to write the notification ID."), NULL},
317 {"replace-id", 'r', 0, G_OPTION_ARG_INT, &notification_id,
318 N_ ("The ID of the notification to replace."), N_("REPLACE_ID")},
319 {"wait", 'w', 0, G_OPTION_ARG_NONE, &wait,
320 N_("Wait for the notification to be closed before exiting."),
321 NULL},
322 {"action", 'A', 0, G_OPTION_ARG_FILENAME_ARRAY, &actions,
323 N_
324 ("Specifies the actions to display to the user. Implies --wait to wait for user input."
325 " May be set multiple times. The name of the action is output to stdout. If NAME is "
326 "not specified, the numerical index of the option is used (starting with 0)."),
327 N_("[NAME=]Text...")},
328 {"selected-action-fd", 0, 0, G_OPTION_ARG_INT, &selected_action_fd,
329 N_ ("File descriptor where to write the action chosen by the user."), NULL},
330 {"activation-token-fd", 0, 0, G_OPTION_ARG_INT, &activation_token_fd ,
331 N_ ("File descriptor where to write the action activation token. The daemon must support it."), NULL},
332 {"version", 'v', 0, G_OPTION_ARG_NONE, &do_version,
333 N_("Version of the package."),
334 NULL},
335 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
336 &n_text, NULL,
337 NULL},
338 {NULL}
339 };
340
341 body = NULL;
342
343 setlocale (LC_ALL, "");
344
345 g_set_prgname (argv[0]);
346 g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
347
348 opt_ctx = g_option_context_new (N_("<SUMMARY> [BODY] - "
349 "create a notification"));
350 g_option_context_add_main_entries (opt_ctx, entries, GETTEXT_PACKAGE);
351 retval = g_option_context_parse (opt_ctx, &argc, &argv, &error);
352 g_option_context_free (opt_ctx);
353
354 if (!retval) {
355 g_printerr ("%s\n", error->message);
356 g_error_free (error);
357 exit (1);
358 }
359
360 if (do_version) {
361 g_printf ("%s %s\n", g_get_prgname (), VERSION);
362 exit (0);
363 }
364
365 if (n_text != NULL && n_text[0] != NULL)
366 {
367 summary = n_text[0];
368 /* Translators: XDG notification component to be translated
369 * consistently as within the specification.
370 */
371 validate_utf8_or_die (summary, N_("Summary"));
372 }
373
374 if (summary == NULL) {
375 g_printerr ("%s\n", N_("No summary specified."));
376 exit (1);
377 }
378
379 if (n_text[1] != NULL) {
380 body = g_strcompress (n_text[1]);
381
382 /* Translators: XDG notification component to be translated
383 * consistently as within the specification.
384 */
385 validate_utf8_or_die (body, N_("Body"));
386
387 if (n_text[2] != NULL) {
388 g_printerr ("%s\n", N_("Invalid number of options."));
389 exit (1);
390 }
391 }
392
393 if (!notify_init ("notify-send"))
394 exit (1);
395
396 notify_get_server_info (&server_name,
397 &server_vendor,
398 &server_version,
399 &server_spec_version);
400
401 g_debug ("Using server %s %s, v%s - Supporting Notification Spec %s",
402 server_name, server_vendor, server_version, server_spec_version);
403 g_free (server_name);
404 g_free (server_vendor);
405 g_free (server_version);
406 g_free (server_spec_version);
407
408 notify = g_object_new (NOTIFY_TYPE_NOTIFICATION,
409 "summary", summary,
410 "body", body,
411 "app-icon", app_icon,
412 "icon-name", icon_str,
413 "id", notification_id,
414 NULL);
415
416 notify_notification_set_category (notify, type);
417 notify_notification_set_urgency (notify, urgency);
418 notify_notification_set_timeout (notify, expire_timeout);
419 notify_notification_set_app_name (notify, app_name);
420
421 if (transient) {
422 notify_notification_set_hint (notify,
423 NOTIFY_NOTIFICATION_HINT_TRANSIENT,
424 g_variant_new_boolean (TRUE));
425
426 if (!server_has_capability ("persistence")) {
427 g_debug ("Persistence is not supported by the "
428 "notifications server. "
429 "All notifications are transient.");
430 }
431 }
432
433 g_free (body);
434
435 /* Set hints */
436 if (hints != NULL) {
437 gint i = 0;
438 gint l;
439 char *hint = NULL;
440 char **tokens = NULL;
441
442 while ((hint = hints[i++])) {
443 tokens = g_strsplit (hint, ":", 3);
444 l = g_strv_length (tokens);
445
446 if (l != 3) {
447 g_printerr ("%s\n",
448 N_("Invalid hint syntax specified. "
449 "Use TYPE:NAME:VALUE."));
450 hint_error = TRUE;
451 } else {
452 retval = notify_notification_set_hint_variant (notify,
453 tokens[0],
454 tokens[1],
455 tokens[2],
456 &error);
457
458 if (!retval) {
459 g_printerr ("%s\n", error->message);
460 g_clear_error (&error);
461 hint_error = TRUE;
462 }
463 }
464
465 g_strfreev (tokens);
466 if (hint_error)
467 break;
468 }
469 }
470
471 if (actions != NULL) {
472 gint i = 0;
473 char *action = NULL;
474 gchar **spl = NULL;
475 gboolean have_actions;
476
477 have_actions = server_has_capability ("actions");
478 if (!have_actions) {
479 g_printerr (N_("Actions are not supported by this "
480 "notifications server. "
481 "Displaying non-interactively.\n"));
482 show_error = TRUE;
483 }
484
485 while (have_actions && (action = actions[i++])) {
486 g_autofree char *name = NULL;
487 const gchar *label;
488
489 spl = g_strsplit (action, "=", 2);
490
491 if (g_strv_length (spl) == 1) {
492 name = g_strdup_printf ("%d", i - 1);
493 label = g_strstrip (spl[0]);
494 } else {
495 name = g_strdup (g_strstrip (spl[0]));
496 label = g_strstrip (spl[1]);
497 }
498
499 if (*label != '\0' && *name != '\0') {
500 g_autofree ActionData *action_data = NULL;
501
502 action_data = g_new0 (ActionData, 1);
503 action_data->selected_action_fd = selected_action_fd;
504 action_data->activation_token_fd = activation_token_fd;
505
506 notify_notification_add_action (notify,
507 name,
508 label,
509 handle_action,
510 g_steal_pointer (&action_data),
511 g_free);
512 wait = TRUE;
513 }
514
515 g_strfreev (spl);
516 }
517
518 g_strfreev (actions);
519 }
520
521 if (wait) {
522 g_signal_connect (G_OBJECT (notify),
523 "closed",
524 G_CALLBACK (handle_closed),
525 NULL);
526
527 if (expire_timeout != NOTIFY_EXPIRES_NEVER) {
528 g_timeout_add (expire_timeout, on_wait_timeout, NULL);
529 }
530 }
531
532 if (!hint_error) {
533 retval = notify_notification_show (notify, &error);
534
535 if (!retval) {
536 g_autofree char *msg = NULL;
537
538 msg = g_strdup_printf ("Failed to show notification: %s",
539 error->message);
540 g_printerr ("%s\n", msg);
541 g_clear_error (&error);
542 show_error = TRUE;
543 }
544 }
545
546 if (print_id) {
547 g_object_get (notify, "id", &notification_id, NULL);
548 g_printf ("%d\n", notification_id);
549 fflush (stdout);
550 }
551
552 if (id_fd >= 0) {
553 g_autofree char *id_str = NULL;
554
555 g_object_get (notify, "id", &notification_id, NULL);
556 id_str = g_strdup_printf ("%d\n", notification_id);
557 write (id_fd, id_str, strlen (id_str));
558 fsync (id_fd);
559 }
560
561 if (wait) {
562 g_unix_signal_add (SIGINT, on_sigint, notify);
563 loop = g_main_loop_new (NULL, FALSE);
564 g_main_loop_run (loop);
565 g_main_loop_unref (loop);
566 loop = NULL;
567 }
568
569 g_object_unref (G_OBJECT (notify));
570
571 notify_uninit ();
572
573 if (hint_error || show_error)
574 exit (1);
575
576 return 0;
577 }