home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / relay / ihave.c < prev    next >
C/C++ Source or Header  |  1993-03-13  |  3KB  |  141 lines

  1. /*
  2.  * Implement the Usenet ihave/sendme control messages, as per RFC 1036. (NCMP)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include "fixerrno.h"
  11. #include <sys/types.h>
  12.  
  13. #include "libc.h"
  14. #include "news.h"
  15. #include "config.h"
  16. #include "batchnames.h"
  17. #include "headers.h"
  18. #include "relay.h"
  19. #include "history.h"
  20. #include "fgetmfs.h"
  21. #include "msgs.h"
  22.  
  23. #ifndef AVEARTSIZE
  24. #define AVEARTSIZE 3000
  25. #endif
  26. #ifndef SENDMEBATFILE
  27. #define SENDMEBATFILE ".ihave/togo"
  28. #endif
  29. #ifndef SENDITBATFILE
  30. #define SENDITBATFILE ".sendme/togo"
  31. #endif
  32.  
  33. #define PROTO_IHAVE 0
  34. #define PROTO_SENDME 1
  35.  
  36. /* imports */
  37. extern char *ismsgidbad();
  38.  
  39. /*
  40.  * write the name of the article file on the batch file appropriate to
  41.  * this remotesys and protocol.
  42.  */
  43. STATIC void
  44. appmsgnmtonxtbatfile(art, remotesys, proto)
  45. register struct article *art;
  46. char *remotesys;
  47. int proto;
  48. {
  49.     register char *batchname;
  50.     register FILE *batf;
  51.     extern boolean safecmd();
  52.  
  53.     if (!safecmd(remotesys)) {    /* nasty site name? a bit paranoid */
  54.         errno = 0;
  55.         transient(art, 'b', "site name `%s' in ihave/sendme is unsafe",
  56.             remotesys);
  57.         return;
  58.     }
  59.     batchname = str3save(artfile(BTCHDIR), remotesys,
  60.         (proto == PROTO_IHAVE? SENDMEBATFILE: SENDITBATFILE));
  61.     batf = fopenwclex(batchname, "a");
  62.     if (batf != NULL) {
  63.         (void) fputs(art->a_tmpf, batf);
  64.         (void) putc('\n', batf);
  65.         if (fclose(batf) == EOF)
  66.             fulldisk(art, batchname);
  67.     }
  68.     free(batchname);
  69. }
  70.  
  71. STATIC void
  72. doproto(args, art, proto)
  73. char *args;
  74. register struct article *art;
  75. int proto;
  76. {
  77.     register char *argscp = skipsp(args), *remotesys, *p;
  78.     char *myname;
  79.  
  80.     if (*argscp == '\n' || *argscp == '\0')    /* no args */
  81.         return;
  82.  
  83.     argscp = strsave(argscp);
  84.  
  85.     /* dig out the remote system name */
  86.     remotesys = NULL;
  87.     for (p = argscp; *p != '\0'; p++)
  88.         if (iswhite(*p))
  89.             remotesys = p;
  90.     if (remotesys == NULL)            /* no msg-ids in command */
  91.         remotesys = argscp;
  92.     else {
  93.         remotesys = argscp + strlen(argscp) - 1;    /* last byte */
  94.         while (isascii(*remotesys) && isspace(*remotesys))
  95.             *remotesys-- = '\0';    /* back up to non-whitespace */
  96.         remotesys = NULL;
  97.         for (p = argscp; *p != '\0'; p++)
  98.             if (iswhite(*p))
  99.                 remotesys = p;
  100.         if (remotesys == NULL)        /* no msg-ids in command */
  101.             remotesys = argscp;
  102.         else
  103.             *remotesys++ = '\0';    /* split msg-ids & sys name */
  104.     }
  105.     myname = hostname();
  106.     if (!STREQ(remotesys, myname))        /* remotesys may not be me */
  107.         appmsgnmtonxtbatfile(art, remotesys, proto);
  108.     free(argscp);
  109. }
  110.  
  111. /*
  112.  *    ihave [message-ID-list] remotesys    generate a sendme for remotesys
  113.  *                        from message-ID-list
  114.  * Read message-IDs from args or control message body,
  115.  * look them up in history, post a sendme to to.remotesys (via the batcher
  116.  * to avoid deadlock) consisting of the message-IDs not in history.
  117.  * The "posting" consists of transmitting to a system matching
  118.  * "to.remotesys" in sys, which had better have the I flag on.
  119.  */
  120. void
  121. ihave(args, art)
  122. char *args;
  123. struct article *art;
  124. {
  125.     doproto(args, art, PROTO_IHAVE);
  126. }
  127.  
  128. /*
  129.  *    sendme [message-ID-list] remotesys    send articles named to remotesys
  130.  * Read message-IDs from args or control message body,
  131.  * transmit the corresponding articles to a system matching
  132.  * "to.remotesys/sendme" in sys, which will typically name a batch file.
  133.  */
  134. void
  135. sendme(args, art)
  136. char *args;
  137. struct article *art;
  138. {
  139.     doproto(args, art, PROTO_SENDME);
  140. }
  141.