home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 2 BBS
/
02-BBS.zip
/
MAXMAILP.ZIP
/
INIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-01-02
|
17KB
|
551 lines
/****************************************************************************/
/* Init.c MaxMail initialization stuff */
/* Version 1.0 by Craig Derouen */
/* */
/****************************************************************************/
/* Note that the function timeon() has a conflict with a var timeon in the
Max.h header. When I include my copy of max.h, timeon() and timeon don't cut
it! You must have altered max.h to get it to work so I think it would be
better to simply change your function timeon() to Timeon(). And that is
what I did. */
#include "MaxMail.h"
char dummy[81];
char adrive[_MAX_DRIVE];
/* Initialize MaxMail */
void init()
{
int x;
startsecs = clock();
parmread = FALSE;
get_currents(); /* Get the current directory,drive and save */
LogMode = 0; /* Turn off logging by default */
update = 0;
MaxScan = 999;
NewHelpFil[0] = 0; /* Turn off newusers help file */
LogFile[0] = 0; /* Turn Off logfile by default */
SignOff[0] = 0;
MsgHdrFile[0] = 0;
PACKER_1 = NULL;
PROTO_1 = NULL;
SkipAreas = (int **) calloc(1,sizeof(int *));
if (SkipAreas == NULL)
aborterror(BADALLOC,NULL);
SkipAreas[0] = NULL;
load_cfgfile(MCfgFile);
if (Task && LogFile[0]) { /* Redo logfile name with task # */
_splitpath(LogFile,adrive,dummy,temp,temp1);
temp[6] = 0; /* Trim out 2 chars */
sprintf(LogFile,"%s%s%s%02d%s",adrive,dummy,temp,Task,temp1);
}
if (!parmread) {
if(read_prm(PrmFile) < 0)
aborterror(FILEOPEN,"Error opening PRM file!\r\n");
if(switch_dir(PRM(sys_path))) /* Change to Maximus home dir */
deinit(2);
}
if (IsLocal >= 2) MaxMsgs = 9999; /* Allow tons of messages in local mode */
sprintf(temp,"MxMlAct.%03d",Task); /* Create a task id */
x = sopen(temp,O_RDWR | O_BINARY | O_CREAT,SH_DENYWR,S_IWRITE);
close(x); /* Create just a null file */
if (Task && SumFile[0]) {
sprintf(temp,".%03d",Task);
strcat(SumFile,temp);
}
}
/* Close up MaxMail, clean up loose ends */
void deinit(int errcode)
{
int x,y,z;
long secdiff;
struct _restart *rst;
struct stat fbuf;
fclose(afile);
if (packer_del)
unlink(ArcFile);
unlink(TxtFile);
if (StatName[0])
unlink(StatName);
sprintf(temp,"MxMlAct.%03d",Task); /* Create a task id */
if(unlink(temp) == 0) { /* Delete our task file */
x = sopen(CfgFile,O_RDWR | O_BINARY,SH_DENYWR,S_IWRITE);
if (x != -1) {
time(&USERCFG.lasttime);
USERCFG.isused = TRUE;
USERCFG.calls++;
if (USERCFG.calls < USERCFG.packcount)
USERCFG.calls = USERCFG.packcount;
update_usercfg(x);
close(x);
}
else logit("Error updating User Config file",'!');
}
sprintf(temp,"End, v%2.2f (%d)",Version,Timeon());
logit(temp,':');
if (LogFile[0]) {
x = sopen(LogFile,O_BINARY | O_RDWR | O_APPEND,SH_DENYWR,S_IWRITE);
if (x > 0) {
write(x,"\r\n",2);
close(x);
}
}
if (TimeAdjust) {
sprintf(temp,"Restar%02d.bbs",Task);
if(stat(temp,&fbuf) == 0 ) {
rst = (struct _restart *) malloc(sizeof(struct _restart));
if (rst != NULL) {
x = sopen(temp,O_BINARY | O_RDWR,SH_DENYWR,S_IWRITE);
if (x != -1) {
if(read(x,(char *)rst,sizeof(struct _restart)) != 0) {
secdiff = rst->timeoff - rst->timeon;
z = secdiff / 60L; /* convert to minutes */
y = Timeon();
if ((long)(y + z) <= rst->max_time) /* We can bump it up */
rst->timeoff += 60 * Timeon();
lseek(x,0L,SEEK_SET); /* Rewind the file */
write(x,(char *)rst,sizeof(struct _restart));
}
close(x);
}
}
}
else { /* RestarXX doesn't exist so we muck LastUser instead */
x = sopen(LastUse,O_RDWR | O_BINARY ,SH_DENYWR,S_IWRITE);
if (x != -1) {
lseek(x,0L,SEEK_SET); /* Rewind the file */
LastUser.timeremaining += Timeon();
write(x,(char *) &LastUser,user_slen);
close(x);
}
}
}
/* The problem here is I've yet to figure a way to change drives under
OS/2. If you can resolve this issue, please do. Otherwise, I'll
try to before the next OS/2 Version of maxmail. For now, get_drive and
set_drive are not defined and we must assume that we will use maxmailp
on the same drive as Maxp. */
#if !defined OS2
sprintf(temp,"%s%s",sdrive,sdir);
#endif
switch_dir(sdir); /* Change back to startup dir */
#if !defined OS2
fos_deinit(prm.com_port); Turn off port from us */
#endif
exit(errcode);
}
void aborterror(int code,char *strng)
{
fclose(outfile);
if (strng) {
strcpy(temp,strng);
}
else {
switch (code) {
case BADALLOC:
strcpy(temp,"Error allocating memory");
break;
case BADEXEC:
strcpy(temp,"Error executing external program");
break;
case FILEOPEN:
strcpy(temp,"Error opening file");
break;
case FILECREATE:
strcpy(temp,"Error creating file");
break;
case FILEWRITE:
strcpy(temp,"Error writing file");
break;
case DROPCARRIER:
strcpy(temp,"User dropped carrier!");
IsLocal = TRUE;
break;
case USERIDLE:
sprintf(temp,"User was idle more than %d minutes!",IDLETIME);
IsLocal = TRUE;
break;
default:
strcpy(temp,"General failure");
break;
}
}
strout("\r\n");
strout(temp);
logit(temp,'!');
strout("\r\n");
deinit(code);
}
int load_cfgfile(char *cfgfile)
{
FILE *cfile;
char *p1,*p2,*p3;
int x,y;
cfile = _fsopen(cfgfile,"rt",SH_DENYNO);
if (cfile == NULL) { /* Try to open it in executable maxmail dir */
_splitpath(cfgfile,dummy,dummy,temp,temp1); /* Extract just filename */
_makepath(dummy,hdrive,hdir,temp,temp1);
cfile = _fsopen(dummy,"rt",SH_DENYNO);
if (cfile == NULL) { /* Not there? Then its hopeless! */
sprintf(temp1,"Error opening '%s' config file!\r\n",dummy);
aborterror(FILEOPEN,temp1);
}
}
fgets(temp,81,cfile);
while (!feof(cfile)) {
if (*temp != '\0') { /* Parse out token string */
p1 = strtok(temp," \n\t");
p2 = strtok(NULL," \n\t;");
if (strcmpi(p1,";") == 0) { /* Just ignore comment lines */
fgets(temp,81,cfile);
continue;
}
if (strcmpi(p1,"TxtFile") == 0) {
if (p2) strcpy(TxtFile,p2);
}
else if (strcmpi(p1,"CfgFile") == 0) {
if (p2) strcpy(CfgFile,p2);
}
else if (strcmpi(p1,"LogFile") == 0) {
if (p2) strcpy(LogFile,p2);
LogMode = VERBOSE; /* Default mode */
}
else if (strcmpi(p1,"NewHelpFile") == 0) {
if (p2) strcpy(NewHelpFil,p2);
}
else if (strcmpi(p1,"MaximumMessages") == 0) {
if (p2) MaxMsgs = atoi(p2);
}
else if (strcmpi(p1,"MessageHeader") == 0) {
if (p2) strcpy(MsgHdrFile,p2);
}
else if (strcmpi(p1,"SignOutMsg") == 0) {
x = 0;
p3 = strtok(p2," ;_\n\t");
while (p3) {
x += strlen(p3);
strncat(SignOff,p3,79);
strcat(SignOff," ");
p3 = strtok(NULL," ;_\n\t");
if (strlen(SignOff) >= 80)
break;
}
}
else if (strcmpi(p1,"Packer") == 0) {
x = 0;
temp1[0] = '\0';
while (p2) {
strcat(temp1,p2); /* Description string */
strcat(temp1," "); /* Add a blank for safety */
if (++x > 4) /* Packer requires 4 parameter fields */
break;
p2 = strtok(NULL," ;\n\t");
}
if (x >= 4) enable_packer(temp1);
}
else if (strcmpi(p1,"Protos") == 0) {
x = 0;
temp1[0] = '\0';
while (p2) {
strcat(temp1,p2); /* Description string */
strcat(temp1," "); /* Add a blank for safety */
if (++x >= 3) /* Packer requires 3 parameter fields */
break;
p2 = strtok(NULL," ;\n\t");
}
if (x >= 3) enable_proto(temp1);
}
else if (strcmpi(p1,"MaxAreaScan") == 0) {
x = atoi(p2);
if (x)
MaxScan = x <= 999 ? x : 9999;
}
else if (strcmpi(p1,"SkipAreas") == 0) {
strcpy(temp1,p2); /* Copy area numbers */
p2 = strtok(temp1,",");
y = 1;
while (p2) {
x = atoi(p2);
if (x) { /* Always skip message area #0 */
SkipAreas = (int **) realloc(SkipAreas,sizeof(int *) * (y+1));
if (SkipAreas == NULL)
aborterror(BADALLOC,NULL);
SkipAreas[y-1] = (int *) malloc(sizeof(int));
if (SkipAreas[y-1] == NULL)
aborterror(BADALLOC,NULL);
*SkipAreas[y-1] = x; /* Plug in the area number */
SkipAreas[y] = NULL; /* Terminate */
y++;
}
p2 = strtok(NULL,",");
}
}
}
fgets(temp,81,cfile);
}
fclose(cfile);
return(TRUE);
}
/* Setup a protocol using string format as follows:
"Description Filename Commandline"
| | |----------- Command line for program.
| |
| |---------------------- Full filename (No path)
|
|---------------------------------- Desribe protocol in 1-2 words
*/
void enable_proto(char *strng)
{
char cmndline[40];
char describe[20];
char fname[14];
char *p,*p1;
int x;
struct proto_st *proto1;
p = strtok(strng," "); /* Fetch description */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 29 ? 29 : x;
strncpy(describe,p,x);
describe[x] = '\0';
p = strtok(NULL," "); /* Fetch filename */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 13 ? 13 : x;
strncpy(fname,p,x);
fname[x] = '\0';
p = strtok(NULL," "); /* Fetch command line */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 39 ? 39 : x;
strncpy(cmndline,p,x);
cmndline[x] = '\0';
proto1 = (struct proto_st *) malloc(sizeof(struct proto_st));
if (proto1 == NULL)
aborterror(BADALLOC,NULL);
if (totprotocols)
PROTO_cur->next = proto1;
else PROTO_1 = proto1;
proto1->protoexe = (char *) malloc(strlen(fname) + 1);
if (proto1->protoexe == NULL)
aborterror(BADALLOC,NULL);
strcpy(proto1->protoexe,fname);
proto1->protostring = (char *) malloc(strlen(cmndline) + 1);
if (proto1->protostring == NULL)
aborterror(BADALLOC,NULL);
strcpy(proto1->protostring,cmndline);
proto1->protoname = (char *) malloc(strlen(describe) + 1);
if (proto1->protoname == NULL)
aborterror(BADALLOC,NULL);
strcpy(proto1->protoname,describe);
PROTO_cur = proto1;
proto1->next = NULL;
totprotocols++;
}
/* Setup a packer using string format as follows:
"Description Filename ArcFname Commandline"
| | | |------ Command line for program.
| | |
| | |--------------- Name of packed file
| |
| |---------------------- Full filename (No path)
|
|---------------------------------- Desribe packer in 1-2 words
*/
void enable_packer(char *strng)
{
char cmndline[40];
char viewline[40];
char describe[20];
char fname[14];
char aname[14];
char *p,*p1;
int x;
struct packer_st *pack1;
p = strtok(strng," "); /* Fetch description */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 29 ? 29 : x;
strncpy(describe,p,x);
describe[x] = '\0';
p = strtok(NULL," "); /* Fetch filename */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 13 ? 13 : x;
strncpy(fname,p,x);
fname[x] = '\0';
p = strtok(NULL," "); /* Fetch packed filename */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 13 ? 13 : x;
strncpy(aname,p,x);
aname[x] = '\0';
p = strtok(NULL," "); /* Fetch command line */
if (!p)
return; /* Invalid */
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 39 ? 39 : x;
strncpy(cmndline,p,x);
cmndline[x] = '\0';
viewline[0] = '\0';
p = strtok(NULL," "); /* Fetch viewline */
if (p) {
p1 = strchr(p,'_'); /* Look for replacements */
while (p1) {
*p1 = ' '; /* Put a blank instead */
p1++;
p1 = strchr(p1,'_'); /* Look for replacements */
}
x = strlen(p);
x = x > 39 ? 39 : x;
strncpy(viewline,p,x);
viewline[x] = '\0';
}
pack1 = (struct packer_st *) malloc(sizeof(struct packer_st));
if (pack1 == NULL)
aborterror(BADALLOC,NULL);
if (totpackers)
PACKER_cur->next = pack1;
else PACKER_1 = pack1; /* Start up linked list */
pack1->viewstring = NULL;
pack1->packexe = (char *) malloc(strlen(fname) + 1);
if (pack1->packexe == NULL)
aborterror(BADALLOC,NULL);
strcpy(pack1->packexe,fname);
pack1->packstring = (char *) malloc(strlen(cmndline) + 1);
if (pack1->packstring == NULL)
aborterror(BADALLOC,NULL);
strcpy(pack1->packstring,cmndline);
if (viewline[0]) {
pack1->viewstring = (char *) malloc(strlen(viewline) + 1);
if (pack1->viewstring == NULL)
aborterror(BADALLOC,NULL);
strcpy(pack1->viewstring,viewline);
}
pack1->packname = (char *) malloc(strlen(describe) + 1);
if (pack1->packname == NULL)
aborterror(BADALLOC,NULL);
strcpy(pack1->packname,describe);
pack1->packpname = (char *) malloc(strlen(aname) + 1);
if (pack1->packpname == NULL)
aborterror(BADALLOC,NULL);
strcpy(pack1->packpname,aname);
PACKER_cur = pack1;
pack1->next = NULL;
totpackers++;
}