]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - notifier/rss.c
Import CUPS v1.7.1
[thirdparty/cups.git] / notifier / rss.c
index f82a78caebd496b0a19f4f562ad6a5128533f99f..65e577f0b5650d9876b5d9330f4f53db27d605c3 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: rss.c 6596 2007-06-21 22:42:25Z mike $"
+ * "$Id: rss.c 10996 2013-05-29 11:51:34Z msweet $"
  *
- *   RSS notifier for the Common UNIX Printing System (CUPS).
+ *   RSS notifier for CUPS.
  *
+ *   Copyright 2007-2012 by Apple Inc.
  *   Copyright 2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
 
 #include <cups/cups.h>
 #include <cups/language.h>
-#include <cups/string.h>
+#include <cups/string-private.h>
 #include <cups/array.h>
-#include <errno.h>
+#include <sys/select.h>
+#include <cups/ipp-private.h>  /* TODO: Update so we don't need this */
 
 
 /*
@@ -118,6 +110,10 @@ main(int  argc,                            /* I - Number of command-line arguments */
   cups_array_t *rss;                   /* RSS message array */
   _cups_rss_t  *msg;                   /* RSS message */
   char         baseurl[1024];          /* Base URL */
+  fd_set       input;                  /* Input set for select() */
+  struct timeval timeout;              /* Timeout for select() */
+  int          changed;                /* Has the RSS data changed? */
+  int          exit_status;            /* Exit status */
 
 
   fprintf(stderr, "DEBUG: argc=%d\n", argc);
@@ -238,6 +234,8 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
   load_rss(rss, filename);
 
+  changed = cupsArrayCount(rss) == 0;
+
  /*
   * Localize for the user's chosen language...
   */
@@ -248,8 +246,59 @@ main(int  argc,                            /* I - Number of command-line arguments */
   * Read events and update the RSS file until we are out of events.
   */
 
-  for (;;)
+  for (exit_status = 0, event = NULL;;)
   {
+    if (changed)
+    {
+     /*
+      * Save the messages to the file again, uploading as needed...
+      */
+
+      if (save_rss(rss, newname, baseurl))
+      {
+       if (http)
+       {
+        /*
+          * Upload the RSS file...
+         */
+
+          if ((status = cupsPutFile(http, resource, filename)) != HTTP_CREATED)
+            fprintf(stderr, "ERROR: Unable to PUT %s from %s on port %d: %d %s\n",
+                   resource, host, port, status, httpStatus(status));
+       }
+       else
+       {
+        /*
+          * Move the new RSS file over top the old one...
+         */
+
+          if (rename(newname, filename))
+            fprintf(stderr, "ERROR: Unable to rename %s to %s: %s\n",
+                   newname, filename, strerror(errno));
+       }
+
+       changed = 0;
+      }
+    }
+
+   /*
+    * Wait up to 30 seconds for an event...
+    */
+
+    timeout.tv_sec  = 30;
+    timeout.tv_usec = 0;
+
+    FD_ZERO(&input);
+    FD_SET(0, &input);
+
+    if (select(1, &input, NULL, NULL, &timeout) < 0)
+      continue;
+    else if (!FD_ISSET(0, &input))
+    {
+      fprintf(stderr, "DEBUG: %s is bored, exiting...\n", argv[1]);
+      break;
+    }
+
    /*
     * Read the next event...
     */
@@ -265,16 +314,7 @@ main(int  argc,                            /* I - Number of command-line arguments */
       fputs("DEBUG: ippReadFile() returned IPP_ERROR!\n", stderr);
 
     if (state <= IPP_IDLE)
-    {
-      ippDelete(event);
-
-      if (http)
-        unlink(filename);
-
-      httpClose(http);
-
-      return (0);
-    }
+      break;
 
    /*
     * Collect the info from the event...
@@ -317,15 +357,8 @@ main(int  argc,                            /* I - Number of command-line arguments */
       {
         fprintf(stderr, "ERROR: Unable to create message: %s\n",
                strerror(errno));
-
-        ippDelete(event);
-
-       if (http)
-          unlink(filename);
-
-        httpClose(http);
-
-       return (1);
+        exit_status = 1;
+       break;
       }
 
      /*
@@ -334,6 +367,8 @@ main(int  argc,                             /* I - Number of command-line arguments */
 
       cupsArrayAdd(rss, msg);
 
+      changed = 1;
+
      /*
       * Trim the array as needed...
       */
@@ -346,34 +381,6 @@ main(int  argc,                            /* I - Number of command-line arguments */
 
        delete_message(msg);
       }
-
-     /*
-      * Save the messages to the file again, uploading as needed...
-      */ 
-
-      if (save_rss(rss, newname, baseurl))
-      {
-       if (http)
-       {
-        /*
-          * Upload the RSS file...
-         */
-
-          if ((status = cupsPutFile(http, resource, filename)) != HTTP_CREATED)
-            fprintf(stderr, "ERROR: Unable to PUT %s from %s on port %d: %d %s\n",
-                   resource, host, port, status, httpStatus(status));
-       }
-       else
-       {
-        /*
-          * Move the new RSS file over top the old one...
-         */
-
-          if (rename(newname, filename))
-            fprintf(stderr, "ERROR: Unable to rename %s to %s: %s\n",
-                   newname, filename, strerror(errno));
-       }
-      }
     }
 
     if (subject)
@@ -383,7 +390,22 @@ main(int  argc,                            /* I - Number of command-line arguments */
       free(text);
 
     ippDelete(event);
+    event = NULL;
   }
+
+ /*
+  * We only get here when idle or error...
+  */
+
+  ippDelete(event);
+
+  if (http)
+  {
+    unlink(filename);
+    httpClose(http);
+  }
+
+  return (exit_status);
 }
 
 
@@ -532,6 +554,15 @@ load_rss(cups_array_t *rss,                /* I - RSS messages */
       sequence_number = atoi(start + 6);
   }
 
+  if (subject)
+    free(subject);
+
+  if (text)
+    free(text);
+
+  if (link_url)
+    free(link_url);
+
   fclose(fp);
 }
 
@@ -588,6 +619,7 @@ save_rss(cups_array_t *rss,         /* I - RSS messages */
   FILE         *fp;                    /* File pointer */
   _cups_rss_t  *msg;                   /* Current message */
   char         date[1024];             /* Current date */
+  char         *href;                  /* Escaped base URL */
 
 
   if ((fp = fopen(filename, "w")) == NULL)
@@ -601,7 +633,11 @@ save_rss(cups_array_t *rss,                /* I - RSS messages */
   fputs("<rss version=\"2.0\">\n", fp);
   fputs("  <channel>\n", fp);
   fputs("    <title>CUPS RSS Feed</title>\n", fp);
-  fprintf(fp, "    <link>%s</link>\n", baseurl);
+
+  href = xml_escape(baseurl);
+  fprintf(fp, "    <link>%s</link>\n", href);
+  free(href);
+
   fputs("    <description>CUPS RSS Feed</description>\n", fp);
   fputs("    <generator>" CUPS_SVERSION "</generator>\n", fp);
   fputs("    <ttl>1</ttl>\n", fp);
@@ -701,5 +737,5 @@ xml_escape(const char *s)           /* I - String to escape */
 
 
 /*
- * End of "$Id: rss.c 6596 2007-06-21 22:42:25Z mike $".
+ * End of "$Id: rss.c 10996 2013-05-29 11:51:34Z msweet $".
  */