home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / ngdata.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  9KB  |  361 lines

  1. /* $Id: ngdata.c,v 4.4.3.1 1992/02/01 03:09:32 sob PATCH_3 sob $
  2.  *
  3.  * $Log: ngdata.c,v $
  4.  * Revision 4.4.3.1  1992/02/01  03:09:32  sob
  5.  * Release 4.4 Patchlevel 3
  6.  *
  7.  * Revision 4.4.2.1  1991/12/01  18:05:42  sob
  8.  * Patchlevel 2 changes
  9.  *
  10.  * Revision 4.4.1.1  1991/09/25  19:38:08  sob
  11.  * Some adaptions for CNEWS
  12.  *
  13.  * Revision 4.4  1991/09/09  20:23:31  sob
  14.  * release 4.4
  15.  *
  16.  *
  17.  * 
  18.  */
  19. /* This software is Copyright 1991 by Stan Barber. 
  20.  *
  21.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  22.  * use this software as long as: there is no monetary profit gained
  23.  * specifically from the use or reproduction of this software, it is not
  24.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  25.  * included prominently in any copy made. 
  26.  *
  27.  * The author make no claims as to the fitness or correctness of this software
  28.  * for any use whatsoever, and it is provided as is. Any use of this software
  29.  * is at the user's own risk. 
  30.  */
  31.  
  32. #include "EXTERN.h"
  33. #include "common.h"
  34. #include "ndir.h"
  35. #include "rcstuff.h"
  36. #include "rn.h"
  37. #include "ng.h"
  38. #include "intrp.h"
  39. #include "final.h"
  40. #include "rcln.h"
  41. #include "util.h"
  42. #ifdef SERVER
  43. #include "server.h"
  44. #endif
  45. #include "INTERN.h"
  46. #include "ngdata.h"
  47.  
  48. void
  49. ngdata_init()
  50. {
  51.     char *cp;
  52.  
  53.     /* open the active file */
  54.  
  55. #ifdef SERVER
  56. #ifdef DEBUGGING
  57.     if (debug & DEB_NNTP)
  58.     printf(">LIST\n") ; FLUSH;
  59. #endif
  60.     put_server("LIST");        /* tell server we want the active file */
  61.     nntp_get(ser_line, sizeof(ser_line));
  62. #ifdef DEBUGGING
  63.     if (debug & DEB_NNTP)
  64.     printf("<%s\n", ser_line) ; FLUSH;
  65. #endif
  66.     if (*ser_line != CHAR_OK) {        /* and then see if that's ok */
  67.     fprintf(stdout, "Can't get active file from server: \n%s\n", ser_line);
  68.     finalize(1);
  69.     }
  70.     time(&lastactfetch);
  71.  
  72.     cp = filexp("%P/rrnact.%$");    /* make a temporary name */
  73.     strcpy(active_name, cp);
  74.     actfp = fos2open(active_name, "w+");   /* and get ready */
  75.     if (actfp == Nullfp) {
  76.     printf(cantopen,active_name) ; FLUSH;
  77.     finalize(1);
  78.     }
  79.  
  80.     activeitems = 0;
  81.     while (1) {
  82.     if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  83.         printf("Can't get active file from server:\n\
  84. transfer failed after %d entries\n", activeitems);
  85.         finalize(1);
  86.     }
  87.     if (ser_line[0] == '.')        /* while there's another line */
  88.         break;            /* get it and write it to */
  89.     activeitems++;
  90.     fputs(ser_line, actfp);
  91.     putc('\n', actfp);
  92.     }
  93.  
  94.     if (ferror(actfp)) {
  95.     printf("Error writing to active file %s.\n", active_name) ; FLUSH;
  96.     finalize(1);
  97.     }
  98. #else /* not SERVER */
  99.  
  100. /*** OS2: because we get settings out of uupc-rc-files,
  101.           we don't use filexp to expand the filename ***/
  102. /*    cp = filexp(ACTIVE);    */
  103. /*    cp = ACTIVE; */
  104. /*    actfp = fos2open(cp,"r");   */
  105. /*    if (actfp == Nullfp) {   */
  106. /*        cp = ACTIVE_OPT;   */
  107. /*        actfp = fos2open(cp,"r");   */
  108. /* bla    if (actfp == Nullfp) {   */
  109. /*        printf(cantopen,cp) ; FLUSH;   */
  110. /*        finalize(1);   */
  111. /*    }   */
  112. /*    }   */
  113. /*    activeitems = 0;   */
  114.     /* count entries */
  115. /*    while(fgets(buf,LBUFLEN,actfp) != NULL)   */
  116. /*    activeitems++;   */
  117.     ngdatansrv_init();
  118. #endif
  119.     if (fseek(actfp,0L,0) == -1) {    /* just get to the beginning */
  120.     printf("Error seeking in active file.\n") ; FLUSH;
  121.     finalize(1);
  122.     }
  123.  
  124.     return;
  125. }
  126.  
  127. /* find the maximum article number of a newsgroup */
  128.  
  129. ART_NUM
  130. getngsize(num)
  131. register NG_NUM num;
  132. {
  133.     register int len;
  134.     register char *nam;
  135.     char tmpbuf[80];
  136.     ART_POS oldsoft;
  137.     long last;
  138.  
  139.     nam = rcline[num];
  140.     len = rcnums[num] - 1;
  141.     softtries++;
  142. #ifdef DEBUGGING
  143.     if (debug & DEB_SOFT_POINTERS)
  144.     printf("Softptr = %ld\n",(long)softptr[num]) ; FLUSH;
  145. #endif
  146.     oldsoft = softptr[num];
  147.     fseek(actfp,100000L,1);    /* hopefully this forces a reread */
  148.     if ((softptr[num] = findact(tmpbuf, nam, len, (long)oldsoft)) >= 0) {
  149.     if (softptr[num] != oldsoft) {
  150.         softmisses++;
  151.         writesoft = TRUE;
  152.     }
  153.     }
  154.     else {
  155.     softptr[num] = 0;
  156. #ifdef USETHREADS
  157.     if (RCCHAR(rcchar[num]) == ':')
  158. #else
  159.     if (rcchar[num] == ':')        /* unsubscribe quietly */
  160. #endif
  161.         rcchar[num] = NEGCHAR;
  162.     return TR_BOGUS;        /* well, not so quietly, actually */
  163.     }
  164.     
  165. #ifdef DEBUGGING
  166.     if (debug & DEB_SOFT_POINTERS) {
  167.     printf("Should be %ld\n",(long)softptr[num]) ; FLUSH;
  168.     }
  169. #endif
  170.     {
  171.     register char *s;
  172.     ART_NUM tmp;
  173.  
  174.     for (s=tmpbuf+len+1; isdigit(*s); s++) ;
  175.     if (tmp = atol(s))
  176. #ifdef MININACT
  177. #ifdef CACHEFIRST
  178.         abs1st[num] = tmp;
  179. #else
  180.         abs1st = tmp;
  181. #endif
  182. #endif
  183.     if (!in_ng) {
  184.         for (s++; isdigit(*s); s++) ;
  185.         while (isspace(*s)) s++;
  186.         switch (*s) {
  187.         case 'n': moderated = getval("NOPOSTRING"," (no posting)"); break;
  188.         case 'm': moderated = getval("MODSTRING", " (moderated)"); break;
  189.         /* This shouldn't even occur.  What are we doing in a non-existent
  190.            group?  Disallow it. */
  191.         case 'x': return TR_BOGUS;
  192.         /* what should be done about refiled groups?  rn shouldn't even
  193.            be in them (ie, if sci.aquaria is refiled to rec.aquaria, then
  194.            get the news there) */
  195.         case '=': return TR_BOGUS;
  196.         default: moderated = nullstr;
  197.         }
  198.     }
  199.     }
  200.     last = atol(tmpbuf+len+1);
  201.     if (last < ngmax[num]) {
  202. #ifdef SERVER
  203.     if (time(Null(time_t*)) - lastactfetch > MINFETCHTIME) {
  204.         fclose(actfp);
  205.         ngdata_init();    /* re-grab the active file */
  206.     }
  207. #endif
  208.     return ngmax[num];
  209.     }
  210.     return (ART_NUM)last;
  211. }
  212.  
  213. /* determine the absolutely first existing article number */
  214. #ifdef SERVER
  215. ART_NUM
  216. getabsfirst(ngnum,ngsize)
  217. register NG_NUM ngnum;
  218. ART_NUM ngsize;
  219. {
  220.     long a1st, last;
  221.  
  222. #ifdef CACHEFIRST
  223.     if (a1st = abs1st[ngnum])
  224.     return (ART_NUM)a1st;
  225. #endif
  226. #ifdef MININACT
  227.     getngsize(ngnum);
  228. # ifdef CACHEFIRST
  229.     return abs1st[ngnum];
  230. # else
  231.     return abs1st;
  232. # endif
  233. #else
  234.     getngsize(ngnum); /* set moderated as side effect */
  235.     sprintf(ser_line,"GROUP %s",rcline[ngnum]);
  236. #ifdef DEBUGGING
  237.     if (debug & DEB_NNTP)
  238.     printf(">%s\n", ser_line) ; FLUSH;
  239. #endif
  240.     put_server(ser_line);
  241.     if (nntp_get(ser_line, sizeof(ser_line)) < 0) {
  242.     fprintf(stderr, "\nrrn: Unexpected close of server socket.\n");
  243.     finalize(1);
  244.     }
  245. #ifdef DEBUGGING
  246.     if (debug & DEB_NNTP)
  247.     printf("<%s\n", ser_line) ; FLUSH;
  248. #endif
  249.     if (*ser_line == CHAR_FATAL){
  250.     fprintf(stderr,"\nrrn: %s\n",ser_line);
  251.     finalize(1);
  252.     }
  253.     if (*ser_line != CHAR_OK)        /* and then see if that's ok */
  254.     a1st = ngsize+1;        /* nothing there */
  255.     else {
  256.     (void) sscanf(ser_line,"%*d%*d%ld%ld",&a1st,&last);
  257.     ngmax[ngnum] = last;
  258.     }
  259. # ifdef CACHEFIRST
  260.     abs1st[ngnum] = (ART_NUM)a1st;
  261. # endif
  262.     return (ART_NUM)a1st;
  263. #endif
  264. }
  265. /* we already know the lowest article number with NNTP */
  266. ART_NUM
  267. getngmin(dirname,floor)
  268. char *dirname;
  269. ART_NUM floor;
  270. {
  271.     return(floor);        /* dirname not used */
  272. }
  273.  
  274. #else /*SERVER*/
  275.  
  276. ART_NUM
  277. getabsfirst(ngnum,ngsize)
  278. register NG_NUM ngnum;
  279. ART_NUM ngsize;
  280. {
  281.     register ART_NUM a1st;
  282. #ifndef MININACT
  283.     char dirname[MAXFILENAME];
  284. #endif
  285.  
  286. #ifdef CACHEFIRST
  287.     if (a1st = abs1st[ngnum])
  288.     return a1st;
  289. #endif
  290. #ifdef MININACT
  291.     getngsize(ngnum);
  292. # ifdef CACHEFIRST
  293.     return abs1st[ngnum];
  294. # else
  295.     return abs1st;
  296. # endif
  297. #else /* not MININACT */
  298.     getngsize(ngnum); /* set moderate as side effect */
  299.     sprintf(dirname,"%s/%s",spool,getngdir(rcline[ngnum]));
  300.     a1st = getngmin(dirname,0L);
  301.     if (!a1st)                /* nothing there at all? */
  302.     a1st = ngsize+1;        /* aim them at end of newsgroup */
  303. # ifdef CACHEFIRST
  304.     abs1st[ngnum] = a1st;
  305. # endif
  306.     return a1st;
  307. #endif /* MININACT */
  308. }
  309.  
  310. /* scan a directory for minimum article number greater than floor */
  311.  
  312. ART_NUM
  313. getngmin(dirname,floor)
  314. char *dirname;
  315. ART_NUM floor;
  316. {
  317.     register DIR *dirp;
  318.     register struct DIRTYPE *dp;
  319.     register ART_NUM min = 0;
  320.     register ART_NUM maybe;
  321.     register char *p;
  322. #ifdef notdef
  323.     char tmpbuf[128];
  324. #endif
  325.  
  326.     change_bsl2sl(dirname);
  327.     dirp = opendir(dirname);
  328.     if (!dirp)
  329.     return 0;
  330.     while ((dp = readdir(dirp)) != Null(struct DIRTYPE *)) {
  331.     if (((maybe = atol(dp->d_name)) < min || !min) && maybe > floor) {
  332.         for (p = dp->d_name; *p; p++)
  333.         if (!isdigit(*p))
  334.             goto nope;
  335. #ifdef notdef
  336.        /* 
  337.         * If newsgroup names ever go entirely numeric, then
  338.         * this code will have to be reinserted.
  339.         * For the time being, we assume that if a numeric name is
  340.         * found, it must be an article (and not a directory).
  341.         * This will avoid two stat(2) calls for those running
  342.         * rn.
  343.         */
  344.         if (*dirname == '.' && !dirname[1])
  345.         stat(dp->d_name, &filestat);
  346.         else {
  347.         sprintf(tmpbuf,"%s/%s",dirname,dp->d_name);
  348.         stat(tmpbuf, &filestat);
  349.         }
  350.         if (! (filestat.st_mode & S_IFDIR))
  351. #endif
  352.         min = maybe;
  353.     }
  354.       nope:
  355.     ;
  356.     }
  357.     closedir(dirp);
  358.     return min;
  359. }
  360. #endif
  361.