2 * RSS notifier for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups.h>
16 #include <cups/language.h>
17 #include <cups/string-private.h>
18 #include <cups/array.h>
19 #include <sys/select.h>
20 #include <cups/ipp-private.h> /* TODO: Update so we don't need this */
27 typedef struct _cups_rss_s /**** RSS message data ****/
29 int sequence_number; /* notify-sequence-number */
30 char *subject, /* Message subject/summary */
31 *text, /* Message text */
32 *link_url; /* Link to printer */
33 time_t event_time; /* When the event occurred */
41 static char *rss_password; /* Password for remote RSS */
48 static int compare_rss(_cups_rss_t *a, _cups_rss_t *b);
49 static void delete_message(_cups_rss_t *rss);
50 static void load_rss(cups_array_t *rss, const char *filename);
51 static _cups_rss_t *new_message(int sequence_number, char *subject,
52 char *text, char *link_url,
54 static const char *password_cb(const char *prompt);
55 static int save_rss(cups_array_t *rss, const char *filename,
57 static char *xml_escape(const char *s);
61 * 'main()' - Main entry for the test notifier.
64 int /* O - Exit status */
65 main(int argc, /* I - Number of command-line arguments */
66 char *argv[]) /* I - Command-line arguments */
68 int i; /* Looping var */
69 ipp_t *event; /* Event from scheduler */
70 ipp_state_t state; /* IPP event state */
71 char scheme[32], /* URI scheme ("rss") */
72 username[256], /* Username for remote RSS */
73 host[1024], /* Hostname for remote RSS */
74 resource[1024], /* RSS file */
75 *options; /* Options */
76 int port, /* Port number for remote RSS */
77 max_events; /* Maximum number of events */
78 http_t *http; /* Connection to remote server */
79 http_status_t status; /* HTTP GET/PUT status code */
80 char filename[1024], /* Local filename */
81 newname[1024]; /* filename.N */
82 cups_lang_t *language; /* Language information */
83 ipp_attribute_t *printer_up_time, /* Timestamp on event */
84 *notify_sequence_number,/* Sequence number */
85 *notify_printer_uri; /* Printer URI */
86 char *subject, /* Subject for notification message */
87 *text, /* Text for notification message */
88 link_url[1024], /* Link to printer */
89 link_scheme[32], /* Scheme for link */
90 link_username[256], /* Username for link */
91 link_host[1024], /* Host for link */
92 link_resource[1024]; /* Resource for link */
93 int link_port; /* Link port */
94 cups_array_t *rss; /* RSS message array */
95 _cups_rss_t *msg; /* RSS message */
96 char baseurl[1024]; /* Base URL */
97 fd_set input; /* Input set for select() */
98 struct timeval timeout; /* Timeout for select() */
99 int changed; /* Has the RSS data changed? */
100 int exit_status; /* Exit status */
103 fprintf(stderr, "DEBUG: argc=%d\n", argc);
104 for (i = 0; i < argc; i ++)
105 fprintf(stderr, "DEBUG: argv[%d]=\"%s\"\n", i, argv[i]);
108 * See whether we are publishing this RSS feed locally or remotely...
111 if (httpSeparateURI(HTTP_URI_CODING_ALL, argv[1], scheme, sizeof(scheme),
112 username, sizeof(username), host, sizeof(host), &port,
113 resource, sizeof(resource)) < HTTP_URI_OK)
115 fprintf(stderr, "ERROR: Bad RSS URI \"%s\"!\n", argv[1]);
121 if ((options = strchr(resource, '?')) != NULL)
125 if (!strncmp(options, "max_events=", 11))
127 max_events = atoi(options + 11);
134 rss = cupsArrayNew((cups_array_func_t)compare_rss, NULL);
139 * Remote feed, see if we can get the current file...
142 int fd; /* Temporary file */
145 if ((rss_password = strchr(username, ':')) != NULL)
146 *rss_password++ = '\0';
148 cupsSetPasswordCB(password_cb);
149 cupsSetUser(username);
151 if ((fd = cupsTempFd(filename, sizeof(filename))) < 0)
153 fprintf(stderr, "ERROR: Unable to create temporary file: %s\n",
159 if ((http = httpConnect(host, port)) == NULL)
161 fprintf(stderr, "ERROR: Unable to connect to %s on port %d: %s\n",
162 host, port, strerror(errno));
170 status = cupsGetFd(http, resource, fd);
174 if (status != HTTP_OK && status != HTTP_NOT_FOUND)
176 fprintf(stderr, "ERROR: Unable to GET %s from %s on port %d: %d %s\n",
177 resource, host, port, status, httpStatus(status));
185 strlcpy(newname, filename, sizeof(newname));
187 httpAssembleURI(HTTP_URI_CODING_ALL, baseurl, sizeof(baseurl), "http",
188 NULL, host, port, resource);
192 const char *cachedir, /* CUPS_CACHEDIR */
193 *server_name, /* SERVER_NAME */
194 *server_port; /* SERVER_PORT */
199 if ((cachedir = getenv("CUPS_CACHEDIR")) == NULL)
200 cachedir = CUPS_CACHEDIR;
202 if ((server_name = getenv("SERVER_NAME")) == NULL)
203 server_name = "localhost";
205 if ((server_port = getenv("SERVER_PORT")) == NULL)
208 snprintf(filename, sizeof(filename), "%s/rss%s", cachedir, resource);
209 snprintf(newname, sizeof(newname), "%s.N", filename);
211 httpAssembleURIf(HTTP_URI_CODING_ALL, baseurl, sizeof(baseurl), "http",
212 NULL, server_name, atoi(server_port), "/rss%s", resource);
216 * Load the previous RSS file, if any...
219 load_rss(rss, filename);
221 changed = cupsArrayCount(rss) == 0;
224 * Localize for the user's chosen language...
227 language = cupsLangDefault();
230 * Read events and update the RSS file until we are out of events.
233 for (exit_status = 0, event = NULL;;)
238 * Save the messages to the file again, uploading as needed...
241 if (save_rss(rss, newname, baseurl))
246 * Upload the RSS file...
249 if ((status = cupsPutFile(http, resource, filename)) != HTTP_CREATED)
250 fprintf(stderr, "ERROR: Unable to PUT %s from %s on port %d: %d %s\n",
251 resource, host, port, status, httpStatus(status));
256 * Move the new RSS file over top the old one...
259 if (rename(newname, filename))
260 fprintf(stderr, "ERROR: Unable to rename %s to %s: %s\n",
261 newname, filename, strerror(errno));
269 * Wait up to 30 seconds for an event...
278 if (select(1, &input, NULL, NULL, &timeout) < 0)
280 else if (!FD_ISSET(0, &input))
282 fprintf(stderr, "DEBUG: %s is bored, exiting...\n", argv[1]);
287 * Read the next event...
291 while ((state = ippReadFile(0, event)) != IPP_DATA)
293 if (state <= IPP_IDLE)
297 if (state == IPP_ERROR)
298 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr);
300 if (state <= IPP_IDLE)
304 * Collect the info from the event...
307 printer_up_time = ippFindAttribute(event, "printer-up-time",
309 notify_sequence_number = ippFindAttribute(event, "notify-sequence-number",
311 notify_printer_uri = ippFindAttribute(event, "notify-printer-uri",
313 subject = cupsNotifySubject(language, event);
314 text = cupsNotifyText(language, event);
316 if (printer_up_time && notify_sequence_number && subject && text)
319 * Create a new RSS message...
322 if (notify_printer_uri)
324 httpSeparateURI(HTTP_URI_CODING_ALL,
325 notify_printer_uri->values[0].string.text,
326 link_scheme, sizeof(link_scheme),
327 link_username, sizeof(link_username),
328 link_host, sizeof(link_host), &link_port,
329 link_resource, sizeof(link_resource));
330 httpAssembleURI(HTTP_URI_CODING_ALL, link_url, sizeof(link_url),
331 "http", link_username, link_host, link_port,
335 msg = new_message(notify_sequence_number->values[0].integer,
336 xml_escape(subject), xml_escape(text),
337 notify_printer_uri ? xml_escape(link_url) : NULL,
338 printer_up_time->values[0].integer);
342 fprintf(stderr, "ERROR: Unable to create message: %s\n",
349 * Add it to the array...
352 cupsArrayAdd(rss, msg);
357 * Trim the array as needed...
360 while (cupsArrayCount(rss) > max_events)
362 msg = cupsArrayFirst(rss);
364 cupsArrayRemove(rss, msg);
381 * We only get here when idle or error...
392 return (exit_status);
397 * 'compare_rss()' - Compare two messages.
400 static int /* O - Result of comparison */
401 compare_rss(_cups_rss_t *a, /* I - First message */
402 _cups_rss_t *b) /* I - Second message */
404 return (a->sequence_number - b->sequence_number);
409 * 'delete_message()' - Free all memory used by a message.
413 delete_message(_cups_rss_t *msg) /* I - RSS message */
429 * 'load_rss()' - Load an existing RSS feed file.
433 load_rss(cups_array_t *rss, /* I - RSS messages */
434 const char *filename) /* I - File to load */
436 FILE *fp; /* File pointer */
437 char line[4096], /* Line from file */
438 *subject, /* Subject */
440 *link_url, /* Link URL */
441 *start, /* Start of element */
442 *end; /* End of element */
443 time_t event_time; /* Event time */
444 int sequence_number; /* Sequence number */
445 int in_item; /* In an item */
446 _cups_rss_t *msg; /* New message */
449 if ((fp = fopen(filename, "r")) == NULL)
452 fprintf(stderr, "ERROR: Unable to open %s: %s\n", filename,
465 while (fgets(line, sizeof(line), fp))
467 if (strstr(line, "<item>"))
469 else if (strstr(line, "</item>") && in_item)
473 msg = new_message(sequence_number, subject, text, link_url,
477 cupsArrayAdd(rss, msg);
501 else if ((start = strstr(line, "<title>")) != NULL)
504 if ((end = strstr(start, "</title>")) != NULL)
507 subject = strdup(start);
510 else if ((start = strstr(line, "<description>")) != NULL)
513 if ((end = strstr(start, "</description>")) != NULL)
516 text = strdup(start);
519 else if ((start = strstr(line, "<link>")) != NULL)
522 if ((end = strstr(start, "</link>")) != NULL)
525 link_url = strdup(start);
528 else if ((start = strstr(line, "<pubDate>")) != NULL)
531 if ((end = strstr(start, "</pubDate>")) != NULL)
534 event_time = httpGetDateTime(start);
537 else if ((start = strstr(line, "<guid>")) != NULL)
538 sequence_number = atoi(start + 6);
555 * 'new_message()' - Create a new RSS message.
558 static _cups_rss_t * /* O - New message */
559 new_message(int sequence_number, /* I - notify-sequence-number */
560 char *subject, /* I - Subject/summary */
561 char *text, /* I - Text */
562 char *link_url, /* I - Link to printer */
563 time_t event_time) /* I - Date/time of event */
565 _cups_rss_t *msg; /* New message */
568 if ((msg = calloc(1, sizeof(_cups_rss_t))) == NULL)
571 msg->sequence_number = sequence_number;
572 msg->subject = subject;
574 msg->link_url = link_url;
575 msg->event_time = event_time;
582 * 'password_cb()' - Return the cached password.
585 static const char * /* O - Cached password */
586 password_cb(const char *prompt) /* I - Prompt string, unused */
590 return (rss_password);
595 * 'save_rss()' - Save messages to a RSS file.
598 static int /* O - 1 on success, 0 on failure */
599 save_rss(cups_array_t *rss, /* I - RSS messages */
600 const char *filename, /* I - File to save to */
601 const char *baseurl) /* I - Base URL */
603 FILE *fp; /* File pointer */
604 _cups_rss_t *msg; /* Current message */
605 char date[1024]; /* Current date */
606 char *href; /* Escaped base URL */
609 if ((fp = fopen(filename, "w")) == NULL)
611 fprintf(stderr, "ERROR: Unable to create %s: %s\n", filename,
616 fchmod(fileno(fp), 0644);
618 fputs("<?xml version=\"1.0\"?>\n", fp);
619 fputs("<rss version=\"2.0\">\n", fp);
620 fputs(" <channel>\n", fp);
621 fputs(" <title>CUPS RSS Feed</title>\n", fp);
623 href = xml_escape(baseurl);
624 fprintf(fp, " <link>%s</link>\n", href);
627 fputs(" <description>CUPS RSS Feed</description>\n", fp);
628 fputs(" <generator>" CUPS_SVERSION "</generator>\n", fp);
629 fputs(" <ttl>1</ttl>\n", fp);
631 fprintf(fp, " <pubDate>%s</pubDate>\n",
632 httpGetDateString2(time(NULL), date, sizeof(date)));
634 for (msg = (_cups_rss_t *)cupsArrayLast(rss);
636 msg = (_cups_rss_t *)cupsArrayPrev(rss))
638 char *subject = xml_escape(msg->subject);
639 char *text = xml_escape(msg->text);
641 fputs(" <item>\n", fp);
642 fprintf(fp, " <title>%s</title>\n", subject);
643 fprintf(fp, " <description>%s</description>\n", text);
645 fprintf(fp, " <link>%s</link>\n", msg->link_url);
646 fprintf(fp, " <pubDate>%s</pubDate>\n",
647 httpGetDateString2(msg->event_time, date, sizeof(date)));
648 fprintf(fp, " <guid>%d</guid>\n", msg->sequence_number);
649 fputs(" </item>\n", fp);
655 fputs(" </channel>\n", fp);
656 fputs("</rss>\n", fp);
658 return (!fclose(fp));
663 * 'xml_escape()' - Copy a string, escaping &, <, and > as needed.
666 static char * /* O - Escaped string */
667 xml_escape(const char *s) /* I - String to escape */
669 char *e, /* Escaped string */
670 *eptr; /* Pointer into escaped string */
671 const char *sptr; /* Pointer into string */
672 size_t bytes; /* Bytes needed for string */
676 * First figure out how many extra bytes we need...
679 for (bytes = 0, sptr = s; *sptr; sptr ++)
681 bytes += 4; /* & */
682 else if (*sptr == '<' || *sptr == '>')
683 bytes += 3; /* < and > */
686 * If there is nothing to escape, just strdup() it...
693 * Otherwise allocate memory and copy...
696 if ((e = malloc(bytes + 1 + strlen(s))) == NULL)
699 for (eptr = e, sptr = s; *sptr; sptr ++)
708 else if (*sptr == '<')
715 else if (*sptr == '>')