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

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    File.c      File i/o routines for MaxMail                             */
  4. /*                Version 1.0                                               */
  5. /*                                                                          */
  6. /****************************************************************************/
  7.  
  8. #include "MaxMail.h"
  9.  
  10. struct msgupd_st *msgupd;
  11. struct msgupd_st *next;
  12. struct proto_st *proto1;
  13. struct packer_st *packer1;
  14. struct stat fbuf;
  15. char fname[_MAX_PATH];
  16. char exec_ret;
  17. #if defined OS2
  18.   extern hfComHandle;
  19. #endif
  20. /* Load up user message areas data into linked list */
  21.  
  22. int build_areas()
  23. {
  24.    int keys,mask,x,anum;
  25.    int index,count;
  26.    long lerr;
  27.    word *bitp;
  28.    struct _aidx IDX;
  29.  
  30.    count = 0;
  31.    if (PackDone)
  32.       return(count);
  33.  
  34.    msgupd = MSGUPD_1;
  35.    while (msgupd) {
  36.       next = msgupd->next;
  37.       free(msgupd);
  38.       msgupd = next;
  39.    }
  40.  
  41.    MSGUPD_1 = NULL;        /* Set up linked list */
  42.    index = sopen(PRM(aidx_name),O_BINARY | O_RDONLY,SH_DENYNO,S_IREAD);
  43.    if (index == -1)
  44.       aborterror(FILEOPEN,"Error opening Area index file");
  45.    keys = read(index,(char *)&IDX,sizeof(struct _aidx));
  46.    fseek(afile,IDX.offset,SEEK_SET);
  47.    fread(&AREA,astrlen,1,afile);
  48.    anum = atoi(AREA.name);
  49.    while (keys) {
  50.       bitp = &USERCFG.msgarea1[anum / 16];
  51.       mask = 1;
  52.       for (x=0; x < (anum % 16); x++)
  53.          mask = mask << 1;
  54.       keys = AREA.msglock;
  55.       if (!keys)         /* No locks for this area */
  56.          keys = TRUE;
  57.       else {        /* User must have same locks */
  58.          keys = AREA.msglock & LastUser.key;
  59.          if (keys != AREA.msglock)
  60.             keys = FALSE;
  61.       }
  62.       if (isskiparea(anum))
  63.          keys = FALSE;         /* If Sysop set skip, don't build this area */
  64.       if (AREA.msgpath[0] && (LastUser.priv >= AREA.msgpriv) && keys) {
  65.          count++;        /* Possible areas count */
  66.          if (*bitp & mask) {        /* User has selected this area */
  67.             msgupd = (struct msgupd_st *) malloc(sizeof(struct msgupd_st));
  68.             if (msgupd == NULL)
  69.                aborterror(BADALLOC,NULL);
  70.             if (MSGUPD_1 == NULL) 
  71.                MSGUPD_1 = msgupd;        /* Link in top of pointer */
  72.             else MSGUPD_cur->next = msgupd;
  73.             msgupd->areano = atoi(AREA.name);
  74.             strcpy(msgupd->msgpath,AREA.msgpath);
  75.             msgupd->update = FALSE;
  76.             switch (AREA.area_type) {
  77.                case 0:        /* Fido 1.msg style */
  78.                   msgupd->himsg = find_fidohigh(AREA.msgpath);
  79.                   break;
  80.  
  81.                default:
  82.                   msgupd->himsg = find_fidohigh(AREA.msgpath);
  83.                   break;
  84.             }
  85.             if (msgupd->himsg == 0xffff )
  86.                msgupd->himsg = 0;
  87.             msgupd->areaindex = ftell(afile) - (long) astrlen;    /* Position */
  88.             msgupd->next = NULL;
  89.             msgupd->startmsg = 1;
  90.             msgupd->readmsgs = 0;
  91.             MSGUPD_cur = msgupd;
  92.             if (msgupd->himsg) {
  93.                if (LastUser.lastread_ptr)
  94.                   sprintf(temp1,"%slastread.bbs",AREA.msgpath);
  95.                else sprintf(temp1,"%slastread",AREA.msgpath);  
  96.                x = sopen(temp1,O_RDONLY | O_BINARY,SH_DENYNO,S_IREAD);    /* Poke into the lastread file */
  97.                if (x != -1)  {
  98.                   lerr = lseek(x,(long)(LastUser.lastread_ptr * sizeof(int)),SEEK_SET);
  99.                   if (lerr != -1L) {
  100.                      read(x,(char *) &msgupd->startmsg,sizeof(int));        /* Get last message read */
  101.                      if (msgupd->startmsg > msgupd->himsg) 
  102.                         msgupd->startmsg = msgupd->himsg;
  103.                      if (msgupd->startmsg < 1)
  104.                         msgupd->startmsg = 1;
  105.                      msgupd->msgcount = msgupd->himsg - msgupd->startmsg;
  106.                      if (msgupd->msgcount < 0)
  107.                         msgupd->msgcount = 0;
  108.                   }
  109.                   close(x);
  110.                }
  111.             }
  112.             else {
  113.                msgupd->startmsg = 0;
  114.                msgupd->msgcount = 0;
  115.             }
  116.          }
  117.       }
  118.       keys = read(index,(char *)&IDX,sizeof(struct _aidx));
  119.       fseek(afile,IDX.offset,SEEK_SET);
  120.       fread(&AREA,astrlen,1,afile);
  121.       anum = atoi(AREA.name);
  122.       while ( anum > MaxScan && keys) {        /* Stop Scan */
  123.          keys = read(index,(char *)&IDX,sizeof(struct _aidx));
  124.          fseek(afile,IDX.offset,SEEK_SET);
  125.          fread(&AREA,astrlen,1,afile);
  126.          anum = atoi(AREA.name);
  127.       }
  128.    }
  129.    close(index);
  130.    return(count);
  131. }
  132.  
  133. /* Scan message areas,build capture file */
  134.  
  135. int scan_msgs()
  136. {
  137.    int x,y,firsttime;
  138.    FILE *headfile;
  139.    FILE *statfile;
  140.    FILE *sfile;
  141.    struct msghead *MSGHD;
  142.  
  143.    if (MSGUPD_1 == NULL) {
  144.       logit("User has no message areas selected",'!');
  145.       strout("\r\nYou have no valid message areas selected.");
  146.       strout("\r\nSelect [C] at menu prompt and change your configuration\r\n");
  147.       return(FALSE);
  148.    }
  149.  
  150.    if (PackDone){
  151.       return(FALSE);
  152.    }
  153.  
  154.    if (Task) {        /* Rename TxtFile to have tasknumber */
  155.       _splitpath(TxtFile,hdrive,hdir,temp,temp1);
  156.       temp[6] = 0;        /* Trim out 2 chars */
  157.       sprintf(TxtFile,"%s%02d%s",temp,Task,temp1);
  158.    }
  159.    outfile = _fsopen(TxtFile,"w+",SH_DENYNO);
  160.    if (outfile == NULL) {
  161.       sprintf(temp1,"Error creating %s",TxtFile);
  162.       aborterror(FILECREATE,temp1);
  163.    }
  164.  
  165.    totmsgs = 0;
  166.    firsttime = TRUE;
  167.  
  168.    MSGHD = (struct msghead *) malloc(sizeof(struct msghead));
  169.    if (MSGHD == NULL)
  170.       aborterror(BADALLOC,NULL);
  171.    MSGHD->answers = NULL;
  172.  
  173.    strcpy(MSGHD->ourname,LastUser.name);
  174.  
  175.    msgupd = MSGUPD_1;
  176.    while (msgupd) {
  177.       fseek(afile,msgupd->areaindex,SEEK_SET);
  178.       fread(&AREA,astrlen,1,afile);
  179.       if (firsttime) {
  180.          fprintf(outfile,"\n MaxMail Version %.2f Custom message archive for %s\n",Version,USERCFG.name);
  181.          fprintf(outfile," ---> Messages were captured on %s\n",timestring());
  182.          if (MsgHdrFile[0]) {        /* Sysop wants to put in a message header file */
  183.             headfile = _fsopen(MsgHdrFile,"rt",SH_DENYNO);
  184.             if (headfile == NULL) 
  185.                logit("Message Header file could not be opened.",'!');
  186.             else {
  187.                fgets(temp,80,headfile);
  188.                while (!feof(headfile)) {
  189.                   fputs(temp,outfile);
  190.                   fgets(temp,80,headfile);
  191.                }
  192.                fclose(headfile);
  193.             }
  194.          }
  195.          if (StatName[0]) {
  196.             if (Task) {
  197.                sprintf(temp,".%d",Task);
  198.                strcat(StatName,temp);
  199.             }
  200.             statfile = _fsopen(StatName,"wt",SH_DENYNO);
  201.          }
  202.          else statfile = NULL;
  203.          if (SumFile[0]) 
  204.             unlink(SumFile);
  205.          strout("\r\n\r\n--------[ Grinding the sysop's disk now ]-----------------");
  206.          strout("\r\n                              Start    High   New     Your");
  207.          strout("\r\nMessage Area                    Msg     Msg   Msgs    Msgs");
  208.          strout("\r\n----------------------------------------------------------\r\n");
  209.          if (statfile != NULL) {
  210.             fprintf(statfile,"\n----------------[ MaxMaxil Statistics ]-------------------");
  211.             fprintf(statfile,"\n                              Start    High   New     Your");
  212.             fprintf(statfile,"\nMessage Area                    Msg     Msg   Msgs    Msgs");
  213.             fprintf(statfile,"\n----------------------------------------------------------\n");
  214.          }
  215.          firsttime = FALSE;
  216.       }
  217.       msgupd->readmsgs = 0;
  218.       if (msgupd->msgcount > 0)     {    /* If there are messages in this area */
  219.          MSGHD->ourmail = 0;
  220.          if (MSGHD->answers != NULL)
  221.             free(MSGHD->answers);
  222.          MSGHD->answers = NULL;
  223.          x = load_area(msgupd,statfile,MSGHD);        /* Return lastread msgnum */
  224.          if (msg_update) {
  225.             msgupd->update = TRUE;
  226.             msgupd->readmsgs = x - msgupd->startmsg;        /* Establish last message read */
  227.          }
  228.          if (statfile != NULL && MSGHD->ourmail) {
  229.             sprintf(temp,"      You have %d personal messages in this area.\r\n",MSGHD->ourmail);
  230.             strout(temp);
  231.             fprintf(statfile,"      You have %d personal messages in this area. See messages: \n",MSGHD->ourmail);
  232.             y = 0;
  233.             fprintf(statfile,"      ");
  234.             for (x = 0; x < MSGHD->ourmail; x++) {
  235.                if (x < (MSGHD->ourmail - 1))
  236.                   fprintf(statfile,"%d,",MSGHD->answers[x]);
  237.                else fprintf(statfile,"%d",MSGHD->answers[x]);
  238.                if(++y > 20) {
  239.                   fprintf(statfile,"\n      ");
  240.                   y = 0;
  241.                }
  242.             }
  243.             fprintf(statfile,"\n");
  244.          }
  245.       }
  246.       if (totmsgs >= MaxMsgs) {
  247.          sprintf(temp1,"\r\nPoof! You've gobbled up the maximum # of messages (%d) for this run\r\n",totmsgs);
  248.          strout(temp1);
  249.          if (LogMode == VERBOSE)
  250.             logit("User has used up maximum messages for this run",'#');
  251.          break;
  252.       }
  253.       msgupd = msgupd->next;
  254.    }
  255.    fprintf(outfile," \n\n ---> %d Total messages were captured <---\n",totmsgs);
  256.    fclose(outfile);
  257.    if (statfile != NULL) {
  258.       sfile = _fsopen(SumFile,"rt",SH_DENYNO);
  259.       if (SumFile[0] && sfile != NULL) {
  260.          while (!feof(sfile)) {
  261.             if(fgets(temp1,80,sfile) != NULL)
  262.                fputs(temp1,statfile);
  263.          }
  264.          fclose(sfile);
  265.       }
  266.       fclose(statfile);
  267.    }
  268.    if (SumFile[0])
  269.       unlink(SumFile);
  270.    if (MSGHD->answers != NULL)
  271.       free(MSGHD->answers);
  272.    free(MSGHD);
  273.    return(TRUE);
  274. }
  275.  
  276. /* Scan individual message area,called from Scan_msgs(), get
  277.    message text and write to capture file, stats file */
  278.  
  279. int load_area(struct msgupd_st *msgupd,FILE *statfile,struct msghead *MSGHD)
  280. {
  281.    int x,y,msgcount,divisor;
  282.    int forward,fidoflags;
  283.    FILE *sumfile;
  284.  
  285.    if (msgupd->himsg > 0) {
  286.       fprintf(outfile,"\n\n\n *************************************************************\n");
  287.       if (AREA.attrib[UserClass] & ECHOMAIL) 
  288.          sprintf(temp," ** Echomail Area %s - %s ",AREA.name,AREA.msginfo);
  289.       else sprintf(temp," ** Local/Public Area %s - %s ",AREA.name,AREA.msginfo);
  290.       while (strlen(temp) < 60)
  291.          strcat(temp," ");
  292.       strcat(temp,"**");
  293.       fprintf(outfile,"%s\n",temp);
  294.       fprintf(outfile," *************************************************************\n");
  295.       strncpy(temp1,AREA.msginfo,24);
  296.       temp1[24] = 0;
  297.       while (strlen(temp1) < 24)
  298.          strcat(temp1," ");
  299.       sprintf(temp,"[%03d] %s %04d    %04d   %04d     ",atoi(AREA.name),temp1,msgupd->startmsg,msgupd->himsg,msgupd->msgcount);
  300.       if (statfile != NULL)
  301.          fprintf(statfile,"[%03d] %s %04d    %04d   %04d    ",atoi(AREA.name),temp1,msgupd->startmsg,msgupd->himsg,msgupd->msgcount);
  302.       if (SumFile[0]) {
  303.          sumfile = _fsopen(SumFile,"at",SH_DENYNO);
  304.          if (statfile != NULL) {
  305.             fprintf(sumfile,"\n\n************ Message Subject Summary ************************\n");
  306.             if (AREA.attrib[UserClass] & ECHOMAIL) 
  307.                sprintf(temp1,"** Echomail Area %s - %s ",AREA.name,AREA.msginfo);
  308.             else sprintf(temp1,"** Local/Public Area %s - %s ",AREA.name,AREA.msginfo);
  309.             while (strlen(temp1) < 59)
  310.                strcat(temp1," ");
  311.             strcat(temp1,"**");
  312.             fprintf(sumfile,"%s\n",temp1);
  313.             fprintf(sumfile,"*************************************************************\n");
  314.             fprintf(sumfile,"Msg#    From                                  Subject\n");
  315.             fprintf(sumfile,"-------------------------------------------------------------\n");
  316.          }
  317.          
  318.       }
  319.       strout(temp);
  320.       msgcount = 0;
  321.       forward = 1;        /* Activity flag */
  322.  
  323.       fidoflags = 0;
  324.       if (LastUser.priv < SYSOP && !(AREA.attrib[UserClass] & ECHOMAIL)) 
  325.          fidoflags |= PRIVATE;
  326.  
  327.       if (msgupd->msgcount > 40)        /* Total messages in this area */
  328.          divisor = 3;
  329.       else divisor = 2;        /* Flip activity flag a little more */
  330.       for (x=msgupd->startmsg+1; x <= msgupd->himsg && (totmsgs < MaxMsgs); x++) {
  331.          memset(MSGHD->subject,0,72);
  332.          switch (AREA.area_type) {
  333.             case 0:        /* Fido 1.msg style */
  334.                y = file_fidomsg(x,fidoflags,outfile,msgupd,MSGHD);
  335.                break;
  336.  
  337.             default:
  338.                y = file_fidomsg(x,fidoflags,outfile,msgupd,MSGHD);
  339.                break;
  340.          }
  341.  
  342.          if (y < 0) {        /* Some kind of error */
  343.             if (y == PVTMSG_ERR)         /* No real error, just a private msg */
  344.                msgcount++;
  345.             else if ( y !=FILE_SRCH_ERR) {      /* No messages to strip */
  346.                strout(" No new msgs!\r\n");     /* This really should be impossible */
  347.                break;
  348.             }
  349.          }
  350.          else {        /* A message was read */
  351.             if (sumfile != NULL) {        /* Muck with the summary file */
  352.                sprintf(temp1,"%d",y);
  353.                while (strlen(temp1) < 8 )
  354.                   strcat(temp1," ");
  355.                fprintf(sumfile,"%s",temp1);
  356.                sprintf(temp1,"%s",MSGHD->from);
  357.                while (strlen(temp1) < 36 )
  358.                   strcat(temp1," ");
  359.                fprintf(sumfile,"%s  ",temp1);
  360.                strncpy(temp1,MSGHD->subject,34);
  361.                temp1[34] = 0;
  362.                fprintf(sumfile,"%s\n",temp1);
  363.             }
  364.             msgcount++;
  365.             totmsgs++;
  366.          }
  367.          if (!(msgcount % divisor)) {        /* A little activity display logic */
  368.             switch (forward) {
  369.                case 1:
  370.                   strout("\b/");
  371.                   forward++;
  372.                   break;
  373.  
  374.                case 2:
  375.                   strout("\b-");
  376.                   forward++;
  377.                   break;
  378.  
  379.                case 3:
  380.                   strout("\b\\");
  381.                   forward = 1;
  382.                   break;
  383.             }
  384.          }
  385.       }
  386.       if (!msgcount)        /* No messages in this area */
  387.          strout("  No new msgs!\r\n");        /* Again, really impossible */
  388.       else {
  389.          if (msgcount >= 3) 
  390.             strout("\b ");
  391.          sprintf(temp,"\b%04d\r\n",msgcount);
  392.          strout(temp);
  393.          if (statfile != NULL)
  394.             fprintf(statfile,"%04d\n",msgcount);
  395.       }
  396.    }
  397.    if (sumfile != NULL)
  398.       fclose(sumfile);
  399.    return y;
  400. }
  401.  
  402. /* Pack user capture file & summary file into designated packer */
  403.  
  404. int packit(void)
  405. {
  406.    int x,old;
  407.    char *p,*p1,*p2;
  408.    char fext[5];
  409.    char command[81]; /* New Var to get around swap() */
  410.    FILE *new;
  411.  
  412.    PackDone = FALSE;
  413.    x = USERCFG.packer;
  414.  
  415.    if (!x)         /* Must ask */
  416.       get_packer(FALSE);
  417.  
  418.    packer1 = get_curpacklnk();
  419.    USERCFG.packer = x;
  420.  
  421.    _splitpath(packer1->packpname,temp,temp,fname,fext);
  422.    x = strlen(fname);
  423.    x = x <= 6 ? x : 6;
  424.    strncpy(ArcFile,fname,x);
  425.    ArcFile[x] = '\0';
  426.  
  427.    old =  USERCFG.packcount;
  428.    while (old > 99) {
  429.     old -= 100;
  430.    }
  431.  
  432.    sprintf(temp,"%02d",old);
  433.    strcat(ArcFile,temp);
  434.    strcat(ArcFile,fext);
  435.  
  436.    while(stat(ArcFile,&fbuf) == 0 ) {     /* Uh oh. File exists! try next one */
  437.       strncpy(ArcFile,fname,x);
  438.       ArcFile[x] = '\0';
  439.       sprintf(temp,"%02d",++old);
  440.       strcat(ArcFile,temp);
  441.       strcat(ArcFile,fext);
  442.    }
  443.  
  444. #if defined OS2
  445.    _searchenv("Cmd.exe","PATH",fname);
  446. #else
  447.    _searchenv("Command.com","PATH",fname);
  448. #endif
  449.    if (*fname == '\0') {
  450.       aborterror(BADEXEC,NULL);
  451.    }
  452.  
  453.    sprintf(temp," /c %s",packer1->packexe);
  454.  
  455. /* All this junk to parse packer command line and build execute string */
  456.  
  457.    strcpy(temp1,packer1->packstring);
  458.    p = strtok(temp1,"%");        /* Up to 1st token */
  459.    strcat(temp,p);
  460.    strcat(temp," ");
  461.    p1 = strtok(NULL,"%");        /* Up to 2nd token */
  462.    if (tolower(p1[0]) == 'a')     /* Arcfile */
  463.       strcat(temp,ArcFile);
  464.    else strcat(temp,TxtFile);     /* Txtfile */
  465.    strcat(temp,p1+2);
  466.    strcat(temp," ");
  467.    p2 = strtok(NULL,"\0");
  468.    p1 = strtok(NULL,"\0");        /* Get rest of line */
  469.    if (tolower(p2[0]) == 'a') /* Arcfile */
  470.       strcat(temp,ArcFile);
  471.    else strcat(temp,TxtFile); /* Txtfile */
  472.  
  473.    sprintf(temp1,"\r\n\r\nNow packing up %d messages using %s \r\n",totmsgs,packer1->packname);
  474.    strout(temp1);
  475.    strout("Please wait while I call the packer program.....\r\n");
  476.  
  477.    printf("Command line: %s %s\n",fname,temp);
  478.    sprintf(temp1,"Maxmal%02d.swp",Task);
  479.  
  480. /* Execute the packer */
  481. #if defined OS2
  482.    strcpy(command,"");
  483.    strcat(command,fname);
  484.    strcat(command,temp);
  485.    x = system(command);
  486.    if (x == -1) {
  487. #else
  488.    x = swap(fname,temp,&exec_ret,temp1);
  489.    if (x || exec_ret) {
  490. #endif
  491.       strout("\r\nOh Oh! Packer had some kind of error\r\n");
  492.       strout("Shipping you back to Maximus!\r\n\r\n");
  493.       sprintf(temp,"Packer program error code: %d",exec_ret);
  494.       logit(temp,'!');
  495.       aborterror(BADEXEC,"Error executing packer program");
  496.    }
  497.  
  498.    if (StatName[0]) {
  499. /* We need to build command string again to add summary file */
  500.       sprintf(temp," /c %s",packer1->packexe);
  501.       strcpy(temp1,packer1->packstring);
  502.       p = strtok(temp1,"%");        /* Up to 1st token */
  503.       strcat(temp,p);
  504.       strcat(temp," ");
  505.       p1 = strtok(NULL,"%");        /* Up to 2nd token */
  506.       if (tolower(p1[0]) == 'a')     /* Arcfile */
  507.          strcat(temp,ArcFile);
  508.       else strcat(temp,StatName);     /* StatName */
  509.       strcat(temp,p1+2);
  510.       strcat(temp," ");
  511.       p2 = strtok(NULL,"\0");
  512.       p1 = strtok(NULL,"\0");        /* Get rest of line */
  513.       if (tolower(p2[0]) == 'a') /* Arcfile */
  514.          strcat(temp,ArcFile);
  515.       else strcat(temp,StatName); /* StatName */
  516.  
  517.       printf("Command line: %s %s\n",fname,temp);
  518.       sprintf(temp1,"Maxmal%02d.swp",Task);
  519.  
  520.    /* Execute the packer again */
  521. #if defined OS2
  522.       strcpy(command,"");
  523.       strcat(command,fname);
  524.       strcat(command,temp);
  525.       x = system(command);
  526.       if (x == -1) {
  527. #else
  528.       x = swap(fname,temp,&exec_ret,temp1);
  529.       if (x || exec_ret) {
  530. #endif
  531.          strout("\r\nOh Oh! Packer had some kind of error\r\n");
  532.          strout("Shipping you back to Maximus!\r\n\r\n");
  533.          sprintf(temp,"Packer program error code: %d",exec_ret);
  534.          logit(temp,'!');
  535.          aborterror(BADEXEC,"Error executing packer program");
  536.       }
  537.    }
  538.  
  539.  
  540.    if (packer1->viewstring) {         /* Call up packer again, showing viewline */
  541.       sprintf(temp," /c %s",packer1->packexe);
  542.       strcpy(temp1,packer1->viewstring);
  543.       p = strtok(temp1,"%");        /* Up to 1st token */
  544.       strcat(temp,p);
  545.       strcat(temp," ");
  546.       p1 = strtok(NULL,"%");        /* Up to 2nd token */
  547.       if (tolower(p1[0]) == 'a')         /* Arcfile */
  548.          strcat(temp,ArcFile);
  549.       strcat(temp," ");
  550.       p1 = strtok(NULL,"\0");        /* Get rest of line */
  551.       if (p1) strcat(temp,p1);
  552.  
  553.       printf("Command line: %s %s\n",fname,temp);
  554.       if (!IsLocal) {
  555.          old = dup(1);        /* old now refers to stdout */
  556.          ReDirect = TRUE;
  557.       }
  558.  
  559.       sprintf(temp1,"tmpmax%02d",Task);
  560.       new = _fsopen(temp1,"w+",SH_DENYNO);
  561.       if (new != NULL) {
  562.          if (!IsLocal)
  563.             dup2(fileno(new),1);        /* Reroute stdout to file */
  564.          sprintf(temp1,"Maxmal%02d.swp",Task);
  565.  
  566. /* Execute the ARCHIVE viewing command */
  567. #if defined OS2
  568.          strcpy(command,"");
  569.          strcat(command,fname);
  570.          strcat(command,temp);
  571.          x = system(command);
  572. #else
  573.          x = swap(fname,temp,&exec_ret,temp1);
  574. #endif
  575. /* Now display tmp file to user */
  576.          fclose(new);
  577.          sprintf(temp1,"tmpmax%02d",Task);
  578.          if (!IsLocal) {
  579.             show_file(temp1);
  580.             dup2(old,1);        /* Restore stdout */
  581.             ReDirect = FALSE;
  582.          }
  583.          unlink(temp1); 
  584.       }
  585.    }
  586.  
  587.    stat(ArcFile,&fbuf);
  588.    sprintf(temp,"Packing up %d messages using %s",totmsgs,packer1->packexe);
  589.    logit(temp,'#');
  590.    sprintf(temp,"Packed filesize is %lu bytes",fbuf.st_size);
  591.    logit(temp,'#');
  592.    PackDone = TRUE;        /* Say yes, it's accepted */
  593.    return(TRUE);
  594. }
  595.  
  596. /* Transmit user archive file, with designated protocol engine */
  597. int sendit(void)
  598. {
  599.    int x;
  600.    long calc;
  601.    char *p,*p1,*p2;
  602.    char portstr[3];
  603.    char command[81]; /* New Var for call to system() */
  604.  
  605.    x = USERCFG.protocol;        /* Save current protocol type */
  606.    if (!x)
  607.       get_proto(FALSE);
  608.  
  609.    proto1 = get_curprotolnk();
  610.    USERCFG.protocol = x;
  611.  
  612.    _searchenv(proto1->protoexe,"PATH",fname);
  613.  
  614.    strcpy(temp1,proto1->protostring);
  615.    p = strtok(temp1,"%");        /* Up to 1st token */
  616.    strcpy(temp,p);
  617.    p1 = strtok(NULL,"%");        /* Up to 2nd token */
  618.  
  619. /* Parse protocol string and build command line */
  620.    while (*p1) {
  621.       switch (tolower(p1[0])) {
  622.          case 'a':
  623.             strcat(temp,ArcFile);
  624.             break;
  625.  
  626.          case 'p':
  627. #if defined OS2
  628.             sprintf(portstr,"%d",hfComHandle);        /* Com1 or com2 */
  629. #else
  630.             sprintf(portstr,"%d",prm.com_port + 1);    /* Com1 or com2 */
  631. #endif
  632.             strcat(temp,portstr);
  633.             break;
  634.  
  635.          case 'b':
  636.             sprintf(portstr,"%d",LastUser.flag);        /* Baudrate */
  637.             strcat(temp,portstr);
  638.             break;
  639.  
  640.          default:
  641.             strcat(temp,p1);
  642.             break;
  643.       }
  644.       p1++;
  645.       if (*p1)
  646.          strcat(temp,p1);
  647.       strcat(temp," ");
  648.       if (*p1) /* Only if (*p1) */
  649.         p1 = strtok(NULL,"%");        /* Up to 2nd token */
  650.    }
  651.  
  652.    stat(ArcFile,&fbuf);
  653.    sprintf(temp1,"\r\nPacked message file (%s) is %lu bytes\r\n",ArcFile,fbuf.st_size);
  654.    strout(temp1);
  655.    calc = (long) get_kminute();
  656.    if (calc) {
  657.       calc = fbuf.st_size / calc;
  658.       if (calc <= 0)
  659.          calc = 1;
  660.    }
  661.    else calc = 1;
  662.    sprintf(temp1,"It will take about %ld minute(s) to download\r\n",calc);
  663.    strout(temp1);
  664.    strout("Are you sure you want to download it? [y,N] -> ");
  665.    strin(temp1);
  666.    strout("\r\n");
  667.    p1 = strtok(temp1," \r\n\;");
  668.    if (!p1)
  669.       return FALSE;
  670.    if (toupper(*p1) == 'Y' || toupper(*p1) == 'H') {
  671.  
  672.       if (IsLocal) {
  673.          USERCFG.packcount++;
  674.          return TRUE;
  675.       }
  676.  
  677.       if (toupper(*p1) == 'H')
  678.          Hangup = TRUE;
  679.  
  680.       sprintf(temp1,"Transmit %s using %s",ArcFile,proto1->protoname);
  681.       logit(temp1,'=');
  682.  
  683.       sprintf(temp1,"Prepare to download %s using %s \r\n",ArcFile,proto1->protoname);
  684.       strout(temp1);
  685.  
  686.       printf("Command line: %s %s\n",fname,temp);
  687.       sprintf(temp1,"Maxmal%02d.swp",Task);
  688.  
  689.    /* Execute the protocol engine */
  690. #if defined OS2
  691.       strcpy(command,"");
  692.       strcat(command,fname);
  693.       strcat(command,temp);
  694.       x = system(command);
  695.       if (x == -1) {
  696. #else
  697.       fos_deinit(prm.com_port);        /* Turn off port from us */
  698.       x = swap(fname,temp,&exec_ret,temp1);
  699.       fos_init(prm.com_port,TRUE,(char _far *) &break_flag); /* Restart port */
  700.       if (x || exec_ret) {
  701. #endif
  702.          strout("\r\nOh Oh! Xfer program had some kind of error\r\n");
  703.          sprintf(temp,"Error code from xfer program: %d",exec_ret);
  704.          logit(temp,'!');
  705.          strout("Popping you back to Maximus!\r\n\r\n");
  706.          aborterror(BADEXEC,"Error executing xfer program");
  707.       }
  708.       strout("\r\nNo errors occured during transfer.\r\n");
  709.       USERCFG.packcount++;
  710.       return(TRUE);
  711.    }
  712.    else return FALSE;
  713. }
  714.  
  715. unsigned get_msgcount(void)
  716. {
  717.    unsigned x;
  718.  
  719.    x = 0;
  720.    msgupd = MSGUPD_1;
  721.    while (msgupd) {
  722.       x += msgupd->msgcount;
  723.       msgupd = msgupd->next;
  724.    }
  725.    return x;
  726. }
  727.  
  728. void newuser_help(void)
  729. {
  730.    if(show_file(NewHelpFil) >= 0) {
  731. #if defined OS2
  732.       pause();
  733.       nl();
  734. #else
  735.       strout("\r\n\r\nPress any key to get the heck outa here!");
  736.       chrin();
  737.       strout("\r\n");
  738. #endif
  739.    }
  740. }
  741.  
  742.  
  743. int show_file(char *strng)
  744. {
  745.    FILE *newfile;
  746.    int linecount;
  747.    char ltemp[81];
  748.  
  749.    if (strng[0] == '\0')
  750.       return(-1);        /* No help file */
  751.  
  752.    newfile = _fsopen(strng,"r",SH_DENYNO);        /* Open in read only, text mode */
  753.    if (newfile == NULL) {
  754.       sprintf(ltemp,"Error opening %s file for user.",strng);
  755.       logit(ltemp,'!');
  756.       return(-1);        /* Just skip it */
  757.    }
  758.  
  759. #if defined OS2
  760.    fclose(newfile);
  761.    printfile(strng);
  762. #else
  763.    linecount = 0;
  764.    while (!feof(newfile)) {
  765.       fgets(ltemp,80,newfile);
  766.       strout(ltemp);
  767.       strout("\r");
  768.       if (++linecount > 20) {        /* Turn on more for paging */
  769.          strout("\r\n\r\nPress any key for more");
  770.          chrin();
  771.          strout("\r\n");
  772.          linecount = 0;
  773.       }
  774.    }
  775.    fclose(newfile);
  776. #endif
  777.    return(0);
  778. }
  779.  
  780.  
  781. struct msgupd_st *build1_area(int msgnum)
  782. {
  783.    int keys,mask,x;
  784.    long lerr;
  785.    struct msgupd_st *this;
  786.  
  787.    rewind(afile);        /* Start at top again */
  788.    fread(&AREA,astrlen,1,afile);
  789.    while (!feof(afile) && msgnum != atoi(AREA.name))
  790.       fread(&AREA,astrlen,1,afile);
  791.    if (feof(afile))
  792.       return NULL;
  793.  
  794.    mask = 1;
  795.    for (x=0; x < (msgnum % 16); x++)
  796.       mask = mask << 1;
  797.    keys = AREA.msglock;
  798.    if (!keys)         /* No locks for this area */
  799.       keys = TRUE;
  800.    else {        /* User must have same locks */
  801.       keys = AREA.msglock & LastUser.key;
  802.       if (keys != AREA.msglock)
  803.          keys = FALSE;
  804.    }
  805.    if (AREA.msgpath[0] && (LastUser.priv >= AREA.msgpriv) && keys) {
  806.       this = (struct msgupd_st *) malloc(sizeof(struct msgupd_st));
  807.       if (this == NULL)
  808.          aborterror(BADALLOC,NULL);
  809.       this->areano = atoi(AREA.name);
  810.       strcpy(this->msgpath,AREA.msgpath);
  811.       this->update = FALSE;
  812.       switch (AREA.area_type) {
  813.          case 0:        /* Fido 1.msg style */
  814.             this->himsg = find_fidohigh(AREA.msgpath);
  815.             break;
  816.  
  817.          default:
  818.             this->himsg = find_fidohigh(AREA.msgpath);
  819.             break;
  820.       }
  821.       this->areaindex = ftell(afile) - (long) astrlen;    /* Position */
  822.       this->next = NULL;
  823.       this->startmsg = 0;
  824.       this->readmsgs = 0;
  825.       if (LastUser.lastread_ptr)
  826.          sprintf(temp1,"%slastread.bbs",AREA.msgpath);
  827.       else sprintf(temp1,"%slastread",AREA.msgpath);  
  828.       x = sopen(temp1,O_RDONLY | O_BINARY,SH_DENYNO,S_IREAD);    /* Poke into the lastread file */
  829.       if (x != -1)  {
  830.          lerr = lseek(x,(long)(LastUser.lastread_ptr * sizeof(int)),SEEK_SET);
  831.          if (lerr != -1L) {
  832.             read(x,(char *) &this->startmsg,sizeof(int));        /* Get last message read */
  833.             if (this->startmsg > this->himsg) 
  834.                this->startmsg = this->himsg;
  835.             this->msgcount = this->himsg - this->startmsg;
  836.             if (this->msgcount < 0)
  837.                this->msgcount = 0;
  838.          }
  839.          close(x);
  840.       }
  841.    }
  842.    return this;
  843. }
  844.  
  845.  
  846. void show_opts(void)
  847. {
  848.    printf("\nMaxmail Version: %2.2f by Craig Derouen\n",Version);
  849.    printf("Last compiled: %s\n",__TIMESTAMP__);
  850.    printf("Command line options:\n");
  851.    printf("\t /A: Adjust for time user is online\n");
  852.    printf("\t /C: Specify alt Maxmail USER config file\n");
  853.    printf("\t /L: Turns on local mode. No fossil handling\n");
  854.    printf("\t /N: Don't delete archive pack on exit (Use with local mode)\n");
  855.    printf("\t /P: Specify alt Max.prm file\n");
  856.    printf("\t /T: Specify Task number. Tells Maxmail we are running multiuser\n");
  857.    printf("\t /U: Don't update LASTREAD message pointers\n");
  858. #if defined OS2
  859.    printf("\t /G: Turns on Ansi Graphics.\n");
  860.    printf("\t /C: Passes the OS/2 Port Handle from a Spawn port\n");
  861.    printf("\t /V: Passes a Number (7=mono,15=Color) for Monitor Type\n");
  862.    printf("\t /S: Passes a Number (1) for High Speed Modems\n");
  863.    printf("\t /W: Passes a Number (1) for Locked High Speed Modems\n");
  864. #endif
  865.    printf("\t {Filename} use this file as alt Maxmail.cfg file\n");
  866.    exit(0);
  867. }
  868.