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

  1. /*
  2.    repl.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Todo: pull the @ file into another buffer for editors that are capable
  5.  
  6.    Derived from bm.
  7.  
  8.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  9.    Permission granted for non-commercial copying and use, provided
  10.    this notice is retained.
  11.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  12.    Permission granted for non-commercial copying and use, provided
  13.    this notice is retained.
  14.  
  15.    920708 : Created
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <dir.h>
  22. #include <process.h>
  23. #include <io.h>
  24. #include "rc.h"
  25. #include "send.h"
  26. #include "misc.h"
  27. #include "header.h"
  28. #include "help.h"
  29. #include "buffer.h"
  30. #include "current.h"
  31. #include "mailer.h"
  32.  
  33. #ifdef BMH
  34. #define main repl_main
  35. #endif
  36.  
  37. /* Given a string of the form <user@host>, extract the part inside the
  38.  * brackets and return a pointer to it.
  39.  */
  40. static char *
  41. getname(char *cp)
  42. {
  43.    char *cp1;
  44.  
  45.    if((cp = strchr(cp,'<')) == NULL)
  46.       return NULL;
  47.    cp++;                               /* cp -> first char of name */
  48.    if( (cp1 = strchr(cp,'>') ) == NULL )
  49.       return NULL;
  50.    *cp1 = '\0';
  51.  
  52.    return cp;
  53. }
  54.  
  55. static int
  56. savemsg(char **start, char **end, FILE *fp)
  57. {
  58.    char line[256];
  59.  
  60.    while (getstring(start, end, line, sizeof(line)) != NULL) {
  61.       if (strncmp(line, "From ", 5) == 0)
  62.          return 1; /* have hit next message */
  63.       else {
  64.          fputs(line, fp);
  65.          fputc('\n', fp);
  66.          }
  67.       }
  68.    return 0;
  69. }
  70.  
  71. static int
  72. getmsgs(char *p, char *end, int msg, FILE *fp)
  73. {
  74.    char s[256];
  75.    int i = 0;
  76.  
  77.    while (1) {
  78.       while ( (p<end) && (*p!='F') )
  79.          p++;
  80.       if ( (p==end) && (refill(&p, &end) != 0) )
  81.          break;
  82.       else { /* may have found a From */
  83.          if (*(p-1) != '\n') {
  84.             p++;
  85.             continue;
  86.             }
  87.          getstring(&p, &end, s, sizeof(s));
  88.          if (strncmp(s, "From ", 5) == 0) {
  89.             i++;
  90.             if (msg == i) {
  91.                savemsg(&p, &end, fp);
  92.                return 0; 
  93.                }
  94.             }
  95.          }
  96.       }
  97.    return -1;
  98. }
  99.  
  100. static int
  101. rmsg2file(int argc, char *argv[], char *to, char *subject)
  102. {
  103.    char *s, *p, *end, *arg[MAXARGS], *f, *g, *h, file[256], lock[256];
  104.    FILE *fp;
  105.    int msg = 1;
  106.  
  107.    if (getcurrent(argc, argv, &s, &msg) == -1)
  108.       return -1;
  109.    
  110.    if ( (argc == 2) && isdigit(argv[1][0]) )
  111.       msg = atoi(argv[1]);
  112.    else if ( (argc == 3) && isdigit(argv[2][0]) )
  113.       msg = atoi(argv[2]);
  114.  
  115.    sprintf(file, "%s/%s%s", getrc(maildir), s, EXT);
  116.    if(access(file, 0)) {
  117.       fprintf(stderr, "repl: can't find %s\n", file);
  118.       return -1;
  119.       }
  120.    sprintf(lock, "%s/%s", getrc(maildir), s);
  121.    if ( loadbuf(file, &p, &end, lock) == -1) {
  122.       fprintf(stderr,"repl: can't load %s\n", file);
  123.       return -1;
  124.       }
  125.  
  126.    if ( (fp=fopen("@", "w+")) == NULL) {
  127.       fprintf(stderr, "repl: can't open '@' temporary file\n");
  128.       return NULL;
  129.       }
  130.  
  131.    if ( getmsgs(p, end, msg, fp) == -1) {
  132.       fprintf(stderr, "repl: can't find msg %d\n", msg);
  133.       return -1;
  134.       }
  135.    if (fseek(fp, 0L, SEEK_SET) != 0) {
  136.       fprintf(stderr, "repl: can't seek\n");
  137.       fclose(fp);
  138.       return -1;
  139.       }
  140.    if (parseheaderfile(fp, arg) == -1) {
  141.       fprintf(stderr, "repl: can't parse message\n");
  142.       return -1;
  143.       }
  144.    fclose(fp);
  145.  
  146.    if (arg[REPLYTO] != NULL)
  147.       f = &arg[REPLYTO][10];
  148.    else if (arg[FROM] != NULL)
  149.       f = &arg[FROM][6];
  150.  
  151.    if (f == NULL) {
  152.       fprintf(stderr, "repl: no reciepient\n");
  153.       return -1;
  154.       }
  155.  
  156.    g = getname(f);
  157.    if (g == NULL) {
  158.       g = f;
  159.       h = f;
  160.       while(*h && (*h != ' ') && (*h != '(') )
  161.          h++;
  162.       *h = '\0';
  163.    }
  164.    strcpy(to, g);
  165.  
  166.    if (arg[SUBJECT] != NULL) {
  167.       if (strncmp(&arg[SUBJECT][9], "Re:", 3))             /* No Re: yet? */
  168.          sprintf(subject,"Re: %s",&arg[SUBJECT][9]);
  169.       else   /* there's an Re:, let's not add another */
  170.          sprintf(subject,"%s",&arg[SUBJECT][9]) ;
  171.       }
  172.    else
  173.       strcpy(subject, "Re:");
  174.  
  175.    freeheader(arg);
  176.  
  177.    return 0;
  178. }
  179.  
  180. int
  181. main(int argc, char *argv[])
  182. {
  183.    char touser[256], subject[256];
  184.    char filename[256];
  185.    FILE *fp;
  186.  
  187.    dohelp(argc, argv, "repl [+folder] [<msg no>]");
  188.  
  189.    if (loadconfig()==-1)
  190.       return -1;     
  191.  
  192.    if (rmsg2file(argc, argv, touser, subject) == -1)
  193.       return -1;
  194.  
  195.    if ((fp = tempfile("repl", filename, "w")) == NULL)
  196.       return -1;
  197.  
  198.    fprintf(fp, "To: %s\nCc: \nSubject: %s\n--------\n", touser, subject);
  199.    dosignature(fp);
  200.    fclose(fp);
  201.  
  202.    if (call_ed("repl", filename) == -1)
  203.       return -1;
  204.  
  205.    unlink("@");
  206.    (void) lastmsg(argc, argv);
  207.  
  208.    return dowhatnow("repl", filename, 1);
  209. }
  210.