home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / MISC / LO241SRV.ZIP / READMAIL.C < prev    next >
C/C++ Source or Header  |  1998-05-17  |  26KB  |  911 lines

  1.  
  2. // LoraBBS Version 2.41 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <dir.h>
  23. #include <io.h>
  24. #include <fcntl.h>
  25. #include <ctype.h>
  26. #include <sys\stat.h>
  27.  
  28. #include <cxl\cxlvid.h>
  29. #include <cxl\cxlwin.h>
  30.  
  31. #include "lsetup.h"
  32. #include "sched.h"
  33. #include "msgapi.h"
  34. #include "externs.h"
  35. #include "prototyp.h"
  36. #include "quickmsg.h"
  37. #include "pipbase.h"
  38.  
  39. void m_print2(char *format, ...);
  40.  
  41. #define MAX_MAIL_BUFFER   20
  42.  
  43. static int last_read_system;
  44.  
  45. static int quick_index (int, int *, int *, int);
  46. static int gold_index (int, int *, int *, int);
  47. static int pipbase_index (int, int *, int *, int);
  48. static int fido_index (int, int *, int *, int);
  49. static int squish_index (int, int *, int *, int);
  50.  
  51. static char *rotate = "|/-\\", rotatepos;
  52.  
  53. int scan_mailbox()
  54. {
  55.    int i, z, *area_msg, *area_num, maxareas, line;
  56.    char filename[80];
  57.    struct _sys tsys;
  58.  
  59.    sprintf (filename, SYSMSG_PATH, config->sys_path);
  60.    if ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  61.       return (0);
  62.  
  63.    z = (int)(filelength (i) / SIZEOF_MSGAREA);
  64.  
  65.    if ((area_msg = (int *)malloc (z * sizeof (int))) == NULL)
  66.       return (0);
  67.  
  68.    if ((area_num = (int *)malloc (z * sizeof (int))) == NULL) {
  69.       free (area_msg);
  70.       return (0);
  71.    }
  72.  
  73.    z = 0;
  74.    while (read (i, &tsys, SIZEOF_MSGAREA) == SIZEOF_MSGAREA)
  75.       area_num[z++] = tsys.msg_num;
  76.    maxareas = z;
  77.  
  78.    close (i);
  79.  
  80.    memset (area_msg, 0, sizeof (int) * z);
  81.    z = 1;
  82.  
  83.    if (msg_list != NULL)
  84.       free (msg_list);
  85.  
  86.    if ((msg_list = (struct _msg_list *)malloc (MAX_PRIV_MAIL * sizeof (struct _msg_list))) == NULL)
  87.       return (0);
  88.  
  89.    m_print (bbstxt[B_CHECK_MAIL]);
  90.  
  91.    rotatepos = 0;
  92.    m_print (" \b%c", rotate[rotatepos]);
  93.    rotatepos = (rotatepos + 1) % 4;
  94.  
  95.    z = fido_index (z, area_msg, area_num, maxareas);
  96.    z = quick_index (z, area_msg, area_num, maxareas);
  97.    z = gold_index (z, area_msg, area_num, maxareas);
  98.    z = pipbase_index (z, area_msg, area_num, maxareas);
  99.    z = squish_index (z, area_msg, area_num, maxareas);
  100.  
  101.    max_priv_mail = z - 1;
  102.    last_mail = 0;
  103.    last_read_system = 0;
  104.  
  105.    if (z == 1) {
  106.       free (area_num);
  107.       free (area_msg);
  108.  
  109.       m_print(bbstxt[B_NO_MAIL_TODAY]);
  110.       press_enter();
  111.  
  112.       return (0);
  113.    }
  114.  
  115.    num_msg = last_msg = max_priv_mail;
  116.    first_msg = 0;
  117.  
  118.    m_print(bbstxt[B_THIS_MAIL]);
  119.    m_print(bbstxt[B_THIS_MAIL_UNDERLINE]);
  120.    line = 3;
  121.  
  122.    for (i = 0; i < maxareas; i++) {
  123.       if (!area_msg[i] || !read_system2 (area_num[i], 1, &tsys))
  124.          continue;
  125.  
  126.       m_print (bbstxt[B_MAIL_LIST_FORMAT], tsys.msg_name, area_msg[i]);
  127.       if ((line = more_question (line)) == 0)
  128.          break;
  129.    }
  130.  
  131.    free (area_num);
  132.    free (area_msg);
  133.  
  134.    m_print (bbstxt[B_READ_MAIL_NOW]);
  135.    i = yesno_question (DEF_YES);
  136.  
  137.    m_print (bbstxt[B_ONE_CR]);
  138.    return (i);
  139. }
  140.  
  141. void mail_read_forward(pause)
  142. int pause;
  143. {
  144.    int start, i;
  145.  
  146.    start = last_mail;
  147.  
  148.    if (start < 0)
  149.       start = 1;
  150.  
  151.    if (start < max_priv_mail)
  152.       start++;
  153.    else {
  154.       m_print(bbstxt[B_NO_MORE_MESSAGE]);
  155.       return;
  156.    }
  157.  
  158.    if (last_read_system != msg_list[start].area) {
  159.       read_system(msg_list[start].area, 1);
  160.       usr.msg = 0;
  161.       if (sys.quick_board || sys.gold_board)
  162.          quick_scan_message_base (sys.quick_board, sys.gold_board, msg_list[start].area, 1);
  163.       else if (sys.pip_board)
  164.          pip_scan_message_base (msg_list[start].area, 1);
  165.       else if (sys.squish)
  166.          squish_scan_message_base (msg_list[start].area, sys.msg_path, 1);
  167.       else
  168.          scan_message_base(msg_list[start].area, 1);
  169.       usr.msg = last_read_system = msg_list[start].area;
  170.       num_msg = last_msg = max_priv_mail;
  171.       first_msg = 0;
  172.    }
  173.  
  174.    for (;;) {
  175.       if (sys.quick_board || sys.gold_board)
  176.          i = quick_read_message(msg_list[start].msg_num, pause, start);
  177.       else if (sys.pip_board)
  178.          i = pip_msg_read(sys.pip_board, msg_list[start].msg_num, 0, pause, start);
  179.       else if (sys.squish)
  180.          i = squish_read_message (msg_list[start].msg_num, pause, start);
  181.       else
  182.          i = read_message(msg_list[start].msg_num, pause, start);
  183.  
  184.       if (!i) {
  185.          if (start < last_msg) {
  186.             start++;
  187.             if (last_read_system != msg_list[start].area) {
  188.                read_system(msg_list[start].area, 1);
  189.                if (sys.quick_board || sys.gold_board)
  190.                   quick_scan_message_base (sys.quick_board, sys.gold_board, msg_list[start].area, 1);
  191.                else if (sys.pip_board)
  192.                   pip_scan_message_base (msg_list[start].area, 1);
  193.                else if (sys.squish)
  194.                   squish_scan_message_base (msg_list[start].area, sys.msg_path, 1);
  195.                else
  196.                   scan_message_base(msg_list[start].area, 1);
  197.                last_read_system = msg_list[start].area;
  198.                num_msg = last_msg = max_priv_mail;
  199.                first_msg = 0;
  200.             }
  201.          }
  202.          else
  203.             break;
  204.       }
  205.       else
  206.          break;
  207.    }
  208.  
  209.    lastread = msg_list[start].msg_num;
  210.    last_mail = start;
  211. }
  212.  
  213. void mail_read_backward(pause)
  214. int pause;
  215. {
  216.    int start, i;
  217.  
  218.    start = last_mail;
  219.  
  220.    if (start > 1)
  221.       start--;
  222.    else {
  223.       m_print(bbstxt[B_NO_MORE_MESSAGE]);
  224.       return;
  225.    }
  226.  
  227.    if (last_read_system != msg_list[start].area) {
  228.       read_system(msg_list[start].area, 1);
  229.       usr.msg = 0;
  230.       if (sys.quick_board || sys.gold_board)
  231.          quick_scan_message_base (sys.quick_board, sys.gold_board, msg_list[start].area, 1);
  232.       else if (sys.pip_board)
  233.          pip_scan_message_base (msg_list[start].area, 1);
  234.       else if (sys.squish)
  235.          squish_scan_message_base (msg_list[start].area, sys.msg_path, 1);
  236.       else
  237.          scan_message_base(msg_list[start].area, 1);
  238.  
  239.       usr.msg = last_read_system = msg_list[start].area;
  240.       num_msg = last_msg = max_priv_mail;
  241.       first_msg = 0;
  242.    }
  243.  
  244.    for (;;) {
  245.       if (sys.quick_board || sys.gold_board)
  246.          i = quick_read_message(msg_list[start].msg_num, pause, start);
  247.       else if (sys.pip_board)
  248.          i = pip_msg_read(sys.pip_board, msg_list[start].msg_num, 0, pause, start);
  249.       else if (sys.squish)
  250.          i = squish_read_message (msg_list[start].msg_num, pause, start);
  251.       else
  252.          i = read_message(msg_list[start].msg_num, pause, start);
  253.  
  254.       if (!i) {
  255.          if (start > 1) {
  256.             start--;
  257.             if (last_read_system != msg_list[start].area) {
  258.                read_system(msg_list[start].area, 1);
  259.                if (sys.quick_board || sys.gold_board)
  260.                   quick_scan_message_base (sys.quick_board, sys.gold_board, msg_list[start].area, 1);
  261.                else if (sys.pip_board)
  262.                   pip_scan_message_base (msg_list[start].area, 1);
  263.                else if (sys.squish)
  264.                   squish_scan_message_base (msg_list[start].area, sys.msg_path, 1);
  265.                else
  266.                   scan_message_base(msg_list[start].area, 1);
  267.                last_read_system = msg_list[start].area;
  268.                num_msg = last_msg = max_priv_mail;
  269.                first_msg = 0;
  270.             }
  271.          }
  272.          else
  273.             break;
  274.       }
  275.       else
  276.          break;
  277.    }
  278.  
  279.    lastread = msg_list[start].msg_num;
  280.    last_mail = start;
  281. }
  282.  
  283. void mail_read_nonstop ()
  284. {
  285.    while (last_mail < max_priv_mail) {
  286.       if (local_mode && local_kbd == 0x03)
  287.          break;
  288.       else if (!local_mode && !RECVD_BREAK())
  289.          break;
  290.       mail_read_forward(1);
  291.       time_release ();
  292.    }
  293. }
  294.  
  295. void mail_list()
  296. {
  297.    int fd, m, z, line = 3, last_read_system = 0;
  298.    char stringa[10], filename[80];
  299.    struct _msg msgt;
  300.  
  301.    if (!get_command_word (stringa, 4)) {
  302.       m_print(bbstxt[B_START_WITH_MSG]);
  303.  
  304.       input(stringa,4);
  305.       if(!strlen(stringa))
  306.          return;
  307.    }
  308.  
  309.    m = atoi(stringa);
  310.    if(m < 1 || m > last_msg)
  311.       return;
  312.  
  313.    cls();
  314.  
  315.    for (z = m; z <= max_priv_mail; z++) {
  316.       if (msg_list[z].area != last_read_system) {
  317.          read_system (msg_list[z].area, 1);
  318.          usr.msg = 0;
  319.          if (sys.quick_board || sys.gold_board)
  320.             quick_scan_message_base (sys.quick_board, sys.gold_board, usr.msg, 1);
  321.          else if (sys.pip_board)
  322.             pip_scan_message_base (sys.quick_board, 1);
  323.          else if (sys.squish)
  324.             squish_scan_message_base (msg_list[z].area, sys.msg_path, 1);
  325.          else
  326.             scan_message_base(sys.msg_num, 1);
  327.          m_print(bbstxt[B_AREALIST_HEADER],sys.msg_num,sys.msg_name);
  328.          if (bbstxt[B_AREALIST_HEADER][strlen (bbstxt[B_AREALIST_HEADER]) - 1] == '\r')
  329.             m_print (bbstxt[B_ONE_CR]);
  330.          usr.msg = last_read_system = msg_list[z].area;
  331.          num_msg = last_msg = max_priv_mail;
  332.       }
  333.  
  334.       if (sys.quick_board) {
  335.          if (!(line = quick_mail_header (msg_list[z].msg_num, line, 0, z)))
  336.             break;
  337.       }
  338.       else if (sys.gold_board) {
  339.          if (!(line = quick_mail_header (msg_list[z].msg_num, line, 1, z)))
  340.             break;
  341.       }
  342.       else if (sys.pip_board) {
  343.          if (!(line=pip_mail_list_header (msg_list[z].msg_num, sys.pip_board, line, z)))
  344.             break;
  345.       }
  346.       else if (sys.squish) {
  347.          if (!(line=squish_mail_list_headers (msg_list[z].msg_num, line, z)))
  348.             break;
  349.       }
  350.       else {
  351.          sprintf(filename,"%s%d.MSG",sys.msg_path,msg_list[z].msg_num);
  352.  
  353.          fd = shopen(filename,O_RDONLY|O_BINARY);
  354.          if (fd == -1)
  355.             continue;
  356.          read(fd,(char *)&msgt,sizeof(struct _msg));
  357.          close(fd);
  358.  
  359.          if((msgt.attr & MSGPRIVATE) && stricmp(msgt.from,usr.name) && stricmp(msgt.to,usr.name)
  360.          && stricmp(msgt.from,usr.handle) && stricmp(msgt.to,usr.handle) && usr.priv < SYSOP)
  361.             continue;
  362.  
  363.          if ((line = msg_attrib(&msgt,z,line,0)) == 0)
  364.             break;
  365.       }
  366.  
  367.       m_print (bbstxt[B_ONE_CR]);
  368.  
  369.       if (!(line = more_question (line)) || !CARRIER || RECVD_BREAK())
  370.          break;
  371.  
  372.       time_release();
  373.    }
  374.  
  375.    if (line && CARRIER)
  376.       press_enter();
  377. }
  378.  
  379. static int quick_index (z, area_msg, area_num, maxareas)
  380. int z, *area_msg, *area_num, maxareas;
  381. {
  382.    #define MAX_TOIDX_READ  20
  383.    FILE *fpi, *fpn;
  384.    int i, m, fdhdr, x;
  385.    word pos, currmsg[201];
  386.    char filename[80], username[36], to[MAX_TOIDX_READ][36];
  387.    byte boards[201];
  388.    struct _msghdr hdr;
  389.    struct _sys tsys;
  390.    struct _msgidx idx[MAX_TOIDX_READ];
  391.  
  392.    for (i = 0; i < 201; i++) {
  393.       boards[i] = 0;
  394.       currmsg[i] = 0;
  395.    }
  396.  
  397.    sprintf (filename, SYSMSG_PATH, config->sys_path);
  398.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  399.       ;
  400.    fpi = fdopen (i, "rb");
  401.    while (fread((char *)&tsys.msg_name,SIZEOF_MSGAREA,1,fpi) == 1) {
  402.       if (!tsys.quick_board)
  403.          continue;
  404.       if (usr.priv < tsys.msg_priv || (usr.flags & tsys.msg_flags) != tsys.msg_flags)
  405.          continue;
  406.       boards[tsys.quick_board] = tsys.msg_num;
  407.    }
  408.    fclose (fpi);
  409.  
  410.    sprintf (filename, "%sMSGHDR.BBS", fido_msgpath);
  411.    fdhdr = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE);
  412.    if (fdhdr == -1)
  413.       return (z);
  414.  
  415.    sprintf (filename, "%sMSGTOIDX.BBS", fido_msgpath);
  416.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  417.       ;
  418.    fpi = fdopen (i, "rb");
  419.    if (fpi == NULL) {
  420.       close (fdhdr);
  421.       return (z);
  422.    }
  423.  
  424.    sprintf (filename, "%sMSGIDX.BBS", fido_msgpath);
  425.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  426.       ;
  427.    fpn = fdopen (i, "rb");
  428.    if (fpn == NULL) {
  429.       close (fdhdr);
  430.       fclose (fpi);
  431.       return (z);
  432.    }
  433.  
  434.    if (!local_mode)
  435.       _BRK_ENABLE();
  436.    pos = 0;
  437.  
  438.    do {
  439.       i = fread (to, 36, MAX_TOIDX_READ, fpi);
  440.       i = fread ((char *)&idx, sizeof (struct _msgidx), MAX_TOIDX_READ, fpn);
  441.  
  442.       if (!local_mode) {
  443.          if (!CARRIER || RECVD_BREAK())
  444.             break;
  445.       }
  446.  
  447.       for (m = 0; m < i; m++) {
  448.          if (!idx[m].msgnum || !boards[idx[m].board]) {
  449.             pos++;
  450.             continue;
  451.          }
  452.  
  453.          currmsg[idx[m].board]++;
  454.          strncpy (username, &to[m][1], to[m][0]);
  455.          username[to[m][0]] = '\0';
  456.          if (!stricmp (username, usr.name) || !stricmp (username, usr.handle)) {
  457.             lseek (fdhdr, (long)pos * sizeof (struct _msghdr), SEEK_SET);
  458.             read (fdhdr, (char *)&hdr, sizeof (struct _msghdr));
  459.  
  460.             strncpy (username, &hdr.whoto[1], hdr.whoto[0]);
  461.             username[hdr.whoto[0]] = '\0';
  462.             if ((!stricmp (username, usr.name) || !stricmp(username,usr.handle)) && hdr.board == idx[m].board) {
  463.                if (boards[hdr.board] && !(hdr.msgattr & Q_RECEIVED) && !(hdr.msgattr & Q_RECKILL)) {
  464.                   if (z >= MAX_PRIV_MAIL)
  465.                      break;
  466.  
  467.                   msg_list[z].area = boards[hdr.board];
  468.                   msg_list[z++].msg_num = currmsg[hdr.board];
  469.  
  470.                   for (x = 0; x < maxareas; x++)
  471.                      if (area_num[x] == boards[hdr.board])
  472.                         break;
  473.                   area_msg[x]++;
  474.                }
  475.             }
  476.          }
  477.          pos++;
  478.          if ((pos % 150) == 0) {
  479.             m_print2 ("\b%c", rotate[rotatepos]);
  480.             rotatepos = (rotatepos + 1) % 4;
  481.          }
  482.       }
  483.    } while (i == MAX_TOIDX_READ);
  484.  
  485.    close (fdhdr);
  486.    fclose (fpn);
  487.    fclose (fpi);
  488.  
  489.    return (z);
  490. }
  491.  
  492. static int gold_index (z, area_msg, area_num, maxareas)
  493. int z, *area_msg, *area_num, maxareas;
  494. {
  495.    #define MAX_TOIDX_READ  20
  496.    FILE *fpi, *fpn;
  497.    int i, m, fdhdr, x;
  498.    word pos, currmsg[501];
  499.    char filename[80], username[36], to[MAX_TOIDX_READ][36];
  500.    int boards[501];
  501.    struct _gold_msghdr hdr;
  502.    struct _sys tsys;
  503.    struct _gold_msgidx idx[MAX_TOIDX_READ];
  504.  
  505.    for (i = 0; i < 501; i++) {
  506.       boards[i] = 0;
  507.       currmsg[i] = 0;
  508.    }
  509.  
  510.    sprintf (filename, SYSMSG_PATH, config->sys_path);
  511.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  512.       ;
  513.    fpi = fdopen (i, "rb");
  514.    while (fread((char *)&tsys.msg_name,SIZEOF_MSGAREA,1,fpi) == 1) {
  515.       if (!tsys.gold_board)
  516.          continue;
  517.       if (usr.priv < tsys.msg_priv || (usr.flags & tsys.msg_flags) != tsys.msg_flags)
  518.          continue;
  519.       boards[tsys.gold_board] = tsys.msg_num;
  520.    }
  521.    fclose (fpi);
  522.  
  523.    sprintf (filename, "%sMSGHDR.DAT", fido_msgpath);
  524.    fdhdr = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE);
  525.    if (fdhdr == -1)
  526.       return (z);
  527.  
  528.    sprintf (filename, "%sMSGTOIDX.DAT", fido_msgpath);
  529.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  530.       ;
  531.    fpi = fdopen (i, "rb");
  532.    if (fpi == NULL) {
  533.       close (fdhdr);
  534.       return (z);
  535.    }
  536.  
  537.    sprintf (filename, "%sMSGIDX.DAT", fido_msgpath);
  538.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  539.       ;
  540.    fpn = fdopen (i, "rb");
  541.    if (fpn == NULL) {
  542.       close (fdhdr);
  543.       fclose (fpi);
  544.       return (z);
  545.    }
  546.  
  547.    if (!local_mode)
  548.       _BRK_ENABLE();
  549.    pos = 0;
  550.  
  551.    do {
  552.       i = fread (to, 36, MAX_TOIDX_READ, fpi);
  553.       i = fread ((char *)&idx, sizeof (struct _gold_msgidx), MAX_TOIDX_READ, fpn);
  554.  
  555.       if (!local_mode) {
  556.          if (!CARRIER || RECVD_BREAK())
  557.             break;
  558.       }
  559.  
  560.       for (m = 0; m < i; m++) {
  561.          if (!idx[m].msgnum || !boards[idx[m].board]) {
  562.             pos++;
  563.             continue;
  564.          }
  565.  
  566.          currmsg[idx[m].board]++;
  567.          strncpy (username, &to[m][1], to[m][0]);
  568.          username[to[m][0]] = '\0';
  569.          if (!stricmp (username, usr.name) || !stricmp (username, usr.handle)) {
  570.             lseek (fdhdr, (long)pos * sizeof (struct _gold_msghdr), SEEK_SET);
  571.             read (fdhdr, (char *)&hdr, sizeof (struct _gold_msghdr));
  572.  
  573.             strncpy (username, &hdr.whoto[1], hdr.whoto[0]);
  574.             username[hdr.whoto[0]] = '\0';
  575.             if ((!stricmp (username, usr.name) || !stricmp(username,usr.handle)) && hdr.board == idx[m].board) {
  576.                if (boards[hdr.board] && !(hdr.msgattr & Q_RECEIVED) && !(hdr.msgattr & Q_RECKILL)) {
  577.                   if (z >= MAX_PRIV_MAIL)
  578.                      break;
  579.  
  580.                   msg_list[z].area = boards[hdr.board];
  581.                   msg_list[z++].msg_num = currmsg[hdr.board];
  582.  
  583.                   for (x = 0; x < maxareas; x++)
  584.                      if (area_num[x] == boards[hdr.board])
  585.                         break;
  586.                   area_msg[x]++;
  587.                }
  588.             }
  589.          }
  590.          pos++;
  591.          if ((pos % 150) == 0) {
  592.             m_print2 ("\b%c", rotate[rotatepos]);
  593.             rotatepos = (rotatepos + 1) % 4;
  594.          }
  595.       }
  596.    } while (i == MAX_TOIDX_READ);
  597.  
  598.    close (fdhdr);
  599.    fclose (fpn);
  600.    fclose (fpi);
  601.  
  602.    return (z);
  603. }
  604.  
  605. static int pipbase_index (z, area_msg, area_num, maxareas)
  606. int z, *area_msg, *area_num, maxareas;
  607. {
  608.    FILE *fpi;
  609.    int f1, i, *boards, oldboard, x, maxboards, xx;
  610.    char filename[80];
  611.    DESTPTR hdr;
  612.    MSGPTR mhdr;
  613.    struct _sys tsys;
  614.  
  615.    sprintf (filename, SYSMSG_PATH, config->sys_path);
  616.    while ((i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  617.       ;
  618.  
  619.    x = (int)(filelength (i) / SIZEOF_MSGAREA);
  620.    if ((boards = (int *)malloc (x * sizeof (int))) == NULL) {
  621.       close (i);
  622.       return (z);
  623.    }
  624.    memset (boards, 0, x * sizeof (int));
  625.  
  626.    fpi = fdopen (i, "rb");
  627.    x = 0;
  628.  
  629.    while (fread((char *)&tsys.msg_name,SIZEOF_MSGAREA,1,fpi) == 1)
  630.       boards[x++] = tsys.pip_board;
  631.    maxboards = x;
  632.  
  633.    fclose (fpi);
  634.  
  635.    sprintf (filename, "%sDESTPTR.PIP", pip_msgpath);
  636.    i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE);
  637.    if (i == -1) {
  638.       free (boards);
  639.       return (z);
  640.    }
  641.    fpi = fdopen (i, "rb");
  642.    if (fpi == NULL) {
  643.       free (boards);
  644.       return (z);
  645.    }
  646.  
  647.    if (!local_mode)
  648.       _BRK_ENABLE();
  649.  
  650.    i = 0;
  651.    f1 = oldboard = -1;
  652.  
  653.    while (fread ((char *)&hdr, sizeof (DESTPTR), 1, fpi) == 1) {
  654.       if (!local_mode) {
  655.          if (!CARRIER || RECVD_BREAK ())
  656.             break;
  657.       }
  658.  
  659.       i++;
  660.       if ((i % 30) == 0) {
  661.          m_print2 ("\b%c", rotate[rotatepos]);
  662.          rotatepos = (rotatepos + 1) % 4;
  663.       }
  664.  
  665.       for (xx = 0; xx < maxboards; xx++)
  666.          if (boards[xx] == hdr.area)
  667.             break;
  668.  
  669.       if (xx >= maxboards)
  670.          continue;
  671.  
  672.       if (!read_system2 (area_num[xx], 1, &tsys))
  673.          continue;
  674.       if (usr.priv < tsys.msg_priv || (usr.flags & tsys.msg_flags) != tsys.msg_flags)
  675.          continue;
  676.  
  677.       if (!stricmp (usr.name, hdr.to) || !stricmp (usr.handle, hdr.to)) {
  678.          if (oldboard != hdr.area) {
  679.             if (f1 != -1)
  680.                close (f1);
  681.             sprintf (filename, "%sMPTR%04x.PIP", pip_msgpath, hdr.area);
  682.             f1 = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE);
  683.             oldboard = hdr.area;
  684.          }
  685.  
  686.          if (f1 != -1) {
  687.             if (lseek( f1, sizeof(mhdr) * hdr.msg, SEEK_SET))
  688.             read (f1, &mhdr, sizeof (mhdr));
  689.             if (mhdr.status & (SET_MPTR_RCVD|SET_MPTR_DEL))
  690.                continue;
  691.          }
  692.          else
  693.             continue;
  694.  
  695.          if (z >= MAX_PRIV_MAIL)
  696.             break;
  697.  
  698.          msg_list[z].area = boards[xx];
  699.          msg_list[z++].msg_num = hdr.msg + 1;
  700.          area_msg[xx]++;
  701.       }
  702.    }
  703.  
  704.    fclose (fpi);
  705.    if (f1 != -1)
  706.       close (f1);
  707.  
  708.    free (boards);
  709.  
  710.    return (z);
  711. }
  712.  
  713. static int fido_index (z, area_msg, area_num, maxareas)
  714. int z, *area_msg, *area_num, maxareas;
  715. {
  716.    FILE *fp;
  717.    int i, m, fd, ct = 0, x;
  718.    char filename[50];
  719.    long crcalias;
  720.    struct _mail idxm[MAX_MAIL_BUFFER];
  721.    struct _sys tsys;
  722.    struct _msg tmsg;
  723.  
  724.    crcalias = crc_name (usr.handle);
  725.  
  726.    sprintf (filename, "%sMSGTOIDX.DAT", config->sys_path);
  727.    i = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE);
  728.    if (i == -1)
  729.       return (z);
  730.    fp = fdopen (i, "rb");
  731.  
  732.    if (fp != NULL) {
  733.       if (!local_mode)
  734.          _BRK_ENABLE();
  735.  
  736.       do {
  737.          i = fread((char *)&idxm, sizeof(struct _mail), MAX_MAIL_BUFFER, fp);
  738.  
  739.          if (!local_mode) {
  740.             if (!CARRIER || RECVD_BREAK())
  741.                break;
  742.          }
  743.  
  744.          for (m = 0; m < i; m++) {
  745.             if (idxm[m].to != usr.id && idxm[m].to != crcalias)
  746.                continue;
  747.             if (!idxm[m].area || !read_system2 (idxm[m].area, 1, &tsys))
  748.                continue;
  749.             if (usr.priv < tsys.msg_priv || (usr.flags & tsys.msg_flags) != tsys.msg_flags)
  750.                continue;
  751.  
  752.             for (x = 0; x < maxareas; x++)
  753.                if (area_num[x] == tsys.msg_num)
  754.                   break;
  755.  
  756.             if (!local_mode) {
  757.                if (!CARRIER || RECVD_BREAK()) {
  758.                   i = 0;
  759.                   break;
  760.                }
  761.             }
  762.  
  763.             if (z >= MAX_PRIV_MAIL) {
  764.                i = 0;
  765.                break;
  766.             }
  767.  
  768.             sprintf (filename, "%s%d.MSG", tsys.msg_path, idxm[m].msg_num);
  769.             fd = shopen (filename, O_RDONLY|O_BINARY);
  770.             if (fd == -1)
  771.                continue;
  772.             read (fd, (char *)&tmsg, sizeof (struct _msg));
  773.             close (fd);
  774.  
  775.             if ((stricmp (tmsg.to, usr.name) && stricmp (tmsg.to, usr.handle)) || (tmsg.attr & MSGREAD))
  776.                continue;
  777.  
  778.             msg_list[z].area = idxm[m].area;
  779.             msg_list[z++].msg_num = idxm[m].msg_num;
  780.             area_msg[x]++;
  781.          }
  782.  
  783.          if ((ct++ % 10) == 0) {
  784.             m_print2 ("\b%c", rotate[rotatepos]);
  785.             rotatepos = (rotatepos + 1) % 4;
  786.          }
  787.       } while (i == MAX_MAIL_BUFFER && z < MAX_PRIV_MAIL);
  788.  
  789.       fclose (fp);
  790.    }
  791.  
  792.    return (z);
  793. }
  794.  
  795. #define MAX_SQIDX  32
  796.  
  797. typedef struct _sqidx
  798. {
  799.    unsigned long offs;     /* offset del messaggio nel file dati */
  800.    unsigned long msgid;    /* unique msgid */
  801.    unsigned long in;       /* hash del destinatario */
  802. } SQIDX;
  803.  
  804. static int squish_index (z, area_msg, area_num, maxareas)
  805. int z, *area_msg, *area_num, maxareas;
  806. {
  807.    int fdidx, fd, i, x;
  808.    unsigned int max;
  809.    char filename[80];
  810.    dword hash, msgn, hashalias;
  811.    struct _sys tsys;
  812.    MSGH *sq_msgh;
  813.    XMSG xmsg;
  814.    SQIDX *sqidx;
  815.  
  816.    hash = SquishHash (usr.name);
  817.    hashalias = SquishHash (usr.handle);
  818.  
  819.    sprintf (filename, SYSMSG_PATH, config->sys_path);
  820.    while ((fd = sopen (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE)) == -1)
  821.       ;
  822.  
  823.    while (read(fd, (char *)&tsys.msg_name, SIZEOF_MSGAREA) == SIZEOF_MSGAREA) {
  824.       if (!tsys.squish)
  825.          continue;
  826.       if (usr.priv < tsys.msg_priv || (usr.flags & tsys.msg_flags) != tsys.msg_flags)
  827.          continue;
  828.  
  829.       if (sq_ptr != NULL) {
  830.          MsgUnlock (sq_ptr);
  831.          MsgCloseArea (sq_ptr);
  832.          sq_ptr = NULL;
  833.       }
  834.  
  835.       for (x = 0; x < maxareas; x++)
  836.          if (area_num[x] == tsys.msg_num)
  837.             break;
  838.  
  839.       m_print2 ("\b%c", rotate[rotatepos]);
  840.       rotatepos = (rotatepos + 1) % 4;
  841.  
  842.       sprintf (filename, "%s.SQI", tsys.msg_path);
  843.       fdidx = sh_open (filename, SH_DENYNONE, O_RDONLY|O_BINARY, S_IREAD|S_IWRITE);
  844.       if (fdidx == -1)
  845.          continue;
  846.  
  847.       sqidx = (SQIDX *)malloc ((unsigned int)filelength (fdidx));
  848.       if (sqidx == NULL) {
  849.          close (fdidx);
  850.          continue;
  851.       }
  852.  
  853.       max = read (fdidx, sqidx, (unsigned int)filelength (fdidx));
  854.       close (fdidx);
  855.  
  856.       max /= sizeof (SQIDX);
  857.       for (i = 0; i < max; i++) {
  858.          if (sqidx[i].in == hash || sqidx[i].in == hashalias) {
  859.             if (sq_ptr == NULL) {
  860.                sq_ptr = MsgOpenArea (tsys.msg_path, MSGAREA_NORMAL, MSGTYPE_SQUISH);
  861.                if (sq_ptr == NULL)
  862.                   continue;
  863.             }
  864.  
  865.             msgn = MsgUidToMsgn (sq_ptr, sqidx[i].msgid, UID_EXACT);
  866.             if (msgn == 0)
  867.                continue;
  868.  
  869.             sq_msgh = MsgOpenMsg (sq_ptr, MOPEN_RW, msgn);
  870.             if (sq_msgh == NULL)
  871.                continue;
  872.  
  873.             if (MsgReadMsg (sq_msgh, &xmsg, 0L, 0L, NULL, 0, NULL) == -1) {
  874.                MsgCloseMsg (sq_msgh);
  875.                continue;
  876.             }
  877.  
  878.             MsgCloseMsg (sq_msgh);
  879.  
  880.             if ((!stricmp (xmsg.to, usr.name) || !stricmp (xmsg.to, usr.handle)) && !(xmsg.attr & MSGREAD)) {
  881.                if (z >= MAX_PRIV_MAIL)
  882.                   break;
  883.  
  884.                msg_list[z].area = tsys.msg_num;
  885.                msg_list[z++].msg_num = (int)msgn;
  886.                area_msg[x]++;
  887.             }
  888.          }
  889.       }
  890.  
  891.       free (sqidx);
  892.  
  893.       if (sq_ptr != NULL) {
  894.          MsgCloseArea (sq_ptr);
  895.          sq_ptr = NULL;
  896.       }
  897.    }
  898.  
  899.    close (fd);
  900.  
  901.    if (sq_ptr != NULL) {
  902.       MsgUnlock (sq_ptr);
  903.       MsgCloseArea (sq_ptr);
  904.       sq_ptr = NULL;
  905.    }
  906.  
  907.    return (z);
  908. }
  909.  
  910.  
  911.