home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / ADDRESS.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  8KB  |  303 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 "lora_api.h"
  21.  
  22. TAddress::TAddress (void)
  23. {
  24.    List.Clear ();
  25. }
  26.  
  27. TAddress::~TAddress (void)
  28. {
  29.    List.Clear ();
  30. }
  31.  
  32. SHORT TAddress::Add (VOID)
  33. {
  34.    return (Add (Zone, Net, Node, Point, Domain));
  35. }
  36.  
  37. SHORT TAddress::Add (PSZ pszAddress)
  38. {
  39.    SHORT RetVal = FALSE;
  40.    PSZ p;
  41.  
  42.    Zone = Net = Node = Point = 0;
  43.    FakeNet = 0;
  44.    Domain[0] = '\0';
  45.  
  46.    if (pszAddress != NULL) {
  47.       if (strchr (pszAddress, ':') != NULL) {
  48.          Zone = (USHORT)atoi (pszAddress);
  49.          pszAddress = strchr (pszAddress, ':') + 1;
  50.       }
  51.       if (strchr (pszAddress, '/') != NULL) {
  52.          Net = (USHORT)atoi (pszAddress);
  53.          pszAddress = strchr (pszAddress, '/') + 1;
  54.       }
  55.       Node = (USHORT)atoi (pszAddress);
  56.       if ((p = strchr (pszAddress, '@')) != NULL) {
  57.          *p++ = '\0';
  58.          strcpy (Domain, p);
  59.       }
  60.       if (strchr (pszAddress, '.') != NULL) {
  61.          pszAddress = strchr (pszAddress, '.') + 1;
  62.          Point = (USHORT)atoi (pszAddress);
  63.       }
  64.  
  65.       RetVal = Add (Zone, Net, Node, Point, Domain);
  66.    }
  67.  
  68.    return (RetVal);
  69. }
  70.  
  71. SHORT TAddress::Add (USHORT usZone, USHORT usNet, USHORT usNode, USHORT usPoint, PSZ pszDomain)
  72. {
  73.    SHORT RetVal = TRUE;
  74.    MAILADDRESS Addr, *Check;
  75.  
  76.    if ((Check = (MAILADDRESS *)List.First ()) != NULL)
  77.       do {
  78.          if (Check->Zone == usZone && Check->Node == usNode && Check->Net == usNet && Check->Point == usPoint)
  79.             RetVal = FALSE;
  80.       } while ((Check = (MAILADDRESS *)List.Next ()) != NULL);
  81.  
  82.    if (RetVal == TRUE) {
  83.       memset (&Addr, 0, sizeof (Addr));
  84.       Addr.Zone = usZone;
  85.       Addr.Net= usNet;
  86.       Addr.Node = usNode;
  87.       Addr.Point = usPoint;
  88.       if (pszDomain != NULL)
  89.          strcpy (Addr.Domain, pszDomain);
  90.       RetVal = List.Add (&Addr, sizeof (Addr));
  91.    }
  92.  
  93.    return (RetVal);
  94. }
  95.  
  96. VOID TAddress::Clear (VOID)
  97. {
  98.    List.Clear ();
  99.    Zone = Net = Node = Point = 0;
  100.    FakeNet = 0;
  101.    Domain[0] = String[0] = '\0';
  102. }
  103.  
  104. VOID TAddress::Delete (VOID)
  105. {
  106.    List.Remove ();
  107. }
  108.  
  109. SHORT TAddress::First (VOID)
  110. {
  111.    SHORT RetVal = FALSE;
  112.    MAILADDRESS *Addr;
  113.  
  114.    Zone = Net = Node = Point = 0;
  115.    FakeNet = 0;
  116.    Domain[0] = String[0] = '\0';
  117.  
  118.    if ((Addr = (MAILADDRESS *)List.First ()) != NULL) {
  119.       Zone = Addr->Zone;
  120.       Net = Addr->Net;
  121.       Node = Addr->Node;
  122.       Point = Addr->Point;
  123.       strcpy (Domain, Addr->Domain);
  124.       if (Point != 0)
  125.          sprintf (String, "%u:%u/%u.%u", Zone, Net, Node, Point);
  126.       else
  127.          sprintf (String, "%u:%u/%u", Zone, Net, Node);
  128.       if (Domain[0] != '\0') {
  129.          strcat (String, "@");
  130.          strcat (String, Domain);
  131.       }
  132.       FakeNet = Addr->FakeNet;
  133.       RetVal = TRUE;
  134.    }
  135.  
  136.    return (RetVal);
  137. }
  138.  
  139. USHORT TAddress::Load (PSZ File)
  140. {
  141.    List.Clear ();
  142.    return (Merge (File));
  143. }
  144.  
  145. USHORT TAddress::Merge (PSZ File)
  146. {
  147.    int fd;
  148.    USHORT RetVal = FALSE;
  149.    CHAR Temp[128];
  150.    MAILADDRESS Addr;
  151.  
  152.    strcpy (Temp, File);
  153.    if (Temp[0] != '\0') {
  154. #if defined(__LINUX__)
  155.       if (Temp[strlen (Temp) - 1] != '/')
  156.          strcat (Temp, "/");
  157. #else
  158.       if (Temp[strlen (Temp) - 1] != '\\')
  159.          strcat (Temp, "\\");
  160. #endif
  161.    }
  162.    strcat (Temp, "address.dat");
  163.    if ((fd = sopen (Temp, O_RDONLY|O_BINARY, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  164.       RetVal = TRUE;
  165.       while (read (fd, &Addr, sizeof (MAILADDRESS)) == sizeof (MAILADDRESS))
  166.          List.Add (&Addr, sizeof (MAILADDRESS));
  167.       close (fd);
  168.    }
  169.  
  170.    return (RetVal);
  171. }
  172.  
  173. SHORT TAddress::Next (VOID)
  174. {
  175.    SHORT RetVal = FALSE;
  176.    MAILADDRESS *Addr;
  177.  
  178.    if ((Addr = (MAILADDRESS *)List.Next ()) != NULL) {
  179.       Zone = Addr->Zone;
  180.       Net = Addr->Net;
  181.       Node = Addr->Node;
  182.       Point = Addr->Point;
  183.       strcpy (Domain, Addr->Domain);
  184.       if (Point != 0)
  185.          sprintf (String, "%u:%u/%u.%u", Zone, Net, Node, Point);
  186.       else
  187.          sprintf (String, "%u:%u/%u", Zone, Net, Node);
  188.       if (Domain[0] != '\0') {
  189.          strcat (String, "@");
  190.          strcat (String, Domain);
  191.       }
  192.       FakeNet = Addr->FakeNet;
  193.       RetVal = TRUE;
  194.    }
  195.  
  196.    return (RetVal);
  197. }
  198.  
  199. VOID TAddress::Parse (PSZ pszAddress)
  200. {
  201.    PSZ p;
  202.  
  203.    Zone = Net = Node = Point = 0;
  204.    FakeNet = 0;
  205.    Domain[0] = '\0';
  206.  
  207.    if (pszAddress != NULL) {
  208.       if (strchr (pszAddress, ':') != NULL) {
  209.          Zone = (USHORT)atoi (pszAddress);
  210.          pszAddress = strchr (pszAddress, ':') + 1;
  211.       }
  212.       if (strchr (pszAddress, '/') != NULL) {
  213.          if (!stricmp (pszAddress, "all") || !strcmp (pszAddress, "*"))
  214.             Net = 65535U;
  215.          else
  216.             Net = (USHORT)atoi (pszAddress);
  217.          pszAddress = strchr (pszAddress, '/') + 1;
  218.       }
  219.       if (!stricmp (pszAddress, "all") || !strcmp (pszAddress, "*")) {
  220.          Node = 65535U;
  221.          if (Net == 0)
  222.             Net = Node;
  223.       }
  224.       else
  225.          Node = (USHORT)atoi (pszAddress);
  226.       if ((p = strchr (pszAddress, '@')) != NULL) {
  227.          *p++ = '\0';
  228.          strcpy (Domain, p);
  229.       }
  230.       if (strchr (pszAddress, '.') != NULL) {
  231.          pszAddress = strchr (pszAddress, '.') + 1;
  232.          if (!stricmp (pszAddress, "all") || !strcmp (pszAddress, "*"))
  233.             Point = 65535U;
  234.          else
  235.             Point = (USHORT)atoi (pszAddress);
  236.       }
  237.  
  238.       if (Point != 0)
  239.          sprintf (String, "%u:%u/%u.%u", Zone, Net, Node, Point);
  240.       else
  241.          sprintf (String, "%u:%u/%u", Zone, Net, Node);
  242.       if (Domain[0] != '\0') {
  243.          strcat (String, "@");
  244.          strcat (String, Domain);
  245.       }
  246.    }
  247. }
  248.  
  249. VOID TAddress::Update (VOID)
  250. {
  251.    MAILADDRESS *Addr;
  252.  
  253.    if ((Addr = (MAILADDRESS *)List.Value ()) != NULL) {
  254.       Addr->Zone = Zone;
  255.       Addr->Net = Net;
  256.       Addr->Node = Node;
  257.       Addr->Point = Point;
  258.       strcpy (Addr->Domain, Domain);
  259.       Addr->FakeNet = FakeNet;
  260.    }
  261.  
  262.    if (Point != 0)
  263.       sprintf (String, "%u:%u/%u.%u", Zone, Net, Node, Point);
  264.    else
  265.       sprintf (String, "%u:%u/%u", Zone, Net, Node);
  266.    if (Domain[0] != '\0') {
  267.       strcat (String, "@");
  268.       strcat (String, Domain);
  269.    }
  270. }
  271.  
  272. USHORT TAddress::Save (PSZ File)
  273. {
  274.    int fd;
  275.    USHORT RetVal = FALSE;
  276.    CHAR Temp[128];
  277.    MAILADDRESS *Addr;
  278.  
  279.    strcpy (Temp, File);
  280.    if (Temp[0] != '\0') {
  281. #if defined(__LINUX__)
  282.       if (Temp[strlen (Temp) - 1] != '/')
  283.          strcat (Temp, "/");
  284. #else
  285.       if (Temp[strlen (Temp) - 1] != '\\')
  286.          strcat (Temp, "\\");
  287. #endif
  288.    }
  289.    strcat (Temp, "address.dat");
  290.    if ((fd = sopen (Temp, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, SH_DENYNO, S_IREAD|S_IWRITE)) != -1) {
  291.       RetVal = TRUE;
  292.       if ((Addr = (MAILADDRESS *)List.First ()) != NULL)
  293.          do {
  294.             write (fd, Addr, sizeof (MAILADDRESS));
  295.          } while ((Addr = (MAILADDRESS *)List.Next ()) != NULL);
  296.       close (fd);
  297.    }
  298.  
  299.    return (RetVal);
  300. }
  301.  
  302.  
  303.