home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Devil's Doorknob BBS Capture (1996-2003)
/
devilsdoorknobbbscapture1996-2003.iso
/
UTIL
/
WWIVE
/
MYWIVE.ZIP
/
SR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-06
|
12KB
|
592 lines
#include "vars.h"
#pragma hdrstop
#include <time.h>
#include <math.h>
char *stripfn(char *fn)
{
static char ofn[15];
int i,i1;
char s[81];
i1=-1;
for (i=0; i<strlen(fn); i++)
if ((fn[i]=='\\') || (fn[i]==':') || (fn[i]=='/'))
i1=i;
if (i1!=-1)
strcpy(s,&(fn[i1+1]));
else
strcpy(s,fn);
for (i=0; i<strlen(s); i++)
if ((s[i]>='A') && (s[i]<='Z'))
s[i]=s[i]-'A'+'a';
i=0;
while (s[i]!=0) {
if (s[i]==32)
strcpy(&s[i],&s[i+1]);
else
++i;
}
strcpy(ofn,s);
return(ofn);
}
void stripfn1(char *fn)
{
int i,i1;
char s[81],s1[81];
i1=0;
for (i=0; i<strlen(fn); i++)
if ((fn[i]=='\\') || (fn[i]==':') || (fn[i]=='/'))
i1=i;
strcpy(s1,fn);
if (i1) {
strcpy(s,&(fn[i1+1]));
s1[i1+1]=0;
} else {
strcpy(s,fn);
s1[0]=0;
}
for (i=0; i<strlen(s); i++)
if ((s[i]>='A') && (s[i]<='Z'))
s[i]=s[i]-'A'+'a';
i=0;
while (s[i]!=0) {
if (s[i]==32)
strcpy(&s[i],&s[i+1]);
else
++i;
}
strcat(s1,s);
strcpy(fn,s1);
}
void calc_CRC(unsigned char b)
{
int i;
checksum += b;
crc ^= (((unsigned short) (b)) << 8);
for (i=0; i<8; i++)
if (crc & 0x8000) {
crc=(crc << 1);
crc ^= 0x1021;
} else
crc=(crc << 1);
}
char gettimeout(double d, int *abort)
{
double d1;
char ch;
if (comhit())
return(get1c());
d1=timer();
while ((fabs(timer()-d1)<d) && (!comhit()) && (!hangup) && (!*abort)) {
if (kbhitb()) {
ch=getchd();
if (ch==0)
getchd();
else
if (ch==27)
*abort=1;
}
checkhangup();
}
if (comhit())
return(get1c());
else
return(0);
}
int extern_prot(int pn, char *fn1, int sending)
{
char s[255],s1[81],s2[81],fn[81],sx1[21],sx2[21],sx3[21];
int i,i1;
if (sending) {
nl();
pl("> Beginning file transmission, ^X to abort.");
strcpy(s1,(externs[pn].sendfn));
} else {
nl();
pl("> Ready to receive, ^X to abort.");
strcpy(s1,(externs[pn].receivefn));
}
strcpy(fn,fn1);
stripfn1(fn);
ultoa(com_speed,sx1,10);
ultoa(modem_speed,sx3,10);
sx2[0]='0'+syscfg.primaryport;
sx2[1]=0;
stuff_in(s,s1,sx1,sx2,fn,sx3,"");
if (s[0]) {
set_protect(0);
outs("\r\n*> Current user: ");
outs(nam(&thisuser,usernum));
outs("\r\n\r\n");
outs(s);
outs("\r\n");
if (incom) {
i=run_external1(s);
topscreen();
return(i);
} else {
topscreen();
return(-5);
}
}
return(-5);
}
int ok_prot(int pn, xfertype xt)
{
int ok=0;
int i;
if (xt==xf_none)
return(0);
if ((pn>0) || (pn<(numextrn+6))) {
switch(pn) {
case 1:
if ((xt==xf_down) || (xt==xf_down_temp))
ok=1;
break;
case 2:
case 3:
case 4:
if ((xt!=xf_up_batch) && (xt!=xf_down_batch) && (xt!=xf_bi))
ok=1;
if ((pn==4) && (xt==xf_down_batch))
ok=1;
break;
case 5:
if (xt==xf_up) {
for (i=0; i<numextrn; i++)
if (externs[i].receivebatchfn[0] || externs[i].bibatchfn)
ok=1;
} else if (xt==xf_down) {
for (i=0; i<numextrn; i++)
if (externs[i].sendbatchfn[0] || externs[i].bibatchfn)
ok=1;
}
if ((xt==xf_up) || (xt==xf_down))
ok=1;
break;
default:
switch(xt) {
case xf_up:
case xf_up_temp:
if (externs[pn-6].receivefn[0])
ok=1;
break;
case xf_down:
case xf_down_temp:
if (externs[pn-6].sendfn[0])
ok=1;
break;
case xf_up_batch:
if (externs[pn-6].receivebatchfn[0])
ok=1;
break;
case xf_down_batch:
if (externs[pn-6].sendbatchfn[0])
ok=1;
break;
case xf_bi:
if (externs[pn-6].bibatchfn[0])
ok=1;
break;
}
if (externs[pn-6].othr & othr_error_correct)
if (!(modem_flag & flag_ec))
ok=0;
break;
}
}
return(ok);
}
char *prot_name(int pn)
{
char *ss=">NONE<";
switch(pn) {
case 1:
ss="ASCII";
break;
case 2:
ss="Xmodem";
break;
case 3:
ss="Xmodem-CRC";
break;
case 4:
ss="Ymodem";
break;
case 5:
ss="Batch";
break;
default:
if ((pn>5) || (pn<(numextrn+6)))
ss=externs[pn-6].description;
break;
}
return(ss);
}
int get_protocol(xfertype xt)
{
char s[81],s1[81],oks[81],s2[81],ch,*ss;
int i,i1,i2,prot,maxprot,done,only;
if (ok_prot(thisuser.defprot, xt))
prot=thisuser.defprot;
else
prot=0;
strcpy(oks,"Q?0");
i1=strlen(oks);
only=0;
maxprot=5+numextrn;
for (i=1; i<=maxprot; i++) {
if (ok_prot(i,xt)) {
if (i<10)
oks[i1++]='0'+i;
else
oks[i1++]='A'+i-10;
if (only==0)
only=i;
else
only=-1;
}
}
oks[i1]=0;
if (only>0)
prot=only;
if ((only==0) && (xt != xf_none)) {
nl();
pl("No protocols available for that.");
nl();
return(-1);
}
done=0;
if (prot) {
ss=prot_name(prot);
sprintf(s,"Option (?=list, <C/R>=%s) : ",ss);
strcpy(s1,oks);
strcat(s1,"\r");
} else {
strcpy(s,"Option (?=list) : ");
strcpy(s1,oks);
}
do {
nl();
prt(2,s);
ch=onek(s1);
if (ch=='?') {
nl();
pl("Q: Quit");
pl("0: Next File");
for (i=1; i<=maxprot; i++) {
if (ok_prot(i,xt)) {
npr("%c. %s\r\n",(i<10)?(i+'0'):(i+'A'-10),prot_name(i));
}
}
nl();
} else
done=1;
} while ((!done) && (!hangup));
if (ch==13)
return(prot);
if ((ch>='0') && (ch<='9'))
return(ch-'0');
else
if (ch=='Q')
return(-1);
else
return(ch-'A'+10);
}
void ascii_send(char *fn, int *sent, double *percent)
{
char b[2048];
int i,i1,done,abort,i2,next;
long pos,max;
i=open(fn,O_RDONLY | O_BINARY);
if (i>0) {
max=filelength(i);
if (!max)
max=1;
i1=read(i,(void *)b,1024);
pos=0L;
abort=0;
while ((i1) && (!hangup) && (!abort)) {
i2=0;
while ((!hangup) && (!abort) && (i2<i1)) {
checkhangup();
outchr(b[i2++]);
checka(&abort,&next);
}
pos += (long) i2;
checka(&abort,&next);
i1=read(i,(void *)b,1024);
}
close(i);
if (!abort)
*sent=1;
else {
*sent=0;
thisuser.dk += ((pos+1023L)/1024L);
}
*percent=((double) pos)/((double)max);
} else {
nl();
pl("File not found.");
nl();
*sent=0;
*percent=0.0;
}
}
void send_file(char *fn, int *sent, int *abort, char ft, char *sfn, int dn, long fs)
{
int i,i1,ok;
double percent,t;
char s[81];
if (fs<0) {
i=get_protocol(xf_none);
} else {
if (dn==-1)
i=get_protocol(xf_down_temp);
else
i=get_protocol(xf_down);
}
ok=0;
percent=0.0;
if (check_batch_queue(sfn)) {
*sent=0;
if (i>0) {
nl();
pl("That file is already in the batch queue.");
nl();
} else if (i==-1)
*abort=1;
} else {
switch(i) {
case -1:
*sent=0;
*abort=1;
ok=1;
break;
case 0:
*sent=0;
*abort=0;
ok=1;
break;
case 1:
*sent=0;
*abort=0;
ascii_send(fn,sent,&percent);
break;
case 2:
if (incom)
xymodem_send(fn,sent,&percent,ft,0,0,0);
break;
case 3:
if (incom)
xymodem_send(fn,sent,&percent,ft,1,0,0);
break;
case 4:
if (incom)
xymodem_send(fn,sent,&percent,ft,1,1,0);
break;
case 5:
*sent=0;
*abort=0;
ok=1;
if (numbatch>=MAX_BATCH) {
nl();
pl("No room left in batch queue.");
nl();
*sent=0;
*abort=0;
} else {
t=(12.656) / ((double) (modem_speed)) * ((double)(fs));
if (nsl()<=(batchtime + t)) {
nl();
pl("Not enough time left in queue.");
nl();
*sent=0;
*abort=0;
} else {
if (dn==-1) {
nl();
pl("Can't add temporary file to batch queue.");
nl();
*sent=0;
*abort=0;
} else {
batchtime += t;
strcpy(batch[numbatch].filename,sfn);
batch[numbatch].dir=dn;
batch[numbatch].time=t;
batch[numbatch].sending=1;
batch[numbatch].len=fs;
numbatch++;
++numbatchdl;
nl();
pl("File added to batch queue.");
sprintf(s,"Batch: Files - %d Time - %s",numbatch,ctim(batchtime));
nl();
pl(s);
nl();
*sent=0;
*abort=0;
}
}
}
break;
default:
i1=extern_prot(i-6,fn,1);
*abort=0;
if (i1==externs[i-6].ok1)
*sent=1;
else
*sent=0;
break;
}
}
if ((*sent==0) && (ok==0))
if (percent==1.0) {
*sent=1;
add_ass(10,"Aborted on last block");
} else {
sprintf(s,"Tried D/L '%s' %3.2f%%",stripfn(fn),percent*100.0);
sysoplog(s);
}
}
void receive_file(char *fn, int *received, char *ft, char *sfn, int dn)
{
int i;
char s[81];
if (dn==-1)
i=get_protocol(xf_up_temp);
else
i=get_protocol(xf_up);
switch(i) {
case -1:
*received=0;
break;
case 0:
*received=0;
break;
case 2:
if (incom)
xymodem_receive(fn,ft,received,0);
break;
case 3:
if (incom)
xymodem_receive(fn,ft,received,1);
break;
case 4:
if (incom)
xymodem_receive(fn,ft,received,1);
break;
case 5:
if (dn!=-1) {
if (numbatch>=MAX_BATCH) {
nl();
pl("No room left in batch queue.");
nl();
*received=0;
} else {
*received=2;
strcpy(batch[numbatch].filename,sfn);
batch[numbatch].dir=dn;
batch[numbatch].time=0;
batch[numbatch].sending=0;
batch[numbatch].len=0;
numbatch++;
nl();
pl("File added to batch queue.");
sprintf(s,"Batch upload: files - %d", numbatch-numbatchdl);
nl();
pl(s);
nl();
}
} else {
nl();
pl("Can't batch upload that.");
nl();
}
break;
default:
if ((i>5) && (incom)) {
extern_prot(i-6,fn,0);
*received=exist(fn);
}
break;
}
}
char end_batch1()
{
char b[128],ch;
int i,i1,done,nerr;
for (i1=0; i1<128; i1++)
b[i1]=0;
done=0;
nerr=0;
i=0;
do {
send_block(b,5,1,0);
ch=gettimeout(5.0,&i);
if ((ch==6) || (ch==24))
done=1;
else {
++nerr;
if (nerr>=9)
done=1;
}
} while ((!done) && (!hangup) && (!i));
if (ch==6)
return(6);
if (ch==24)
return(24);
return(21);
}
void endbatch()
{
char ch;
int abort,ucrc,terr,xx1,yy1;
abort=0;
terr=0;
xx1=wherex();
yy1=wherey();
if (!okstart(&ucrc,&abort))
abort=1;
if ((!abort) && (!hangup)) {
ch=end_batch1();
if (ch==24)
abort=1;
if (ch==21) {
send_b(0,0L,3,0,&ucrc,"",&terr,&abort);
abort=1;
}
/*
if ((!hangup) && (!abort))
send_b(0,0L,2,0,&ucrc,"",&terr,&abort);
*/
}
movecsr(xx1,yy1);
}