home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
SMTP.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-19
|
5KB
|
196 lines
/*
smtp.c
smtp queue management routines
Taken 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.
911218 : Added this header
920802 : Added file locking to get_msgid, bm wasn't locking at all.
920819 : Allow killing of mail in a mqueue directory whose path has
'.' in..
*/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <io.h>
#include "smtp.h"
#include "lock.h"
#include "misc.h"
#include "pc.h"
#include "rc.h"
#include "help.h"
#include "pager.h"
#ifdef BMH
#define main smtp_main
#endif
/*
* get a message id from the sequence file
*/
long
get_msgid(char *mqueue)
{
FILE *sfile = fopenlocked(mqueue, "sequence", ".seq", "r+");
long sequence = 0L;
/*
* if sequence file exists, get the value, otherwise set it
*/
if (sfile != NULL) {
if ( fscanf(sfile, "%ld", &sequence) == EOF)
sequence = -1L;
else {
/*
* Keep it in range, use 8 digit number for dos name prefix.
*/
if ( (sequence < 0L) || (sequence > 99999999L) )
sequence = 0L;
if (fseek(sfile, 0L, SEEK_SET) != 0)
sequence = -1L;
}
}
else {
fprintf(stderr, "smtp: creating sequence file\n");
if ( (sfile = fopenlocked(mqueue, "sequence", ".seq", "w")) == NULL)
return -1L;
}
if (sequence != -1L)
fprintf(sfile, "%ld", ++sequence);
fcloselocked(sfile, mqueue, "sequence");
return sequence;
}
/*
* list jobs wating to be sent in the mqueue
*/
void
listqueue(char *mqueue)
{
char tstring[80];
char workfile[80];
char line[20];
char host[LINELEN];
char to[LINELEN];
char from[LINELEN];
char *p;
char status;
struct stat stbuf;
struct tm *tminfo;
FILE *fp;
char buf[256];
page_setup();
page_puts("S Job Size Date Time Host From");
sprintf(workfile,"%s/*.wrk", mqueue); /* *.wrk */
filedir(workfile, 0, line);
while(line[0] != '\0') {
sprintf(tstring,"%s/%s", mqueue,line);
if ((fp = fopen(tstring,"r")) == NULL) {
perror(tstring);
continue;
}
if ((p = strrchr(line,'.')) != NULL)
*p = '\0';
sprintf(tstring,"%s/%s.lck",mqueue,line);
if (access(tstring,0))
status = ' ';
else
status = 'L';
sprintf(tstring, "%s/%s%s", mqueue, line, EXT);
stat(tstring,&stbuf);
tminfo = localtime(&stbuf.st_ctime);
fgets(host,sizeof(host),fp);
rip(host);
fgets(from,sizeof(from),fp);
rip(from);
sprintf(buf, "%c %7s %7ld %02d/%02d %02d:%02d %-20s %s",
status, line, stbuf.st_size,
tminfo ->tm_mon+1,
tminfo->tm_mday,
tminfo->tm_hour,
tminfo->tm_min,
host,from);
page_puts(buf);
while (fgets(to,sizeof(to),fp) != NULL) {
rip(to);
sprintf(buf, " %s", to);
page_puts(buf);
}
(void) fclose(fp);
filedir(workfile,1,line);
}
fflush(stdout);
}
int
killjob(char *mqueue, char *j)
{
char s[LINELEN];
sprintf(s, "%s/%s.lck", mqueue, j);
if (access(s, 0) == 0) {
char tbuf[20];
fprintf(stderr, "smtp: warning job %s is locked by SMTP, remove? (y,n)", j);
fgets(tbuf, sizeof(tbuf), stdin);
if (tolower(*tbuf) != 'y')
return -1;
if (unlink(s) == -1)
fprintf(stderr, "smtp: error deleting %s\n", s);
}
sprintf(s, "%s/%s.wrk", mqueue, j);
if (unlink(s) == -1)
fprintf(stderr, "smtp: job %s not found\n", s);
sprintf(s, "%s/%s.txt", mqueue, j);
return unlink(s);
}
#ifdef SMTP_PROG
int
main(int argc, char *argv[])
{
int i;
dohelp(argc, argv, "smtp [<job 1> <job 2> ... <job n>]");
if (setupbm() == -1)
return -1;
if (argc == 1)
listqueue(getrc(mqueue));
else /* Kill jobs */
for (i=1; i<argc; i++)
killjob(getrc(mqueue), argv[i]);
return 0;
}
#endif