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 / procbatch.c < prev    next >
Text File  |  1994-09-25  |  9KB  |  319 lines

  1. /*  procbatch.c   Process a batch of news articles.
  2.     Copyright (C) 1994 Brad Spencer
  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. #include "uucp.h"
  26. #include "rnews.h"
  27. #include "mbuf.h"
  28.  
  29. EXTERN int debuglvl;
  30.  
  31. QQ flag gotgroup;
  32. QQ char *refline = NULL;
  33.  
  34. /* Silly, hu.... */
  35. QQ char *junk = "junk";
  36. QQ char *space = " ";
  37.  
  38. char lbuf[512];
  39.  
  40. int free();
  41. long xatol();
  42. struct mbuf *mwrite();
  43.  
  44.  
  45. int procbatch (fd, initalbuf, gh)
  46. FILE *fd;
  47. char *initalbuf;
  48. struct mbuf *gh;
  49. {
  50.      struct mbuf *gg, *mp = NULL, *mh = NULL;
  51.      long origlen, length = -1;
  52.      char *groupname = NULL;
  53.      int rr, r;
  54.  
  55.      while (feof (fd) == 0)
  56.        {
  57.           if (strncmp (initalbuf, "#! rnews ", 9) != 0)
  58.                log ("Not a batch line");
  59.  
  60.           origlen = length = xatol (initalbuf+9);
  61.  
  62.           if (debuglvl > 8)
  63.             {
  64.                sprintf (lbuf, "loop... %ld", length);
  65.                log (lbuf);
  66.             }
  67.           gotgroup = FALSE;
  68.  
  69.           if (length > 0)
  70.             {
  71.                if (debuglvl > 5)
  72.                  {
  73.                     sprintf (lbuf, "article length: %ld", length);
  74.                     log (lbuf);
  75.                  }
  76.  
  77.                while ((length > 0) && (feof(fd) == 0))
  78.                  {
  79.                     if (!gotgroup)
  80.                          r = procnewgroup (fd, initalbuf, mp, mh, groupname);
  81.                     else
  82.                       {
  83.                          if (gotgroup)
  84.                               procoldgroup (origlen, gh, mp, mh, groupname);
  85.  
  86.                          /* Read, either the remaining length or BIGBUF,
  87.                             whichever fits */
  88.  
  89.                          r = (length > BIGBUF) ? BIGBUF : length;
  90.  
  91.                          if (debuglvl > 8)
  92.                            {
  93.                               sprintf (lbuf, "reading: %d %ld", r, length);
  94.                               log (lbuf);
  95.                            }
  96.  
  97.                          if ((rr = fread (initalbuf, 1, r, fd)) != r)
  98.                               log ("procbatch: Read error");
  99.  
  100.                          r = rr;
  101.  
  102.                          if (debuglvl > 8)
  103.                            {
  104.                               sprintf (lbuf, "read: %d %ld", r, length);
  105.                               log (lbuf);
  106.                            }
  107.  
  108.                          crlf (initalbuf, r);
  109.  
  110.                          /* Write the just-read-in block */
  111.                          for (gg = gh;  gg != NULL;  gg = gg->mbuf_next)
  112.                               if (((struct groups *)gg->cbuf)->artfd != NULL)
  113.                                 {
  114.                                    fwrite (initalbuf, 1, r,
  115.                                           ((struct groups *)gg->cbuf)->artfd);
  116.                                 }
  117.                       }
  118.  
  119.                     /* Subtract the amount read */
  120.                     length -= r;
  121.                  }
  122.  
  123.                /* Close all the open files */
  124.                for (gg = gh;  gg != NULL;  gg = gg->mbuf_next)
  125.                     if (((struct groups *)gg->cbuf)->artfd != NULL)
  126.                       {
  127.                          fclose (((struct groups *)gg->cbuf)->artfd);
  128.                          ((struct groups *)gg->cbuf)->artfd = NULL;
  129.                       }
  130.  
  131.                /* Reset the groupname and refline strings. */
  132.                groupname = refline = NULL;
  133.  
  134.                /* Free up the mbuf list, since these mbufs have simple cbufs
  135.                   then just use 'free'  */
  136.  
  137.                if (mh != NULL)
  138.                  {
  139.                     mfree (mh, free);
  140.                     mp = mh = NULL;
  141.                  }
  142.  
  143.                /* There might be more batches */
  144.                if (getline (fd, initalbuf) != -1)
  145.                  {
  146.  
  147.                     /* If something is messed up, then skip until something
  148.                        sane is found */
  149.  
  150.                     if (strncmp (initalbuf, "#! rnews ", 9) != 0)
  151.                       {
  152.                          if (debuglvl > 1)
  153.                            {
  154.                               strcpy (lbuf, "Mugged article, '");
  155.                               strncat(lbuf, initalbuf, 20);
  156.                               log (lbuf);
  157.                            }
  158.  
  159.                          if (resync (fd, initalbuf) == TRUE)
  160.                               break;
  161.                       }
  162.                  }
  163.             }
  164.           else
  165.             {
  166.                /* If the length is messed up, then skip until something sane
  167.                   is found */
  168.  
  169.                if (debuglvl > 1)
  170.                  {
  171.                     sprintf (lbuf, "Bad length '%ld'", length);
  172.                     log (lbuf);
  173.                  }
  174.  
  175.                if (resync (fd, initalbuf) == TRUE)
  176.                     break;
  177.             }
  178.        }
  179. }
  180.  
  181.  
  182.  
  183. int procnewgroup (fd, initalbuf, mp, mh, groupname)
  184. FILE *fd;
  185. char *initalbuf;
  186. struct mbuf *mp, *mh;
  187. char *groupname;
  188. {
  189.      int r;
  190.  
  191.      r = getline (fd, initalbuf);
  192.  
  193.      if (debuglvl > 8)
  194.           lineis (lbuf, initalbuf);
  195.  
  196.      mp = mwrite (mp, &mh, initalbuf, strlen (initalbuf) + 1);
  197.  
  198.      if (strncmp (mp->cbuf,"Newsgroups: ",12) == 0)
  199.        {
  200.           groupname = &mp->cbuf[12];
  201.  
  202.           if (debuglvl > 1)
  203.             {
  204.                sprintf (lbuf, "Newsgroup: %s", groupname);
  205.                log (lbuf);
  206.             }
  207.  
  208.           if (refline != NULL)
  209.                gotgroup = TRUE;
  210.        }
  211.  
  212.      /* Bob says that the Reference line shouldn't have tabs in it */
  213.      if (strncmp (mp->cbuf, "References: ", 12) == 0)
  214.        {
  215.           refline = mp->cbuf;
  216.           fixref (refline);
  217.  
  218.           if (debuglvl > 2)
  219.             {
  220.                sprintf (lbuf, "Reference line is: '%s'", refline);
  221.                log (lbuf);
  222.             }
  223.  
  224.           if (groupname != NULL)
  225.                gotgroup = TRUE;
  226.        }
  227.  
  228.      if (mp->cbuf[0] == '\0')
  229.        {
  230.           if (groupname == NULL)
  231.             {
  232.                if (debuglvl > 1)
  233.                     log ("No newsgroup given, junking article");
  234.  
  235.                groupname = junk;
  236.             }
  237.           gotgroup = TRUE;
  238.        }
  239.      return (r);
  240. }
  241.  
  242.  
  243.  
  244. int procoldgroup (origlength, gh, mp, mh, groupname)
  245. long origlength;
  246. struct mbuf *gh, *mp, *mh;
  247. char *groupname;
  248. {
  249.      register struct mbuf *gg;  
  250.  
  251.      openarts (gh, groupname);
  252.  
  253.      /* See note in procart.c */
  254.      if ((origlength - 1) > 0)
  255.           for (gg = gh;  gg != NULL;  gg = gg->mbuf_next)
  256.                if (((struct groups *)gg->cbuf)->artfd != NULL)
  257.                  {
  258.                     if (lseek (fileno (((struct groups *)gg->cbuf)->artfd),
  259.                                       origlength - 1, 0) == -1)
  260.                       {
  261.                          sprintf (lbuf, "Couldn't seek %ld %d",
  262.                                         origlength-1, errno);
  263.                          log (lbuf);
  264.                       }
  265.                     write (fileno (((struct groups *) gg->cbuf)->artfd),
  266.                             space, 1);
  267.  
  268.                     if (lseek (fileno (((struct groups *)gg->cbuf)->artfd),
  269.                                0l, 0) == -1)
  270.                       {
  271.                          sprintf (lbuf, "Couldn't seek back %ld %d",
  272.                                         origlength, errno);
  273.                          log (lbuf);
  274.                       }
  275.                  }
  276.  
  277.      for (gg = gh;  gg != NULL; gg = gg->mbuf_next)
  278.           if (((struct groups *)gg->cbuf)->artfd != NULL)
  279.                for (mp = mh;  mp != NULL; mp = mp->mbuf_next)
  280.                     fprintf (((struct groups *)gg->cbuf)->artfd,
  281.                              "%s\n", mp->cbuf);
  282.      gotgroup = TRUE;
  283. }
  284.  
  285.  
  286.  
  287. /* We hit a bad length.  Try to get things back in sync.  Return TRUE if we
  288.    succeed.  Return FALSE on EOF. */
  289.  
  290. int resync (fd, initalbuf)
  291. FILE *fd;
  292. char *initalbuf;
  293. {
  294.      while (getline (fd, initalbuf) != -1)
  295.           if (strncmp (initalbuf, "#! rnews ", 9) == 0)
  296.                return (TRUE);
  297.           else if (debuglvl > 5)
  298.             {
  299.                strncat (strcpy (lbuf, "skip.... '"), initalbuf, 20);
  300.                log (lbuf);
  301.             }
  302.      return (FALSE);
  303. }
  304.  
  305.  
  306.  
  307. /* Change all the line feeds to carrage returns. */
  308.  
  309. int crlf (s, len)
  310. char *s;
  311. int len;
  312. {
  313.      register char *bscan;
  314.  
  315.      for (bscan = s + len;  --bscan >= s; )
  316.           if (*bscan == '\x0A')
  317.                *bscan = '\x0D';
  318. }
  319.