]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/rss.c
2 * RSS notifier for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
15 * Include necessary headers...
18 #include <cups/cups.h>
20 #include <cups/language.h>
21 #include <cups/string-private.h>
22 #include <cups/array.h>
23 #include <sys/select.h>
24 #include <cups/ipp-private.h> /* TODO: Update so we don't need this */
31 typedef struct _cups_rss_s
/**** RSS message data ****/
33 int sequence_number
; /* notify-sequence-number */
34 char *subject
, /* Message subject/summary */
35 *text
, /* Message text */
36 *link_url
; /* Link to printer */
37 time_t event_time
; /* When the event occurred */
45 static char *rss_password
; /* Password for remote RSS */
52 static int compare_rss(_cups_rss_t
*a
, _cups_rss_t
*b
);
53 static void delete_message(_cups_rss_t
*rss
);
54 static void load_rss(cups_array_t
*rss
, const char *filename
);
55 static _cups_rss_t
*new_message(int sequence_number
, char *subject
,
56 char *text
, char *link_url
,
58 static const char *password_cb(const char *prompt
);
59 static int save_rss(cups_array_t
*rss
, const char *filename
,
61 static char *xml_escape(const char *s
);
65 * 'main()' - Main entry for the test notifier.
68 int /* O - Exit status */
69 main(int argc
, /* I - Number of command-line arguments */
70 char *argv
[]) /* I - Command-line arguments */
72 int i
; /* Looping var */
73 ipp_t
*event
; /* Event from scheduler */
74 ipp_state_t state
; /* IPP event state */
75 char scheme
[32], /* URI scheme ("rss") */
76 username
[256], /* Username for remote RSS */
77 host
[1024], /* Hostname for remote RSS */
78 resource
[1024], /* RSS file */
79 *options
; /* Options */
80 int port
, /* Port number for remote RSS */
81 max_events
; /* Maximum number of events */
82 http_t
*http
; /* Connection to remote server */
83 http_status_t status
; /* HTTP GET/PUT status code */
84 char filename
[1024], /* Local filename */
85 newname
[1024]; /* filename.N */
86 cups_lang_t
*language
; /* Language information */
87 ipp_attribute_t
*printer_up_time
, /* Timestamp on event */
88 *notify_sequence_number
,/* Sequence number */
89 *notify_printer_uri
; /* Printer URI */
90 char *subject
, /* Subject for notification message */
91 *text
, /* Text for notification message */
92 link_url
[1024], /* Link to printer */
93 link_scheme
[32], /* Scheme for link */
94 link_username
[256], /* Username for link */
95 link_host
[1024], /* Host for link */
96 link_resource
[1024]; /* Resource for link */
97 int link_port
; /* Link port */
98 cups_array_t
*rss
; /* RSS message array */
99 _cups_rss_t
*msg
; /* RSS message */
100 char baseurl
[1024]; /* Base URL */
101 fd_set input
; /* Input set for select() */
102 struct timeval timeout
; /* Timeout for select() */
103 int changed
; /* Has the RSS data changed? */
104 int exit_status
; /* Exit status */
107 fprintf(stderr
, "DEBUG: argc=%d\n", argc
);
108 for (i
= 0; i
< argc
; i
++)
109 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
112 * See whether we are publishing this RSS feed locally or remotely...
115 if (httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[1], scheme
, sizeof(scheme
),
116 username
, sizeof(username
), host
, sizeof(host
), &port
,
117 resource
, sizeof(resource
)) < HTTP_URI_OK
)
119 fprintf(stderr
, "ERROR: Bad RSS URI \"%s\"!\n", argv
[1]);
125 if ((options
= strchr(resource
, '?')) != NULL
)
129 if (!strncmp(options
, "max_events=", 11))
131 max_events
= atoi(options
+ 11);
138 rss
= cupsArrayNew((cups_array_func_t
)compare_rss
, NULL
);
143 * Remote feed, see if we can get the current file...
146 int fd
; /* Temporary file */
149 if ((rss_password
= strchr(username
, ':')) != NULL
)
150 *rss_password
++ = '\0';
152 cupsSetPasswordCB(password_cb
);
153 cupsSetUser(username
);
155 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
157 fprintf(stderr
, "ERROR: Unable to create temporary file: %s\n",
163 if ((http
= httpConnect(host
, port
)) == NULL
)
165 fprintf(stderr
, "ERROR: Unable to connect to %s on port %d: %s\n",
166 host
, port
, strerror(errno
));
174 status
= cupsGetFd(http
, resource
, fd
);
178 if (status
!= HTTP_OK
&& status
!= HTTP_NOT_FOUND
)
180 fprintf(stderr
, "ERROR: Unable to GET %s from %s on port %d: %d %s\n",
181 resource
, host
, port
, status
, httpStatus(status
));
189 strlcpy(newname
, filename
, sizeof(newname
));
191 httpAssembleURI(HTTP_URI_CODING_ALL
, baseurl
, sizeof(baseurl
), "http",
192 NULL
, host
, port
, resource
);
196 const char *cachedir
, /* CUPS_CACHEDIR */
197 *server_name
, /* SERVER_NAME */
198 *server_port
; /* SERVER_PORT */
203 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
204 cachedir
= CUPS_CACHEDIR
;
206 if ((server_name
= getenv("SERVER_NAME")) == NULL
)
207 server_name
= "localhost";
209 if ((server_port
= getenv("SERVER_PORT")) == NULL
)
212 snprintf(filename
, sizeof(filename
), "%s/rss%s", cachedir
, resource
);
213 snprintf(newname
, sizeof(newname
), "%s.N", filename
);
215 httpAssembleURIf(HTTP_URI_CODING_ALL
, baseurl
, sizeof(baseurl
), "http",
216 NULL
, server_name
, atoi(server_port
), "/rss%s", resource
);
220 * Load the previous RSS file, if any...
223 load_rss(rss
, filename
);
225 changed
= cupsArrayCount(rss
) == 0;
228 * Localize for the user's chosen language...
231 language
= cupsLangDefault();
234 * Read events and update the RSS file until we are out of events.
237 for (exit_status
= 0, event
= NULL
;;)
242 * Save the messages to the file again, uploading as needed...
245 if (save_rss(rss
, newname
, baseurl
))
250 * Upload the RSS file...
253 if ((status
= cupsPutFile(http
, resource
, filename
)) != HTTP_CREATED
)
254 fprintf(stderr
, "ERROR: Unable to PUT %s from %s on port %d: %d %s\n",
255 resource
, host
, port
, status
, httpStatus(status
));
260 * Move the new RSS file over top the old one...
263 if (rename(newname
, filename
))
264 fprintf(stderr
, "ERROR: Unable to rename %s to %s: %s\n",
265 newname
, filename
, strerror(errno
));
273 * Wait up to 30 seconds for an event...
282 if (select(1, &input
, NULL
, NULL
, &timeout
) < 0)
284 else if (!FD_ISSET(0, &input
))
286 fprintf(stderr
, "DEBUG: %s is bored, exiting...\n", argv
[1]);
291 * Read the next event...
295 while ((state
= ippReadFile(0, event
)) != IPP_DATA
)
297 if (state
<= IPP_IDLE
)
301 if (state
== IPP_ERROR
)
302 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr
);
304 if (state
<= IPP_IDLE
)
308 * Collect the info from the event...
311 printer_up_time
= ippFindAttribute(event
, "printer-up-time",
313 notify_sequence_number
= ippFindAttribute(event
, "notify-sequence-number",
315 notify_printer_uri
= ippFindAttribute(event
, "notify-printer-uri",
317 subject
= cupsNotifySubject(language
, event
);
318 text
= cupsNotifyText(language
, event
);
320 if (printer_up_time
&& notify_sequence_number
&& subject
&& text
)
323 * Create a new RSS message...
326 if (notify_printer_uri
)
328 httpSeparateURI(HTTP_URI_CODING_ALL
,
329 notify_printer_uri
->values
[0].string
.text
,
330 link_scheme
, sizeof(link_scheme
),
331 link_username
, sizeof(link_username
),
332 link_host
, sizeof(link_host
), &link_port
,
333 link_resource
, sizeof(link_resource
));
334 httpAssembleURI(HTTP_URI_CODING_ALL
, link_url
, sizeof(link_url
),
335 "http", link_username
, link_host
, link_port
,
339 msg
= new_message(notify_sequence_number
->values
[0].integer
,
340 xml_escape(subject
), xml_escape(text
),
341 notify_printer_uri
? xml_escape(link_url
) : NULL
,
342 printer_up_time
->values
[0].integer
);
346 fprintf(stderr
, "ERROR: Unable to create message: %s\n",
353 * Add it to the array...
356 cupsArrayAdd(rss
, msg
);
361 * Trim the array as needed...
364 while (cupsArrayCount(rss
) > max_events
)
366 msg
= cupsArrayFirst(rss
);
368 cupsArrayRemove(rss
, msg
);
385 * We only get here when idle or error...
396 return (exit_status
);
401 * 'compare_rss()' - Compare two messages.
404 static int /* O - Result of comparison */
405 compare_rss(_cups_rss_t
*a
, /* I - First message */
406 _cups_rss_t
*b
) /* I - Second message */
408 return (a
->sequence_number
- b
->sequence_number
);
413 * 'delete_message()' - Free all memory used by a message.
417 delete_message(_cups_rss_t
*msg
) /* I - RSS message */
433 * 'load_rss()' - Load an existing RSS feed file.
437 load_rss(cups_array_t
*rss
, /* I - RSS messages */
438 const char *filename
) /* I - File to load */
440 FILE *fp
; /* File pointer */
441 char line
[4096], /* Line from file */
442 *subject
, /* Subject */
444 *link_url
, /* Link URL */
445 *start
, /* Start of element */
446 *end
; /* End of element */
447 time_t event_time
; /* Event time */
448 int sequence_number
; /* Sequence number */
449 int in_item
; /* In an item */
450 _cups_rss_t
*msg
; /* New message */
453 if ((fp
= fopen(filename
, "r")) == NULL
)
456 fprintf(stderr
, "ERROR: Unable to open %s: %s\n", filename
,
469 while (fgets(line
, sizeof(line
), fp
))
471 if (strstr(line
, "<item>"))
473 else if (strstr(line
, "</item>") && in_item
)
477 msg
= new_message(sequence_number
, subject
, text
, link_url
,
481 cupsArrayAdd(rss
, msg
);
505 else if ((start
= strstr(line
, "<title>")) != NULL
)
508 if ((end
= strstr(start
, "</title>")) != NULL
)
511 subject
= strdup(start
);
514 else if ((start
= strstr(line
, "<description>")) != NULL
)
517 if ((end
= strstr(start
, "</description>")) != NULL
)
520 text
= strdup(start
);
523 else if ((start
= strstr(line
, "<link>")) != NULL
)
526 if ((end
= strstr(start
, "</link>")) != NULL
)
529 link_url
= strdup(start
);
532 else if ((start
= strstr(line
, "<pubDate>")) != NULL
)
535 if ((end
= strstr(start
, "</pubDate>")) != NULL
)
538 event_time
= httpGetDateTime(start
);
541 else if ((start
= strstr(line
, "<guid>")) != NULL
)
542 sequence_number
= atoi(start
+ 6);
559 * 'new_message()' - Create a new RSS message.
562 static _cups_rss_t
* /* O - New message */
563 new_message(int sequence_number
, /* I - notify-sequence-number */
564 char *subject
, /* I - Subject/summary */
565 char *text
, /* I - Text */
566 char *link_url
, /* I - Link to printer */
567 time_t event_time
) /* I - Date/time of event */
569 _cups_rss_t
*msg
; /* New message */
572 if ((msg
= calloc(1, sizeof(_cups_rss_t
))) == NULL
)
575 msg
->sequence_number
= sequence_number
;
576 msg
->subject
= subject
;
578 msg
->link_url
= link_url
;
579 msg
->event_time
= event_time
;
586 * 'password_cb()' - Return the cached password.
589 static const char * /* O - Cached password */
590 password_cb(const char *prompt
) /* I - Prompt string, unused */
594 return (rss_password
);
599 * 'save_rss()' - Save messages to a RSS file.
602 static int /* O - 1 on success, 0 on failure */
603 save_rss(cups_array_t
*rss
, /* I - RSS messages */
604 const char *filename
, /* I - File to save to */
605 const char *baseurl
) /* I - Base URL */
607 FILE *fp
; /* File pointer */
608 _cups_rss_t
*msg
; /* Current message */
609 char date
[1024]; /* Current date */
610 char *href
; /* Escaped base URL */
613 if ((fp
= fopen(filename
, "w")) == NULL
)
615 fprintf(stderr
, "ERROR: Unable to create %s: %s\n", filename
,
620 fchmod(fileno(fp
), 0644);
622 fputs("<?xml version=\"1.0\"?>\n", fp
);
623 fputs("<rss version=\"2.0\">\n", fp
);
624 fputs(" <channel>\n", fp
);
625 fputs(" <title>CUPS RSS Feed</title>\n", fp
);
627 href
= xml_escape(baseurl
);
628 fprintf(fp
, " <link>%s</link>\n", href
);
631 fputs(" <description>CUPS RSS Feed</description>\n", fp
);
632 fputs(" <generator>" CUPS_SVERSION
"</generator>\n", fp
);
633 fputs(" <ttl>1</ttl>\n", fp
);
635 fprintf(fp
, " <pubDate>%s</pubDate>\n",
636 httpGetDateString2(time(NULL
), date
, sizeof(date
)));
638 for (msg
= (_cups_rss_t
*)cupsArrayLast(rss
);
640 msg
= (_cups_rss_t
*)cupsArrayPrev(rss
))
642 char *subject
= xml_escape(msg
->subject
);
643 char *text
= xml_escape(msg
->text
);
645 fputs(" <item>\n", fp
);
646 fprintf(fp
, " <title>%s</title>\n", subject
);
647 fprintf(fp
, " <description>%s</description>\n", text
);
649 fprintf(fp
, " <link>%s</link>\n", msg
->link_url
);
650 fprintf(fp
, " <pubDate>%s</pubDate>\n",
651 httpGetDateString2(msg
->event_time
, date
, sizeof(date
)));
652 fprintf(fp
, " <guid>%d</guid>\n", msg
->sequence_number
);
653 fputs(" </item>\n", fp
);
659 fputs(" </channel>\n", fp
);
660 fputs("</rss>\n", fp
);
662 return (!fclose(fp
));
667 * 'xml_escape()' - Copy a string, escaping &, <, and > as needed.
670 static char * /* O - Escaped string */
671 xml_escape(const char *s
) /* I - String to escape */
673 char *e
, /* Escaped string */
674 *eptr
; /* Pointer into escaped string */
675 const char *sptr
; /* Pointer into string */
676 size_t bytes
; /* Bytes needed for string */
680 * First figure out how many extra bytes we need...
683 for (bytes
= 0, sptr
= s
; *sptr
; sptr
++)
685 bytes
+= 4; /* & */
686 else if (*sptr
== '<' || *sptr
== '>')
687 bytes
+= 3; /* < and > */
690 * If there is nothing to escape, just strdup() it...
697 * Otherwise allocate memory and copy...
700 if ((e
= malloc(bytes
+ 1 + strlen(s
))) == NULL
)
703 for (eptr
= e
, sptr
= s
; *sptr
; sptr
++)
712 else if (*sptr
== '<')
719 else if (*sptr
== '>')