home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / comm / bbs / eazybbs / source / eazy2news.c < prev    next >
C/C++ Source or Header  |  1994-09-08  |  4KB  |  171 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <time.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8.  
  9.  
  10.  
  11. /* ... from AmigaElm */
  12. #include "getsystime.c"
  13.  
  14.  
  15.  
  16. #define NEWS_BATCHSCRIPT    "MB_DATA:UUCP/DeliverNews.scp"
  17. #define NEWS_BATCHLOCK        "MB_DATA:UUCP/DeliverNews.lock"
  18. #define NEWS_BATCHNUM        "MB_DATA:UUCP/DeliverNews.num"
  19. #define NEWS_BATCHFILE        "MB_DATA:UUCP/News.%ld"
  20.  
  21.  
  22.  
  23. #define VERSION "1.8"
  24. static char *version = "\0$VER: eazy2news " VERSION " (" __AMIGADATE__ ")";
  25. static char *day_names[] =
  26. {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  27. static char *month_names[] =
  28. {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  29.  
  30.  
  31.  
  32. void space2under(char *s)
  33. {
  34.     if (s) {
  35.         while (*s) {
  36.             if (*s == ' ')
  37.                 *s = '_';
  38.             s++;
  39.         }
  40.     }
  41. }
  42.  
  43.  
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47.     if (argc == 7 && OpenTimer4MsgId()) {
  48.         char *excld = argv[2];
  49.         FILE *fp, *scp, *lock, *num;
  50.         long seqnum = 0;
  51.         char tmp[80];
  52.  
  53.         /* try to get lock */
  54.  
  55.         while (!(lock = fopen(NEWS_BATCHLOCK, "w")))
  56.             Delay(100);
  57.  
  58.         /* we have the lock, now want the maximum seq-number */
  59.  
  60.         if (num = fopen(NEWS_BATCHNUM, "r")) {
  61.             fscanf(num, "%ld", &seqnum);
  62.             fclose(num);
  63.         }
  64.  
  65.         ++seqnum;
  66.  
  67.         sprintf(tmp, NEWS_BATCHFILE, seqnum);
  68.  
  69.         if (seqnum > 1) {
  70.             while (!(scp = fopen(NEWS_BATCHSCRIPT, "a"))) {
  71.                 Delay(100);
  72.             }
  73.         }
  74.         else {
  75.             while (!(scp = fopen(NEWS_BATCHSCRIPT, "w"))) {
  76.                 Delay(100);
  77.             }
  78.             fprintf(scp, "delete %s\n", NEWS_BATCHNUM);
  79.         }
  80.  
  81.         /* write back new seq-number while script-file is locked by us */
  82.         if (num = fopen(NEWS_BATCHNUM, "w")) {
  83.             fprintf(num, "%ld\n", seqnum);
  84.             fclose(num);
  85.         }
  86.  
  87.         /* now finish writing to script-file */
  88.         fprintf(scp, "relaynews -x %s -r -s <%s\ndelete %s\n", excld, tmp, tmp);
  89.  
  90.         if (fp = fopen(tmp, "w")) {
  91.             register int c = 0, last = 0;
  92.             time_t t;
  93.             struct timeval tv;
  94.             struct tm *ut;
  95.             char uname[16];
  96.             char *sname = argv[3];
  97.             char *ngrps = argv[1];
  98.             char *realn = argv[5];
  99.             char *sbjct = argv[6];
  100.  
  101.             /* for Date: */
  102.             time(&t);
  103.             ut = gmtime(&t);
  104.  
  105.             /* for Message-Id: */
  106.             GetTimer4MsgId(&tv);
  107.  
  108.             strcpy(uname, argv[4]);
  109.             space2under(uname);
  110.  
  111.             fprintf(fp, "Path: news\n");
  112.             fprintf(fp, "Newsgroups: %s\n", ngrps);
  113.             fprintf(fp, "From: %s@%s (%s)\n", uname, sname, realn);
  114.             fprintf(fp, "Sender: eazybbs@%s\n", sname);
  115.             fprintf(fp, "Subject: %s\n", sbjct);
  116.             fprintf(fp, "Message-Id: <%lx.%lx-eazybbs@%s>\n", tv.tv_secs, tv.tv_micro, sname);
  117.             fprintf(fp, "Date: %s, %.2d %s %d %.2d:%.2d:%.2d %sGMT\n",
  118.                 day_names[ut->tm_wday],
  119.                 ut->tm_mday, month_names[ut->tm_mon], ut->tm_year + 1900,
  120.                 ut->tm_hour, ut->tm_min, ut->tm_sec, ut->tm_isdst ? "DST" : "");
  121.             fprintf(fp, "MIME-Version: 1.0\n");
  122.  
  123.             /* This is not strictly MIME compliant, but who cares? */
  124.  
  125.             fprintf(fp, "Content-Type: text/plain; charset=ISO-8859-1\n");
  126.             fprintf(fp, "Content-Transfer-Encoding: 8bit\n");
  127.             fprintf(fp, "\n");
  128.  
  129.             while ((c = getchar()) != EOF) {
  130.                 if (last == '\\') {
  131.                     switch (c) {
  132.                     case '\\':
  133.                         fputc(c, fp);
  134.                         break;
  135.                     default:
  136.                         break;
  137.                     }
  138.                     last = 0;
  139.                 }
  140.                 else if (c == '\\') {
  141.                     last = c;
  142.                 }
  143.                 else {
  144.                     fputc(c, fp);
  145.                     last = c;
  146.                 }
  147.             }
  148.             fprintf(fp, "\n");
  149.  
  150.             fclose(fp);
  151.         }
  152.  
  153.         /* close script-file here to prevent it from being started */
  154.         fclose(scp);
  155.  
  156.         while (chmod(NEWS_BATCHSCRIPT, S_IWRITE | S_IREAD | S_IEXECUTE | S_IDELETE | S_ISCRIPT))
  157.             Delay(100);
  158.  
  159.         /* close lock-file here to prevent other ports from processing news */
  160.         fclose(lock);
  161.  
  162.         CloseTimer4MsgId();
  163.     }
  164.     else {
  165.         fprintf(stderr, "\033[1;33mEazyBBS\033[0m © 1988-1994 Andreas M. Kirchwitz\n"
  166.             "Usage: eazy2news \033[3m<newsgroup> <excl-cmd> <site> <user> <real> <subject> \033[0m <text\n");
  167.     }
  168.  
  169.     exit(0);
  170. }
  171.