home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
philmail.lzh
/
readmail.c
< prev
Wrap
C/C++ Source or Header
|
1992-06-27
|
9KB
|
373 lines
/* see below for SENDMAIL hack... */
/* a simple mail reader.
* because reading mail was just getting TOO yeuchy long-distance.
* This was originally made so that I could grab my mailbox from my UNIX
* account, read it, then output a bunch of outbound mail files, with
* first line as To:, and second line as Subject:,
* then a REALLY SIMPLE shell-script can mail each one on the
* UNIX end.
*
* (foreach i (mail.*) ; sendmail -t < $i; done)
*
* folks may use this source for whatever they want, as long as they
* mention that they used my source in their copyright/credits
* Philip Brown
*/
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <strings.h>
#define MAXARTICLES 50
#define TRUE 1
#define FALSE 0
char *includestring = " ";
/*
* SENDMAIL HACK HERE ...
* send sendmail = "rmail"
* or something that reads from stdin, and sends to its first argument.
* ie "rmail user@machine <file"
*/
char *sendmail = NULL;
/* gracefull quit.
* call only on user-invoked quit
*/
quit(){
puts(" quitting philmail.");
puts("Don't forget to delete mailfile manually if ya don't want it no mo.");
exit(0);
}
char *progname;
FILE *fp;
typedef struct article ARTICLE;
struct article {
long position;
char From[512];
char Subject[512];
char Cc[1024];
};
ARTICLE *articles[MAXARTICLES];
char buf[1024];
int numofarticles=0;
int currentarticle=0;
int lastread=0;
/* mail sends mail to a person , without
* replying to a prior specific letter.
* Note: "to" WILL have a \n at the end, the way i call it.
*/
void mail(to)
char *to;
{
FILE *sendfile;
char filename[100];
long timeval;
while(*to == ' ') to++;
timeval = _getsys(D_Ticks,4);
sprintf(filename,"mail.%d",timeval);
if((sendfile=fopen(filename,"w")) == NULL){
fprintf(stderr,"Error: could not open file to send mail\n");
return;
}
printf("Successfully opened \"%s\" for reply\n",filename);
fprintf(sendfile,"To: %s",to);
if( index(to,'\n') == NULL)
fprintf(sendfile,"\n");
fprintf(sendfile,"Subject: <None>\n");
fprintf(sendfile,"Cc: \n");
fprintf(sendfile,"X-Mailer: philmail\n");
fprintf(sendfile,"\n");
writemessage(sendfile,filename);
}
ARTICLE *newarticle(){
ARTICLE *ret;
ret = (ARTICLE *) malloc(sizeof (ARTICLE));
if(ret == NULL){
fprintf(stderr,"out of memory\n");
exit(-1);
}
ret->position =0L;
ret->From[0] = '\0';
ret->Subject[0] = '\0';
ret->Cc[0] = '\0';
return (ret);
}
void getpositions(){
char *parse;
short inheader = 1; /* always start in header!! */
int i=0;
while( fgets(buf,1023,fp) != NULL){
switch(buf[0]){
case 10:
case 13:
inheader = 0; /* end of header */
case 'C':
if(inheader == 0) break;
if(strncmp(buf,"Cc: ",4) == 0){
strcpy(articles[i]->Cc,&buf[4]);
}
break;
/* From */
case 'F':
if(strncmp(buf,"From ",5) == 0){
/* this is actual message START:
* set both From, and position
*/
i++;
inheader = 1;
articles[i] = newarticle();
articles[i]->position = ftell(fp) - strlen(buf);
parse=&buf[4];
while(*parse == ' ') parse++;
strcpy(articles[i]->From,parse);
} else if(strncmp(buf,"From: ",6) == 0){
if(inheader == 0) break;
/* this is a "From:" adjustment */
parse=&buf[5];
while(*parse == ' ') parse++;
setfrom(articles[i]->From,parse);
}
break;
/* Subject */
case 'S':
if(inheader == 0) break;
if(strncmp(buf,"Subject:",8) == 0){
strcpy(articles[i]->Subject,&buf[9]);
}
break;
default:
break;
}
}
numofarticles= i + 1;
}
void help(){
puts("Wel, Mon, we got de 'h' comman, which give ye de headers");
puts("Den we got de 'p', wich stan fer de previus messeg");
puts(" Actually, it's de las' messeg read");
puts(" note dat the 's'ave messeg counts as reading a messeg");
puts(" OOPS... fer de s, use 's <number> filename' ");
puts("\n 'n' an <enter> do de same ting, read de messeg");
puts("Reply to a messeg wid de 'r' comman, or sen' to EVER-one");
puts(" with 'R'. Note dat you kin give ea nummah wid dis.");
puts("Also, in addishaahn, you can give de number by itself, an");
puts(" you'll go to dat messeg, mon");
puts(" Den dere's the 'c' command, wich let's ya change files, I hope");
puts(" Note dat with de 's' an 'r' comman', you kin use a numba after de lettr");
puts("\n De '?' ting give yer dis ting heah!");
}
/* headers();
* prints out headers of
* currentarticle to currentarticle+10, roughly.
* also prints current article number
*/
void headers(){
int i;
i = currentarticle;
if(currentarticle >=numofarticles) {
puts("No more articles below");
}
do{
printf("%d ",i);fflush(stdout);
printheader(stdout,i);
i++;
} while(( i < numofarticles) &&( (i - currentarticle) <10 ));
printf("Current article is %d ( %d is last article )\n",currentarticle,numofarticles-1);
}
void previous(){
/* This reads the LAST ARTICLE READ.
* That article then becomes the last article read.
* this is to avoid nasty nastiness with "save", etc.
* Ergo, no hopping between #4 and #8.
*/
currentarticle = lastread;
readmsg(stdout);
/* readmsg sets lastread */
}
main(argc,argv)
int argc;
char * argv[];
{
char c;
int i;
char *parse;
char *filename;
char *envsendmail;
progname = argv[0];
/* if we are called as "mail", we had better send mail using
* "rmail". sigh...
*/
if(strncmp(progname,"mail",4) == 0)
sendmail = "rmail";
if((envsendmail = (char *)getenv("SENDMAIL")) != NULL)
sendmail = envsendmail;
switch(argc){
case 1:
if((filename = (char *) getenv("MAIL")) == NULL){
fprintf("no mail file specified on comamnd line or in MAIL variable\n");
exit(0);
}
break;
case 2:
if(argv[1][0] == '-') {
usage();
exit(0);
}
mail(argv[1]);
exit(0);
case 3:
if(argv[1][0] == '-') {
switch(argv[1][1]){
case '?':
usage();
exit(0);
case 'f':
filename = argv[2];
break;
default:
usage();
exit(0);
}
} /* if(argv....) */
break;
default:
usage();
exit(0);
}
/* now open file and test if right format?!! */
puts("Welcome to philmail. Checking out file.....");
readfile(filename);
puts("Hit enter to start reading, 'h' to show headings, '?' for help");
puts(" [ hit enter after each command]");
for(;;){
/* MAIN LOOP HERE */
FILE *temp;
printf("> ");fflush(stdout);
if(fgets(buf,1024,stdin) == NULL){
puts(" EOF encoutered. presuming exit desired");
quit();
}
parse = buf;
while(*parse == ' ') parse++;
switch(parse[0]){
case '0': case '1':
case '2': case '3':
case '4': case '5':
case '6': case '7':
case '8': case '9':
setarticle(parse);
printf("Current article is now %d\n",currentarticle);
break;
case '?': help();
break;
case 'c':
while(*++parse == ' ');
if(*parse == '\n') break;
if((temp=fopen(parse,"r")) == NULL){
fprintf(stderr,"Can't open %s for reading\n",parse);
break;
}
fgets(buf,1023,temp);
if(strncmp("From ",buf,5) != 0){
fprintf(stderr,"File is NOT in proper format\n",parse);
break;
}
/* file is okay */
fclose(temp);
restart();
readfile(parse);
break;
case 'h': headers();
break;
case 'm': mail(++parse);
break;
case 'p': previous();
break;
case 'q': quit();
break;
/* 'r' for reply */
case 'r':
/* skip whitespace */
while(*++parse == ' ');
if(isdigit(*parse))
setarticle(parse);
else
currentarticle=lastread;
reply(FALSE);
break;
case 'R':
/* skip whitespace */
while(*++parse == ' ');
if(isdigit(*parse))
setarticle(parse);
else
/* hack */
currentarticle=lastread;
reply(TRUE);
break;
case 's':
/* saves "lastread"
* UNLESS number given. then we SET lastread
* save counts as "reading"
*/
while(*++parse == ' ');
if(isdigit(*parse)){
setarticle(parse);
lastread = currentarticle++;
while(isdigit(*++parse));
while(*parse == ' ')parse++;
}
if(*parse == '\n') break;
save(parse);
/* hack */
currentarticle = lastread+1;
if(currentarticle == numofarticles)
currentarticle--;
break;
case 'n':
case 10:
case 13:
readmsg(stdout);
break;
default:
puts("pardon?");
while(fgets(buf,1023,stdin) != NULL);
/* clear input buffer, above */
break;
}
}
}