home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B78.ZIP
/
EXP.CPP
< prev
next >
Wrap
Text File
|
1997-12-29
|
56KB
|
1,969 lines
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <mem.h>
#include <conio.h>
#include <io.h>
#include <share.h>
#include <errno.h>
#include <dir.h>
#include <time.h>
#include <malloc.h>
#include <process.h>
#include <direct.h>
#include "vardec.h"
#include "net.h"
#include "retcode.h"
#include "version.h"
#define WAIT_TIME 10
#define TRIES 100
#define SHARE_LEVEL 10
#define MT_DESQVIEW 0x01
#define MT_WINDOWS 0x02
#define MT_OS2 0x04
#define MT_NB 0x40
struct msghdr {
char fromUserName[205];
char toUserName[81];
char subject[81];
char dateTime[81];
};
typedef struct {
char ownername[60];
char subtype[8];
char opttext[30];
} MAILLISTREC;
MAILLISTREC *maillist;
configrec syscfg;
char net_name[31], postmaster[31], net_data[MAXPATH];
char POPNAME[21], REPLYTO[81], DOMAIN[81], tagfile[MAXPATH], maindir[MAXPATH], spamname[81];
unsigned short net_sysnum, defuser, use_alias, usermail, instance, spam;
unsigned curuser, num_users, replyaddr;
int multitasker = 0;
int nlists = 0;
char alphasubtype[8];
unsigned long cur_daten;
void dv_pause(void)
{
__emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
__emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
__emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
}
void win_pause(void)
{
__emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
}
int get_dos_version(void)
{
_AX = 0x3000;
geninterrupt(0x21);
if (_AX % 256 >= 10) {
multitasker |= MT_OS2;
}
return (_AX);
}
int get_dv_version(void)
{
int v;
if (multitasker & MT_OS2)
return 0;
_AX = 0x2b01;
_CX = 0x4445;
_DX = 0x5351;
geninterrupt(0x21);
if (_AL == 0xff) {
return 0;
} else {
v = _BX;
multitasker |= MT_DESQVIEW;
return v;
}
}
int get_win_version(void)
{
int v = 0;
__emit__(0x55, 0x06, 0x53);
_AX = 0x352f;
geninterrupt(0x21);
_AX = _ES;
if (_AX | _BX) {
_AX = 0x1600;
geninterrupt(0x2f);
v = _AX;
if (v % 256 <= 1)
v = 0;
}
__emit__(0x5b, 0x07, 0x5d);
if (v != 0)
multitasker |= MT_WINDOWS;
return (v);
}
int get_nb_version(void)
{
_AX = 0;
geninterrupt(0x2A);
return (_AH);
}
void detect_multitask(void)
{
get_dos_version();
get_win_version();
get_dv_version();
if (multitasker < 2)
if (get_nb_version())
multitasker = MT_NB;
}
void giveup_timeslice(void)
{
if (multitasker) {
switch (multitasker) {
case 1:
case 3:
dv_pause();
break;
case 2:
case 4:
case 5:
case 6:
case 7:
win_pause();
break;
default:
break;
}
}
}
int sh_write(int handle, void *buffer, unsigned long len)
{
if (handle == -1) {
return (-1);
}
return (write(handle, buffer, (unsigned) len));
}
int sh_open(char *path, int file_access, unsigned fmode)
{
int handle, count, share;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
share = SH_DENYRW;
} else {
share = SH_DENYWR;
}
handle = open(path, file_access | share, fmode);
if (handle < 0) {
count = 1;
fnsplit(path, drive, dir, file, ext);
if (access(path, 0) != -1) {
delay(WAIT_TIME);
handle = open(path, file_access | share, fmode);
while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
if (count % 2)
delay(WAIT_TIME);
else
giveup_timeslice();
count++;
handle = open(path, file_access | share, fmode);
}
}
}
return (handle);
}
int sh_open1(char *path, int access)
{
unsigned fmode;
fmode = 0;
if ((access & O_RDWR) || (access & O_WRONLY))
fmode |= S_IWRITE;
if ((access & O_RDWR) || (access & O_RDONLY))
fmode |= S_IREAD;
return (sh_open(path, access, fmode));
}
int sh_close(int f)
{
if (f != -1)
close(f);
return (-1);
}
int sh_read(int handle, void *buf, unsigned len)
{
if (handle == -1) {
return (-1);
}
return (read(handle, buf, len));
}
long sh_lseek(int handle, long offset, int fromwhere)
{
if (handle == -1) {
return (-1L);
}
return (lseek(handle, offset, fromwhere));
}
FILE *fsh_open(char *path, char *fmode)
{
FILE *f;
int count, share, md, fd;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
share = SH_DENYWR;
md = 0;
if (((char *) _fstrchr(fmode, 'w')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT | O_TRUNC;
} else
if (((char *) _fstrchr(fmode, 'a')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT;
} else {
md = O_RDONLY;
}
if (((char *) _fstrchr(fmode, 'b')) != NULL) {
md |= O_BINARY;
}
if (((char *) _fstrchr(fmode, '+')) != NULL) {
md &= ~O_RDONLY;
md |= O_RDWR;
share = SH_DENYRD;
}
fd = open(path, md | share, S_IREAD | S_IWRITE);
if (fd < 0) {
count = 1;
fnsplit(path, drive, dir, file, ext);
if ((access(path, 0)) != -1) {
delay(WAIT_TIME);
fd = open(path, md | share, S_IREAD | S_IWRITE);
while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
delay(WAIT_TIME);
count++;
fd = open(path, md | share, S_IREAD | S_IWRITE);
}
}
}
if (fd > 0) {
if (((char *) _fstrchr(fmode, 'a')) != NULL)
sh_lseek(fd, 0L, SEEK_END);
f = fdopen(fd, fmode);
if (!f) {
close(fd);
}
} else
f = 0;
return (f);
}
size_t fsh_write(void *ptr, size_t size, size_t n, FILE * stream)
{
if (stream == NULL) {
return (0);
}
return (fwrite(ptr, size, n, stream));
}
int exist(char *s)
{
int i;
struct ffblk ff;
i = findfirst(s, &ff, 0);
if (i)
return (0);
else
return (1);
}
char *stripspace(char *str)
{
char *obuf, *nbuf;
if (str) {
for (obuf = str, nbuf = str; *obuf; ++obuf) {
if (!isspace(*obuf))
*nbuf++ = *obuf;
}
*nbuf = NULL;
}
return (str);
}
static unsigned char *trimstr1(unsigned char *s)
{
int i;
static char *whitespace = " \r\n\t";
i = (int) strlen(s);
while ((i > 0) && ((char *) _fstrchr(whitespace, s[i - 1]) != NULL))
--i;
while ((i > 0) && ((char *) _fstrchr(whitespace, *s) != NULL)) {
memmove(s, s + 1, --i);
}
s[i] = 0;
return (s);
}
void output(char *fmt,...)
{
va_list v;
char s[255];
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, stderr);
}
int log_it(int display, char *fmt,...)
{
va_list v;
char s[255], fn[MAXPATH];
FILE *fp;
sprintf(fn, "%sNEWS.LOG", net_data);
if ((fp = fsh_open(fn, "at")) == NULL) {
output("\n ! Error accessing %s.", fn);
return 1;
}
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, fp);
fclose(fp);
if (display)
fputs(s, stderr);
return 0;
}
int num_to_name(int out, char *where, int whichuser, int alias)
{
char *ss, fn[MAXPATH], s[101], buf[60];
int i, userfile, num_users, found;
long pos;
FILE *fp;
userrec ur;
found = 0;
if (out) {
sprintf(fn, "%sNET.INI", maindir);
if ((fp = fsh_open(fn, "rt")) != NULL) {
while ((fgets(s, 100, fp)) && (!found)) {
if (strnicmp(s, "USER", 4) == 0) {
ss = strtok(s, "=");
strcpy(buf, &s[4]);
if (isdigit(buf[0])) {
i = atoi(buf);
if (i == whichuser) {
ss = strtok(NULL, "\r\n");
if (ss) {
trimstr1(ss);
strcpy(where, ss);
found = 1;
replyaddr = 1;
}
}
}
}
}
if (fp != NULL)
fclose(fp);
if (found)
return (found);
}
}
sprintf(fn, "%sUSER.LST", syscfg.datadir);
userfile = sh_open1(fn, O_RDONLY | O_BINARY);
if (userfile < 0) {
log_it(1, "\n ■ Cannot open %s.", fn);
return (found);
}
num_users = ((int) (filelength(userfile) / sizeof(userrec)));
if (whichuser > num_users) {
log_it(1, "\n ■ User #%d out of range.", whichuser);
return (found);
}
pos = ((long) sizeof(userrec) * ((long) whichuser));
lseek(userfile, pos, SEEK_SET);
sh_read(userfile, &ur, sizeof(userrec));
if (ur.realname[0] == 0)
log_it(1, "\n ■ User #%d has blank real name field!", whichuser);
else {
if (ur.inact == inact_deleted)
log_it(1, "\n ■ User #%d is marked as deleted!", whichuser);
else {
if (!alias)
strcpy(where, ur.realname);
else {
strcpy(where, ur.name);
strlwr(where);
}
found = 1;
}
}
sh_close(userfile);
return (found);
}
void parse_net_ini(void)
{
char s[MAXPATH], origline[121], line[121], *ss, inlist = 0;
FILE *fp;
long fptr;
defuser = 1;
use_alias = 1;
usermail = 1;
nlists = 0;
maillist = NULL;
*REPLYTO = 0;
sprintf(s, "%sNET.INI", maindir);
if ((fp = fsh_open(s, "rt")) == NULL) {
log_it(1, "\n ■ Unable to open %s.", s);
return;
}
while (fgets(line, 80, fp)) {
ss = NULL;
strcpy(origline, line);
stripspace(line);
if ((line[0] == ';') || (line[0] == '\n') || (line[0] == 0))
continue;
if (strnicmp(line, "[MAILLIST]", 10) == 0) {
fptr = ftell(fp);
while ((fgets(line, 80, fp)) && (line[0] != '[')) {
if ((line[0] != '[') && (line[0] != ';') && (line[0] != 0))
++nlists;
}
fseek(fp, fptr, SEEK_SET);
maillist = (MAILLISTREC *) malloc((nlists + 1) * sizeof(MAILLISTREC));
if (maillist == NULL)
log_it(1, "\n ■ Not enough memory to process %d mailing lists.", nlists);
else
inlist = 1;
nlists = 0;
continue;
} else if (line[0] == '[') {
inlist = 0;
continue;
}
if (inlist) {
if ((line[0] != ';') && (line[0] != 0)) {
ss = strtok(line, "\"");
trimstr1(ss);
if (*ss) {
ss = strtok(NULL, "\"");
if (*ss) {
strncpy(maillist[nlists].opttext, ss, 29);
maillist[nlists].opttext[30] = '\0';
} else
maillist[nlists].opttext[0] = '\0';
}
ss = strtok(line, "*\n");
trimstr1(ss);
strcpy(maillist[nlists].ownername, ss);
ss = strtok(NULL, "\"\n");
trimstr1(ss);
if (*ss) {
strlwr(maillist[nlists].ownername);
strcpy(maillist[nlists++].subtype, ss);
} else
log_it(1, "\n ■ Missing *subtype in maillist for %s.",
maillist[nlists].ownername);
}
continue;
}
if (strnicmp(line, "POSTMASTER", 10) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
if (ss)
defuser = atoi(ss);
}
continue;
}
if (strnicmp(line, "SPAMCONTROL", 11) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
if ((ss[0] == 'y') || (ss[0] == 'Y'))
spam = 1;
}
continue;
}
if (strnicmp(line, "USERMAIL", 8) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
if ((ss[0] == 'n') || (ss[0] == 'N'))
usermail = 0;
}
continue;
}
if (strnicmp(line, "SPAMADDRESS", 9) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(spamname, ss);
if (stricmp(spamname, "DEFAULT") == 0)
strcpy(spamname, "WWIV_BBS@nospam.net");
}
continue;
}
if (strnicmp(line, "REPLYTO", 7) == 0) {
ss = strtok(origline, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(REPLYTO, ss);
}
continue;
}
if (strnicmp(line, "SIGNATURE", 9) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
trimstr1(ss);
strcpy(tagfile, ss);
if (!exist(tagfile)) {
log_it(1, "\n ■ Default signature file %s not found!", tagfile);
tagfile[0] = 0;
}
}
continue;
}
if (strnicmp(line, "REALNAME", 8) == 0) {
ss = strtok(line, "=");
if (ss) {
ss = strtok(NULL, "\n");
if ((ss[0] == 'y') || (ss[0] == 'Y'))
use_alias = 0;
}
}
}
num_to_name(0, postmaster, defuser, 1);
if (fp != NULL)
fclose(fp);
return;
}
unsigned char *strrep(char *str, char old, char New)
{
int i;
for (i = 0; str[i]; i++)
if (str[i] == old)
str[i] = New;
return (str);
}
char *stristr(char *String, char *Pattern)
{
char *pptr, *sptr, *start;
unsigned int slen, plen;
for (start = String, pptr = Pattern, slen = strlen(String),
plen = strlen(Pattern); slen >= plen; start++, slen--) {
while (toupper(*start) != toupper(*Pattern)) {
start++;
slen--;
if (slen < plen)
return (NULL);
}
sptr = start;
pptr = Pattern;
while (toupper(*sptr) == toupper(*pptr)) {
sptr++;
pptr++;
if ('\0' == *pptr)
return (start);
}
}
return (NULL);
}
int name_to_acct(char *name)
{
char *ss, s[101], fn[MAXPATH], buf[61], hold[10];
int found, usernum;
FILE *fp;
sprintf(fn, "%sNET.INI", maindir);
if ((fp = fsh_open(fn, "rt")) == NULL) {
log_it(1, "\n ! Unable to open %s.", fn);
return 0;
}
found = usernum = 0;
while ((fgets(s, 100, fp)) && (!found)) {
if (strnicmp(s, "USER", 4) == 0) {
ss = strtok(s, "=");
strcpy(hold, ss);
ss = strtok(NULL, "\r\n");
if (ss) {
trimstr1(ss);
if (stristr(name, ss) != NULL) {
strcpy(buf, &hold[4]);
usernum = atoi(buf);
found = 1;
}
}
}
}
if (fp != NULL)
fclose(fp);
if (usernum > 0)
log_it(1, "\n ■ Matched %s to NET.INI user #%d.", name, usernum);
return usernum;
}
unsigned int name_to_num(char *name)
{
int userfile, usernum;
userrec ur;
long pos;
char fn[MAXPATH], ur_name[60], ur_realname[60];
if ((stristr(name, "Multiple recipients of") != NULL) || (strlen(name) == 0))
return 0;
sprintf(fn, "%sM%s.NET", net_data, name);
if (exist(fn)) {
log_it(1, "\n ■ Matched \"%s\" mailing list.", name);
strcpy(alphasubtype, name);
return (unsigned) (65535L);
}
if ((usernum = name_to_acct(name)) > 0)
return usernum;
sprintf(fn, "%sUSER.LST", syscfg.datadir);
userfile = sh_open1(fn, O_RDONLY | O_BINARY);
if (userfile < 0) {
log_it(1, "\n ■ Cannot open %s", fn);
return (0);
} else
log_it(1, "\n ■ Searching for user \"%s\"...", name);
num_users = ((int) (filelength(userfile) / sizeof(userrec)));
for (usernum = 1; usernum < num_users; usernum++) {
pos = ((long) sizeof(userrec) * ((long) usernum));
lseek(userfile, pos, SEEK_SET);
sh_read(userfile, &ur, sizeof(userrec));
strcpy(ur_realname, ur.realname);
strrep(ur_realname, ' ', '_');
strcpy(ur_name, ur.name);
strrep(ur_name, ' ', '_');
if ((strcmpi(ur.realname, name) == 0) || (strcmpi(ur_realname, name) == 0) ||
(strcmpi(ur.name, name) == 0) || (strcmpi(ur_name, name) == 0)) {
if (ur.inact == inact_deleted) {
log_it(1, " user #%d is deleted account.", usernum);
usernum = 0;
break;
} else {
log_it(1, " matched to user #%d.", usernum);
break;
}
}
}
userfile = sh_close(userfile);
if (usernum >= num_users) {
log_it(1, "... no match found.");
return 0;
}
return (usernum);
}
char *find_name(char *name)
{
char *ss, *ss1, hold[81];
int focus = 0;
curuser = 0;
strcpy(hold, name);
ss1 = NULL;
if ((ss = strchr(name, '(')) != NULL) {
ss1 = strtok(name, "(");
ss1 = strtok(NULL, ")");
} else if ((ss = strchr(name, '\"')) != NULL) {
ss1 = strtok(ss, "\"");
} else if ((ss = strchr(name, '<')) != NULL) {
ss1 = strtok(name, "<");
} else
focus = 1;
trimstr1(ss1);
if (focus) {
stripspace(hold);
curuser = name_to_num(hold);
} else
curuser = name_to_num(ss1);
if (curuser == 0)
return ('\0');
else if (curuser == ((unsigned) 65535L))
return (alphasubtype);
else
return (ss1);
}
void name_packet(char *pktname)
{
int ok;
struct stat info;
unsigned i;
ok = 0;
for (i = 0; ((i < 1000) && (!ok)); i++) {
sprintf(pktname, "%sP0-%u.%3.3hu", net_data, i, instance);
if (stat(pktname, &info) == -1)
ok = 1;
}
}
#define FROM_RETURN 0x01
#define FROM_FROM 0x02
#define FROM_REPLY 0x04
int import(char *fn)
{
char s[513], s1[121], pktname[MAXPATH], msgdate[61], *ss, *p, *id, *name;
char alphatype[21], recvdate[81];
int i, f, from, match, subj, intext, done, tolist;
long textlen, reallen;
struct msghdr mh;
net_header_rec nh;
FILE *fp;
tolist = 0;
intext = 0;
f = sh_open1(fn, O_RDONLY | O_BINARY);
if (f < 0)
return (1);
textlen = filelength(f);
if (textlen > 32767L) {
sh_close(f);
log_it(1, "\n ■ Skipping UU/Base64 %s.", fn);
return (1);
}
p = (char *) malloc((int) (textlen + 1));
if (p == NULL) {
sh_close(f);
log_it(1, "\n ■ Unable to allocate %ld bytes.", textlen);
return (1);
}
sh_read(f, (void *) p, (int) textlen);
sh_close(f);
nh.tosys = net_sysnum;
nh.fromsys = 32767;
nh.fromuser = 0;
nh.touser = defuser;
nh.main_type = main_type_email;
nh.minor_type = 0;
nh.list_len = 0;
++cur_daten;
nh.daten = cur_daten;
strncpy(msgdate, ctime(&(time_t) nh.daten), 24);
msgdate[24] = '\0';
sprintf(recvdate, "0RReceived: PPP Project %s on %s\r\n", VERSION, msgdate);
strcat(msgdate, "\r\n");
nh.method = 0;
strcpy(mh.fromUserName, "Unknown");
strcpy(mh.toUserName, "Unknown");
strcpy(mh.subject, "None");
if ((fp = fsh_open(fn, "rb")) == NULL) {
free(p);
return 1;
}
match = subj = done = 0;
while ((fgets(s, 254, fp)) && !done) {
if (s[0] == 4) {
ss = strtok(s, "R");
ss = strtok(NULL, "\r\n");
if (ss == NULL)
s[0] = 0;
else
strcpy(s, ss);
} else
intext = 1;
if (!intext) {
if (strncmpi(s, "from:", 5) == 0)
from = FROM_FROM;
else
if (strncmpi(s, "return-path:", 12) == 0)
from = FROM_RETURN;
else
if (strncmpi(s, "sender:", 7) == 0)
from = FROM_RETURN;
else
if (strncmpi(s, "x-sender:", 9) == 0)
from = FROM_RETURN;
else
if (strncmpi(s, "x-to:", 5) == 0)
from = FROM_RETURN;
else
if (strncmpi(s, "reply-to:", 9) == 0)
from = FROM_REPLY;
else
if (strncmpi(s, "x-reply-to:", 11) == 0)
from = FROM_REPLY;
else
from = 0;
if (from) {
ss = strtok(s, ": ");
ss = strtok(NULL, "\r\n");
trimstr1(ss);
if ((from & (FROM_RETURN | FROM_REPLY)) && (nh.main_type == main_type_email)) {
strcpy(s1, ss);
strlwr(s1);
for (i = 0; (i < nlists) && (nh.main_type == main_type_email); i++) {
if (stristr(s1, maillist[i].ownername) != NULL) {
if (atoi(maillist[i].subtype)) {
nh.main_type = main_type_pre_post;
nh.minor_type = atoi(maillist[i].subtype);
} else {
nh.main_type = main_type_new_post;
nh.minor_type = 0;
strcpy(alphatype, maillist[i].subtype);
}
strcpy(alphasubtype, maillist[i].subtype);
nh.touser = 0;
from = 0;
}
}
}
if ((from > match) && ((nh.main_type == main_type_email) || (from == FROM_FROM))) {
match = from;
if (strcspn(ss, "<") != strlen(ss)) {
if ((strcspn(ss, " ")) < (strcspn(ss, "<"))) {
name = strtok(ss, "<");
trimstr1(name);
id = strtok(NULL, ">");
trimstr1(id);
sprintf(mh.fromUserName, "%s <%s>", name, id);
} else {
strncpy(mh.fromUserName, ss, 205);
trimstr1(mh.fromUserName);
if (strcspn(ss, " ") != strlen(ss))
log_it(1, "\nName is *after* host in \"%s\"", name);
}
} else
strncpy(mh.fromUserName, ss, 205);
mh.fromUserName[190] = 0;
strcat(mh.fromUserName, "\r\n");
}
} else if ((strncmpi(s, "subject:", 8) == 0) && (!subj)) {
ss = strtok(s, ": ");
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strncpy(mh.subject, ss, 81);
mh.subject[72] = 0;
subj = 1;
} else if (strncmpi(s, "date:", 5) == 0) {
ss = strtok(s, ": ");
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strncpy(msgdate, ss, 58);
msgdate[58] = '\0';
strcat(msgdate, "\r\n");
} else if ((strncmpi(s, "to:", 3) == 0) || (strncmpi(s, "cc:", 3) == 0)) {
ss = strtok(s, ":");
ss = strtok(NULL, "\r\n");
strncpy(mh.toUserName, ss, 81);
mh.toUserName[80] = 0;
curuser = 0;
trimstr1(mh.toUserName);
find_name(mh.toUserName);
/*
if ((_fstrchr(mh.toUserName, ' ') != NULL) || (_fstrchr(mh.toUserName, '\"') != NULL) ||
(_fstrchr(mh.toUserName, '(') != NULL))
find_name(mh.toUserName);
else
mh.toUserName[0] = 0;
*/
if ((stristr(mh.toUserName, "Multiple recipients of") != NULL) && (curuser != (unsigned) 65535L)) {
for (i = 0; (i < nlists) && (nh.main_type == main_type_email); i++) {
if (stristr(mh.toUserName, maillist[i].opttext) != NULL) {
if (atoi(maillist[i].subtype)) {
nh.main_type = main_type_pre_post;
nh.minor_type = atoi(maillist[i].subtype);
} else {
nh.main_type = main_type_new_post;
nh.minor_type = 0;
strcpy(alphatype, maillist[i].subtype);
}
strcpy(alphasubtype, maillist[i].subtype);
nh.touser = 0;
}
}
}
if ((curuser == (unsigned) 65535L) && (nh.main_type == main_type_email)) {
strcpy(alphatype, alphasubtype);
nh.main_type = main_type_new_post;
nh.minor_type = 0;
nh.touser = 0;
tolist = 1;
} else
if ((mh.toUserName[0] == 0) || (curuser == 0)) {
nh.touser = defuser;
strcpy(mh.toUserName, postmaster);
} else {
nh.touser = curuser;
}
} else if (strncmpi(s, "apparently-to:", 14) == 0) {
ss = strtok(s, ": ");
ss = strtok(NULL, "\r\n");
strncpy(mh.toUserName, ss, 81);
mh.toUserName[80] = 0;
curuser = 0;
trimstr1(mh.toUserName);
if (_fstrstr(mh.toUserName, " "))
find_name(mh.toUserName);
else
mh.toUserName[0] = 0;
if ((curuser == (unsigned) 65535L) && (nh.main_type == main_type_email)) {
strcpy(alphatype, alphasubtype);
nh.main_type = main_type_new_post;
nh.minor_type = 0;
nh.touser = 0;
tolist = 1;
} else if ((mh.toUserName[0] == 0) || (curuser == 0)) {
nh.touser = defuser;
strcpy(mh.toUserName, postmaster);
} else
nh.touser = curuser;
}
} else
done = 1;
}
if (fp != NULL)
fclose(fp);
trimstr1(mh.fromUserName);
strcat(mh.fromUserName, "\r\n");
log_it(1, "\n ■ From : %s", strlwr(mh.fromUserName));
if ((nh.main_type == main_type_pre_post) ||
(nh.main_type == main_type_new_post))
log_it(1, " ■ Post to : Sub %s", alphasubtype);
else
log_it(1, " ■ Sent to : %s #%hd", strupr(mh.toUserName), nh.touser);
log_it(1, "\n ■ Subject : %s", mh.subject);
name_packet(pktname);
if ((fp = fsh_open(pktname, "wb")) == NULL) {
log_it(1, "\n ■ Unable to create packet %s", pktname);
free(p);
return (1);
}
nh.length = textlen + strlen(mh.fromUserName) + strlen(mh.subject)
+ strlen(msgdate) + strlen(recvdate) + 1;
if (nh.main_type == main_type_new_post)
nh.length += strlen(alphatype) + 1;
while (tolist >= 0) {
fsh_write(&nh, sizeof(net_header_rec), 1, fp);
if (nh.main_type == main_type_new_post)
fsh_write(alphatype, sizeof(char), strlen(alphatype) +1, fp);
fsh_write(mh.subject, sizeof(char), strlen(mh.subject) +1, fp);
fsh_write(mh.fromUserName, sizeof(char), strlen(mh.fromUserName), fp);
fsh_write(msgdate, sizeof(char), strlen(msgdate), fp);
fsh_write(recvdate, sizeof(char), strlen(recvdate), fp);
reallen = fsh_write(p, sizeof(char), (int) textlen, fp);
if (reallen != textlen)
log_it(1, "\n ■ Expected %ld bytes, wrote %ld bytes.", textlen, reallen);
nh.tosys = 32767;
--tolist;
}
if (fp != NULL)
fclose(fp);
free(p);
return (0);
}
char *stripcolors(char *str)
{
char *obuf, *nbuf;
if (str) {
for (obuf = str, nbuf = str; *obuf; ++obuf) {
if (*obuf == 3)
++obuf;
else
if (((*obuf < 32) && (*obuf != 9)) || (*obuf > 126))
continue;
else
*nbuf++ = *obuf;
}
*nbuf = NULL;
}
return (str);
}
void properize(char *s)
{
int i;
for (i = 0; i < strlen(s); i++) {
if ((i == 0) || ((i > 0) && ((s[i - 1] == ' ') || (s[i - 1] == '.') ||
((s[i - 1] == 'c') && (s[i - 2] == 'M')) || ((s[i - 1] == 'c') &&
(s[i - 2] == 'a') && (s[i - 3] == 'M')) || (s[i - 1] == '-') ||
(s[i - 1] == '_') || ((s[i - 1] == '\'') && (s[i - 2] == 'O'))))) {
s[i] = toupper(s[i]);
} else
s[i] = tolower(s[i]);
}
}
void move_dead(net_header_rec *nh, char *text)
{
char fn[81];
int f;
long l, l1;
sprintf(fn, "%sDEAD.NET", net_data);
f = sh_open(fn, O_RDWR | O_BINARY | SH_DENYRW | O_CREAT, S_IREAD | S_IWRITE);
if (f > 0) {
lseek(f, 0L, SEEK_END);
l = l1 = tell(f);
write(f, (void *)nh, sizeof(net_header_rec));
l1 += sizeof(net_header_rec);
write(f, (void *)text, (int)(nh->length));
l1 += nh->length;
if (l1 != tell(f))
chsize(f,l);
f = sh_close(f);
} else
log_it(1, "\n ! Couldn't open '%s'", fn);
}
int export(char *fn)
{
char fn1[121], tagfn[121], groupname[81], outfn[121], _temp_buffer[256];
char *ss, *buffer, *text, alphatype[21], hold[21], tempoutfn[21];
unsigned stype, ttype;
int infile, outfile, inloc, outloc, term, ok, i, j, ns, i6, tolist;
net_header_rec nhr;
struct msghdr mh;
struct tm *time_msg;
FILE *fp;
time_t some;
char *main_type[] =
{
"Network Update", "email by usernum", "post from sub host", "file",
"post to sub host", "external message", "email by name",
"NetEdit message", "SUBS.LST", "Extra Data", "BBSLIST from GC",
"CONNECT from GC", "Unused_1", "Info from GC", "SSM", "Sub Add Request",
"Sub Drop Request", "Sub Add Response", "Sub Drop Response", "Sub Info",
"Unused 1", "Unused 2", "Unused 3", "Unused 4", "Unused 5", "new post",
"new external"
};
if ((infile = sh_open1(fn, O_RDONLY | O_BINARY)) == -1)
return 1;
if ((buffer = (char *) malloc(32 * 1024)) == NULL) {
sh_close(infile);
log_it(1, "\n ■ Out of memory allocating input buffer!");
return 1;
}
if ((text = (char *) malloc(32 * 1024)) == NULL) {
log_it(1, "\n ■ Out of memory allocating output buffer!");
sh_close(infile);
if (buffer != NULL)
free(buffer);
return 1;
}
while (sh_read(infile, &nhr, sizeof(nhr))) {
sh_read(infile, buffer, (int) nhr.length);
if (nhr.tosys != 32767) {
log_it(1, "\n ■ System @%hd routing through @32767... moving to DEAD.NET.",
nhr.fromsys);
move_dead(&nhr, buffer);
continue;
}
tolist = 0;
if (nhr.main_type == main_type_pre_post)
nhr.main_type = main_type_post;
if ((nhr.main_type == main_type_post) ||
(nhr.main_type == main_type_new_post) ||
(nhr.main_type == main_type_email_name) ||
(nhr.main_type == main_type_ssm)) {
inloc = 0;
sprintf(hold, "%hu", nhr.minor_type);
if ((nhr.main_type == main_type_email_name) ||
(nhr.main_type == main_type_ssm) ||
(nhr.main_type == main_type_new_post)) {
stype = nhr.minor_type;
inloc = strlen(buffer) + 1;
if ((nhr.main_type == main_type_new_post) || ((nhr.fromsys != net_sysnum) &&
(nhr.fromsys != 32767))) {
strcpy(alphasubtype, buffer);
strcpy(hold, alphasubtype);
} else
strcpy(mh.toUserName, buffer);
} else
if (nhr.main_type == main_type_post) {
stype = nhr.minor_type;
} else {
stype = atoi(&buffer[inloc]);
sprintf(_temp_buffer, "%u", stype);
inloc += strlen(_temp_buffer) + 1;
}
strncpy(mh.subject, &buffer[inloc], sizeof(mh.subject));
stripcolors(mh.subject);
inloc += strlen(&buffer[inloc]) + 1;
for (term = inloc; buffer[term] != '\r'; term++);
buffer[term] = '\0';
if ((nhr.fromsys == net_sysnum) && (nhr.fromuser)) {
if ((nhr.main_type != main_type_post) &&
(nhr.main_type != main_type_new_post)) {
replyaddr = 0;
if (!num_to_name(1, mh.fromUserName, nhr.fromuser, use_alias)) {
log_it(1, "\n ■ No match for user #%hd... skipping message!",
nhr.fromuser);
continue;
}
} else {
strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
stripcolors(mh.fromUserName);
strtok(mh.fromUserName, "#");
trimstr1(mh.fromUserName);
}
} else {
strncpy(mh.fromUserName, &buffer[inloc], sizeof(mh.fromUserName));
stripcolors(mh.fromUserName);
strtok(mh.fromUserName, "#");
trimstr1(mh.fromUserName);
if ((nhr.main_type == main_type_post) &&
(nhr.main_type == main_type_new_post)) {
sprintf(_temp_buffer, " #%hd @%hu", nhr.fromuser, nhr.fromsys);
strcat(mh.fromUserName, _temp_buffer);
log_it(1, "\nFrom: %s\n", mh.fromUserName);
}
}
inloc = term + 2;
while (buffer[inloc] != '\n')
inloc++;
inloc++;
if (strnicmp(&buffer[inloc], "RE: ", 4) == 0) {
for (term = inloc; buffer[term] != '\r'; term++);
buffer[term] = '\0';
strncpy(mh.subject, &buffer[inloc + 4], sizeof(mh.subject));
if (strnicmp(mh.subject, "RE: ", 4) != 0) {
strcpy(_temp_buffer, "Re: ");
strcat(_temp_buffer, mh.subject);
}
strcpy(mh.subject, _temp_buffer);
inloc = term + 2;
}
if ((strncmp(&buffer[inloc], "BY: ", 4) == 0) ||
(strncmp(&buffer[inloc], "TO: ", 4) == 0)) {
for (term = inloc; buffer[term] != '\r'; term++);
buffer[term] = '\0';
strncpy(mh.toUserName, &buffer[inloc + 4], sizeof(mh.toUserName));
stripcolors(mh.toUserName);
if (strcspn(mh.toUserName, "<") != strlen(mh.toUserName)) {
if ((_fstrstr(mh.toUserName, "\"") == 0) && (_fstrstr(mh.toUserName, "(") == 0)) {
ss = strtok(mh.toUserName, "<");
ss = strtok(NULL, ">");
strcpy(mh.toUserName, ss);
}
}
inloc = term + 2;
} else {
if (nhr.main_type != main_type_email_name) {
strcpy(mh.toUserName, "ALL");
}
}
outloc = 0;
do {
if (buffer[inloc] == 2) {
i = inloc + 1;
for (j = 1; j < 255; j++) {
if (buffer[i] == 'π')
buffer[i] = '\r';
if ((buffer[i] == '\r') || (i > nhr.length))
break;
i++;
}
if (j < 80) {
i = (80 - j) / 2;
for (j = 1; j <= i; j++)
text[outloc++] = ' ';
}
inloc++;
} else
if (buffer[inloc] == 3)
inloc += 2;
else
if ((buffer[inloc] == 124) && (isdigit(buffer[inloc + 1])) && (isdigit(buffer[inloc + 2])))
inloc += 3;
else
if ((buffer[inloc] == 4) && (isdigit(buffer[inloc + 1]))) {
i = inloc;
for (j = 1; j < 255; j++) {
if ((buffer[i] == '\r') || (i > nhr.length))
break;
i++;
inloc++;
}
inloc++;
if (buffer[inloc] == '\n')
inloc++;
} else
if (buffer[inloc] >= 127)
inloc++;
else
if (buffer[inloc] == 1)
inloc++;
else
text[outloc++] = buffer[inloc++];
} while (inloc < nhr.length);
text[outloc] = '\0';
if ((nhr.main_type == main_type_post) ||
(nhr.main_type == main_type_pre_post) ||
(nhr.main_type == main_type_new_post)) {
if (nhr.main_type == main_type_new_post) {
sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
if (exist(fn1)) {
tolist = 1;
nhr.main_type = main_type_email_name;
}
}
for (i = 0; (i < nlists) && (nhr.main_type != main_type_email_name); i++) {
if (nhr.main_type == main_type_new_post) {
if (strcmpi(alphasubtype, maillist[i].subtype) == 0) {
nhr.main_type = main_type_email_name;
strcpy(mh.toUserName, maillist[i].ownername);
}
} else {
ttype = atoi(maillist[i].subtype);
if (ttype == stype) {
nhr.main_type = main_type_email_name;
strcpy(mh.toUserName, maillist[i].ownername);
}
}
}
}
switch (nhr.main_type) {
case main_type_email:
case main_type_email_name:
case main_type_ssm:
i = 1;
sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, i);
while (exist(outfn))
sprintf(outfn, "%sMQUEUE\\MSG.%d", net_data, ++i);
break;
case main_type_new_post:
case main_type_post:
case main_type_pre_post:
i = 1;
strcpy(tempoutfn, hold);
sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, i);
while (exist(outfn))
sprintf(outfn, "%sOUTBOUND\\%s.%d", net_data, tempoutfn, ++i);
break;
default:
continue;
}
if (!replyaddr)
properize(mh.fromUserName);
log_it(1, "\n ■ Creating: %s", outfn);
if (nhr.fromsys != net_sysnum)
log_it(1, "\n ■ From : %s", mh.fromUserName);
else if (usermail) {
if (replyaddr)
log_it(1, "\n ■ From : %s", mh.fromUserName);
else
log_it(1, "\n ■ From : \"%s\" <%s@%s>", mh.fromUserName, POPNAME, DOMAIN);
} else
log_it(1, "\n ■ From : <%s@%s>", POPNAME, DOMAIN);
if ((nhr.main_type == main_type_post) ||
(nhr.main_type == main_type_pre_post) ||
(nhr.main_type == main_type_new_post)) {
sprintf(fn1, "%sNEWS.RC", net_data);
if ((fp = fsh_open(fn1, "rt")) == NULL) {
log_it(1, "\n ■ %s not found!", fn1);
sh_close(infile);
if (text)
free(text);
if (buffer)
free(buffer);
return 1;
} else {
ok = 0;
while ((fgets(_temp_buffer, 80, fp) != NULL) && (!ok)) {
groupname[0] = 0;
ss = strtok(_temp_buffer, " ");
if (ss) {
strcpy(groupname, ss);
ss = strtok(NULL, " ");
ss = strtok(NULL, "\r");
if (nhr.main_type == main_type_new_post) {
strcpy(alphatype, ss);
if (strncmpi(alphasubtype, alphatype, strlen(alphasubtype)) == 0)
ok = 1;
} else {
ttype = atoi(ss);
if (ttype == stype)
ok = 1;
}
}
}
*ss = NULL;
if (fp != NULL)
fclose(fp);
if (!ok) {
if (nhr.main_type != main_type_new_post)
sprintf(alphatype, "%u", stype);
log_it(1, "\n ■ Subtype %s not found in NEWS.RC!", alphatype);
sh_close(infile);
if (text)
free(text);
if (buffer)
free(buffer);
return 1;
}
}
}
if ((nhr.main_type == main_type_email) ||
(nhr.main_type == main_type_email_name) ||
(nhr.main_type == main_type_ssm)) {
if (tolist)
log_it(1, "\n ■ Sent to : %s Mailing List", alphasubtype);
else
log_it(1, "\n ■ Rcpt to : %s", mh.toUserName);
} else
log_it(1, "\n ■ Post to : %s", groupname);
strcpy(_temp_buffer, mh.subject);
j = 0;
for (i = 0; i < strlen(mh.subject); i++) {
if (_temp_buffer[i] == 3)
++i;
else
mh.subject[j++] = _temp_buffer[i];
}
mh.subject[j] = '\0';
log_it(1, "\n ■ Subject : %s", mh.subject);
outfile = sh_open(outfn, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC | O_EXCL, S_IWRITE);
if (outfile == -1) {
sh_close(infile);
if (buffer)
free(buffer);
if (text)
free(text);
log_it(1, "\n ■ Unable to open output file %s", outfn);
return 1;
}
time(&some);
time_msg = localtime(&some);
strftime(mh.dateTime, 80, "%a, %d %b %y %H:%M:%S (%Z)", time_msg);
if (nhr.fromsys == 32767) {
sprintf(_temp_buffer, "From: %s\n", mh.fromUserName);
} else if (nhr.fromsys != net_sysnum) {
sprintf(_temp_buffer, "From: \"%s #%hd-%hu\" <%s@%s>\n", mh.fromUserName,
nhr.fromuser, nhr.fromsys, POPNAME, DOMAIN);
} else {
if ((spam) && ((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)))
if (spamname[0] == 0)
sprintf(_temp_buffer, "From: \"%s\" <%s@dont.spam.me.%s>\n",
mh.fromUserName, POPNAME, DOMAIN);
else
sprintf(_temp_buffer, "From: \"%s\" <%s>\n", mh.fromUserName, spamname);
else {
if (usermail) {
if (replyaddr)
sprintf(_temp_buffer, "From: %s\n", mh.fromUserName);
else
sprintf(_temp_buffer, "From: \"%s\" <%s@%s>\n",
mh.fromUserName, POPNAME, DOMAIN);
} else
sprintf(_temp_buffer, "From: <%s@%s>\n", POPNAME, DOMAIN);
}
}
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
++cur_daten;
sprintf(_temp_buffer, "Message-ID: <%lx-%s@WWIV-BBS>\n",
cur_daten, POPNAME);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
if ((nhr.main_type == main_type_email) ||
(nhr.main_type == main_type_email_name) ||
(nhr.main_type == main_type_ssm)) {
if (!tolist) {
sprintf(_temp_buffer, "To: %s\n", mh.toUserName);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
} else {
sprintf(fn1, "%sM%s.NET", net_data, alphasubtype);
i = 0;
if ((fp = fsh_open(fn1, "rt")) != NULL) {
while (fgets(_temp_buffer, 80, fp) != NULL) {
if (_fstrstr(_temp_buffer, "@")) {
strcpy(mh.toUserName, _temp_buffer);
sprintf(_temp_buffer, "To: %s", mh.toUserName);
if (_temp_buffer[strlen(_temp_buffer) - 1] != '\n')
strcat(_temp_buffer, "\n");
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
i = 1;
}
}
if (fp != NULL)
fclose(fp);
}
if (!i) {
sh_close(infile);
if (buffer)
free(buffer);
if (text)
free(text);
log_it(1, "\n ■ Error processing mailing list %s.", alphasubtype);
return (1);
} else {
sprintf(_temp_buffer, "Reply-To: \"%s\" <%s@%s>\n", alphasubtype,
POPNAME, DOMAIN);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
}
} else {
sprintf(_temp_buffer, "Newsgroups: %s\n", groupname);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
sprintf(_temp_buffer, "Subject: %s\n", mh.subject);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
sprintf(_temp_buffer, "Date: %s\n", mh.dateTime);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
if (nhr.main_type != main_type_email_name) {
sprintf(_temp_buffer, "Path: %s!%s\n", POPNAME, DOMAIN);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
sprintf(_temp_buffer, "Organization: %s * %s\n",
syscfg.systemname, syscfg.systemphone);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
if (tolist) {
sprintf(_temp_buffer, "X-Reply-To: \"%s\" <%s@%s>\n",
alphasubtype, POPNAME, DOMAIN);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
/*
else {
if ((!spam) && ((nhr.main_type != main_type_post) || (nhr.main_type != main_type_new_post))) {
sprintf(_temp_buffer, "Reply-To: \"%s\" <%s@%s>\n",
mh.fromUserName, POPNAME, DOMAIN);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
}
*/
if (tolist) {
sprintf(_temp_buffer, "Source: %s Mail List\n", alphasubtype);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
if (((nhr.main_type == main_type_post) || (nhr.main_type == main_type_new_post)) && (!tolist)) {
if (*REPLYTO)
sprintf(_temp_buffer, "Reply-to: \"%s\" <%s>\n", mh.fromUserName, REPLYTO);
else
sprintf(_temp_buffer, "Reply-to: \"%s\" <%s@%s>\n",
mh.fromUserName, POPNAME, DOMAIN);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
sprintf(_temp_buffer, "Version: WWIV PPP Project %s\n\n", VERSION);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
if ((nhr.main_type != main_type_email) &&
(nhr.main_type != main_type_email_name) &&
(strncmp(mh.toUserName, "ALL", 3) != 0)) {
sprintf(_temp_buffer, "Responding to: %s\n", mh.toUserName);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
for (i = 0; i < strlen(text); i++)
if (text[i] == 'π')
text[i] = '\n';
sh_write(outfile, text, strlen(text));
sprintf(tagfn, "%sI%u.TAG", syscfg.datadir, stype);
if (exist(tagfn))
strcpy(tagfile, tagfn);
tagfn[0] = 0;
ns = 0;
for (i6 = 0; i6 < 99; i6++) {
sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, i6);
if (exist(tagfn))
ns++;
else
break;
}
sprintf(tagfn, "%sI%u.T%d", syscfg.datadir, random(ns));
if (exist(tagfn))
strcpy(tagfile, tagfn);
if (tagfile[0] == 0) {
sprintf(_temp_buffer, "\n\nOrigin: %s * %s\n\n",
syscfg.systemname, syscfg.systemphone);
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
} else {
if ((fp = fsh_open(tagfile, "rt")) == NULL)
log_it(1, "\n ■ Error reading %s.", tagfile);
else {
sprintf(_temp_buffer, "\n\n");
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
while (fgets(_temp_buffer, 120, fp)) {
for (i = 0; ((i < strlen(_temp_buffer)) &&
(_temp_buffer[i] != '\r') && (_temp_buffer[i] != '\n')); i++)
if ((_temp_buffer[i] < 32) || (_temp_buffer[i] > 126))
_temp_buffer[i] = 32;
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
if (fp != NULL)
fclose(fp);
sprintf(_temp_buffer, "\n\n");
sh_write(outfile, _temp_buffer, strlen(_temp_buffer));
}
}
sh_close(outfile);
} else {
if ((nhr.main_type >= 0x01) && (nhr.main_type <= 0x1b))
log_it(1, "\n ■ %s message skipped",
main_type[nhr.main_type - 1]);
else
log_it(1, "\n ■ Unknown Main_type %hd skipped", nhr.main_type);
}
}
sh_close(infile);
unlink(fn);
if (text)
free(text);
if (buffer)
free(buffer);
return (0);
}
void get_dir(char *s, int be)
{
strcpy(s, "X:\\");
s[0] = 'A' + getdisk();
getcurdir(0, s + 3);
if (be) {
if (s[strlen(s) - 1] != '\\')
strcat(s, "\\");
}
}
void ssm(char *s)
{
int f, i, i1;
char s1[161];
shortmsgrec sm;
sprintf(s1, "%sSMW.DAT", syscfg.datadir);
f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
if (f < 0)
return;
i = (int) (filelength(f) / sizeof(shortmsgrec));
i1 = i - 1;
if (i1 >= 0) {
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_read(f, (void *) &sm, sizeof(shortmsgrec));
while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
--i1;
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_read(f, (void *) &sm, sizeof(shortmsgrec));
}
if ((sm.tosys) || (sm.touser))
++i1;
} else
i1 = 0;
sm.tosys = 0;
sm.touser = 1;
strncpy(sm.message, s, 80);
sm.message[80] = 0;
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_write(f, (void *) &sm, sizeof(shortmsgrec));
sh_close(f);
}
/*
action: 1 = unsubscribed,
2 = subscribed,
3 = already subscribed,
4 = not subscribed
5 = invalid list
*/
void send_note(int action, char *mailname, char *listname)
{
char s[81], fn[MAXPATH];
int i;
FILE *fp, *rlz;
i = 1;
sprintf(fn, "%sMQUEUE\\MSG.%d", net_data, i);
while (exist(fn))
sprintf(fn, "%sMQUEUE\\MSG.%d", net_data, ++i);
if ((fp = fsh_open(fn, "wt+")) == NULL) {
log_it(1, "\n ! Unable to create %s.", fn);
return;
}
if (action != 5)
fprintf(fp, "From: \"%s\" <%s@%s>\n", listname, POPNAME, DOMAIN);
else
fprintf(fp, "From: <%s@%s>\n", POPNAME, DOMAIN);
fprintf(fp, "To: %s\n", mailname);
fprintf(fp, "Subject: %s Mailing List\n\n", listname);
switch (action) {
case 1:
sprintf(s, "%s removed from mailing list: %s", mailname, listname);
fprintf(fp, "\n%s\n\n", s);
log_it(1, "\n ■ %s", s);
ssm(s);
break;
case 2:
sprintf(fn, "%sR%s.RLZ", net_data, listname);
if (!(exist(fn)))
sprintf(fn, "%sGLOBAL.RLZ", net_data);
if ((rlz = fsh_open(fn, "rt")) != NULL) {
while (fgets(s, 80, rlz))
fprintf(fp, s);
fprintf(fp, "\n\n");
fclose(rlz);
sprintf(s, "%s added to mailing list: %s", mailname, listname);
log_it(1, "\n%s", s);
ssm(s);
} else {
sprintf(s, "%s added to mailing list: %s", mailname, listname);
fprintf(fp, "\n%s\n\n", s);
log_it(1, "\n ■ %s", s);
ssm(s);
}
break;
case 3:
sprintf(fn, "%sR%s.RLZ", net_data, listname);
if (!(exist(fn)))
sprintf(fn, "%sGLOBAL.RLZ", net_data);
if ((rlz = fsh_open(fn, "rt")) != NULL) {
while (fgets(s, 80, rlz))
fprintf(fp, s);
fprintf(fp, "\n\n");
fclose(rlz);
} else
fprintf(fp, "\n%s was already subscribed to mailing list:\n\n %s\n\n",
mailname, listname);
break;
case 4:
fprintf(fp, "\n%s not subscribed to mailing list:\n\n %s\n\n",
mailname, listname);
break;
case 5:
sprintf(s, "%s requested an invalid mailing list: %s", mailname, listname);
fprintf(fp, "\n%s\n\n", s);
log_it(1, "\n ■ %s", s);
ssm(s);
break;
case 6:
sprintf(fn, "%sMAILLIST.TXT", syscfg.gfilesdir);
if ((rlz = fsh_open(fn, "rt")) != NULL) {
while (fgets(s, 80, rlz))
fprintf(fp, s);
fprintf(fp, "\n\n");
fclose(rlz);
sprintf(s, "Sent MAILLIST.TXT to %s.", mailname);
} else
sprintf(s, "No mailing list file found. Notify system operator.");
fprintf(fp, "\n%s\n\n", s);
log_it(1, "\n ■ %s", s);
ssm(s);
break;
}
if (fp != NULL)
fclose(fp);
}
int copyfile(char *input, char *output)
{
int f1, f2, i;
char *b;
struct ftime ft;
if ((strcmp(input, output) != 0) && (exist(input)) && (!exist(output))) {
if ((b = (char *) malloc(16400)) == NULL)
return 0;
f1 = sh_open1(input, O_RDONLY | O_BINARY);
if (!f1) {
free(b);
b = NULL;
return 0;
}
getftime(f1, &ft);
f2 = sh_open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
if (!f2) {
free(b);
b = NULL;
f1 = sh_close(f1);
return 0;
}
i = read(f1, (void *) b, 16384);
while (i > 0) {
sh_write(f2, (void *) b, i);
i = read(f1, (void *) b, 16384);
}
f1 = sh_close(f1);
setftime(f2, &ft);
f2 = sh_close(f2);
free(b);
b = NULL;
}
return 1;
}
int subscribe(char *fn)
{
char *ss, s[81], s1[81], mailname[81], subtype[81];
int done, found, unsubscribe;
FILE *fp, *oldfp, *newfp;
if ((fp = fsh_open(fn, "rt")) == NULL) {
log_it(1, "\n ! Unable to open %s.", fn);
return 1;
}
done = unsubscribe = 0;
while ((fgets(s1, 80, fp)) && (!done)) {
strcpy(s, &(s1[3]));
if (strnicmp(s, "from:", 5) == 0) {
ss = strtok(s, ":");
if (ss) {
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strcpy(mailname, ss);
}
}
if (strnicmp(s, "subject:", 8) == 0) {
done = 1;
ss = strtok(s, ":");
if (ss) {
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strcpy(s1, ss);
if (strnicmp(s1, "subscribe", 9) == 0) {
ss = strtok(s1, " ");
if (ss) {
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strcpy(subtype, ss);
}
}
if (strnicmp(s1, "unsubscribe", 11) == 0) {
unsubscribe = 1;
ss = strtok(s1, " ");
if (ss) {
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strcpy(subtype, ss);
}
}
}
ss = NULL;
}
}
if (fp != NULL)
fclose(fp);
if ((!*mailname) || (!*subtype)) {
log_it(1, "\n ! Invalid subscription request %s.", fn);
return 1;
}
if (strlen(subtype) == 1) {
log_it(1, "\n ! %s attempted to write to M%s.NET!", mailname, subtype);
return 1;
}
if (strnicmp(subtype, "LISTS", 10) == 0) {
send_note(6, mailname, subtype);
return 1;
}
sprintf(s, "%sM%s.NET", net_data, subtype);
if (!exist(s)) {
log_it(1, "\n ! %s subscriber list not found.", s);
send_note(5, mailname, subtype);
return 1;
}
if ((oldfp = fsh_open(s, "rt")) == NULL) {
log_it(1, "\n ! Unable to open input file %s.", s);
return 1;
}
sprintf(s1, "%sM%s.TMP", net_data, subtype);
if (exist(s1))
unlink(s1);
if ((newfp = fsh_open(s1, "wt+")) == NULL) {
log_it(1, "\n ! Unable to open output file %s.", s1);
return 1;
}
found = 0;
while (fgets(s, 80, oldfp)) {
trimstr1(s);
if (unsubscribe) {
if (stricmp(s, mailname) == 0) {
log_it(1, "\n ■ Removing %s from %s mailing list.", mailname, subtype);
send_note(2, mailname, subtype);
found = 1;
} else
fprintf(newfp, s);
} else {
if (stricmp(s, mailname) == 0) {
log_it(1, "\n ■ %s already in %s mailing list.", mailname, subtype);
send_note(3, mailname, subtype);
found = 1;
}
fprintf(newfp, s);
}
}
if (!found) {
if (unsubscribe)
send_note(4, mailname, subtype);
else {
log_it(1, "\n ■ Adding %s to %s mailing list.", mailname, subtype);
fprintf(newfp, "\n%s\n", mailname);
send_note(2, mailname, subtype);
}
}
if (oldfp != NULL)
fclose(oldfp);
if (newfp != NULL)
fclose(newfp);
sprintf(s, "%sM%s.TMP", net_data, subtype);
sprintf(s1, "%sM%s.NET", net_data, subtype);
if (exist(s1))
unlink(s1);
copyfile(s, s1);
return 0;
}
int main(int argc, char *argv[])
{
char fn[MAXPATH], *ss;
int f, f1, i;
struct ffblk ff;
struct date dt;
struct time tm;
log_it(1, "\n ■ PPP Import/Export %s", VERSION);
if (argc != 7) {
log_it(1, "\n ■ EXP <filename> <net_data> <net_sysnum> <POPNAME> <DOMAIN> <net_name>\n\n");
if (argc > 1) {
log_it(1, "Command line was: ");
for (i = 0; i < argc; i++)
log_it(1, "%s ", argv[i]);
log_it(1, "\n\n");
}
return (1);
}
strcpy(net_data, argv[2]);
f = sh_open1("CONFIG.DAT", O_RDONLY | O_BINARY);
if (f < 0) {
log_it(1, "Could not open CONFIG.DAT!\n\n");
return 1;
}
sh_read(f, (void *) &syscfg, sizeof(configrec));
sh_close(f);
detect_multitask();
get_dir(maindir, 1);
sprintf(fn, "%s%s", net_data, argv[1]);
strcpy(net_name, argv[6]);
strcpy(POPNAME, argv[4]);
strcpy(DOMAIN, argv[5]);
net_sysnum = atoi(argv[3]);
tagfile[0] = 0;
spam = 0;
ss = getenv("WWIV_INSTANCE");
if (ss) {
instance = atoi(ss);
if (instance >= 1000) {
log_it(1, "\n ■ WWIV_INSTANCE set to %hd. Can only be 1..999!",
instance);
instance = 1;
}
} else
instance = 1;
parse_net_ini();
gettime(&tm);
getdate(&dt);
cur_daten = dostounix(&dt, &tm);
strupr(postmaster);
export(fn);
sprintf(fn, "%sSPOOL\\UNK*.*", net_data);
f1 = findfirst(fn, &ff, 0);
while (f1 == 0) {
sprintf(fn, "%sSPOOL\\%s", net_data, ff.ff_name);
if (!import(fn)) {
unlink(fn);
}
f1 = findnext(&ff);
}
sprintf(fn, "%sINBOUND\\SUB*.*", net_data);
f1 = findfirst(fn, &ff, 0);
while (f1 == 0) {
sprintf(fn, "%sINBOUND\\%s", net_data, ff.ff_name);
log_it(1, "\n ■ Processing subscriber request %s...", ff.ff_name);
subscribe(fn);
unlink(fn);
f1 = findnext(&ff);
}
if (maillist != NULL)
free(maillist);
return 0;
}