home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
IMPORT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-16
|
6KB
|
223 lines
/*
import.c : Copyright Paul Healy, EI9GL, 1992.
Usage: import <filename> [<address>]
Takes one or more messages in bbs export format (optionally from stdin)
and queues them for delivery. Compile with Turbo C v2.0.
To do:
. parse R: lines for correct @<bbs> field. NOS could do with this 2.
911012 : Created.
911124 : Changed /EX comparision to catch a line with space(s) after the
X. (strcmp -> strncmp).
920501 : Cater for "@@" on some ANS bulletins
920718 : bmh version
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <dir.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
#include "misc.h"
#include "rc.h"
#include "send.h"
#include "lock.h"
#include "smtp.h"
#include "help.h"
#include "mailer.h"
static char *
grab(char *to, char *ptr)
{
char *start = to;
if (ptr==NULL)
return NULL;
while (*ptr && (*ptr==' ') )
ptr++;
while (*ptr && (*ptr!=' ') && (*ptr!='@')
&& (*ptr!='$') && (*ptr!='<') && (*ptr!='\n'))
*to++ = *ptr++;
*to = '\0';
return start;
}
static int
addbid(char *bid)
{
FILE *fp = fopenlocked(getrc(spooldir), "history", "", "a");
char *tsb = getrc(tsbids);
if (fp == NULL)
return -1;
if ( (tsb != NULL) && (tolower(*tsb) == 'y') )
fprintf(fp, "%s %ld\n", bid, time(NULL)); /* wg7j format */
else
fprintf(fp, "%s\n", bid);
if (ferror(fp))
fprintf(stderr, "addbid: could not add %s to the history file\n", bid);
return fcloselocked(fp, getrc(spooldir), "history");
}
static int
checkhistory(char *bid)
{
FILE *fp = fopenlocked(getrc(spooldir), "history", "", "r");
char line[256];
int len = strlen(bid);
if (fp == NULL)
return addbid(bid);
while ( fgets(line, sizeof(line), fp) != NULL )
if (strncmp(line, bid, len) == 0) {
fprintf(stderr, "import: %s found in history file. Skipping...\n", bid);
fcloselocked(fp, getrc(spooldir), "history");
return -1;
}
fcloselocked(fp, getrc(spooldir), "history");
return addbid(bid);
}
static int
digestline(char *line, char *bbstype, char *to, char *from, char *msgid,
int nohistory)
{
char temp[256], temp2[256], bid[256], *cp;
*bbstype = line[1];
/*
* Digest TO fields
*/
if ( (cp = strchr(line, '@')) != NULL)
if (*(cp+1) == '@') /* double @ on some ANS bulletins */
cp++;
if ( cp != NULL)
sprintf(to, "%s@%s", grab(temp, line+3), grab(temp2, cp+1));
else
sprintf(to, "%s@%s", grab(temp, line+3), getrc(hostname));
/*
* Digest FROM field
*/
if ( (cp = strchr(line, '<')) != NULL)
sprintf(from, "%s@%s", grab(temp, cp+1), getrc(hostname));
else
sprintf(from, "%s@%s", getrc(username), getrc(hostname));
/*
* Digest BID field
*/
if ( (cp = strchr(line, '$')) != NULL) {
sprintf(msgid, "%s@%s.bbs", grab(bid, cp+1), getrc(username));
return nohistory ? 0 : checkhistory(bid);
}
else {
sprintf(msgid, "%ld@%s", get_msgid(getrc(mqueue)), getrc(hostname));
return 0;
}
}
int
main(int argc, char *argv[])
{
char line[256];
FILE *fp, *msgfp;
int fin = 0, nohistory = 0;
dohelp(argc, argv, "import [<filename> [<address>] -nohistory]");
if (loadconfig()==-1)
return -1;
if ( (argc >=4) && (strcmp(argv[3], "-nohistory") == 0) )
nohistory = 1;
if ( (argc == 1) || (argv[1][0] == '-'))
fp = stdin;
else if ((fp=fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "import: can't open %s\n", argv[1]);
return -1;
}
while (fgets(line, sizeof(line), fp) != NULL) {
char bbstype, msgid[256], to[256], from[256], subj[256],
msgfn[256];
if ( (strlen(line) < 3) || (line[0] != 'S') )
continue;
if ( !( (line[1] == 'B') || (line[1] == 'P') ) )
continue;
fprintf(stderr,"importing: %s", line);
if (digestline(strlwr(line), &bbstype, to, from, msgid, nohistory) == -1) {
while (fgets(line, sizeof(line), fp) != NULL)
if (strncmpi(line, "/EX", 3) == 0)
break;
continue;
}
/*
* Get SUBJECT
*/
if (fgets(subj, sizeof(subj), fp) == NULL) {
subj[0] = '\0';
fin = 1;
}
else
rip(subj);
if ( (msgfp = tempfile("import", msgfn, "w+")) == NULL) {
if (fp != stdin) /* for bmh */
fclose(fp);
return -1;
}
if (fprintf(msgfp, "X-BBS-Msg-Type: %c\n\n", toupper(bbstype)) == EOF) {
fprintf(stderr, "import: unable to write to %s\n", msgfn);
fclose(msgfp);
if (fp != stdin)
fclose(fp);
return -1;
}
while (!fin && (fgets(line, sizeof(line), fp) != NULL) )
if (strncmpi(line, "/EX", 3) == 0)
break;
else
if ( fputs(line, msgfp) == EOF) {
fprintf(stderr, "import: unable to write to %s\n", msgfn);
fclose(msgfp);
return -1;
}
fseek(msgfp, 0L, SEEK_SET);
if ( dosend(msgfp, argc>=3 ? argv[2] : to,
NULL, subj, from, msgid) == -1) {
fprintf(stderr, "import: unable to queue message to %s\n", to);
return -1;
}
fclose(msgfp);
unlink(msgfn);
}
if (fp != stdin) /* for bmh */
fclose(fp);
return 0;
}