]>
git.ipfire.org Git - thirdparty/cups.git/blob - notifier/rss.c
2 * "$Id: rss.c 6326 2007-03-11 17:50:18Z mike $"
4 * RSS notifier for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for the test notifier.
27 * compare_rss() - Compare two messages.
28 * delete_message() - Free all memory used by a message.
29 * load_rss() - Load an existing RSS feed file.
30 * new_message() - Create a new RSS message.
31 * password_cb() - Return the cached password.
32 * save_rss() - Save messages to a RSS file.
33 * xml_escape() - Copy a string, escaping &, <, and > as needed.
37 * Include necessary headers...
40 #include <cups/cups.h>
41 #include <cups/language.h>
42 #include <cups/string.h>
43 #include <cups/array.h>
51 typedef struct _cups_rss_s
/**** RSS message data ****/
53 int sequence_number
; /* notify-sequence-number */
54 char *subject
, /* Message subject/summary */
55 *text
, /* Message text */
56 *link_url
; /* Link to printer */
57 time_t event_time
; /* When the event occurred */
65 static char *rss_password
; /* Password for remote RSS */
72 static int compare_rss(_cups_rss_t
*a
, _cups_rss_t
*b
);
73 static void delete_message(_cups_rss_t
*rss
);
74 static void load_rss(cups_array_t
*rss
, const char *filename
);
75 static _cups_rss_t
*new_message(int sequence_number
, char *subject
,
76 char *text
, char *link_url
,
78 static const char *password_cb(const char *prompt
);
79 static int save_rss(cups_array_t
*rss
, const char *filename
,
81 static char *xml_escape(const char *s
);
85 * 'main()' - Main entry for the test notifier.
88 int /* O - Exit status */
89 main(int argc
, /* I - Number of command-line arguments */
90 char *argv
[]) /* I - Command-line arguments */
92 int i
; /* Looping var */
93 ipp_t
*event
; /* Event from scheduler */
94 ipp_state_t state
; /* IPP event state */
95 char scheme
[32], /* URI scheme ("rss") */
96 username
[256], /* Username for remote RSS */
97 host
[1024], /* Hostname for remote RSS */
98 resource
[1024], /* RSS file */
99 *options
; /* Options */
100 int port
, /* Port number for remote RSS */
101 max_events
; /* Maximum number of events */
102 http_t
*http
; /* Connection to remote server */
103 http_status_t status
; /* HTTP GET/PUT status code */
104 char filename
[1024], /* Local filename */
105 newname
[1024]; /* filename.N */
106 cups_lang_t
*language
; /* Language information */
107 ipp_attribute_t
*printer_up_time
, /* Timestamp on event */
108 *notify_sequence_number
,/* Sequence number */
109 *notify_printer_uri
; /* Printer URI */
110 char *subject
, /* Subject for notification message */
111 *text
, /* Text for notification message */
112 link_url
[1024], /* Link to printer */
113 link_scheme
[32], /* Scheme for link */
114 link_username
[256], /* Username for link */
115 link_host
[1024], /* Host for link */
116 link_resource
[1024]; /* Resource for link */
117 int link_port
; /* Link port */
118 cups_array_t
*rss
; /* RSS message array */
119 _cups_rss_t
*msg
; /* RSS message */
120 char baseurl
[1024]; /* Base URL */
123 fprintf(stderr
, "DEBUG: argc=%d\n", argc
);
124 for (i
= 0; i
< argc
; i
++)
125 fprintf(stderr
, "DEBUG: argv[%d]=\"%s\"\n", i
, argv
[i
]);
128 * See whether we are publishing this RSS feed locally or remotely...
131 if (httpSeparateURI(HTTP_URI_CODING_ALL
, argv
[1], scheme
, sizeof(scheme
),
132 username
, sizeof(username
), host
, sizeof(host
), &port
,
133 resource
, sizeof(resource
)) < HTTP_URI_OK
)
135 fprintf(stderr
, "ERROR: Bad RSS URI \"%s\"!\n", argv
[1]);
141 if ((options
= strchr(resource
, '?')) != NULL
)
145 if (!strncmp(options
, "max_events=", 11))
147 max_events
= atoi(options
+ 11);
154 rss
= cupsArrayNew((cups_array_func_t
)compare_rss
, NULL
);
159 * Remote feed, see if we can get the current file...
162 int fd
; /* Temporary file */
165 if ((rss_password
= strchr(username
, ':')) != NULL
)
166 *rss_password
++ = '\0';
168 cupsSetPasswordCB(password_cb
);
169 cupsSetUser(username
);
171 if ((fd
= cupsTempFd(filename
, sizeof(filename
))) < 0)
173 fprintf(stderr
, "ERROR: Unable to create temporary file: %s\n",
179 if ((http
= httpConnect(host
, port
)) == NULL
)
181 fprintf(stderr
, "ERROR: Unable to connect to %s on port %d: %s\n",
182 host
, port
, strerror(errno
));
190 status
= cupsGetFd(http
, resource
, fd
);
194 if (status
!= HTTP_OK
&& status
!= HTTP_NOT_FOUND
)
196 fprintf(stderr
, "ERROR: Unable to GET %s from %s on port %d: %d %s\n",
197 resource
, host
, port
, status
, httpStatus(status
));
205 strlcpy(newname
, filename
, sizeof(newname
));
207 httpAssembleURI(HTTP_URI_CODING_ALL
, baseurl
, sizeof(baseurl
), "http",
208 NULL
, host
, port
, resource
);
212 const char *cachedir
, /* CUPS_CACHEDIR */
213 *server_name
, /* SERVER_NAME */
214 *server_port
; /* SERVER_PORT */
219 if ((cachedir
= getenv("CUPS_CACHEDIR")) == NULL
)
220 cachedir
= CUPS_CACHEDIR
;
222 if ((server_name
= getenv("SERVER_NAME")) == NULL
)
223 server_name
= "localhost";
225 if ((server_port
= getenv("SERVER_PORT")) == NULL
)
228 snprintf(filename
, sizeof(filename
), "%s/rss%s", cachedir
, resource
);
229 snprintf(newname
, sizeof(newname
), "%s.N", filename
);
231 httpAssembleURIf(HTTP_URI_CODING_ALL
, baseurl
, sizeof(baseurl
), "http",
232 NULL
, server_name
, atoi(server_port
), "/rss%s", resource
);
236 * Load the previous RSS file, if any...
239 load_rss(rss
, filename
);
242 * Localize for the user's chosen language...
245 language
= cupsLangDefault();
248 * Read events and update the RSS file until we are out of events.
254 * Read the next event...
258 while ((state
= ippReadFile(0, event
)) != IPP_DATA
)
260 if (state
<= IPP_IDLE
)
264 if (state
== IPP_ERROR
)
265 fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr
);
267 if (state
<= IPP_IDLE
)
280 * Collect the info from the event...
283 printer_up_time
= ippFindAttribute(event
, "printer-up-time",
285 notify_sequence_number
= ippFindAttribute(event
, "notify-sequence-number",
287 notify_printer_uri
= ippFindAttribute(event
, "notify-printer-uri",
289 subject
= cupsNotifySubject(language
, event
);
290 text
= cupsNotifyText(language
, event
);
292 if (printer_up_time
&& notify_sequence_number
&& subject
&& text
)
295 * Create a new RSS message...
298 if (notify_printer_uri
)
300 httpSeparateURI(HTTP_URI_CODING_ALL
,
301 notify_printer_uri
->values
[0].string
.text
,
302 link_scheme
, sizeof(link_scheme
),
303 link_username
, sizeof(link_username
),
304 link_host
, sizeof(link_host
), &link_port
,
305 link_resource
, sizeof(link_resource
));
306 httpAssembleURI(HTTP_URI_CODING_ALL
, link_url
, sizeof(link_url
),
307 "http", link_username
, link_host
, link_port
,
311 msg
= new_message(notify_sequence_number
->values
[0].integer
,
312 xml_escape(subject
), xml_escape(text
),
313 notify_printer_uri
? xml_escape(link_url
) : NULL
,
314 printer_up_time
->values
[0].integer
);
318 fprintf(stderr
, "ERROR: Unable to create message: %s\n",
332 * Add it to the array...
335 cupsArrayAdd(rss
, msg
);
338 * Trim the array as needed...
341 while (cupsArrayCount(rss
) > max_events
)
343 msg
= cupsArrayFirst(rss
);
345 cupsArrayRemove(rss
, msg
);
351 * Save the messages to the file again, uploading as needed...
354 if (save_rss(rss
, newname
, baseurl
))
359 * Upload the RSS file...
362 if ((status
= cupsPutFile(http
, resource
, filename
)) != HTTP_CREATED
)
363 fprintf(stderr
, "ERROR: Unable to PUT %s from %s on port %d: %d %s\n",
364 resource
, host
, port
, status
, httpStatus(status
));
369 * Move the new RSS file over top the old one...
372 if (rename(newname
, filename
))
373 fprintf(stderr
, "ERROR: Unable to rename %s to %s: %s\n",
374 newname
, filename
, strerror(errno
));
391 * 'compare_rss()' - Compare two messages.
394 static int /* O - Result of comparison */
395 compare_rss(_cups_rss_t
*a
, /* I - First message */
396 _cups_rss_t
*b
) /* I - Second message */
398 return (a
->sequence_number
- b
->sequence_number
);
403 * 'delete_message()' - Free all memory used by a message.
407 delete_message(_cups_rss_t
*msg
) /* I - RSS message */
423 * 'load_rss()' - Load an existing RSS feed file.
427 load_rss(cups_array_t
*rss
, /* I - RSS messages */
428 const char *filename
) /* I - File to load */
430 FILE *fp
; /* File pointer */
431 char line
[4096], /* Line from file */
432 *subject
, /* Subject */
434 *link_url
, /* Link URL */
435 *start
, /* Start of element */
436 *end
; /* End of element */
437 time_t event_time
; /* Event time */
438 int sequence_number
; /* Sequence number */
439 int in_item
; /* In an item */
440 _cups_rss_t
*msg
; /* New message */
443 if ((fp
= fopen(filename
, "r")) == NULL
)
446 fprintf(stderr
, "ERROR: Unable to open %s: %s\n", filename
,
459 while (fgets(line
, sizeof(line
), fp
))
461 if (strstr(line
, "<item>"))
463 else if (strstr(line
, "</item>") && in_item
)
467 msg
= new_message(sequence_number
, subject
, text
, link_url
,
471 cupsArrayAdd(rss
, msg
);
495 else if ((start
= strstr(line
, "<title>")) != NULL
)
498 if ((end
= strstr(start
, "</title>")) != NULL
)
501 subject
= strdup(start
);
504 else if ((start
= strstr(line
, "<description>")) != NULL
)
507 if ((end
= strstr(start
, "</description>")) != NULL
)
510 text
= strdup(start
);
513 else if ((start
= strstr(line
, "<link>")) != NULL
)
516 if ((end
= strstr(start
, "</link>")) != NULL
)
519 link_url
= strdup(start
);
522 else if ((start
= strstr(line
, "<pubDate>")) != NULL
)
525 if ((end
= strstr(start
, "</pubDate>")) != NULL
)
528 event_time
= httpGetDateTime(start
);
531 else if ((start
= strstr(line
, "<guid>")) != NULL
)
532 sequence_number
= atoi(start
+ 6);
540 * 'new_message()' - Create a new RSS message.
543 static _cups_rss_t
* /* O - New message */
544 new_message(int sequence_number
, /* I - notify-sequence-number */
545 char *subject
, /* I - Subject/summary */
546 char *text
, /* I - Text */
547 char *link_url
, /* I - Link to printer */
548 time_t event_time
) /* I - Date/time of event */
550 _cups_rss_t
*msg
; /* New message */
553 if ((msg
= calloc(1, sizeof(_cups_rss_t
))) == NULL
)
556 msg
->sequence_number
= sequence_number
;
557 msg
->subject
= subject
;
559 msg
->link_url
= link_url
;
560 msg
->event_time
= event_time
;
567 * 'password_cb()' - Return the cached password.
570 static const char * /* O - Cached password */
571 password_cb(const char *prompt
) /* I - Prompt string, unused */
575 return (rss_password
);
580 * 'save_rss()' - Save messages to a RSS file.
583 static int /* O - 1 on success, 0 on failure */
584 save_rss(cups_array_t
*rss
, /* I - RSS messages */
585 const char *filename
, /* I - File to save to */
586 const char *baseurl
) /* I - Base URL */
588 FILE *fp
; /* File pointer */
589 _cups_rss_t
*msg
; /* Current message */
590 char date
[1024]; /* Current date */
593 if ((fp
= fopen(filename
, "w")) == NULL
)
595 fprintf(stderr
, "ERROR: Unable to create %s: %s\n", filename
,
600 fputs("<?xml version=\"1.0\"?>\n", fp
);
601 fputs("<rss version=\"2.0\">\n", fp
);
602 fputs(" <channel>\n", fp
);
603 fputs(" <title>CUPS RSS Feed</title>\n", fp
);
604 fprintf(fp
, " <link>%s</link>\n", baseurl
);
605 fputs(" <description>CUPS RSS Feed</title>\n", fp
);
606 fputs(" <generator>" CUPS_SVERSION
"</generator>\n", fp
);
607 fputs(" <ttl>1</ttl>\n", fp
);
609 fprintf(fp
, " <pubDate>%s</pubDate>\n",
610 httpGetDateString2(time(NULL
), date
, sizeof(date
)));
612 for (msg
= (_cups_rss_t
*)cupsArrayLast(rss
);
614 msg
= (_cups_rss_t
*)cupsArrayPrev(rss
))
616 fputs(" <item>\n", fp
);
617 fprintf(fp
, " <title>%s</title>\n", msg
->subject
);
618 fprintf(fp
, " <description>%s</description>\n", msg
->text
);
620 fprintf(fp
, " <link>%s</link>\n", msg
->link_url
);
621 fprintf(fp
, " <pubDate>%s</pubDate>\n",
622 httpGetDateString2(msg
->event_time
, date
, sizeof(date
)));
623 fprintf(fp
, " <guid>%d</guid>\n", msg
->sequence_number
);
624 fputs(" </item>\n", fp
);
627 fputs(" </channel>\n", fp
);
628 fputs("</rss>\n", fp
);
630 return (!fclose(fp
));
635 * 'xml_escape()' - Copy a string, escaping &, <, and > as needed.
638 static char * /* O - Escaped string */
639 xml_escape(const char *s
) /* I - String to escape */
641 char *e
, /* Escaped string */
642 *eptr
; /* Pointer into escaped string */
643 const char *sptr
; /* Pointer into string */
644 size_t bytes
; /* Bytes needed for string */
648 * First figure out how many extra bytes we need...
651 for (bytes
= 0, sptr
= s
; *sptr
; sptr
++)
653 bytes
+= 4; /* & */
654 else if (*sptr
== '<' || *sptr
== '>')
655 bytes
+= 3; /* < and > */
658 * If there is nothing to escape, just strdup() it...
665 * Otherwise allocate memory and copy...
668 if ((e
= malloc(bytes
+ 1 + strlen(s
))) == NULL
)
671 for (eptr
= e
, sptr
= s
; *sptr
; sptr
++)
680 else if (*sptr
== '<')
687 else if (*sptr
== '>')
704 * End of "$Id: rss.c 6326 2007-03-11 17:50:18Z mike $".