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

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    Misc.c      :Miscellaneous routines for MaxMail                       */
  4. /*                                                                          */
  5. /****************************************************************************/
  6.  
  7. #include "MaxMail.h"
  8. #include <sys\timeb.h>
  9.  
  10. struct user_cfg uscfg;
  11. int do_reset;
  12.  
  13. void signon()
  14. {
  15.    char *p;
  16.  
  17.    strout("MaxMail - a MAXIMUS mail/message archiver utility by Craig Derouen\r\n");
  18.    sprintf(temp,"\t *** Version %.2f (beta) ***\r\n",Version);
  19.    strout(temp);
  20.    strcpy(temp1,LastUser.name);
  21.    p = strtok(temp1," ");        /* Fetch 1st name only, shows how friendly we are */
  22.    if (USERCFG.packcount) {
  23.       sprintf(temp,"\r\nWelcome back to MaxMail, %s.\r\n",p);
  24.       strout(temp);
  25.       sprintf(temp,"You have had %d previous successful downloads. Good luck!\r\n",USERCFG.packcount);
  26.       strout(temp);
  27.    }
  28.    else {
  29.       sprintf(temp,"\r\nWelcome to MaxMail, %s.\r\n",p);
  30.       strout(temp);
  31.   }
  32. }
  33.  
  34. void menu(void)
  35. {
  36.    char resp;
  37.    char *p1;
  38.    int choice;
  39.    int good;
  40.    unsigned msgs;
  41.    struct msgupd_st *msgupd;
  42.  
  43.    choice = TRUE;
  44.    PackDone = FALSE;
  45.    while (choice) {
  46.       msgs = get_msgcount();
  47.       if (msgs) {
  48.          if (PackDone){
  49.             msgs = msgs < MaxMsgs ? msgs : MaxMsgs;
  50.             sprintf(temp,"\r\nYou have %u messages in %s. Select [T]ransmit to download\r\n",msgs,ArcFile);
  51.             strout(temp);
  52.             strout("or select [R]eset,[A]dd or [D]elete to scan more messages/areas\r\n");
  53.             strout("\r\nSelect a choice\r\n");
  54.          }
  55.          else {
  56.             sprintf(temp,"\r\n\r\nYou have %u new messages in areas:\r\n",msgs);
  57.             strout(temp);
  58.             show_areas(TRUE);
  59.             if (msgs > MaxMsgs) {
  60.                sprintf(temp,"\r\nSorry, but only %u new messages can be packed up during this run.\r\n",MaxMsgs);
  61.                strout(temp);
  62.                sprintf(temp,"MaxMail will pack up the first %u messages it finds.\r\n",MaxMsgs);
  63.                strout(temp);
  64.             }
  65.             strout("\r\nPress ENTER to capture new messages, or select a choice\r\n");
  66.          }
  67.       }
  68.       else {
  69.          strout("\r\n Sorry, but you have NO new messages to capture\r\n");
  70.          strout(" in the current message areas you have selected.\r\n");
  71.          strout("\r\nSelect a choice\r\n");
  72.       }
  73.       strout("-------------------------------------------------------\r\n");
  74.       strout("[C]hange configuration   [L]ist configuration\r\n");
  75.       strout("[A]dd message areas      [D]elete message areas\r\n");
  76.       strout("[R]eset message pointers [S]can for new messages\r\n");
  77.       strout("[Q]uit back to Maximus\r\n");
  78.       if (PackDone)
  79.          strout("[T]ransmit archive\r\n");
  80.       if (LastUser.priv >= SYSOP) {
  81.          strout("[U]ser stats (Sysop function)\r\n");
  82.       }
  83.       strout("\r\n");
  84.       good = FALSE;
  85.       while (!good) {
  86.          timeremain();
  87.          strout("Choice --> ");
  88.          strin(temp);
  89.          p1 = strtok(temp," \t\r\n");
  90.          if (p1) 
  91.             resp = toupper(*p1);
  92.          else resp = 'S';        /* Scan messages as default */
  93.          good = TRUE;
  94.          switch (resp) {
  95.             case 'S':
  96.                if (msgs) {
  97.                   if (scan_msgs() && totmsgs) {
  98.                      if(Packit()) {
  99.                         if (sendit()) {
  100.                            PackDone = FALSE;
  101.                            choice = FALSE;
  102.                         }
  103.                      }
  104.                   }
  105.                }
  106.                else {
  107.                   if (!IsLocal)
  108.                      strout("\r\n");
  109.                   good = FALSE;
  110.                }
  111.                break;
  112.  
  113.             case 'C':
  114.                getconfig();
  115.                break;
  116.  
  117.             case 'A':
  118.                add_msgareas();
  119.                break;
  120.  
  121.             case 'D':
  122.                del_msgareas();
  123.                break;
  124.  
  125.             case 'R':
  126.                edit_msgptrs();
  127.                break;
  128.  
  129.             case 'T':
  130.                if (PackDone) {
  131.                   if (sendit()) {
  132.                      PackDone = FALSE;
  133.                      choice = FALSE;
  134.                   }
  135.                }
  136.                else good = FALSE;
  137.                break;
  138.  
  139.             case 'L':
  140.                list_config();
  141.                break;
  142.  
  143.             case 'Q':
  144.                choice = FALSE;
  145.                break;
  146.  
  147.             case 'U':
  148.                if (LastUser.priv >= SYSOP) 
  149.                   Sys_menu();        /* We won't return from Sysop menu */
  150.                else good = FALSE;
  151.                break;
  152.  
  153.             default:
  154.                good = FALSE;
  155.                if (!IsLocal)
  156.                   strout("\r\n");
  157.                break;
  158.          }
  159.       }
  160.    }
  161.    if (PackDone) {        /* Pack file aborted */
  162.       if (LogMode == VERBOSE) logit("User aborted message pack",'~');
  163.       unlink(ArcFile);        /* Erase it and start over */
  164.       msgupd = MSGUPD_1;
  165.       while (msgupd) {
  166.          msgupd->update = FALSE;
  167.          msgupd = msgupd->next;
  168.       }
  169.    }
  170.    return;
  171. }
  172.  
  173. void display_packers(void)
  174. {
  175.    int x,col;
  176.    struct packer_st *pack1;
  177.  
  178.    pack1 = PACKER_1;
  179.  
  180.    col = 0;
  181.    for (x = 0; x < totpackers; x++) {
  182.       sprintf(temp,"[%d]%s",x +1,pack1->packname);
  183.       while (strlen(temp) < 32) 
  184.          strcat(temp," ");
  185.       strout(temp);
  186.       if (col++) {
  187.          strout("\r\n");
  188.          col = 0;
  189.       }
  190.       pack1 = pack1->next;
  191.    }
  192.    if (col)
  193.       strout("\r\n");
  194.    return;
  195. }
  196.  
  197. void display_protos(void)
  198. {
  199.    int x,col;
  200.    struct proto_st *proto1;
  201.  
  202.    proto1 = PROTO_1;
  203.  
  204.    col = 0;
  205.    for (x = 0; x < totprotocols; x++) {
  206.       sprintf(temp,"[%d]%s",x +1,proto1->protoname);
  207.       while (strlen(temp) < 32) 
  208.          strcat(temp," ");
  209.       strout(temp);
  210.       if (col++) {
  211.          strout("\r\n");
  212.          col = 0;
  213.       }
  214.       proto1 = proto1->next;
  215.    }
  216.    if (col)
  217.       strout("\r\n");
  218.    return;
  219. }
  220.  
  221. /* Find the user in the config file */
  222.  
  223. int find_config(int handle,char *name,struct user_cfg *uscfg)
  224. {
  225.    int x,y;
  226.  
  227.    x = 0;
  228.    do_reset = FALSE;
  229.    lseek(handle,0L,SEEK_SET);        /* Rewind to begginning */
  230.    while (1) {
  231.       y = read(handle,(char *) uscfg,sizeof(struct user_cfg));
  232.       if( y < sizeof(struct user_cfg)) 
  233.          return(-1);
  234.       if (strcmpi(uscfg->name,name) == 0) { /* It's found! */
  235.          return(x);
  236.       }
  237.       x++;        /* Try next user */
  238.    }
  239. }
  240.  
  241. /* Find a blank slot in the config file */
  242.  
  243.  
  244. int find_blconfig(int handle)
  245. {
  246.    int x,y;
  247.  
  248.    x = 0;
  249.    lseek(handle,0L,SEEK_SET);        /* Rewind to begginning */
  250.    while (1) {
  251.       y = read(handle,(char *) &uscfg,sizeof(struct user_cfg));
  252.       if( y < sizeof(struct user_cfg)) 
  253.          return(-1);
  254.       if (uscfg.isused == 0)  /* It's found! */
  255.          return(x);
  256.       x++;        /* Try next user */
  257.    }
  258. }
  259. /* Changed name so to not conflict with Maxi timeon var. */
  260. int Timeon(void)
  261. {
  262.    clock_t elapsed;
  263.    int t;
  264.  
  265.    elapsed = clock();
  266.    elapsed = startsecs - elapsed;
  267.    elapsed = elapsed / CLOCKS_PER_SEC;        /* Compute seconds */
  268.    t = (int) (elapsed / 60);
  269.    if (t < 0)
  270.       t = t * (-1);
  271.    return(t);
  272. }
  273.  
  274. void timeremain(void)
  275. {
  276.    int x;
  277.    char tstr[18];
  278.  
  279.    x = LastUser.timeremaining - Timeon();        /* Fetch # of minutes on */
  280.    sprintf(tstr,"{%d min left} ",x);
  281.    strout(tstr);   
  282. }
  283.  
  284. struct proto_st *get_curprotolnk(void)
  285. {
  286.    int x;
  287.    struct proto_st *proto1;
  288.  
  289.    proto1 = PROTO_1;        /* Point to first 1! */
  290.    for (x = 1; x < USERCFG.protocol; x++) {
  291.       proto1 = proto1->next;
  292.    }
  293.    return(proto1);
  294. }
  295.  
  296. struct packer_st *get_curpacklnk(void)
  297. {
  298.    int x;
  299.    struct packer_st *pack1;
  300.  
  301.    if (!USERCFG.packer)
  302.       return NULL;
  303.  
  304.    pack1 = PACKER_1;        /* Point to first 1! */
  305.    for (x = 1; x < USERCFG.packer; x++) {
  306.       pack1 = pack1->next;
  307.    }
  308.    return(pack1);
  309. }
  310.  
  311. int get_kminute(void)
  312. {
  313.    int x,y;
  314.  
  315.    x = LastUser.flag / 10;         /* Compute cps */
  316.    y = LastUser.flag;
  317.    switch (LastUser.flag) {
  318.       case 300:
  319.          x -=  (x * 0.08f);        /* Slop off 8% */
  320.          break;
  321.  
  322.       case 1200:
  323.          x -=  (x * 0.18f);        /* Slop off 18% */
  324.          break;
  325.  
  326.       case 2400:
  327.          x -=  (x * 0.22f); 
  328.          break;
  329.  
  330.       case 4800:
  331.          x -=  (x * 0.25f); 
  332.          break;
  333.  
  334.       case 9600:
  335.          x -= (x * 0.28f);     
  336.          break;
  337.  
  338.       default:
  339.          x -= (x * 0.28f);     
  340.          break;
  341.    }
  342.    x *= 60;        /* Compute to minutes */
  343.    return x;
  344. }
  345.  
  346. /* The sysop menu */
  347. void Sys_menu(void)
  348. {
  349.    int x,choice;
  350.    char *p1;
  351.    char resp;
  352.  
  353.    choice = TRUE;
  354.  
  355.    while (choice) {
  356.       strout("\r\n\r\nSysop menu. Select a choice.\r\n");
  357.       strout("----------------------------\r\n");
  358.       strout("[D]elete a user          [F]orce reconfig\r\n");
  359.       strout("[L]ist users             [K]ill old users\r\n");
  360.       strout("[E]xamine user msgareas  [Q]uit\r\n");
  361.       strout("\r\n");
  362.       timeremain();
  363.       strout("Choice --> ");
  364.       strin(temp);
  365.       if (!IsLocal)
  366.          strout("\r\n");
  367.       p1 = strtok(temp," \t\r\n");
  368.       if (p1) {
  369.          resp = toupper(*p1);
  370.          switch (resp) {
  371.             case 'D':        /* Delete single users */
  372.                delete_users();
  373.                break;
  374.  
  375.             case 'E':        /* List user message areas */
  376.                examine_user();
  377.                break;
  378.  
  379.             case 'F':        /* Force all users to re-do configuration */
  380.                if (!do_reset) {
  381.                   strout("This will force all users to re-do their configuration.\r\n");
  382.                   strout("Are you sure you want this? (y,N) ---> ");
  383.                   strin(temp);
  384.                   if (!IsLocal)
  385.                      strout("\r\n");
  386.                   p1 = strtok(temp," \t\r\n");
  387.                   if (p1) {
  388.                      resp = toupper(*p1);
  389.                      if (resp == 'Y') 
  390.                         do_reset = reset_config();
  391.                   }
  392.                }
  393.                else strout("\r\nYou have already reset all the users during this session.\r\n");
  394.                break;
  395.  
  396.             case 'L':        /* User stats */
  397.                user_stats();
  398.                break;
  399.  
  400.             case 'K':        /* Kill older users */
  401.                strout("\r\nThis will delete all MaxMail users who are no longer active on your bbs\r\n");
  402.                strout("Are you sure you want this? (y,N) ---> ");
  403.                strin(temp);
  404.                strout("\r\n");
  405.                p1 = strtok(temp," \t\r\n");
  406.                if (p1) {
  407.                   resp = toupper(*p1);
  408.                   if (resp == 'Y') 
  409.                      kill_oldusers();
  410.                }
  411.                break;
  412.  
  413.             case 'Q':        /* Quit */
  414.                choice = FALSE;
  415.                break;
  416.          }
  417.       }
  418.    }
  419.    strout("\r\n\r\n");
  420.    return;
  421. }
  422.  
  423. void show_areas(int flag)
  424. {
  425.    int x,col,msgpos;
  426.    struct msgupd_st *msgupd;
  427.  
  428.    msgupd = MSGUPD_1;
  429.    col = TRUE;
  430.    while (col && msgupd) {
  431.       msgpos = 0;
  432.       while (msgupd && msgpos < 20) {
  433.          fseek(afile,msgupd->areaindex,SEEK_SET);
  434.          fread(&AREA,astrlen,1,afile);
  435.          if ((flag && msgupd->msgcount) || !flag) {
  436.             strncpy(temp1,AREA.msginfo,24);
  437.             temp1[24] = 0;
  438.             sprintf(temp,"  [%03d] %s",msgupd->areano,temp1);
  439.             while (strlen(temp) < 32) 
  440.                strcat(temp," ");
  441.             sprintf(temp1," (%u new messages)\r\n",msgupd->msgcount);
  442.             strcat(temp,temp1);
  443.             strout(temp);
  444.          }
  445.          msgupd = msgupd->next;
  446.          msgpos++;
  447.       }
  448.       if (col && msgupd) {
  449.          strout("\r\nPress ESC to abort or any other key to continue");
  450.          x = chrin();
  451.          strout("\r\n");
  452.          if (x == 0x1b)
  453.             col = FALSE;
  454.       }
  455.    }
  456. }
  457.  
  458. int is_selarea(int msgnum)
  459. {
  460.    struct msgupd_st *msgupd;
  461.    int x;
  462.  
  463.    msgupd = MSGUPD_1;
  464.    x = 1;
  465.    while (msgupd) {
  466.       if (msgupd->areano == (word) msgnum)
  467.          return x;        /* Return linked list number */
  468.       msgupd = msgupd->next;
  469.       x++;
  470.    }
  471.    return(FALSE);
  472. }
  473.  
  474. void update_msgs(void)
  475. {
  476.    int x,first;
  477.    struct msgupd_st *msgupd;
  478.    char LastRdFile[65];
  479.  
  480.    msgupd = MSGUPD_1;
  481.    first = TRUE;
  482.    while (msgupd) {
  483.       if (msgupd->update) {
  484.          if (LastUser.lastread_ptr)
  485.             sprintf(LastRdFile,"%slastread.bbs",msgupd->msgpath);
  486.          else sprintf(LastRdFile,"%slastread",msgupd->msgpath);  
  487.          x = sopen(LastRdFile,O_RDWR | O_BINARY,SH_DENYNO,S_IWRITE);    /* Poke into the lastread file */
  488.          if (x == -1) {        /* Couldn't find! We must have it */
  489.             msgupd = msgupd->next;
  490.             continue;
  491.          }
  492.          if (lseek(x,(long)(LastUser.lastread_ptr * sizeof(int)),SEEK_SET) == -1)
  493.             close(x);
  494.          else {
  495.             msgupd->startmsg += msgupd->readmsgs;
  496.             write(x,(char *) &msgupd->startmsg,sizeof(int));
  497.             close(x);
  498.             if (first) {
  499.                strout("\r\nYour lastread message pointers are now being updated\r\n");
  500.                first = FALSE;
  501.             }
  502.          }
  503.       }
  504.       msgupd = msgupd->next;
  505.    }
  506. }
  507.  
  508. struct msgupd_st *find_area(int msgnum)
  509. {
  510.    struct msgupd_st *msgst;
  511.  
  512.    msgst = MSGUPD_1;
  513.    while (msgst) {
  514.       if (msgst->areano == (word) msgnum)
  515.          break;
  516.       msgst = msgst->next;
  517.    }
  518.    return msgst;
  519. }
  520.  
  521. void delay_s(unsigned n)
  522. /* n = Number of seconds */
  523. {
  524.    n *= 1000;        /* Convert to seconds */
  525.    delay_ms(n);
  526. }
  527.  
  528. void delay_ms(unsigned n)
  529.  /* milleseconds to delay */
  530. {
  531.     struct timeb timebuf;
  532.     long end_time;
  533.     unsigned end_millitm;
  534.  
  535.     /* get current time and calculate ending time */
  536.     ftime(&timebuf);
  537.     end_time = timebuf.time + (n / 1000);
  538.     end_millitm = timebuf.millitm + (n % 1000);
  539.     if (end_millitm >= 1000) {
  540.         ++end_time;
  541.         end_millitm -= 1000;
  542.     }
  543.  
  544.     /* loop until ending time reached */
  545.     do ftime(&timebuf);
  546.     while (timebuf.time < end_time ||
  547.           (timebuf.time == end_time && timebuf.millitm < end_millitm));
  548. }
  549.  
  550. int find_realuser(char *name,int handle)
  551. {
  552.    int x;
  553.    struct _usr USER;
  554.  
  555.    lseek(handle,0L,SEEK_SET);
  556.    x = read(handle,(char *) &USER,user_slen);
  557.    while (x == user_slen) {
  558.       if (strcmpi(name,USER.name) == 0) {
  559.          if (USER.flag & UFLAG_deleted)
  560.             return(FALSE);        /* It's there but it is deleted! */
  561.          else return(TRUE);
  562.       }
  563.       x = read(handle,(char *) &USER,user_slen);
  564.    }
  565.    return(FALSE);        /* Couldn't find it */
  566. }
  567.  
  568. /* Test to see if this message area is a skip one, if so, return TRUE */
  569. int isskiparea(int msgarea)
  570. {
  571.    int x;
  572.  
  573.    if (LastUser.priv >= SYSOP || msgarea == 0)
  574.       return FALSE;        /* Sysops should always be able to get ALL areas */
  575.    x = 0;
  576.    while (SkipAreas[x]) {
  577.       if (*SkipAreas[x] == msgarea)
  578.          return TRUE;
  579.       x++;
  580.    }
  581.    return FALSE;
  582. }
  583.