home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
lan
/
netx
/
bm
/
bmutil.c
< prev
next >
Wrap
Text File
|
1987-06-11
|
8KB
|
303 lines
/* bmutil.c */
#include <stdio.h>
#include "../src/smtp.h"
#include "bm.h"
/* list headers of a notesfile a message */
listnotes(notefn)
char *notefn;
{
char notefile[LINELEN],
smtp_date[LINELEN],
smtp_from[LINELEN],
smtp_subject[LINELEN],
tstring[LINELEN]; /* temp for integer conv */
char *tch1, *tch2, *index();
int sequence,current;
FILE *mfile;
current = seqget(notefn); /* get current notesfile index */
sprintf(notefile,"%s/%s.txt",maildir,notefn);
if((mfile = fopen(notefile,"r")) == NULL) {
printf("Cannot open notesfile %s \n",notefn);
return ;
}
sequence = 1;
while(!feof(mfile)){
fgets(tstring,LINELEN,mfile);
/* scan for Received: i.e. begin of a message */
if(strncmp(tstring,"Received",8) == 0) {
strncpy(smtp_date,&tstring[10],16);
if(strlen(smtp_date) > 16)
smtp_date[16] = '\0';
/* scan for From: in message */
fgets(tstring,LINELEN,mfile); /* read empty line */
while(strncmp(tstring,"From:",5) != 0)
fgets(tstring,LINELEN,mfile);
strncpy(smtp_from,&tstring[10],13);
if(strlen(smtp_from) > 13)
smtp_from[13] = '\0';
/* try to make a nice user@system string */
if((tch1 = index(smtp_from,'@')) != NULL)
if((tch2 = index(tch1,'.')) != NULL)
*tch2 = '\0';
fgets(tstring,LINELEN,mfile); /* read To: line */
fgets(tstring,LINELEN,mfile); /* read Subj: line */
strncpy(smtp_subject,&tstring[10],34);
rip(smtp_subject);
printf("%c%4d | %16s | %14s | %s \n",
( sequence == current ? '*' : ' '), sequence,
smtp_date,smtp_from,smtp_subject);
sequence++;
if((sequence % 24) == 0) {
printf(" - more -");
getchar();
}
}
}
}
/* readmsg - read a single message in current notesfile */
readmsg(notefn,msg)
char *notefn;
int msg;
{
char notefile[LINELEN],
tstring[LINELEN]; /* temp for integer conv */
int sequence,current,flag;
FILE *mfile;
current = seqget(notefn); /* get current notesfile index */
sprintf(notefile,"%s/%s.txt",maildir,notefn);
if((mfile = fopen(notefile,"r")) == NULL) {
printf("Cannot open notesfile %s \n",notefn);
return ;
}
sequence = 1;
flag = 0;
while(!feof(mfile)){
fgets(tstring,LINELEN,mfile);
/* scan for Received: i.e. begin of a message */
if(strncmp(tstring,"Received",8) == 0) {
if( sequence == msg)
flag = 1;
else
flag = 0;
/* scan for From: in message */
while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
if(msg == sequence || flag )
printf("%s",tstring);
fgets(tstring,LINELEN,mfile);
}
sequence++;
}
if(flag)
printf("%s",tstring);
}
fclose(mfile);
if(++current == msg)
seqset(notefn,msg);
return;
}
/* tmpmsg - write message in current notesfile to file*/
tmpmsg(notefn,msg,tfile)
char *notefn;
int msg;
FILE *tfile; /* already open for write */
{
char notefile[LINELEN],
tstring[LINELEN]; /* temp for integer conv */
int sequence,current,flag;
FILE *mfile;
current = seqget(notefn); /* get current notesfile index */
sprintf(notefile,"%s/%s.txt",maildir,notefn);
if((mfile = fopen(notefile,"r")) == NULL) {
printf("Cannot open notesfile %s \n",notefn);
return ;
}
sequence = 0;
flag = 0;
while(!feof(mfile)){
fgets(tstring,LINELEN,mfile);
/* scan for Received: i.e. begin of a message */
if(strncmp(tstring,"Received",8) == 0) {
sequence++;
if( sequence == msg)
flag = 1;
else
flag = 0;
/* scan for From: in message */
while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
if( flag )
fprintf(tfile,"%s",tstring);
fgets(tstring,LINELEN,mfile);
}
}
if(flag)
fprintf(tfile,"%s",tstring);
}
fclose(mfile);
if(++current == msg)
seqset(notefn,msg);
return;
}
/* updmsg - read new messages in current notesfile */
updmsg(notefn)
char *notefn;
{
char notefile[LINELEN],
tstring[LINELEN]; /* temp for integer conv */
int sequence,current,flag;
FILE *mfile;
current = seqget(notefn); /* get current notesfile index */
sprintf(notefile,"%s/%s.txt",maildir,notefn);
if((mfile = fopen(notefile,"r")) == NULL) {
printf("Cannot open notesfile %s \n",notefn);
return ;
}
sequence = 0;
flag = 0;
while(!feof(mfile)){
fgets(tstring,LINELEN,mfile);
/* scan for Received: i.e. begin of a message */
if(strncmp(tstring,"Received",8) == 0) {
sequence++;
if( sequence > current) {
flag = 1;
printf("------------------------------------ %d\n",sequence);
} else
flag = 0;
/* scan for From: in message */
while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
if( flag )
printf("%s",tstring);
fgets(tstring,LINELEN,mfile);
}
}
if(flag)
printf("%s",tstring);
}
fclose(mfile);
if(sequence > current)
seqset(notefn,sequence);
else
printf("No new messages in notefile\n");
}
/* delmsg - delete message in current notesfile */
delmsg(notefn,msg)
char *notefn;
int msg;
{
char notefile[LINELEN],
tempfile[LINELEN],
tstring[LINELEN]; /* temp for integer conv */
char *tch1, *tch2;
char ch;
int sequence,current, flag;
FILE *mfile,*tfile;
current = seqget(notefn); /* get current notesfile index */
if(current < msg) {
printf("Warning: You are deleting a message you did not yet read \n");
printf("Do you want to continue?");
gets(tstring);
if(toupper(tstring[0]) != 'Y')
return;
}
printf("Deleting message #%d from notesfile \"%s\" \n",msg,notefn);
sprintf(notefile,"%s/%s.txt",maildir,notefn);
if((mfile = fopen(notefile,"r")) == NULL) {
printf("Cannot open notesfile %s \n",notefn);
return ;
}
sprintf(tempfile,"%s/%s.tmp",maildir,notefn);
tfile = fopen(tempfile,"w");
sequence = 1;
while(!feof(mfile)){
/* scan for Received: i.e. begin of a message */
fgets(tstring,LINELEN,mfile);
if(strncmp(tstring,"Received",8) == 0) {
if( sequence == msg)
flag = 1;
else
flag = 0;
/* scan for From: in message */
while((strncmp(tstring,"From:",5) != 0) && !feof(mfile)) {
if(msg != sequence || !flag )
fputs(tstring,tfile);
fgets(tstring,LINELEN,mfile);
}
/* We are shure the header of a message is skipped. */
sequence++;
}
if(!flag)
fputs(tstring,tfile);
}
fclose(tfile);
fclose(mfile);
unlink(notefile);
rename(tempfile,notefile);
if(msg <= current)
seqset(notefn,--current);
}
/* seqset - set the sequence number (last read) for the current notesfile */
seqset(notefn,num)
char *notefn;
int num;
{
FILE *sfile;
char sfilename[80];
/* open the sequence file */
sprintf(sfilename,"%s/%s.seq",maildir,notefn);
if((sfile = fopen(sfilename,"w")) == NULL) {
printf("Cannot create Notefile Sequence file!\n");
return;
}
fprintf(sfile,"%d",num);
/* close the sequence file */
fclose(sfile);
}
/* seqget - get the sequence number (last read) for the current notesfile */
int
seqget(notefn)
char *notefn;
{
FILE *sfile;
char sfilename[80],
tstring[LINELEN];
int num;
/* open the sequence file */
sprintf(sfilename,"%s/%s.seq",maildir,notefn);
if((sfile = fopen(sfilename,"r")) != NULL) {
fgets(tstring,LINELEN,sfile);
num = atoi(tstring);
} else {
/* sequence number did not exist yet,create one and write to sequence file */
num = 1;
sfile = fopen(sfilename,"w");
fprintf(sfile,"%d",num);
}
/* close the sequence file */
fclose(sfile);
return num;
}