home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / DUPES.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  6KB  |  219 lines

  1.  
  2. // LoraBBS Version 2.99 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 "_ldefs.h"
  20. #include "msgbase.h"
  21.  
  22. TDupes::TDupes (void)
  23. {
  24.    strcpy (DataFile, "dupes.dat");
  25.    strcpy (IndexFile, "dupes.idx");
  26.    dd.EchoTag[0] = '\0';
  27. }
  28.  
  29. TDupes::TDupes (PSZ pszDataPath)
  30. {
  31.    strcpy (DataFile, pszDataPath);
  32.    if (DataFile[0] != '\0') {
  33. #if defined(__LINUX__)
  34.       if (DataFile[strlen (DataFile) - 1] != '/')
  35.          strcat (DataFile, "/");
  36. #else
  37.       if (DataFile[strlen (DataFile) - 1] != '\\')
  38.          strcat (DataFile, "\\");
  39. #endif
  40.    }
  41.    strcpy (IndexFile, DataFile);
  42.  
  43.    strcat (DataFile, "dupes.dat");
  44.    strcat (IndexFile, "dupes.idx");
  45.    dd.EchoTag[0] = '\0';
  46. }
  47.  
  48. TDupes::~TDupes (void)
  49. {
  50. }
  51.  
  52. VOID TDupes::Add (PSZ pszEchoTag, class TMsgBase *Msg)
  53. {
  54.    if (stricmp (dd.EchoTag, pszEchoTag))
  55.       Load (pszEchoTag);
  56.  
  57.    dd.Dupes[dd.Position++] = GetEID (Msg);
  58.    if (dd.Position >= MAX_DUPES)
  59.       dd.Position = 0;
  60. }
  61.  
  62. USHORT TDupes::Check (PSZ pszEchoTag, class TMsgBase *Msg)
  63. {
  64.    USHORT i, RetVal = FALSE;
  65.    ULONG Crc;
  66.  
  67.    if (stricmp (dd.EchoTag, pszEchoTag))
  68.       Load (pszEchoTag);
  69.  
  70.    Crc = GetEID (Msg);
  71.  
  72.    for (i = 0; i < MAX_DUPES; i++) {
  73.       if (dd.Dupes[i] == Crc) {
  74.          RetVal = TRUE;
  75.          break;
  76.       }
  77.    }
  78.  
  79.    return (RetVal);
  80. }
  81.  
  82. VOID TDupes::Delete (VOID)
  83. {
  84.    int fd;
  85.    CHAR EchoTag[64];
  86.    ULONG WritePos, ReadPos;
  87.  
  88.    strcpy (EchoTag, dd.EchoTag);
  89.    WritePos = ReadPos = 0L;
  90.  
  91.    if ((fd = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  92.       while (read (fd, &dd, sizeof (DUPEDATA)) == sizeof (DUPEDATA)) {
  93.          if (stricmp (dd.EchoTag, EchoTag)) {
  94.             lseek (fd, WritePos, SEEK_SET);
  95.             write (fd, &dd, sizeof (DUPEDATA));
  96.             WritePos += sizeof (DUPEDATA);
  97.          }
  98.          ReadPos += sizeof (DUPEDATA);
  99.          lseek (fd, ReadPos, SEEK_SET);
  100.       }
  101.       close (fd);
  102.    }
  103.  
  104.    dd.EchoTag[0] = '\0';
  105. }
  106.  
  107. ULONG TDupes::GetEID (class TMsgBase *Msg)
  108. {
  109.    USHORT Found = FALSE;
  110.    CHAR *Text;
  111.    ULONG Crc = 0L;
  112.  
  113.    if ((Text = (CHAR *)Msg->Text.First ()) != NULL) {
  114.       do {
  115.          if (*Text != '\001' && strncmp (Text, "AREA:", 5))
  116.             break;
  117.          if (!strncmp (Text, "\001MSGID: ", 8) || !strncmp (Text, "\001Message-ID: ", 13)) {
  118.             Crc = StringCrc32 (Text, 0xFFFFFFFFL);
  119.             Found = TRUE;
  120.             break;
  121.          }
  122.       } while ((Text = (CHAR *)Msg->Text.Next ()) != NULL);
  123.    }
  124.  
  125.    if (Found == FALSE) {
  126.       Crc = StringCrc32 (Msg->From, 0xFFFFFFFFL);
  127.       Crc = StringCrc32 (Msg->To, Crc);
  128.       Crc = StringCrc32 (Msg->Subject, Crc);
  129.    }
  130.  
  131.    return (Crc);
  132. }
  133.  
  134. #define MAX_INDEX    32
  135.  
  136. USHORT TDupes::Load (PSZ pszEchoTag)
  137. {
  138.    int fd, i, readed;
  139.    USHORT Found = FALSE;
  140.    DUPEIDX *idx;
  141.  
  142.    if ((idx = (DUPEIDX *)malloc (sizeof (DUPEIDX) * MAX_INDEX)) != NULL) {
  143.       if ((fd = sopen (IndexFile, O_RDONLY|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  144.          while ((readed = read (fd, idx, sizeof (DUPEIDX) * MAX_INDEX)) != 0) {
  145.             readed /= sizeof (DUPEIDX);
  146.             for (i = 0; i < readed; i++) {
  147.                if (!stricmp (pszEchoTag, idx[i].EchoTag)) {
  148.                   Found = TRUE;
  149.                   break;
  150.                }
  151.             }
  152.          }
  153.          close (fd);
  154.       }
  155.       free (idx);
  156.    }
  157.  
  158.    if (Found == TRUE) {
  159.       if ((fd = sopen (DataFile, O_RDONLY|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  160.          lseek (fd, idx[i].Position, SEEK_SET);
  161.          read (fd, &dd, sizeof (DUPEDATA));
  162.          close (fd);
  163.       }
  164.    }
  165.    else {
  166.       memset (&dd, 0, sizeof (DUPEDATA));
  167.       strcpy (dd.EchoTag, pszEchoTag);
  168.    }
  169.  
  170.    return (Found);
  171. }
  172.  
  173. VOID TDupes::Save (VOID)
  174. {
  175.    int fd, i, readed;
  176.    USHORT Found = FALSE;
  177.    DUPEIDX *idx, nidx;
  178.  
  179.    if (dd.EchoTag[0] != '\0') {
  180.       if ((idx = (DUPEIDX *)malloc (sizeof (DUPEIDX) * MAX_INDEX)) != NULL) {
  181.          if ((fd = sopen (IndexFile, O_RDONLY|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  182.             while ((readed = read (fd, idx, sizeof (DUPEIDX) * MAX_INDEX)) != 0) {
  183.                readed /= sizeof (DUPEIDX);
  184.                for (i = 0; i < readed; i++) {
  185.                   if (!stricmp (dd.EchoTag, idx[i].EchoTag)) {
  186.                      Found = TRUE;
  187.                      break;
  188.                   }
  189.                }
  190.             }
  191.             close (fd);
  192.          }
  193.          free (idx);
  194.       }
  195.  
  196.       if ((fd = sopen (DataFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  197.          if (Found == TRUE)
  198.             lseek (fd, idx[i].Position, SEEK_SET);
  199.          else {
  200.             lseek (fd, 0L, SEEK_END);
  201.             memset (&nidx, 0, sizeof (DUPEIDX));
  202.             strcpy (nidx.EchoTag, dd.EchoTag);
  203.             nidx.Position = tell (fd);
  204.          }
  205.          write (fd, &dd, sizeof (DUPEDATA));
  206.          close (fd);
  207.  
  208.          if (Found == FALSE) {
  209.             if ((fd = sopen (IndexFile, O_RDWR|O_BINARY|O_CREAT, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  210.                lseek (fd, 0L, SEEK_END);
  211.                write (fd, &nidx, sizeof (DUPEIDX));
  212.                close (fd);
  213.             }
  214.          }
  215.       }
  216.    }
  217. }
  218.  
  219.