home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Devil's Doorknob BBS Capture (1996-2003)
/
devilsdoorknobbbscapture1996-2003.iso
/
UTIL
/
WWIVE
/
OLDDEV.ARJ
/
BATCH.C
next >
Wrap
Text File
|
1992-05-21
|
15KB
|
729 lines
/*****************************************************************************
WWIV Version 4
Copyright (C) 1988-1991 by Wayne Bell
Distribution of the source code for WWIV, in any form, modified or unmodified,
without PRIOR, WRITTEN APPROVAL by the author, is expressly prohibited.
Distribution of compiled versions of WWIV is limited to copies compiled BY
THE AUTHOR. Distribution of any copies of WWIV not compiled by the author
is expressly prohibited.
*****************************************************************************/
#include "vars.h"
#pragma hdrstop
#include <dir.h>
#define SETREC(i) lseek(dlf,((long) (i))*((long)sizeof(uploadsrec)),SEEK_SET);
void listbatch()
{
char s[81];
int abort,i;
abort=0;
nl();
if (numbatchdl)
npr("Files - %d Time - %s\r\n",numbatch,ctim(batchtime));
else
npr("Files - %d\r\n",numbatch);
nl();
for (i=0; (i<numbatch) && (!abort) && (!hangup); i++) {
if (batch[i].sending)
sprintf(s,"%d. (D) %s %s %s",i+1, batch[i].filename,
ctim(batch[i].time),
directories[batch[i].dir].name);
else
sprintf(s,"%d. (U) %s %s",i+1,batch[i].filename,
directories[batch[i].dir].name);
pla(s,&abort);
}
}
void delbatch(int i)
{
int i1;
if (i<numbatch) {
if (batch[i].sending) {
batchtime -= batch[i].time;
--numbatchdl;
}
--numbatch;
for (i1=i; i1<=numbatch; i1++) {
batch[i1]=batch[i1+1];
}
}
}
void downloaded(char *fn)
{
int i,i1;
uploadsrec u;
char s[81];
for (i1=0; i1<numbatch; i1++) {
if ((strcmp(fn,batch[i1].filename)==0) && (batch[i1].sending)) {
dliscan1(batch[i1].dir);
i=recno((batch[i1].filename));
if (i>0) {
SETREC(i);
read(dlf,(void *)&u,sizeof(uploadsrec));
++thisuser.downloaded;
thisuser.dk += (int) ((u.numbytes+1023)/1024);
++u.numdloads;
SETREC(i);
write(dlf,(void *)&u,sizeof(uploadsrec));
sprintf(s,"Leeched '%s'",u.filename);
sysoplog(s);
if (syscfg.sysconfig & sysconfig_log_dl) {
sprintf(s,"%s leeched '%s' on %s",
nam(&thisuser,usernum), u.filename, date());
ssm(u.ownerusr,0,s);
}
}
closedl();
delbatch(i1);
return;
}
}
sprintf(s,"!!! Couldn't find '%s' in DL batch queue.",fn);
sysoplog(s);
}
void didnt_upload(int ind)
{
int i,i1;
char s[81];
uploadsrec u;
if (batch[ind].sending)
return;
dliscan1(batch[ind].dir);
i=recno(batch[ind].filename);
if (i>0) {
do {
SETREC(i);
read(dlf, &u, sizeof(uploadsrec));
if (u.numbytes!=0)
i=nrecno(batch[ind].filename, i);
} while ((i!=-1) && (u.numbytes!=0));
if ((i!=-1) && (u.numbytes==0)) {
if (u.mask & mask_extended)
delete_extended_description(u.filename);
for (i1=i; i1<numf; i1++) {
SETREC(i1+1);
read(dlf,(void *)&u,sizeof(uploadsrec));
SETREC(i1);
write(dlf,(void *)&u,sizeof(uploadsrec));
}
--i;
--numf;
SETREC(0);
read(dlf, &u, sizeof(uploadsrec));
u.numbytes=numf;
SETREC(0);
write(dlf,(void *)&u,sizeof(uploadsrec));
closedl();
return;
}
}
sprintf(s,"!!! Couldn't find '%s' in transfer area.",batch[ind].filename);
sysoplog(s);
closedl();
}
void uploaded(char *fn)
{
int i,i1, d1, d2, rn;
uploadsrec u;
char s[81], s1[81], s2[81];
char *b;
for (i1=0; i1<numbatch; i1++) {
if ((strcmp(fn,batch[i1].filename)==0) && (!batch[i1].sending)) {
dliscan1(batch[i1].dir);
rn=recno((batch[i1].filename));
if (rn>0) {
do {
SETREC(rn);
read(dlf, &u, sizeof(uploadsrec));
if (u.numbytes!=0)
rn=nrecno(batch[i1].filename, rn);
} while ((rn!=-1) && (u.numbytes!=0));
if ((rn!=-1) && (u.numbytes==0)) {
sprintf(s1,"%s%s",syscfg.batchdir, fn);
sprintf(s2,"%s%s", directories[batch[i1].dir].path, fn);
if ((strcmp(s1,s2)!=0) && (exist(s1))) {
d2=0;
if ((s1[1]!=':') && (s2[1]!=':'))
d2=1;
if ((s1[1]==':') && (s2[1]==':') && (s1[0]==s2[0]))
d2=1;
if (d2) {
rename(s1,s2);
unlink(s1);
} else {
if ((b=malloca(16400))==NULL)
return;
d1=open(s1,O_RDONLY | O_BINARY);
d2=open(s2,O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
i=read(d1,(void *)b,16384);
while (i>0) {
write(d2,(void *)b,i);
i=read(d1,(void *)b,16384);
}
close(d1);
close(d2);
unlink(s1);
farfree(b);
}
}
d1=open(s2,O_RDONLY | O_BINARY);
if (d1>0) {
if (syscfg.upload_c[0]) {
close(d1);
if (check_ul_event(batch[i1].dir, &u)) {
closedl();
didnt_upload(i1);
delbatch(i1);
d1=-1;
} else {
d1=open(s2,O_RDONLY | O_BINARY);
}
}
if (d1>=0) {
u.numbytes = filelength(d1);
close(d1);
++thisuser.uploaded;
thisuser.uk += (int) ((u.numbytes+1023)/1024);
++status.uptoday;
save_status();
SETREC(rn);
write(dlf,(void *)&u,sizeof(uploadsrec));
sprintf(s,"+%s uploaded on %s",
u.filename,
directories[batch[i1].dir].name);
sysoplog(s);
npr("Uploaded '%s' to %s\r\n",u.filename,
directories[batch[i1].dir].name);
}
}
closedl();
delbatch(i1);
return;
}
}
sprintf("!!! Couldn't find file '%s' in directory.", fn);
sysoplog(s);
npr("Couldn't find data for file '%s'; deleting\r\n",fn);
closedl();
didnt_upload(i1);
delbatch(i1);
return;
}
}
sprintf(s,"!!! Couldn't find '%s' in UL batch queue.",fn);
sysoplog(s);
npr("Don't know what to do with file '%s'; deleting\r\n",fn);
sprintf(s,"%s%s",syscfg.batchdir, fn);
unlink(s);
}
void ymbatchdl(int had)
{
int rr,i,ok,cur=0;
char s[81];
uploadsrec u;
double percent;
if (!incom)
return;
sprintf(s,"Ymodem BATCH DL, %d files, time=%s",numbatchdl, ctim(batchtime));
if (had)
strcat(s,", HAD");
sysoplog(s);
nl();
pl(s);
nl();
rr=0;
do {
tleft(1);
if ((syscfg.req_ratio>0.0001) && (ratio()<syscfg.req_ratio))
rr=1;
if (thisuser.exempt & exempt_ratio)
rr=0;
if (!batch[cur].sending) {
rr=0;
++cur;
}
if ((nsl()>=batch[cur].time) && (!rr)) {
dliscan1(batch[cur].dir);
i=recno(batch[cur].filename);
if (i<=0) {
delbatch(cur);
closedl();
} else {
sprintf(s,"%d files left, Time left = %s\r\n",numbatchdl,ctim(batchtime));
outs(s);
SETREC(i);
read(dlf,(void *)&u,sizeof(uploadsrec));
closedl();
sprintf(s,"%s%s",directories[batch[cur].dir].path,u.filename);
xymodem_send(s,&ok,&percent,u.filetype,1,1,1);
if (ok) {
downloaded(u.filename);
} else {
closedl();
}
}
} else
delbatch(cur);
} while ((ok) && (!hangup) && (numbatch>cur) && (!rr));
if ((ok) && (!hangup))
endbatch();
if (rr) {
nl();
pl("6Your ratio is too low to continue the transfer.");
nl();
}
if (had) {
dtr(0);
hangup=1;
}
}
void handle_dszline(char *l)
{
char *ss;
int i;
char s[161];
/* find the filename */
ss=strtok(l," \t");
for (i=0; (i<10) && (ss); i++)
ss=strtok(NULL," \t");
if (ss) {
strcpy(s,stripfn(ss));
align(s);
switch(*l) {
case 'Z':
case 'S':
case 'R':
case 'B':
/* received a file */
uploaded(s);
break;
case 'z':
case 's':
case 'r':
case 'b':
/* sent a file */
downloaded(s);
break;
case 'E':
case 'L':
case 'U':
/* error */
sprintf(s,"Error transferring '%s'",ss);
sysoplog(s);
break;
}
}
}
double ratio1(long a)
{
double r;
if ((thisuser.dk==0) && (a==0))
return(99.999);
r=((float) thisuser.uk) / ((float) (thisuser.dk + a));
if (r>99.998)
r=99.998;
return(r);
}
void make_ul_batch_list(char *listfn)
{
int f,i;
char s[255];
sprintf(listfn,"%s\\FILES.UL",cdir);
_chmod(listfn,1,0);
unlink(listfn);
f=open(listfn,O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
if (f<0) {
listfn[0]=0;
return;
}
for (i=0; i<numbatch; i++) {
if (!batch[i].sending) {
sprintf(s,"%s%s\r\n",directories[batch[i].dir].path,stripfn(batch[i].filename));
write(f,s,strlen(s));
}
}
close(f);
}
void make_dl_batch_list(char *listfn)
{
char s[255];
int i, f, ok;
double at=0.0;
long addk=0,thisk;
sprintf(listfn,"%s\\FILES.DL",cdir);
_chmod(listfn,1,0);
unlink(listfn);
f=open(listfn,O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
if (f<0) {
listfn[0]=0;
return;
}
for (i=0; i<numbatch; i++) {
if (batch[i].sending) {
sprintf(s,"%s%s\r\n",directories[batch[i].dir].path,stripfn(batch[i].filename));
ok=1;
if (nsl() < (batch[i].time + at))
ok=0;
thisk=(batch[i].len+1023)/1024;
if ((syscfg.req_ratio>0.0001) && (ratio1(addk+thisk)<syscfg.req_ratio) &&
(!(thisuser.exempt & exempt_ratio)))
ok=0;
if (ok) {
write(f,s,strlen(s));
at += batch[i].time;
addk += thisk;
}
}
}
close(f);
}
void run_cmd(char *cmdln, char *downlist, char *uplist, char *dl, int had)
{
char sx1[21], sx2[21], sx3[21], s[161];
ultoa(com_speed,sx1,10);
ultoa(modem_speed,sx3,10);
sx2[0]='0'+syscfg.primaryport;
sx2[1]=0;
stuff_in(s,cmdln,sx1,sx2,downlist,sx3,uplist);
if (s[0]) {
make_abs_cmd(s);
clrscrb();
outs(dl);
outs("\r\n");
outs(s);
outs("\r\n");
if (incom) {
_chmod(dszlog,1,0);
unlink(dszlog);
if (uplist[0])
cd_to(syscfg.batchdir);
run_external1(s);
cd_to(cdir);
if (had) {
dtr(0);
hangup=1;
wait1(2);
if (!cdet()) {
dtr(1);
wait1(2);
holdphone(1);
}
} else {
nl();
pl("3Please wait5...");
nl();
}
process_dszlog();
_chmod(dszlog,1,0);
unlink(dszlog);
topscreen();
}
}
if (downlist[0]) {
_chmod(downlist,1,0);
unlink(downlist);
}
if (uplist[0]) {
_chmod(uplist,1,0);
unlink(uplist);
}
}
void process_dszlog()
{
int f,i,i1;
char *ss;
char *lines[100];
f=open(dszlog,O_RDONLY | O_TEXT);
if (f>0) {
i1=(int)filelength(f);
ss=malloca(i1);
if (ss) {
i=read(f,ss,i1);
if (i>0) {
ss[i]=0;
lines[0]=strtok(ss,"\n");
for (i=1; (i<90) && (lines[i-1]); i++)
lines[i]=strtok(NULL,"\n");
lines[99]=NULL;
for (i1=0; lines[i1]; i1++) {
handle_dszline(lines[i1]);
}
}
farfree(ss);
}
close(f);
}
_chmod(dszlog,1,0);
unlink(dszlog);
}
void dszbatchdl(int had, char *cmdln, char *desc)
{
char listfn[81],dl[100];
sprintf(dl,"%s BATCH DL, %d files, time=%s",
desc, numbatchdl, ctim(batchtime));
if (had)
strcat(dl,", HAD");
sysoplog(dl);
nl();
pl(dl);
nl();
make_dl_batch_list(listfn);
run_cmd(cmdln, listfn, "", dl, had);
}
void dszbatchul(int had, char *cmdln, char *desc)
{
char listfn[81],dl[100];
int i,i1,f,ok;
sprintf(dl,"%s BATCH UL, %d files", desc, numbatch-numbatchdl);
if (had)
strcat(dl,", HAD");
sysoplog(dl);
nl();
pl(dl);
nl();
make_ul_batch_list(listfn);
run_cmd(cmdln, "", listfn, dl, had);
}
/****************************************************************************/
/*
* Bimodem batch has not yet been implemented.
*/
#pragma warn -par
void bibatch(int had, int pn)
{
}
#pragma warn +par
/****************************************************************************/
void batchdl()
{
int i,abort,done,i1,i2,had,dsz;
char s[81],s1[81],ch,ch1;
done=0;
if (numbatch==0) {
nl();
pl("6No files in queue.");
nl();
return;
}
do {
nl();
prt(7,"[1Batch7]3:7 L1,7R1,7Q1,7C1,7D1,7U1,7B1,7?3 : ");
ch=onek("Q?CLRDUB");
switch(ch) {
case '?':
printmenu(9);
break;
case 'Q':
done=1;
break;
case 'L':
listbatch();
break;
case 'R':
nl();
prt(1,"Remove which5? ");
input(s,2);
i=atoi(s);
if ((i>0) && (i<=numbatch)) {
didnt_upload(i-1);
delbatch(i-1);
}
if (numbatch==0) {
nl();
pl("1Batch queue empty5.");
nl();
done=1;
}
break;
case 'C':
prt(5,"Clear queue? ");
if (yn()) {
for (i=0; i<numbatch; i++)
didnt_upload(i);
numbatch=0;
numbatchdl=0;
batchtime=0.0;
done=1;
pl("1Queue cleared5.");
}
break;
case 'U':
if (numbatchdl==numbatch) {
nl();
pl("6Nothing in batch upload queue.");
nl();
break;
}
nl();
prt(3,"Do you wanna hang up after transfer5? ");
had=yn();
nl();
i=get_protocol(xf_up_batch);
if (i>0) {
dszbatchul(had, externs[i-6].receivebatchfn, externs[i-6].description);
if (!had) {
nl();
/*npr("1Your ratio is now3:6 %-6.3f\r\n",ratio());*/
}
done=1;
}
break;
case 'B':
nl();
prt(3,"Do you wanna hang up after transfer5? ");
had=yn();
nl();
i=get_protocol(xf_bi);
if (i>0) {
bibatch(had, i);
if (!had) {
nl();
/* npr("1Your ratio is now3:6 %-6.3f\r\n",ratio());*/
}
done=1;
}
break;
case 'D':
if (numbatchdl==0) {
nl();
pl("3Nothing in batch download queue5.");
nl();
break;
}
nl();
if (!ratio_ok()) {
nl();
pl("6Sorry, your ratio is too low.");
nl();
break;
}
nl();
prt(3,"Do you wanna hang up after transfer5? ");
had=yn();
nl();
i=get_protocol(xf_down_batch);
if (i>0) {
if (i==4)
ymbatchdl(had);
else
dszbatchdl(had,externs[i-6].sendbatchfn, externs[i-6].description);
if (!had) {
nl();
/* npr("1Your ratio is now3:6 %-6.3f\r\n",ratio());*/
}
done=1;
}
break;
}
} while ((!done) && (!hangup));
}