]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/rss.c
2 * "$Id: rss.c 7451 2008-04-14 20:02:39Z mike $"
4 * RSS notifier for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Main entry for the test notifier.
18 * compare_rss() - Compare two messages.
19 * delete_message() - Free all memory used by a message.
20 * load_rss() - Load an existing RSS feed file.
21 * new_message() - Create a new RSS message.
22 * password_cb() - Return the cached password.
23 * save_rss() - Save messages to a RSS file.
24 * xml_escape() - Copy a string, escaping &, <, and > as needed.
28 * Include necessary headers...
31 #include <cups/cups.h>
32 #include <cups/language.h>
33 #include <cups/string.h>
34 #include <cups/array.h>
42 typedef struct _cups_rss_s
/**** RSS message data ****/
44 int sequence_number
; /* notify-sequence-number */
45 char *subject
, /* Message subject/summary */
46 *text
, /* Message text */
47 *link_url
; /* Link to printer */
48 time_t event_time
; /* When the event occurred */
56 static char *rss_password
; /* Password for remote RSS */
63 static int compare_rss(_cups_rss_t
*a
, _cups_rss_t
*b
);
64 static void delete_message(_cups_rss_t
*rss
);
65 static void load_rss(cups_array_t
*rss
, const char *filename
);
66 static _cups_rss_t
*new_message(int sequence_number
, char *subject
,
67 char *text
, char *link_url
,
69 static const char *password_cb(const char *prompt
);
70 static int save_rss(cups_array_t
*rss
, const char *filename
,
72 static char *xml_escape(const char *s
);
76 * 'main()' - Main entry for the test notifier.
79 int /* O - Exit status */
80 main(int argc
, /* I - Number of command-line arguments */
81 char *argv
[]) /* I - Command-line arguments */
83 int i
; /* Looping var */
84 ipp_t
*event
; /* Event from scheduler */
85 ipp_state_t state
; /* IPP event state */
86 char scheme
[32], /* URI scheme ("rss") */
87 username
[256], /* Username for remote RSS */
88 host
[1024], /* Hostname for remote RSS */
89 resource
[1024], /* RSS file */
90 *options
; /* Options */
91 int port
, /* Port number for remote RSS */
92 max_events
; /* Maximum number of events */
93 http_t
*http
; /* Connection to remote server */
94 http_status_t status
; /* HTTP GET/PUT status code */
95 char filename
[1024], /* Local filename */
96 newname
[1024]; /* filename.N */
97 cups_lang_t
*language
; /* Language information */
98 ipp_attribute_t
*printer_up_time
, /* Timestamp on event */
99 *notify_sequence_number
,/* Sequence number */
100 *notify_printer_uri
; /* Printer URI */
101 char *subject
, /* Subject for notification message */
102 *text
, /* Text for notification message */
103 link_url
[1024], /* Link to printer */
104 link_scheme
[32], /* Scheme for link */
105 link_username
[256], /* Username for link */
106 link_host
[1024], /* Host for link */
107 link_resource
[1024]; /* Resource for link */
108 int link_port
; /* Link port */
109 cups_array_t
*rss
; /* RSS message array */
110 _cups_rss_t
*msg
; /* RSS message */
111 char baseurl
[1024]; /* Base URL */
114 fprintf(stderr
, "DEBUG: argc=%d\n", argc
);
115 for (i
= 0; i
< argc
; i
++)
116 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
119 * See whether we are publishing this RSS feed locally or remotely...
122 if (httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[1], scheme
, sizeof(scheme
),
123 username
, sizeof(username
), host
, sizeof(host
), &port
,
124 resource
, sizeof(resource
)) < HTTP_URI_OK
)
126 fprintf(stderr
, "ERROR: Bad RSS URI \"%s\"!\n", argv
[1]);
132 if ((options
= strchr(resource
, '?')) != NULL
)
136 if (!strncmp(options
, "max_events=", 11))
138 max_events
= atoi(options
+ 11);
145 rss
= cupsArrayNew((cups_array_func_t
)compare_rss
, NULL
);
150 * Remote feed, see if we can get the current file...
153 int fd
; /* Temporary file */
156 if ((rss_password
= strchr(username
, ':')) != NULL
)
157 *rss_password
++ = '\0';
159 cupsSetPasswordCB(password_cb
);
160 cupsSetUser(username
);
162 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
164 fprintf(stderr
, "ERROR: Unable to create temporary file: %s\n",
170 if ((http
= httpConnect(host
, port
)) == NULL
)
172 fprintf(stderr
, "ERROR: Unable to connect to %s on port %d: %s\n",
173 host
, port
, strerror(errno
));
181 status
= cupsGetFd(http
, resource
, fd
);
185 if (status
!= HTTP_OK
&& status
!= HTTP_NOT_FOUND
)
187 fprintf(stderr
, "ERROR: Unable to GET %s from %s on port %d: %d %s\n",
188 resource
, host
, port
, status
, httpStatus(status
));
196 strlcpy(newname
, filename
, sizeof(newname
));
198 httpAssembleURI(HTTP_URI_CODING_ALL
, baseurl
, sizeof(baseurl
), "http",
199 NULL
, host
, port
, resource
);
203 const char *cachedir
, /* CUPS_CACHEDIR */
204 *server_name
, /* SERVER_NAME */
205 *server_port
; /* SERVER_PORT */
210 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
211 cachedir
= CUPS_CACHEDIR
;
213 if ((server_name
= getenv("SERVER_NAME")) == NULL
)
214 server_name
= "localhost";
216 if ((server_port
= getenv("SERVER_PORT")) == NULL
)
219 snprintf(filename
, sizeof(filename
), "%s/rss%s", cachedir
, resource
);
220 snprintf(newname
, sizeof(newname
), "%s.N", filename
);
222 httpAssembleURIf(HTTP_URI_CODING_ALL
, baseurl
, sizeof(baseurl
), "http",
223 NULL
, server_name
, atoi(server_port
), "/rss%s", resource
);
227 * Load the previous RSS file, if any...
230 load_rss(rss
, filename
);
233 * Localize for the user's chosen language...
236 language
= cupsLangDefault();
239 * Read events and update the RSS file until we are out of events.
245 * Read the next event...
249 while ((state
= ippReadFile(0, event
)) != IPP_DATA
)
251 if (state
<= IPP_IDLE
)
255 if (state
== IPP_ERROR
)
256 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr
);
258 if (state
<= IPP_IDLE
)
271 * Collect the info from the event...
274 printer_up_time
= ippFindAttribute(event
, "printer-up-time",
276 notify_sequence_number
= ippFindAttribute(event
, "notify-sequence-number",
278 notify_printer_uri
= ippFindAttribute(event
, "notify-printer-uri",
280 subject
= cupsNotifySubject(language
, event
);
281 text
= cupsNotifyText(language
, event
);
283 if (printer_up_time
&& notify_sequence_number
&& subject
&& text
)
286 * Create a new RSS message...
289 if (notify_printer_uri
)
291 httpSeparateURI(HTTP_URI_CODING_ALL
,
292 notify_printer_uri
->values
[0].string
.text
,
293 link_scheme
, sizeof(link_scheme
),
294 link_username
, sizeof(link_username
),
295 link_host
, sizeof(link_host
), &link_port
,
296 link_resource
, sizeof(link_resource
));
297 httpAssembleURI(HTTP_URI_CODING_ALL
, link_url
, sizeof(link_url
),
298 "http", link_username
, link_host
, link_port
,
302 msg
= new_message(notify_sequence_number
->values
[0].integer
,
303 xml_escape(subject
), xml_escape(text
),
304 notify_printer_uri
? xml_escape(link_url
) : NULL
,
305 printer_up_time
->values
[0].integer
);
309 fprintf(stderr
, "ERROR: Unable to create message: %s\n",
323 * Add it to the array...
326 cupsArrayAdd(rss
, msg
);
329 * Trim the array as needed...
332 while (cupsArrayCount(rss
) > max_events
)
334 msg
= cupsArrayFirst(rss
);
336 cupsArrayRemove(rss
, msg
);
342 * Save the messages to the file again, uploading as needed...
345 if (save_rss(rss
, newname
, baseurl
))
350 * Upload the RSS file...
353 if ((status
= cupsPutFile(http
, resource
, filename
)) != HTTP_CREATED
)
354 fprintf(stderr
, "ERROR: Unable to PUT %s from %s on port %d: %d %s\n",
355 resource
, host
, port
, status
, httpStatus(status
));
360 * Move the new RSS file over top the old one...
363 if (rename(newname
, filename
))
364 fprintf(stderr
, "ERROR: Unable to rename %s to %s: %s\n",
365 newname
, filename
, strerror(errno
));
382 * 'compare_rss()' - Compare two messages.
385 static int /* O - Result of comparison */
386 compare_rss(_cups_rss_t
*a
, /* I - First message */
387 _cups_rss_t
*b
) /* I - Second message */
389 return (a
->sequence_number
- b
->sequence_number
);
394 * 'delete_message()' - Free all memory used by a message.
398 delete_message(_cups_rss_t
*msg
) /* I - RSS message */
414 * 'load_rss()' - Load an existing RSS feed file.
418 load_rss(cups_array_t
*rss
, /* I - RSS messages */
419 const char *filename
) /* I - File to load */
421 FILE *fp
; /* File pointer */
422 char line
[4096], /* Line from file */
423 *subject
, /* Subject */
425 *link_url
, /* Link URL */
426 *start
, /* Start of element */
427 *end
; /* End of element */
428 time_t event_time
; /* Event time */
429 int sequence_number
; /* Sequence number */
430 int in_item
; /* In an item */
431 _cups_rss_t
*msg
; /* New message */
434 if ((fp
= fopen(filename
, "r")) == NULL
)
437 fprintf(stderr
, "ERROR: Unable to open %s: %s\n", filename
,
450 while (fgets(line
, sizeof(line
), fp
))
452 if (strstr(line
, "<item>"))
454 else if (strstr(line
, "</item>") && in_item
)
458 msg
= new_message(sequence_number
, subject
, text
, link_url
,
462 cupsArrayAdd(rss
, msg
);
486 else if ((start
= strstr(line
, "<title>")) != NULL
)
489 if ((end
= strstr(start
, "</title>")) != NULL
)
492 subject
= strdup(start
);
495 else if ((start
= strstr(line
, "<description>")) != NULL
)
498 if ((end
= strstr(start
, "</description>")) != NULL
)
501 text
= strdup(start
);
504 else if ((start
= strstr(line
, "<link>")) != NULL
)
507 if ((end
= strstr(start
, "</link>")) != NULL
)
510 link_url
= strdup(start
);
513 else if ((start
= strstr(line
, "<pubDate>")) != NULL
)
516 if ((end
= strstr(start
, "</pubDate>")) != NULL
)
519 event_time
= httpGetDateTime(start
);
522 else if ((start
= strstr(line
, "<guid>")) != NULL
)
523 sequence_number
= atoi(start
+ 6);
531 * 'new_message()' - Create a new RSS message.
534 static _cups_rss_t
* /* O - New message */
535 new_message(int sequence_number
, /* I - notify-sequence-number */
536 char *subject
, /* I - Subject/summary */
537 char *text
, /* I - Text */
538 char *link_url
, /* I - Link to printer */
539 time_t event_time
) /* I - Date/time of event */
541 _cups_rss_t
*msg
; /* New message */
544 if ((msg
= calloc(1, sizeof(_cups_rss_t
))) == NULL
)
547 msg
->sequence_number
= sequence_number
;
548 msg
->subject
= subject
;
550 msg
->link_url
= link_url
;
551 msg
->event_time
= event_time
;
558 * 'password_cb()' - Return the cached password.
561 static const char * /* O - Cached password */
562 password_cb(const char *prompt
) /* I - Prompt string, unused */
566 return (rss_password
);
571 * 'save_rss()' - Save messages to a RSS file.
574 static int /* O - 1 on success, 0 on failure */
575 save_rss(cups_array_t
*rss
, /* I - RSS messages */
576 const char *filename
, /* I - File to save to */
577 const char *baseurl
) /* I - Base URL */
579 FILE *fp
; /* File pointer */
580 _cups_rss_t
*msg
; /* Current message */
581 char date
[1024]; /* Current date */
582 char *href
; /* Escaped base URL */
585 if ((fp
= fopen(filename
, "w")) == NULL
)
587 fprintf(stderr
, "ERROR: Unable to create %s: %s\n", filename
,
592 fputs("<?xml version=\"1.0\"?>\n", fp
);
593 fputs("<rss version=\"2.0\">\n", fp
);
594 fputs(" <channel>\n", fp
);
595 fputs(" <title>CUPS RSS Feed</title>\n", fp
);
597 href
= xml_escape(baseurl
);
598 fprintf(fp
, " <link>%s</link>\n", href
);
601 fputs(" <description>CUPS RSS Feed</description>\n", fp
);
602 fputs(" <generator>" CUPS_SVERSION
"</generator>\n", fp
);
603 fputs(" <ttl>1</ttl>\n", fp
);
605 fprintf(fp
, " <pubDate>%s</pubDate>\n",
606 httpGetDateString2(time(NULL
), date
, sizeof(date
)));
608 for (msg
= (_cups_rss_t
*)cupsArrayLast(rss
);
610 msg
= (_cups_rss_t
*)cupsArrayPrev(rss
))
612 fputs(" <item>\n", fp
);
613 fprintf(fp
, " <title>%s</title>\n", msg
->subject
);
614 fprintf(fp
, " <description>%s</description>\n", msg
->text
);
616 fprintf(fp
, " <link>%s</link>\n", msg
->link_url
);
617 fprintf(fp
, " <pubDate>%s</pubDate>\n",
618 httpGetDateString2(msg
->event_time
, date
, sizeof(date
)));
619 fprintf(fp
, " <guid>%d</guid>\n", msg
->sequence_number
);
620 fputs(" </item>\n", fp
);
623 fputs(" </channel>\n", fp
);
624 fputs("</rss>\n", fp
);
626 return (!fclose(fp
));
631 * 'xml_escape()' - Copy a string, escaping &, <, and > as needed.
634 static char * /* O - Escaped string */
635 xml_escape(const char *s
) /* I - String to escape */
637 char *e
, /* Escaped string */
638 *eptr
; /* Pointer into escaped string */
639 const char *sptr
; /* Pointer into string */
640 size_t bytes
; /* Bytes needed for string */
644 * First figure out how many extra bytes we need...
647 for (bytes
= 0, sptr
= s
; *sptr
; sptr
++)
649 bytes
+= 4; /* & */
650 else if (*sptr
== '<' || *sptr
== '>')
651 bytes
+= 3; /* < and > */
654 * If there is nothing to escape, just strdup() it...
661 * Otherwise allocate memory and copy...
664 if ((e
= malloc(bytes
+ 1 + strlen(s
))) == NULL
)
667 for (eptr
= e
, sptr
= s
; *sptr
; sptr
++)
676 else if (*sptr
== '<')
683 else if (*sptr
== '>')
700 * End of "$Id: rss.c 7451 2008-04-14 20:02:39Z mike $".