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 >
Wrap
C/C++ Source or Header
|
1992-08-16
|
5KB
|
210 lines
/*
repl.c : Copyright Paul Healy, EI9GL, 1992.
Todo: pull the @ file into another buffer for editors that are capable
Derived from bm.
Copyright 1986 Bdale Garbee, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
920708 : Created
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <dir.h>
#include <process.h>
#include <io.h>
#include "rc.h"
#include "send.h"
#include "misc.h"
#include "header.h"
#include "help.h"
#include "buffer.h"
#include "current.h"
#include "mailer.h"
#ifdef BMH
#define main repl_main
#endif
/* Given a string of the form <user@host>, extract the part inside the
* brackets and return a pointer to it.
*/
static char *
getname(char *cp)
{
char *cp1;
if((cp = strchr(cp,'<')) == NULL)
return NULL;
cp++; /* cp -> first char of name */
if( (cp1 = strchr(cp,'>') ) == NULL )
return NULL;
*cp1 = '\0';
return cp;
}
static int
savemsg(char **start, char **end, FILE *fp)
{
char line[256];
while (getstring(start, end, line, sizeof(line)) != NULL) {
if (strncmp(line, "From ", 5) == 0)
return 1; /* have hit next message */
else {
fputs(line, fp);
fputc('\n', fp);
}
}
return 0;
}
static int
getmsgs(char *p, char *end, int msg, FILE *fp)
{
char s[256];
int i = 0;
while (1) {
while ( (p<end) && (*p!='F') )
p++;
if ( (p==end) && (refill(&p, &end) != 0) )
break;
else { /* may have found a From */
if (*(p-1) != '\n') {
p++;
continue;
}
getstring(&p, &end, s, sizeof(s));
if (strncmp(s, "From ", 5) == 0) {
i++;
if (msg == i) {
savemsg(&p, &end, fp);
return 0;
}
}
}
}
return -1;
}
static int
rmsg2file(int argc, char *argv[], char *to, char *subject)
{
char *s, *p, *end, *arg[MAXARGS], *f, *g, *h, file[256], lock[256];
FILE *fp;
int msg = 1;
if (getcurrent(argc, argv, &s, &msg) == -1)
return -1;
if ( (argc == 2) && isdigit(argv[1][0]) )
msg = atoi(argv[1]);
else if ( (argc == 3) && isdigit(argv[2][0]) )
msg = atoi(argv[2]);
sprintf(file, "%s/%s%s", getrc(maildir), s, EXT);
if(access(file, 0)) {
fprintf(stderr, "repl: can't find %s\n", file);
return -1;
}
sprintf(lock, "%s/%s", getrc(maildir), s);
if ( loadbuf(file, &p, &end, lock) == -1) {
fprintf(stderr,"repl: can't load %s\n", file);
return -1;
}
if ( (fp=fopen("@", "w+")) == NULL) {
fprintf(stderr, "repl: can't open '@' temporary file\n");
return NULL;
}
if ( getmsgs(p, end, msg, fp) == -1) {
fprintf(stderr, "repl: can't find msg %d\n", msg);
return -1;
}
if (fseek(fp, 0L, SEEK_SET) != 0) {
fprintf(stderr, "repl: can't seek\n");
fclose(fp);
return -1;
}
if (parseheaderfile(fp, arg) == -1) {
fprintf(stderr, "repl: can't parse message\n");
return -1;
}
fclose(fp);
if (arg[REPLYTO] != NULL)
f = &arg[REPLYTO][10];
else if (arg[FROM] != NULL)
f = &arg[FROM][6];
if (f == NULL) {
fprintf(stderr, "repl: no reciepient\n");
return -1;
}
g = getname(f);
if (g == NULL) {
g = f;
h = f;
while(*h && (*h != ' ') && (*h != '(') )
h++;
*h = '\0';
}
strcpy(to, g);
if (arg[SUBJECT] != NULL) {
if (strncmp(&arg[SUBJECT][9], "Re:", 3)) /* No Re: yet? */
sprintf(subject,"Re: %s",&arg[SUBJECT][9]);
else /* there's an Re:, let's not add another */
sprintf(subject,"%s",&arg[SUBJECT][9]) ;
}
else
strcpy(subject, "Re:");
freeheader(arg);
return 0;
}
int
main(int argc, char *argv[])
{
char touser[256], subject[256];
char filename[256];
FILE *fp;
dohelp(argc, argv, "repl [+folder] [<msg no>]");
if (loadconfig()==-1)
return -1;
if (rmsg2file(argc, argv, touser, subject) == -1)
return -1;
if ((fp = tempfile("repl", filename, "w")) == NULL)
return -1;
fprintf(fp, "To: %s\nCc: \nSubject: %s\n--------\n", touser, subject);
dosignature(fp);
fclose(fp);
if (call_ed("repl", filename) == -1)
return -1;
unlink("@");
(void) lastmsg(argc, argv);
return dowhatnow("repl", filename, 1);
}