home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES4.ZIP / RNEWS / expire.c next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  11.9 KB  |  357 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    e x p i r e . c                                                 */
  3. /*                                                                    */
  4. /*    Expire old news articles for UUPC/extended                      */
  5. /*                                                                    */
  6. /*    Copyright (c) 1992-1993 by Kendra Electronic Wonderworks, all   */
  7. /*    rights reserved except those explicitly granted by the UUPC/    */
  8. /*    extended license.                                               */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /* new version, rewritten for history-based news database
  12.  *
  13.  * Author:  Kai Uwe Rommel <rommel@ars.muc.de>
  14.  * Created: Sun Aug 15 1993
  15.  */
  16.  
  17. static char *rcsid = "$Id: expire.c 1.2 1993/10/31 21:32:55 ahd Exp $";
  18. static char *rcsrev = "$Revision: 1.2 $";
  19.  
  20. /* $Log: expire.c $
  21.  * Revision 1.2  1993/10/31  21:32:55  ahd
  22.  * Delete unreferenced variables
  23.  *
  24.  * Revision 1.1  1993/09/05  10:56:49  rommel
  25.  * Initial revision
  26.  * */
  27.  
  28. /*--------------------------------------------------------------------*/
  29. /*                        System include files                        */
  30. /*--------------------------------------------------------------------*/
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <time.h>
  37. #include <limits.h>
  38.  
  39. /*--------------------------------------------------------------------*/
  40. /*                    UUPC/extended include files                     */
  41. /*--------------------------------------------------------------------*/
  42.  
  43. #include "lib.h"
  44. #include "active.h"
  45. #include "dater.h"
  46. #include "getopt.h"
  47. #include "history.h"
  48. #include "hlib.h"
  49. #include "import.h"
  50. #include "importng.h"
  51. #include "logger.h"
  52. #include "uundir.h"
  53. #include "pushpop.h"
  54. #include "stater.h"
  55. #include "timestmp.h"
  56.  
  57. /*--------------------------------------------------------------------*/
  58. /*                          Global variables                          */
  59. /*--------------------------------------------------------------------*/
  60.  
  61. extern struct grp *group_list;   /* List of all groups */
  62.  
  63. /*--------------------------------------------------------------------*/
  64. /*                          Global Variables                          */
  65. /*--------------------------------------------------------------------*/
  66.  
  67. #define ONE_DAY (60L*60L*24L)
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*                        Internal prototypes                         */
  71. /*--------------------------------------------------------------------*/
  72.  
  73. static void ExpireAll( char **groups, const time_t expire_date );
  74.  
  75. static void usage( void );
  76.  
  77. long total_articles_purged = 0;
  78. long total_articles_kept   = 0;
  79. long total_cross_purged    = 0;
  80. long total_cross_kept      = 0;
  81. long total_bytes_purged    = 0;
  82. long total_bytes_kept      = 0;
  83.  
  84. void *history;
  85. void *new_history;
  86.  
  87. /*--------------------------------------------------------------------*/
  88. /*    m a i n                                                         */
  89. /*                                                                    */
  90. /*    Main program                                                    */
  91. /*--------------------------------------------------------------------*/
  92.  
  93. void main( int argc, char **argv)
  94. {
  95.    int c;
  96.    extern char *optarg;
  97.    extern int   optind;
  98.    char **groups = NULL;
  99.    struct grp *cur_grp;
  100.    char file_old[FILENAME_MAX], file_new[FILENAME_MAX];
  101.  
  102.    time_t expire_period  = 7; /* Seven days visible to users         */
  103.    time_t expire_date;
  104.  
  105. /*--------------------------------------------------------------------*/
  106. /*     Report our version number and date/time compiled               */
  107. /*--------------------------------------------------------------------*/
  108.  
  109.    debuglevel = 1;
  110.    banner( argv );
  111.  
  112. #if defined(__CORE__)
  113.    copywrong = strdup(copyright);
  114.    checkref(copywrong);
  115. #endif
  116.  
  117. /*--------------------------------------------------------------------*/
  118. /*        Process our arguments                                       */
  119. /*--------------------------------------------------------------------*/
  120.  
  121.    while ((c = getopt(argc, argv, "e:x:")) !=  EOF)
  122.       switch(c) {
  123.  
  124.       case 'e':
  125.          expire_period = atoi( optarg );
  126.          break;
  127.  
  128.       case 'x':
  129.          debuglevel = atoi( optarg );
  130.          break;
  131.  
  132.       case '?':
  133.          usage();
  134.          exit(1);
  135.          break;
  136.  
  137.       default:
  138.          printmsg(0, "expire - invalid option -%c", c);
  139.          usage();
  140.          exit(2);
  141.          break;
  142.    }
  143.  
  144.    if (optind != argc)
  145.      groups = argv + optind;
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*                             Initialize                             */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.    tzset();                      /* Set up time zone information  */
  152.  
  153.    if (!configure( B_NEWS ))
  154.       exit(1);   /* system configuration failed */
  155.  
  156. /*--------------------------------------------------------------------*/
  157. /*                  Switch to the spooling directory                  */
  158. /*--------------------------------------------------------------------*/
  159.  
  160.    PushDir( E_newsdir );
  161.    atexit( PopDir );
  162.  
  163. /*--------------------------------------------------------------------*/
  164. /*                     Initialize logging file                        */
  165. /*--------------------------------------------------------------------*/
  166.  
  167.    openlog( NULL );
  168.  
  169. /*--------------------------------------------------------------------*/
  170. /*                       Load the active file                         */
  171. /*--------------------------------------------------------------------*/
  172.  
  173.    history = open_history("history");
  174.    new_history = open_history("newhist");
  175.  
  176.    get_active();              /* Get sequence numbers for groups from
  177.                                  active file                      */
  178.  
  179.    /* think as if we would purge everything at the moment :-) */
  180.    for ( cur_grp = group_list; cur_grp != NULL;
  181.          cur_grp = cur_grp->grp_next )
  182.      if (stricmp(cur_grp->grp_name, "junk") != 0)
  183.        cur_grp->grp_low = cur_grp->grp_high;
  184.  
  185. /*--------------------------------------------------------------------*/
  186. /*                  Compute times for expiring files                  */
  187. /*--------------------------------------------------------------------*/
  188.  
  189.    time( &expire_date );
  190.    expire_date  -= (expire_period * ONE_DAY);
  191.  
  192.    printmsg(1,"expire: Purging news older than %s (%ld days)",
  193.             dater( expire_date , NULL), (long) expire_period );
  194.  
  195. /*--------------------------------------------------------------------*/
  196. /*    Process groups as requested, otherwise process the entire       */
  197. /*    history database                                                */
  198. /*--------------------------------------------------------------------*/
  199.  
  200.    ExpireAll(groups, expire_date);
  201.  
  202. /*--------------------------------------------------------------------*/
  203. /*                         Clean up and exit                          */
  204. /*--------------------------------------------------------------------*/
  205.  
  206.    put_active();
  207.  
  208.    close_history(history);
  209.    close_history(new_history);
  210.  
  211.    mkfilename(file_old, E_newsdir, "oldhist.dir");
  212.    mkfilename(file_new, E_newsdir, "history.dir");
  213.    unlink(file_old);
  214.    rename(file_new, file_old);
  215.    mkfilename(file_old, E_newsdir, "newhist.dir");
  216.    rename(file_old, file_new);
  217.  
  218.    mkfilename(file_old, E_newsdir, "oldhist.pag");
  219.    mkfilename(file_new, E_newsdir, "history.pag");
  220.    unlink(file_old);
  221.    rename(file_new, file_old);
  222.    mkfilename(file_old, E_newsdir, "newhist.pag");
  223.    rename(file_old, file_new);
  224.  
  225.    if ( total_articles_purged)
  226.       printmsg(1,"Purged %ld articles, %ld cross postings (%ld bytes).",
  227.                total_articles_purged, total_cross_purged, total_bytes_purged );
  228.  
  229.    printmsg(1,"Total of %ld articles, %ld cross postings (%ld bytes)." ,
  230.             total_articles_kept, total_cross_kept, total_bytes_kept );
  231.  
  232.    exit(0);
  233.  
  234. } /* main */
  235.  
  236. /*--------------------------------------------------------------------*/
  237. /*    f i n d _ n e w s g r o u p                                     */
  238. /*                                                                    */
  239. /*    Locate a news group in our list                                 */
  240. /*--------------------------------------------------------------------*/
  241.  
  242. static struct grp *find_newsgroup(const char *grp)
  243. {
  244.    struct grp *cur = group_list;
  245.  
  246.    while ((strcmp(grp,cur->grp_name) != 0)) {
  247.       if (cur->grp_next != NULL) {
  248.          cur = cur->grp_next;
  249.       } else {
  250.          return NULL;
  251.       }
  252.    }
  253.  
  254.    return cur;
  255. }
  256.  
  257. /*--------------------------------------------------------------------*/
  258. /*    S e t G r o u p L o w e r                                       */
  259. /*                                                                    */
  260. /*    Set the lower bounds of all groups an article is posted to      */
  261. /*--------------------------------------------------------------------*/
  262.  
  263. static void SetGroupLower(char *histentry)
  264. {
  265.   char value[BUFSIZ];
  266.   char *group, *num;
  267.   struct grp *cur_grp;
  268.   long article;
  269.  
  270.   strcpy(value, histentry);
  271.   strtok(value, " ");   /* strip off date */
  272.   strtok(NULL, " ");    /* strip off size */
  273.  
  274.   while ((group = strtok(NULL, " ,\n")) != NULL)
  275.   {
  276.     num = strchr(group, ':');
  277.     *num++ = 0;
  278.     article = atol(num);
  279.  
  280.     if ( (cur_grp = find_newsgroup(group)) != NULL && article > 0 )
  281.       if ( cur_grp->grp_low > article )
  282.         cur_grp->grp_low = article;
  283.   }
  284. }
  285.  
  286. /*--------------------------------------------------------------------*/
  287. /*    E x p i r e A l l                                               */
  288. /*                                                                    */
  289. /*    Expire all requested news groups                                */
  290. /*--------------------------------------------------------------------*/
  291.  
  292. static void ExpireAll( char **groups, const time_t expire_date )
  293. {
  294.    char *messageID, *histentry;
  295.    time_t article_date;
  296.    long article_size;
  297.    int total, remaining, deleted;
  298.    int found;
  299.  
  300.    for (found = get_first_histentry(history, &messageID, &histentry); found;
  301.         found = get_next_histentry(history, &messageID, &histentry))
  302.    {
  303.      sscanf(histentry, "%ld %ld",
  304.             &article_date, &article_size);
  305.  
  306.      remaining = total = count_postings(histentry);
  307.  
  308.      if (article_date < expire_date)
  309.      {
  310.        if ((histentry = purge_article(histentry, groups)) == NULL)
  311.          remaining = 0;
  312.        else
  313.          remaining = count_postings(histentry);
  314.      }
  315.  
  316.      if ( remaining )
  317.      {
  318.        add_histentry(new_history, messageID, histentry);
  319.        SetGroupLower(histentry);
  320.      }
  321.  
  322.      deleted = (total - remaining);
  323.  
  324.      if (deleted > 1)
  325.      {
  326.        total_articles_purged += 1;
  327.        total_cross_purged    += (deleted - 1);
  328.      }
  329.      else
  330.        total_articles_purged += deleted;
  331.  
  332.      if (remaining > 1)
  333.      {
  334.        total_articles_kept   += 1;
  335.        total_cross_kept      += (remaining - 1);
  336.      }
  337.      else
  338.        total_articles_kept   += remaining;
  339.  
  340.      total_bytes_purged    += deleted * article_size;
  341.      total_bytes_kept      += remaining * article_size;
  342.    }
  343.  
  344. } /* Expire_All */
  345.  
  346. /*--------------------------------------------------------------------*/
  347. /*    u s a g e                                                       */
  348. /*                                                                    */
  349. /*    Print usage of program                                          */
  350. /*--------------------------------------------------------------------*/
  351.  
  352. static void usage( void )
  353. {
  354.    printf( "Usage:   expire [-edays] [group ...]\n");
  355.    exit(1);
  356. } /* usage */
  357.