home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / expire.c < prev    next >
Text File  |  1994-09-25  |  8KB  |  290 lines

  1. /*  expire.c   This program deletes old Usenet news articles.
  2.     Copyright (C) 1990, 1993  Rick Adams and Bob Billson
  3.  
  4.     This file is part of the OS-9 UUCP package, UUCPbb.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     The author of UUCPbb, Bob Billson, can be contacted at:
  21.     bob@kc2wz.bubble.org  or  uunet!kc2wz!bob  or  by snail mail:
  22.     21 Bates Way, Westfield, NJ 07090
  23. */
  24.  
  25. /* Options:
  26.    --------
  27.       -t         test (don't actually delete articles)
  28.       -x<level>  debug, default is 0 (OFF)
  29.       -n<group>  expire only given group
  30.       -e<days>   expire articles older than specified number of days
  31.  
  32.      With no arguments, expires all articles older than 14 days in
  33.      all newsgroups.  */
  34.  
  35. #define MAIN                                       /* added -- REB */
  36.  
  37. #include "uucp.h"
  38. #include <modes.h>
  39. #include <signal.h>
  40.  
  41. #define MAXDAYS  14                     /* default time article lives --REB */
  42.  
  43. QQ int expireflag = TRUE;
  44. QQ int debuglvl = 0;
  45. QQ int ngroups;
  46. QQ FILE *log;
  47. QQ int gotactive, ramdisk, logopen;
  48. QQ unsigned myuid;                              /* to keep makepath() happy */
  49. char sender[] = "expire";                       /*                          */
  50. char fname[100];                                /* to keep getdirs() happy  */
  51. struct active groups[MAXNEWSGROUPS];
  52.  
  53.  
  54. main (argc, argv)
  55. int argc;
  56. char *argv[];
  57. {
  58.      char newsgroup[100];
  59.      register int i;                                      /* Changed -- REB */
  60.      int limit, found;
  61.      int option, interrupt(), onegroup;                   /* Added -- REB */
  62.      char *fixgroupname();
  63.  
  64.      *newsgroup = '\0';
  65.      found = FALSE;
  66.      limit = MAXDAYS;
  67.      gotactive = FALSE;                          /* Added --REB */
  68.      onegroup = FALSE;                           /*              */
  69.      logopen = FALSE;
  70.  
  71.      myuid = getuid();
  72.  
  73.      if (myuid != 0)
  74.           fatal ("you are not the superuser!");
  75.  
  76.      intercept (interrupt);                      /* signal trap --REB */
  77.  
  78.      if ((newsdir = getdirs ("newsdir")) == NULL)
  79.           fatal ("newsdir not in Parameters");
  80.  
  81.      if ((logdir = getenv ("LOGDIR")) != NULL)
  82.           logdir = strdup (logdir);
  83.      else
  84.           logdir = LOGDIR;
  85.  
  86.      /* Added --REB */
  87.      while ((option = getopt (argc, argv, "e:x:n:t")) != EOF)
  88.           switch (option)
  89.             {
  90.                case 'e':
  91.                     limit = atoi (optarg);
  92.                     break;
  93.  
  94.                case 'x':
  95.                     debuglvl = atoi (optarg);
  96.                     break;
  97.  
  98.                case 'n':
  99.                     strcpy (newsgroup, optarg);
  100.                     onegroup = TRUE;
  101.                     break;
  102.  
  103.                case 't':
  104.                     expireflag = FALSE;
  105.                     break;
  106.  
  107.                case '?':
  108.                     usage();
  109.                     break;
  110.             }
  111.  
  112.      openlog();
  113.  
  114.      if (!onegroup)
  115.           fprintf (log, "%s %s expiring articles over %d days old\n",
  116.                         sender, gtime(), limit);
  117.  
  118.      /* read active file into in-core table */
  119.      readactive (S_IREAD|S_IWRITE|S_ISHARE, FALSE);
  120.      gotactive = TRUE;
  121.  
  122.      /* cycle through available newsgroups */
  123.      for (i = 0;  i < ngroups && !found;  i++)
  124.        {
  125.           char *ngroup;
  126.  
  127.           ngroup = NULL;
  128.  
  129.           /* Only expire one newsgroup? */
  130.           if (*newsgroup != '\0')
  131.                if (strucmp (newsgroup, groups[i].newsgroup) == 0)
  132.                     found = TRUE;
  133.                else
  134.                     continue;
  135.  
  136.           if (expireflag)
  137.             {
  138.                fprintf (log, "%s %s expiring: %s", 
  139.                               sender, gtime(), groups[i].newsgroup);
  140.                if (onegroup)
  141.                     fprintf (log, " articles over %d days old", limit);
  142.  
  143.                putc ('\n', log);
  144.             }
  145.           else
  146.                printf ("would expire '%s' newsgroup\n", groups[i].newsgroup);
  147.  
  148.           if (chdir (newsdir) == ERROR)
  149.             {
  150.                sprintf (fname, "can't change to news directory '%s'",                                         newsdir);
  151.                fatal (fname);
  152.             }
  153.  
  154.           ngroup = fixgroupname (groups[i].newsgroup);
  155.           makepath (groups[i].newsgroup);
  156.  
  157.           if (ngroup != NULL)
  158.             {
  159.                free (ngroup);
  160.                ngroup = NULL;
  161.             }
  162.           expgroup (&(groups[i]), limit);
  163.        }
  164.  
  165.      if (*newsgroup != '\0'  &&  !found)
  166.           fprintf (log, "%s %s newsgroup not found: %s\n",
  167.                         sender, gtime(), newsgroup);
  168.  
  169.      /* Update active file to reflect expired news articles */
  170.      updactive();
  171.      closelog();
  172.      exit (0);
  173. }
  174.  
  175.  
  176.  
  177. int openlog()
  178. {
  179.      char temp[80];
  180.  
  181.      ramdisk = FALSE;
  182.  
  183.      /* For testing send everything to the screen --REB */
  184.      if (expireflag == FALSE)
  185.           log = stdout;
  186.  
  187.      /* with debug on, don't use the RAM disk --REB */
  188.      else if (debuglvl > 0)
  189.        {
  190.           sprintf (temp, "%s/uulog", logdir);
  191.           log = fopen (temp, "a");
  192.           logopen = TRUE;
  193.        }
  194.  
  195.      /* for normal use (debug off), use RAM disk for temporary log -- REB */
  196.      else
  197.        {
  198.           sprintf (temp, "%s/uulog", RAMDISK);
  199.  
  200.           if ((log = fopen (temp, "w")) != NULL)
  201.                ramdisk = TRUE;
  202.           else
  203.             {
  204.                sprintf (temp, "%s/uulog", logdir);
  205.                log = fopen (temp, "a");
  206.             }
  207.  
  208.           logopen = TRUE;
  209.        }
  210.  
  211.      /* log file should be unbuffered -- REB */
  212.      setbuf (log, NULL);
  213. }
  214.  
  215.  
  216.  
  217. int closelog()
  218. {
  219.      char temp[80], temp2[80];
  220.  
  221.      /* close log file -- REB */
  222.      if (logopen)
  223.        {
  224.           fclose (log);
  225.           logopen = FALSE;
  226.  
  227.           /* move RAM disk copy to permanent log file -- REB */
  228.           if (ramdisk)
  229.             {
  230.                asetuid (0);
  231.                sprintf (temp2, "%s/uulog", RAMDISK);
  232.                sprintf (temp, "%s/uulog", logdir);
  233.                fileapnd (temp2, temp, TRUE);
  234.                unlink (temp2);
  235.             }
  236.        }
  237. }
  238.  
  239.  
  240.  
  241. /* clean up if we get a keyboard interrupt --REB */
  242.  
  243. int interrupt (sig)
  244. int sig;
  245. {
  246.      closelog();
  247.      exit (sig);
  248. }
  249.  
  250.  
  251.  
  252. int usage()
  253. {
  254.      char *strdetab(), temp[80];
  255.      register char **use;
  256.      static char *usetxt[] = {
  257.           "expire\t\tnews article expire utility",
  258.           " ",
  259.           "\t-t\t\t test (don't actually delete articles)",
  260.           "\t-x<level>  debug, default is 0 (OFF)",
  261.           "\t-n<group>  expire only given group",
  262.           "\t-e<days>   expire articles older than specified number of days",
  263.           " ",
  264.           "\tWith no arguments, expires all articles older than 14 days in",
  265.           "\tall newsgroups.",
  266.           NULL
  267.        };
  268.  
  269.      for (use = usetxt; *use != NULL; ++use)
  270.           fprintf (stderr, " %s\n", strdetab (strcpy (temp, *use), 4));
  271.  
  272.      fprintf (stderr, "\nv%s (%s) This is free software released under the GNU General Public\n",
  273.                       version, VERDATE);
  274.      fputs ("License.  Please send suggestions/bug reports to:  bob@kc2wz.bubble.org\n", stderr);
  275.      exit (0);
  276. }
  277.  
  278.  
  279.  
  280. int fatal (msg)
  281. char *msg;
  282. {
  283.      fprintf (stderr, "expire: %s", msg);
  284.  
  285.      if (errno != 0)
  286.           fprintf (stderr, "...error %d\n", errno);
  287.  
  288.      exit (0);
  289. }
  290.