home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES4.ZIP / RNEWS / genhist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-06  |  12.2 KB  |  400 lines

  1. /* genhist.c
  2.  *
  3.  * (Re-)Create history database from scratch.
  4.  * Based on parts of the old expire program.
  5.  *
  6.  * Author:  Kai Uwe Rommel <rommel@ars.muc.de>
  7.  * Created: Sun Aug 15 1993
  8.  */
  9.  
  10. /*--------------------------------------------------------------------*/
  11. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  12. /*       Wonderworks.                                                 */
  13. /*                                                                    */
  14. /*       All rights reserved except those explicitly granted by       */
  15. /*       the UUPC/extended license agreement.                         */
  16. /*--------------------------------------------------------------------*/
  17.  
  18. /*--------------------------------------------------------------------*/
  19. /*                          RCS Information                           */
  20. /*--------------------------------------------------------------------*/
  21.  
  22. static char *rcsid = "$Id: genhist.c 1.3 1993/11/06 13:04:13 ahd Exp $";
  23. static char *rcsrev = "$Revision: 1.3 $";
  24.  
  25. /* $Log: genhist.c $
  26.  * Revision 1.3  1993/11/06  13:04:13  ahd
  27.  * Update usage message
  28.  *
  29.  * Revision 1.2  1993/10/30  22:27:57  rommel
  30.  * Lower debug level to 1
  31.  *
  32.  * Revision 1.1  1993/09/05  10:56:49  rommel
  33.  * Initial revision
  34.  * */
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                        System include files                        */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <time.h>
  45. #include <limits.h>
  46.  
  47. /*--------------------------------------------------------------------*/
  48. /*                    UUPC/extended include files                     */
  49. /*--------------------------------------------------------------------*/
  50.  
  51. #include "lib.h"
  52. #include "active.h"
  53. #include "dater.h"
  54. #include "getopt.h"
  55. #include "history.h"
  56. #include "hlib.h"
  57. #include "import.h"
  58. #include "importng.h"
  59. #include "logger.h"
  60. #include "uundir.h"
  61. #include "pushpop.h"
  62. #include "stater.h"
  63. #include "timestmp.h"
  64.  
  65. currentfile();
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*                        Internal prototypes                         */
  69. /*--------------------------------------------------------------------*/
  70.  
  71. static void IndexAll( void );
  72. static void IndexOneGroup( struct grp *cur_grp );
  73. static void IndexDirectory( struct grp *cur_grp, const char *directory );
  74.  
  75. static boolean numeric( char *start);
  76.  
  77. static void usage( void );
  78.  
  79. long total_articles = 0;
  80. long total_files = 0;
  81. long total_bytes = 0;
  82.  
  83. void *history;
  84.  
  85. /*--------------------------------------------------------------------*/
  86. /*    m a i n                                                         */
  87. /*                                                                    */
  88. /*    Main program                                                    */
  89. /*--------------------------------------------------------------------*/
  90.  
  91. void main( int argc, char **argv)
  92. {
  93.    extern char *optarg;
  94.    extern int   optind;
  95.    char *group = NULL;
  96.    char file_old[FILENAME_MAX], file_new[FILENAME_MAX];
  97.    int c;
  98.  
  99. /*--------------------------------------------------------------------*/
  100. /*     Report our version number and date/time compiled               */
  101. /*--------------------------------------------------------------------*/
  102.  
  103.    debuglevel = 1;
  104.    banner( argv );
  105.  
  106. #if defined(__CORE__)
  107.    copywrong = strdup(copyright);
  108.    checkref(copywrong);
  109. #endif
  110.  
  111. /*--------------------------------------------------------------------*/
  112. /*        Process our arguments                                       */
  113. /*--------------------------------------------------------------------*/
  114.  
  115.    while ((c = getopt(argc, argv, "x:")) !=  EOF)
  116.       switch(c) {
  117.  
  118.       case 'x':
  119.          debuglevel = atoi( optarg );
  120.          break;
  121.  
  122.       case '?':
  123.          usage();
  124.          exit(1);
  125.          break;
  126.  
  127.       default:
  128.          printmsg(0, "genhist - invalid option -%c", c);
  129.          usage();
  130.          exit(2);
  131.          break;
  132.    }
  133.  
  134. /*--------------------------------------------------------------------*/
  135. /*                             Initialize                             */
  136. /*--------------------------------------------------------------------*/
  137.  
  138.    tzset();                      /* Set up time zone information  */
  139.  
  140.    if (!configure( B_NEWS ))
  141.       exit(1);   /* system configuration failed */
  142.  
  143.    PushDir( E_newsdir );
  144.    atexit( PopDir );
  145.  
  146.    openlog( NULL );
  147.  
  148.    get_active();
  149.  
  150.    mkfilename(file_old, E_newsdir, "oldhist.dir");
  151.    mkfilename(file_new, E_newsdir, "history.dir");
  152.    unlink(file_old);
  153.    rename(file_new, file_old);
  154.    mkfilename(file_old, E_newsdir, "oldhist.pag");
  155.    mkfilename(file_new, E_newsdir, "history.pag");
  156.    unlink(file_old);
  157.    rename(file_new, file_old);
  158.  
  159.    history = open_history("history");
  160.  
  161.    IndexAll();
  162.  
  163.    close_history(history);
  164.  
  165.    printmsg(1,"%s: Processed %ld total articles in %ld files (%ld bytes).",
  166.                   argv[0], total_articles, total_files, total_bytes );
  167.    exit(0);
  168.  
  169. } /* main */
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*    I n d e x A l l                                                 */
  173. /*--------------------------------------------------------------------*/
  174.  
  175. static void IndexAll( void )
  176. {
  177.    struct grp *cur_grp = group_list;
  178.  
  179.    while ( cur_grp != NULL )
  180.    {
  181.       IndexOneGroup( cur_grp);
  182.       cur_grp = cur_grp->grp_next;
  183.    }
  184. } /* IndexAll */
  185.  
  186. /*--------------------------------------------------------------------*/
  187. /*    I n d e x O n e G r o u p                                       */
  188. /*--------------------------------------------------------------------*/
  189.  
  190. static void IndexOneGroup( struct grp *cur_grp )
  191. {
  192.    char groupdir[FILENAME_MAX];
  193.    char archdir[FILENAME_MAX];
  194.  
  195.    printmsg(3,"Processing news group %s", cur_grp->grp_name );
  196.  
  197. /*--------------------------------------------------------------------*/
  198. /*                     Set up the directory names                     */
  199. /*--------------------------------------------------------------------*/
  200.  
  201.    ImportNewsGroup( groupdir, cur_grp->grp_name, 0 );
  202.    mkfilename( archdir, E_archivedir, &groupdir[ strlen( E_newsdir) + 1] );
  203.  
  204. /*--------------------------------------------------------------------*/
  205. /*            Process the directory                                   */
  206. /*--------------------------------------------------------------------*/
  207.  
  208.    IndexDirectory( cur_grp, groupdir );
  209.  
  210. } /* IndexOneGroup */
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*    G e t H i s t o r y D a t a                                     */
  214. /*--------------------------------------------------------------------*/
  215.  
  216. static void GetHistoryData(char *group, struct direct *dp,
  217.                            char *messageID, char *histentry)
  218. {
  219.   FILE *article;
  220.   char line[BUFSIZ], *ptr, *item;
  221.   int line_len, first, b_xref = 0, b_msgid = 0;
  222.  
  223.   article = FOPEN(dp->d_name, "r", TEXT_MODE);
  224.   if ( article == NULL )
  225.   {
  226.     printerr( dp->d_name );
  227.     panic();
  228.   }
  229.  
  230.   sprintf(histentry, "%ld %ld ", dp->d_modified, dp->d_size);
  231.  
  232.   while ( !b_xref || !b_msgid )
  233.   {
  234.     if ( fgets(line, sizeof(line), article) == NULL )
  235.       break;
  236.  
  237.     if ( (line_len = strlen(line)) <= 1 )
  238.       break;
  239.  
  240.     if (line[line_len - 1] == '\n')
  241.       line[(line_len--) - 1] = '\0';
  242.  
  243.     if (line[line_len - 1] == '\r')
  244.       line[(line_len--) - 1] = '\0';
  245.  
  246.     ptr = "Message-ID:";
  247.     if (equalni(line, ptr, strlen(ptr)))
  248.     {
  249.       ptr = line + strlen(ptr) + 1;
  250.       while (isspace(*ptr))
  251.         ptr++;
  252.       strcpy(messageID, ptr);
  253.       b_msgid++;
  254.       continue;
  255.     }
  256.  
  257.     ptr = "Xref:";
  258.     if (equalni(line, ptr, strlen(ptr)))
  259.     {
  260.       ptr = line + strlen(ptr) + 1;
  261.       while (isspace(*ptr))
  262.         ptr++;
  263.  
  264.       strtok(ptr, " "); /* strip off system name */
  265.       first = 1;
  266.  
  267.       while ((item = strtok(NULL, " ")) != NULL)
  268.       {
  269.         if (!first)
  270.           strcat(histentry, ",");
  271.         first = 0;
  272.         strcat(histentry, item);
  273.       }
  274.  
  275.       b_xref++;
  276.       continue;
  277.     }
  278.   }
  279.  
  280.   fclose(article);
  281.  
  282.   if ( !b_xref )
  283.     sprintf(histentry, "%ld %ld %s:%s",
  284.             dp->d_modified, dp->d_size, group, dp->d_name);
  285. }
  286.  
  287. /*--------------------------------------------------------------------*/
  288. /*    I n d e x D i r e c t o r y                                     */
  289. /*--------------------------------------------------------------------*/
  290.  
  291. static void IndexDirectory( struct grp *cur_grp,
  292.                             const char *directory )
  293. {
  294.    boolean not_built = TRUE;  /* Did not insure archive directory
  295.                                  exists                           */
  296.  
  297.    long low = LONG_MAX;  /* Oldest article number left             */
  298.  
  299.    long articles = 0;
  300.    long files = 0;
  301.    long bytes = 0;
  302.  
  303.    DIR *dirp;
  304.    struct direct *dp;
  305.  
  306.    char messageID[BUFSIZ], histentry[BUFSIZ];
  307.  
  308. /*--------------------------------------------------------------------*/
  309. /*                Open up the directory for processing                */
  310. /*--------------------------------------------------------------------*/
  311.  
  312.    if ((dirp = opendirx(directory,"*.*")) == nil(DIR))
  313.    {
  314.       printmsg(3, "IndexDirectory: couldn't opendir() %s", directory);
  315.       return;
  316.    } /* if */
  317.  
  318. /*--------------------------------------------------------------------*/
  319. /*                 Switch to directory for processing                 */
  320. /*--------------------------------------------------------------------*/
  321.  
  322.    CHDIR( directory );
  323.  
  324. /*--------------------------------------------------------------------*/
  325. /*              Look for the next file in the directory               */
  326. /*--------------------------------------------------------------------*/
  327.  
  328.    while((dp = readdir(dirp)) != nil(struct direct))
  329.    {
  330.  
  331. /*--------------------------------------------------------------------*/
  332. /*                      Add this file to history?                     */
  333. /*--------------------------------------------------------------------*/
  334.  
  335.       if ( numeric( dp->d_name ))/* Article format name?             */
  336.       {                          /* Yes --> Examine it closer        */
  337.  
  338.         printmsg(6,"Processing file %s from %s",
  339.                  dp->d_name, dater( dp->d_modified, NULL));
  340.  
  341.         GetHistoryData(cur_grp->grp_name, dp, messageID, histentry);
  342.  
  343.         if ( add_histentry(history, messageID, histentry) )
  344.           articles++;
  345.  
  346.         files++;
  347.         bytes += dp->d_size;
  348.       }
  349.  
  350.    } /* while */
  351.  
  352. /*--------------------------------------------------------------------*/
  353. /*           Close up the directory and report what we did            */
  354. /*--------------------------------------------------------------------*/
  355.  
  356.    closedir(dirp);
  357.  
  358.    if ( files )
  359.       printmsg(2,"%s: %ld articles in %ld files (%ld bytes)",
  360.                   cur_grp->grp_name, articles, files, bytes);
  361.  
  362.    total_articles += articles;
  363.    total_files += files;
  364.    total_bytes += bytes;
  365.  
  366. } /* IndexDirectory */
  367.  
  368. /*--------------------------------------------------------------------*/
  369. /*    n u m e r i c                                                   */
  370. /*                                                                    */
  371. /*    Examines string, returns true if numeric with period            */
  372. /*--------------------------------------------------------------------*/
  373.  
  374. static boolean numeric( char *start)
  375. {
  376.    char *number = start;
  377.  
  378.    while (*number != '\0')
  379.    {
  380.       if (!isdigit(*number) && (*number != '.'))
  381.          return FALSE;
  382.  
  383.       number++;
  384.    }
  385.  
  386.    return TRUE;
  387. } /* numeric */
  388.  
  389. /*--------------------------------------------------------------------*/
  390. /*    u s a g e                                                       */
  391. /*                                                                    */
  392. /*    Print usage of program                                          */
  393. /*--------------------------------------------------------------------*/
  394.  
  395. static void usage( void )
  396. {
  397.    printf( "Usage:   genhist\t[-x debuglevel]\n");
  398.    exit(1);
  399. } /* usage */
  400.