home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MAXMAILP.ZIP / INIT.C < prev    next >
C/C++ Source or Header  |  1991-01-02  |  17KB  |  551 lines

  1. /****************************************************************************/
  2. /*    Init.c      MaxMail initialization stuff                              */
  3. /*                Version 1.0 by Craig Derouen                              */
  4. /*                                                                          */
  5. /****************************************************************************/
  6. /* Note that the function timeon() has a conflict with a var timeon in the
  7. Max.h header.  When I include my copy of max.h, timeon() and timeon don't cut
  8. it!  You must have altered max.h to get it to work so I think it would be
  9. better to simply change your function timeon() to Timeon().  And that is
  10. what I did. */
  11.  
  12. #include "MaxMail.h"
  13.  
  14. char dummy[81];
  15. char adrive[_MAX_DRIVE];
  16.  
  17. /* Initialize MaxMail */
  18.  
  19. void init()
  20. {
  21.  
  22.    int x;
  23.  
  24.    startsecs = clock();
  25.    parmread = FALSE;
  26.    get_currents();           /* Get the current directory,drive and save */
  27.  
  28.    LogMode = 0;        /* Turn off logging by default */
  29.    update = 0;
  30.    MaxScan = 999;
  31.  
  32.  
  33.    NewHelpFil[0] = 0;        /* Turn off newusers help file */
  34.    LogFile[0] = 0;        /* Turn Off logfile by default */
  35.    SignOff[0] = 0;
  36.    MsgHdrFile[0] = 0;
  37.    PACKER_1 = NULL;
  38.    PROTO_1  = NULL;
  39.  
  40.    SkipAreas = (int **) calloc(1,sizeof(int *));
  41.    if (SkipAreas == NULL)
  42.       aborterror(BADALLOC,NULL);
  43.  
  44.    SkipAreas[0] = NULL;
  45.  
  46.    load_cfgfile(MCfgFile);
  47.    if (Task && LogFile[0]) {        /* Redo logfile name with task # */
  48.       _splitpath(LogFile,adrive,dummy,temp,temp1);
  49.       temp[6] = 0;        /* Trim out 2 chars */
  50.       sprintf(LogFile,"%s%s%s%02d%s",adrive,dummy,temp,Task,temp1);
  51.    }
  52.  
  53.    if (!parmread) {
  54.       if(read_prm(PrmFile) < 0)
  55.          aborterror(FILEOPEN,"Error opening PRM file!\r\n");
  56.  
  57.       if(switch_dir(PRM(sys_path)))        /* Change to Maximus home dir */
  58.          deinit(2);
  59.    }
  60.  
  61.    if (IsLocal >= 2) MaxMsgs = 9999;        /* Allow tons of messages in local mode */
  62.  
  63.    sprintf(temp,"MxMlAct.%03d",Task);        /* Create a task id */
  64.    x = sopen(temp,O_RDWR | O_BINARY | O_CREAT,SH_DENYWR,S_IWRITE);
  65.    close(x);        /* Create just a null file */
  66.  
  67.    if (Task && SumFile[0]) {
  68.       sprintf(temp,".%03d",Task);
  69.       strcat(SumFile,temp);
  70.    }
  71. }
  72.  
  73. /* Close up MaxMail, clean up loose ends */
  74. void deinit(int errcode)
  75. {
  76.    int x,y,z;
  77.    long secdiff;
  78.    struct _restart *rst;
  79.    struct stat fbuf;
  80.  
  81.    fclose(afile);
  82.    if (packer_del)
  83.       unlink(ArcFile);  
  84.    unlink(TxtFile);
  85.    if (StatName[0])
  86.       unlink(StatName);
  87.    sprintf(temp,"MxMlAct.%03d",Task);        /* Create a task id */
  88.    if(unlink(temp) == 0) {        /* Delete our task file */
  89.       x = sopen(CfgFile,O_RDWR | O_BINARY,SH_DENYWR,S_IWRITE);
  90.       if (x != -1) {
  91.          time(&USERCFG.lasttime);
  92.          USERCFG.isused = TRUE;
  93.          USERCFG.calls++;
  94.          if (USERCFG.calls < USERCFG.packcount)
  95.             USERCFG.calls = USERCFG.packcount;
  96.          update_usercfg(x);
  97.          close(x);
  98.       }
  99.       else logit("Error updating User Config file",'!');
  100.    }
  101.  
  102.    sprintf(temp,"End, v%2.2f (%d)",Version,Timeon());
  103.    logit(temp,':');
  104.  
  105.  
  106.    if (LogFile[0]) {
  107.       x = sopen(LogFile,O_BINARY | O_RDWR | O_APPEND,SH_DENYWR,S_IWRITE);
  108.       if (x > 0) {
  109.          write(x,"\r\n",2);
  110.          close(x);
  111.       }
  112.    }
  113.  
  114.    if (TimeAdjust) {
  115.       sprintf(temp,"Restar%02d.bbs",Task); 
  116.       if(stat(temp,&fbuf) == 0 ) { 
  117.          rst = (struct _restart *) malloc(sizeof(struct _restart));
  118.          if (rst != NULL) {
  119.             x = sopen(temp,O_BINARY | O_RDWR,SH_DENYWR,S_IWRITE);
  120.             if (x != -1) {
  121.                if(read(x,(char *)rst,sizeof(struct _restart)) != 0) {
  122.                   secdiff = rst->timeoff - rst->timeon;
  123.                   z = secdiff / 60L;        /* convert to minutes */
  124.                   y = Timeon();
  125.                   if ((long)(y + z) <= rst->max_time) /* We can bump it up */
  126.                      rst->timeoff += 60 * Timeon();
  127.                   lseek(x,0L,SEEK_SET);        /* Rewind the file */
  128.                   write(x,(char *)rst,sizeof(struct _restart));
  129.                }
  130.                close(x);
  131.             }
  132.          }
  133.       }
  134.       else {        /* RestarXX doesn't exist so we muck LastUser instead */
  135.          x = sopen(LastUse,O_RDWR | O_BINARY ,SH_DENYWR,S_IWRITE); 
  136.          if (x != -1) {
  137.             lseek(x,0L,SEEK_SET);        /* Rewind the file */
  138.             LastUser.timeremaining += Timeon();
  139.             write(x,(char *) &LastUser,user_slen);
  140.             close(x);
  141.          }
  142.       }
  143.    }
  144. /* The problem here is I've yet to figure a way to change drives under
  145.    OS/2.  If you can resolve this issue, please do.  Otherwise, I'll
  146.    try to before the next OS/2 Version of maxmail.  For now, get_drive and
  147.    set_drive are not defined and we must assume that we will use maxmailp
  148.    on the same drive as Maxp. */
  149. #if !defined OS2
  150.      sprintf(temp,"%s%s",sdrive,sdir);
  151. #endif
  152.    switch_dir(sdir);        /* Change back to startup dir */
  153. #if !defined OS2
  154.    fos_deinit(prm.com_port);    Turn off port from us */
  155. #endif
  156.    exit(errcode);
  157. }
  158.  
  159. void aborterror(int code,char *strng)
  160. {
  161.    fclose(outfile);
  162.  
  163.    if (strng) {
  164.       strcpy(temp,strng);
  165.    }
  166.    else {
  167.       switch (code) {
  168.          case BADALLOC:
  169.             strcpy(temp,"Error allocating memory");
  170.             break;
  171.  
  172.          case BADEXEC:
  173.             strcpy(temp,"Error executing external program");
  174.             break;
  175.  
  176.          case FILEOPEN:
  177.             strcpy(temp,"Error opening file");
  178.             break;
  179.  
  180.          case FILECREATE:
  181.             strcpy(temp,"Error creating file");
  182.             break;
  183.  
  184.          case FILEWRITE:
  185.             strcpy(temp,"Error writing file");
  186.             break;
  187.  
  188.          case DROPCARRIER:
  189.             strcpy(temp,"User dropped carrier!");
  190.             IsLocal = TRUE;
  191.             break;
  192.  
  193.          case USERIDLE:
  194.             sprintf(temp,"User was idle more than %d minutes!",IDLETIME);
  195.             IsLocal = TRUE;
  196.             break;
  197.  
  198.          default:
  199.             strcpy(temp,"General failure");
  200.             break;
  201.       }
  202.    }
  203.    strout("\r\n");
  204.    strout(temp);
  205.    logit(temp,'!');
  206.    strout("\r\n");
  207.    deinit(code);
  208. }
  209.  
  210. int load_cfgfile(char *cfgfile)
  211. {
  212.    FILE *cfile;
  213.    char *p1,*p2,*p3;
  214.    int x,y;
  215.  
  216.    cfile = _fsopen(cfgfile,"rt",SH_DENYNO);
  217.    if (cfile == NULL) {        /* Try to open it in executable maxmail dir */
  218.       _splitpath(cfgfile,dummy,dummy,temp,temp1);        /* Extract just filename */
  219.       _makepath(dummy,hdrive,hdir,temp,temp1);
  220.       cfile = _fsopen(dummy,"rt",SH_DENYNO);
  221.       if (cfile == NULL) {        /* Not there? Then its hopeless! */
  222.          sprintf(temp1,"Error opening '%s' config file!\r\n",dummy);
  223.          aborterror(FILEOPEN,temp1);
  224.       }
  225.    }
  226.  
  227.    fgets(temp,81,cfile);
  228.    while (!feof(cfile)) {
  229.       if (*temp != '\0') {        /* Parse out token string */
  230.          p1 = strtok(temp," \n\t");
  231.          p2 = strtok(NULL," \n\t;");
  232.          if (strcmpi(p1,";") == 0) {        /* Just ignore comment lines */
  233.             fgets(temp,81,cfile);
  234.             continue;
  235.          }
  236.          if (strcmpi(p1,"TxtFile") == 0) {
  237.             if (p2) strcpy(TxtFile,p2);
  238.          }
  239.          else if (strcmpi(p1,"CfgFile") == 0) {
  240.             if (p2) strcpy(CfgFile,p2);
  241.          }
  242.          else if (strcmpi(p1,"LogFile") == 0) {
  243.             if (p2) strcpy(LogFile,p2);
  244.             LogMode = VERBOSE;        /* Default mode */
  245.          }
  246.          else if (strcmpi(p1,"NewHelpFile") == 0) {
  247.             if (p2) strcpy(NewHelpFil,p2);
  248.          }
  249.          else if (strcmpi(p1,"MaximumMessages") == 0) {
  250.             if (p2) MaxMsgs = atoi(p2);
  251.          }
  252.          else if (strcmpi(p1,"MessageHeader") == 0) {
  253.             if (p2) strcpy(MsgHdrFile,p2);
  254.          }
  255.          else if (strcmpi(p1,"SignOutMsg") == 0) {
  256.             x = 0;
  257.             p3 = strtok(p2," ;_\n\t");
  258.             while (p3) {
  259.                x += strlen(p3);
  260.                strncat(SignOff,p3,79);
  261.                strcat(SignOff," ");
  262.                p3 = strtok(NULL," ;_\n\t");
  263.                if (strlen(SignOff) >= 80)
  264.                   break;
  265.             }
  266.          }
  267.          else if (strcmpi(p1,"Packer") == 0) {
  268.             x = 0;
  269.             temp1[0] = '\0';
  270.             while (p2) {
  271.                strcat(temp1,p2);        /* Description string */
  272.                strcat(temp1," ");        /* Add a blank for safety */
  273.                if (++x > 4)        /* Packer requires 4 parameter fields */
  274.                   break;
  275.                p2 = strtok(NULL," ;\n\t");
  276.             }
  277.             if (x >= 4) enable_packer(temp1);
  278.          }
  279.          else if (strcmpi(p1,"Protos") == 0) {
  280.             x = 0;
  281.             temp1[0] = '\0';
  282.             while (p2) {
  283.                strcat(temp1,p2);        /* Description string */
  284.                strcat(temp1," ");        /* Add a blank for safety */
  285.                if (++x >= 3)        /* Packer requires 3 parameter fields */
  286.                   break;
  287.                p2 = strtok(NULL," ;\n\t");
  288.             }
  289.             if (x >= 3) enable_proto(temp1);
  290.          }
  291.          else if (strcmpi(p1,"MaxAreaScan") == 0) {
  292.             x = atoi(p2);
  293.             if (x)
  294.                MaxScan = x <= 999 ? x : 9999;
  295.          }
  296.          else if (strcmpi(p1,"SkipAreas") == 0) {
  297.             strcpy(temp1,p2);        /* Copy area numbers */
  298.             p2 = strtok(temp1,",");
  299.             y = 1;
  300.             while (p2) {
  301.                x = atoi(p2);
  302.                if (x) {        /* Always skip message area #0 */
  303.                   SkipAreas = (int **) realloc(SkipAreas,sizeof(int *) * (y+1));
  304.                   if (SkipAreas == NULL)
  305.                      aborterror(BADALLOC,NULL);
  306.                   SkipAreas[y-1] = (int *) malloc(sizeof(int));
  307.                   if (SkipAreas[y-1] == NULL)
  308.                      aborterror(BADALLOC,NULL);
  309.                   *SkipAreas[y-1] = x;        /* Plug in the area number */
  310.                   SkipAreas[y] = NULL;        /* Terminate */
  311.                   y++;
  312.                }
  313.                p2 = strtok(NULL,",");
  314.             }
  315.          }
  316.       }
  317.       fgets(temp,81,cfile);
  318.    }
  319.    fclose(cfile);
  320.    return(TRUE);
  321. }
  322.  
  323.  
  324. /* Setup a protocol using string format as follows:
  325.    "Description Filename Commandline"
  326.         |           |          |----------- Command line for program.
  327.         |           |
  328.         |           |---------------------- Full filename (No path)
  329.         |
  330.         |---------------------------------- Desribe protocol in 1-2 words
  331. */
  332.  
  333. void enable_proto(char *strng)
  334. {
  335.    char cmndline[40];
  336.    char describe[20];
  337.    char fname[14];
  338.    char *p,*p1;
  339.    int x;
  340.    struct proto_st *proto1;
  341.  
  342.  
  343.    p = strtok(strng," ");        /* Fetch description */
  344.    if (!p)
  345.       return;        /* Invalid */
  346.    p1 = strchr(p,'_');        /* Look for replacements */
  347.    while (p1) {
  348.       *p1 = ' ';                    /* Put a blank instead */
  349.       p1++;
  350.       p1 = strchr(p1,'_');        /* Look for replacements */
  351.    }
  352.    x = strlen(p);
  353.    x = x > 29 ? 29 : x;
  354.    strncpy(describe,p,x);
  355.    describe[x] = '\0';
  356.    
  357.    p = strtok(NULL," ");        /* Fetch filename */
  358.    if (!p)
  359.       return;        /* Invalid */
  360.    p1 = strchr(p,'_');        /* Look for replacements */
  361.    while (p1) {
  362.       *p1 = ' ';                    /* Put a blank instead */
  363.       p1++;
  364.       p1 = strchr(p1,'_');        /* Look for replacements */
  365.    }
  366.    x = strlen(p);
  367.    x = x > 13 ? 13 : x;
  368.    strncpy(fname,p,x);
  369.    fname[x] = '\0';
  370.    
  371.    p = strtok(NULL," ");        /* Fetch command line */
  372.    if (!p)
  373.       return;        /* Invalid */
  374.    p1 = strchr(p,'_');        /* Look for replacements */
  375.    while (p1) {
  376.       *p1 = ' ';                    /* Put a blank instead */
  377.       p1++;
  378.       p1 = strchr(p1,'_');        /* Look for replacements */
  379.    }
  380.    x = strlen(p);
  381.    x = x > 39 ? 39 : x;
  382.    strncpy(cmndline,p,x);
  383.    cmndline[x] = '\0';
  384.    
  385.  
  386.    proto1 = (struct proto_st *) malloc(sizeof(struct proto_st));
  387.    if (proto1 == NULL)
  388.       aborterror(BADALLOC,NULL);
  389.  
  390.    if (totprotocols)
  391.       PROTO_cur->next = proto1;
  392.    else PROTO_1 = proto1;
  393.  
  394.    proto1->protoexe = (char *) malloc(strlen(fname) + 1);
  395.    if (proto1->protoexe == NULL)
  396.       aborterror(BADALLOC,NULL);
  397.    strcpy(proto1->protoexe,fname);
  398.  
  399.    proto1->protostring = (char *) malloc(strlen(cmndline) + 1);
  400.    if (proto1->protostring == NULL)
  401.       aborterror(BADALLOC,NULL);
  402.    strcpy(proto1->protostring,cmndline);
  403.  
  404.    proto1->protoname = (char *) malloc(strlen(describe) + 1);
  405.    if (proto1->protoname == NULL)
  406.       aborterror(BADALLOC,NULL);
  407.    strcpy(proto1->protoname,describe);
  408.  
  409.    PROTO_cur = proto1;
  410.    proto1->next = NULL;
  411.  
  412.    totprotocols++;
  413. }
  414.  
  415. /* Setup a packer using string format as follows:
  416.    "Description Filename ArcFname Commandline"
  417.         |           |       |        |------ Command line for program.
  418.         |           |       |
  419.         |           |       |--------------- Name of packed file
  420.         |           |
  421.         |           |---------------------- Full filename (No path)
  422.         |
  423.         |---------------------------------- Desribe packer in 1-2 words
  424. */
  425.  
  426. void enable_packer(char *strng)
  427. {
  428.    char cmndline[40];
  429.    char viewline[40];
  430.    char describe[20];
  431.    char fname[14];
  432.    char aname[14];
  433.    char *p,*p1;
  434.    int x;
  435.    struct packer_st *pack1;
  436.  
  437.  
  438.    p = strtok(strng," ");        /* Fetch description */
  439.    if (!p)
  440.       return;        /* Invalid */
  441.    p1 = strchr(p,'_');        /* Look for replacements */
  442.    while (p1) {
  443.       *p1 = ' ';                    /* Put a blank instead */
  444.       p1++;
  445.       p1 = strchr(p1,'_');        /* Look for replacements */
  446.    }
  447.    x = strlen(p);
  448.    x = x > 29 ? 29 : x;
  449.    strncpy(describe,p,x);
  450.    describe[x] = '\0';
  451.    
  452.    p = strtok(NULL," ");        /* Fetch filename */
  453.    if (!p)
  454.       return;        /* Invalid */
  455.    p1 = strchr(p,'_');        /* Look for replacements */
  456.    while (p1) {
  457.       *p1 = ' ';                    /* Put a blank instead */
  458.       p1++;
  459.       p1 = strchr(p1,'_');        /* Look for replacements */
  460.    }
  461.    x = strlen(p);
  462.    x = x > 13 ? 13 : x;
  463.    strncpy(fname,p,x);
  464.    fname[x] = '\0';
  465.    
  466.    p = strtok(NULL," ");        /* Fetch packed filename */
  467.    if (!p)
  468.       return;        /* Invalid */
  469.    p1 = strchr(p,'_');        /* Look for replacements */
  470.    while (p1) {
  471.       *p1 = ' ';                    /* Put a blank instead */
  472.       p1++;
  473.       p1 = strchr(p1,'_');        /* Look for replacements */
  474.    }
  475.    x = strlen(p);
  476.    x = x > 13 ? 13 : x;
  477.    strncpy(aname,p,x);
  478.    aname[x] = '\0';
  479.    
  480.    p = strtok(NULL," ");        /* Fetch command line */
  481.    if (!p)
  482.       return;        /* Invalid */
  483.    p1 = strchr(p,'_');        /* Look for replacements */
  484.    while (p1) {
  485.       *p1 = ' ';                    /* Put a blank instead */
  486.       p1++;
  487.       p1 = strchr(p1,'_');        /* Look for replacements */
  488.    }
  489.    x = strlen(p);
  490.    x = x > 39 ? 39 : x;
  491.    strncpy(cmndline,p,x);
  492.    cmndline[x] = '\0';
  493.    
  494.    viewline[0] = '\0';
  495.    p = strtok(NULL," ");        /* Fetch viewline */
  496.    if (p) {
  497.       p1 = strchr(p,'_');        /* Look for replacements */
  498.       while (p1) {
  499.          *p1 = ' ';                    /* Put a blank instead */
  500.          p1++;
  501.          p1 = strchr(p1,'_');        /* Look for replacements */
  502.       }
  503.       x = strlen(p);
  504.       x = x > 39 ? 39 : x;
  505.       strncpy(viewline,p,x);
  506.       viewline[x] = '\0';
  507.    }
  508.    
  509.    pack1 = (struct packer_st *) malloc(sizeof(struct packer_st));
  510.    if (pack1 == NULL)
  511.       aborterror(BADALLOC,NULL);
  512.  
  513.    if (totpackers) 
  514.       PACKER_cur->next = pack1;
  515.    else PACKER_1 = pack1;        /* Start up linked list */
  516.  
  517.    pack1->viewstring = NULL;
  518.  
  519.    pack1->packexe = (char *) malloc(strlen(fname) + 1);
  520.    if (pack1->packexe == NULL)
  521.       aborterror(BADALLOC,NULL);
  522.    strcpy(pack1->packexe,fname);
  523.  
  524.    pack1->packstring = (char *) malloc(strlen(cmndline) + 1);
  525.    if (pack1->packstring == NULL)
  526.       aborterror(BADALLOC,NULL);
  527.    strcpy(pack1->packstring,cmndline);
  528.  
  529.    if (viewline[0]) {
  530.       pack1->viewstring = (char *) malloc(strlen(viewline) + 1);
  531.       if (pack1->viewstring == NULL)
  532.          aborterror(BADALLOC,NULL);
  533.       strcpy(pack1->viewstring,viewline);
  534.    }
  535.  
  536.    pack1->packname = (char *) malloc(strlen(describe) + 1);
  537.    if (pack1->packname == NULL)
  538.       aborterror(BADALLOC,NULL);
  539.    strcpy(pack1->packname,describe);
  540.  
  541.    pack1->packpname = (char *) malloc(strlen(aname) + 1);
  542.    if (pack1->packpname == NULL)
  543.       aborterror(BADALLOC,NULL);
  544.    strcpy(pack1->packpname,aname);
  545.  
  546.    PACKER_cur = pack1;
  547.    pack1->next = NULL;
  548.  
  549.    totpackers++;
  550. }
  551.