home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / contrib / snntpd / post.c < prev    next >
C/C++ Source or Header  |  1993-02-04  |  3KB  |  113 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7.  
  8. #include "libcnews.h"
  9. #include "config.h"
  10. #include "fgetfln.h"
  11. #include "history.h"
  12.  
  13. #include "batch.h"
  14. #include "msgs.h"
  15. #include "debug.h"
  16. #include "netdata.h"
  17. #include "log.h"
  18.  
  19. #define NULLSTR        ((char *) NULL)
  20.  
  21. /* NOTE: x *MUST* be an array or you'll only get 3 chars */
  22. #define SAY(x, y) \
  23.     if (net_ackwrite((x), sizeof (x) - 1, (y), stdout) == 0) \
  24.         ; \
  25.     else \
  26.         unlock(), error("net_ackwrite(%s) failed", (x))
  27.  
  28. /* imports */
  29. extern char *mktemp();
  30. extern int postarts, postfail;
  31. extern unsigned int debug;
  32. extern FILE *dfp;
  33.  
  34. void
  35. postarticle(fp, ndp)
  36. FILE *fp;
  37. struct netdata *ndp;
  38. {
  39.     FILE *inewsfp;
  40.     int ret;
  41.     static char *inewscmd, *inewserrfile;
  42.  
  43.     SAY(NNTP_POST, NULLSTR);
  44.     if (net_getdata(fp, ndp) != 0) {
  45.         SAY(NNTP_POSTFAIL, NULLSTR);
  46.         postfail++;
  47.         return;
  48.     }
  49.     if (inewscmd == NULL) {
  50.         char *tmp1 = str3save("HOME=", fullartfile(NULLSTR),
  51.                       "/in.coming/bad PATH=");
  52.         char *tmp2 = str3save(newspath(), " ", INEWS);
  53.         char *tmp3 = str3save(tmp1, tmp2, " > ");
  54.  
  55.         inewserrfile = strsave("in.coming/post.XXXXXX");
  56.         (void) mktemp(inewserrfile);
  57.         if (*inewserrfile == '\0') {
  58.             SAY(NNTP_POSTFAIL, NULLSTR);
  59.             postfail++;
  60.             return;
  61.         }
  62.         inewscmd = str3save(tmp3, inewserrfile, " 2>&1");
  63.         free(tmp1);
  64.         free(tmp2);
  65.         free(tmp3);
  66.     }
  67.     ddprintf(dfp, "popening `%s'\n", inewscmd);
  68.     inewsfp = popen(inewscmd, "w");
  69.     if (inewsfp == NULL) {
  70.         SAY(NNTP_POSTFAIL, NULLSTR);
  71.         postfail++;
  72.         return;
  73.     }
  74.     if (batchwrite(inewsfp, ndp->nd_buf, ndp->nd_bufcnt, ndp->nd_fp,
  75.         ndp->nd_spilled) != 0) {
  76.         SAY(NNTP_POSTFAIL, NULLSTR);
  77.         postfail++;
  78.         /* could we have a partial article here? */
  79.         (void) pclose(inewsfp);
  80.         return;
  81.     }
  82.     ret = pclose(inewsfp);
  83.     if (ret == 0) {
  84.         SAY(NNTP_POSTOK, NULLSTR);
  85.         postarts++;
  86.     } else {
  87.         struct stat stbuf;
  88.         
  89.         ddprintf(dfp, "inews returned %x\n", ret);
  90.         if (stat(inewserrfile, &stbuf) < 0)
  91.             warning("stat(%s) failed", inewserrfile);
  92.         else if (stbuf.st_size != 0)  {
  93.             char *cp;
  94.             sizeint len;
  95.             FILE *fp = efopen(inewserrfile, "r");
  96.  
  97.             SAY(NNTP_POSTFAILC, NULLSTR);
  98.             while ((cp = fgetline(fp, &len)) != NULL) {
  99.                 if (cp[--len] == '\n')
  100.                     cp[len] = '\0';
  101.                 else
  102.                     len++;    /* shouldn't happen */
  103.                 net_ackwrite(cp, len, NULLSTR, stdout);
  104.             }
  105.             (void) fclose(fp);
  106.         }
  107.         SAY(NNTP_POSTFAIL, itos("inews returned %d", ret));
  108.         postfail++;
  109.     }
  110.     if (unlink(inewserrfile))
  111.         warning("unlink(%s) failed", inewserrfile);
  112. }
  113.