home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / IMPORT.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  6KB  |  223 lines

  1. /*
  2.    import.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Usage: import <filename> [<address>]
  5.  
  6.    Takes one or more messages in bbs export format (optionally from stdin)
  7.    and queues them for delivery. Compile with Turbo C v2.0.
  8.  
  9.    To do:
  10.      . parse R: lines for correct @<bbs> field. NOS could do with this 2.
  11.  
  12.    911012 : Created.
  13.    911124 : Changed /EX comparision to catch a line with space(s) after the
  14.             X. (strcmp -> strncmp).
  15.    920501 : Cater for "@@" on some ANS bulletins
  16.    920718 : bmh version
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <dir.h>
  22. #include <time.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include "misc.h"
  26. #include "rc.h"
  27. #include "send.h"
  28. #include "lock.h"
  29. #include "smtp.h"
  30. #include "help.h"
  31. #include "mailer.h"
  32.  
  33. static char *
  34. grab(char *to, char *ptr)
  35. {
  36.    char *start = to;
  37.  
  38.    if (ptr==NULL)
  39.       return NULL;
  40.    while (*ptr && (*ptr==' ') )
  41.       ptr++;
  42.    while (*ptr && (*ptr!=' ') && (*ptr!='@')
  43.                && (*ptr!='$') && (*ptr!='<') && (*ptr!='\n'))
  44.       *to++ = *ptr++;
  45.    *to = '\0';
  46.  
  47.    return start;
  48. }
  49.  
  50. static int
  51. addbid(char *bid)
  52. {
  53.    FILE *fp = fopenlocked(getrc(spooldir), "history", "", "a");
  54.    char *tsb = getrc(tsbids);
  55.    
  56.    if (fp == NULL)
  57.       return -1;
  58.  
  59.    if ( (tsb != NULL) && (tolower(*tsb) == 'y') )
  60.       fprintf(fp, "%s %ld\n", bid, time(NULL));   /* wg7j format */
  61.    else 
  62.       fprintf(fp, "%s\n", bid);
  63.  
  64.    if (ferror(fp))
  65.       fprintf(stderr, "addbid: could not add %s to the history file\n", bid);
  66.  
  67.    return fcloselocked(fp, getrc(spooldir), "history");
  68. }
  69.  
  70. static int
  71. checkhistory(char *bid)
  72. {
  73.    FILE *fp = fopenlocked(getrc(spooldir), "history", "", "r");
  74.    char line[256];
  75.    int len = strlen(bid);
  76.    
  77.    if (fp == NULL)
  78.       return addbid(bid);
  79.  
  80.    while ( fgets(line, sizeof(line), fp) != NULL )
  81.       if (strncmp(line, bid, len) == 0) {
  82.          fprintf(stderr, "import: %s found in history file. Skipping...\n", bid);
  83.          fcloselocked(fp, getrc(spooldir), "history");
  84.          return -1;
  85.          }
  86.  
  87.    fcloselocked(fp, getrc(spooldir), "history");
  88.  
  89.    return addbid(bid);
  90. }
  91.  
  92. static int
  93. digestline(char *line, char *bbstype, char *to, char *from, char *msgid,
  94.     int nohistory)
  95. {
  96.    char temp[256], temp2[256], bid[256], *cp;
  97.  
  98.    *bbstype = line[1];
  99.  
  100.    /*
  101.     * Digest TO fields
  102.     */
  103.    if ( (cp = strchr(line, '@')) != NULL)
  104.       if (*(cp+1) == '@')                 /* double @ on some ANS bulletins */
  105.          cp++;
  106.  
  107.    if ( cp != NULL)
  108.       sprintf(to, "%s@%s", grab(temp, line+3), grab(temp2, cp+1));
  109.    else
  110.       sprintf(to, "%s@%s", grab(temp, line+3), getrc(hostname));
  111.  
  112.    /*
  113.     * Digest FROM field
  114.     */
  115.    if ( (cp = strchr(line, '<')) != NULL) 
  116.       sprintf(from, "%s@%s", grab(temp, cp+1), getrc(hostname));
  117.    else
  118.       sprintf(from, "%s@%s", getrc(username), getrc(hostname));
  119.  
  120.    /*
  121.     * Digest BID field
  122.     */
  123.    if ( (cp = strchr(line, '$')) != NULL) {
  124.       sprintf(msgid, "%s@%s.bbs", grab(bid, cp+1), getrc(username));
  125.       return nohistory ? 0 : checkhistory(bid);
  126.       }
  127.    else {
  128.       sprintf(msgid, "%ld@%s", get_msgid(getrc(mqueue)), getrc(hostname));
  129.       return 0;
  130.       }
  131. }
  132.  
  133. int
  134. main(int argc, char *argv[])
  135. {
  136.    char line[256];
  137.    FILE *fp, *msgfp;
  138.    int fin = 0, nohistory = 0;
  139.  
  140.    dohelp(argc, argv, "import [<filename> [<address>] -nohistory]");
  141.  
  142.    if (loadconfig()==-1)
  143.       return -1;
  144.  
  145.    if ( (argc >=4) && (strcmp(argv[3], "-nohistory") == 0) )
  146.       nohistory = 1;
  147.  
  148.    if ( (argc == 1) || (argv[1][0] == '-'))
  149.       fp = stdin;
  150.    else if ((fp=fopen(argv[1], "r")) == NULL) {
  151.       fprintf(stderr, "import: can't open %s\n", argv[1]);
  152.       return -1;
  153.       }
  154.  
  155.    while (fgets(line, sizeof(line), fp) != NULL) {
  156.       char bbstype, msgid[256], to[256], from[256], subj[256],
  157.            msgfn[256];
  158.  
  159.       if ( (strlen(line) < 3) || (line[0] != 'S') )
  160.          continue;
  161.       if ( !( (line[1] == 'B') || (line[1] == 'P') ) )
  162.          continue;
  163.  
  164.       fprintf(stderr,"importing: %s", line);
  165.  
  166.       if (digestline(strlwr(line), &bbstype, to, from, msgid, nohistory) == -1) {
  167.          while (fgets(line, sizeof(line), fp) != NULL)
  168.             if (strncmpi(line, "/EX", 3) == 0)
  169.                break;
  170.          continue;
  171.          }
  172.  
  173.       /*
  174.        * Get SUBJECT
  175.        */
  176.       if (fgets(subj, sizeof(subj), fp) == NULL) {
  177.          subj[0] = '\0';
  178.          fin = 1;
  179.          }
  180.       else 
  181.          rip(subj);
  182.  
  183.       if ( (msgfp = tempfile("import", msgfn, "w+")) == NULL) {
  184.          if (fp != stdin)  /* for bmh */
  185.             fclose(fp);
  186.          return -1;
  187.          }
  188.  
  189.       if (fprintf(msgfp, "X-BBS-Msg-Type: %c\n\n", toupper(bbstype)) == EOF) {
  190.          fprintf(stderr, "import: unable to write to %s\n", msgfn);
  191.          fclose(msgfp);
  192.          if (fp != stdin)
  193.             fclose(fp);
  194.          return -1;
  195.          }
  196.  
  197.       while (!fin && (fgets(line, sizeof(line), fp) != NULL) )
  198.          if (strncmpi(line, "/EX", 3) == 0)
  199.             break;
  200.          else
  201.             if ( fputs(line, msgfp) == EOF) {
  202.                fprintf(stderr, "import: unable to write to %s\n", msgfn);
  203.                fclose(msgfp);
  204.                return -1;
  205.                }
  206.  
  207.       fseek(msgfp, 0L, SEEK_SET);
  208.       if ( dosend(msgfp, argc>=3 ? argv[2] : to,
  209.                   NULL, subj, from, msgid) == -1) {
  210.          fprintf(stderr, "import: unable to queue message to %s\n", to);
  211.          return -1;
  212.          }
  213.  
  214.       fclose(msgfp);
  215.       unlink(msgfn);
  216.       }
  217.  
  218.    if (fp != stdin)  /* for bmh */
  219.       fclose(fp);
  220.    
  221.    return 0;
  222. }
  223.