home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / MODEM.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  12KB  |  419 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. #include "lorawin.h"
  22.  
  23. TModem::TModem (void)
  24. {
  25.    Serial = NULL;
  26.    Log = NULL;
  27.    Position = 0;
  28.    strcpy (Ring, "RING");
  29.  
  30. #if defined(__LINUX__)
  31.    strcpy (NodelistPath, "./nodes");
  32. #else
  33.    strcpy (NodelistPath, ".\\nodes");
  34. #endif
  35.    strcpy (DialCmd, "ATDT%s");
  36.    Terminal = FALSE;
  37. #if defined(__DOS__) || defined(__LINUX__)
  38.    if (Daemon == FALSE) {
  39.       if ((window = wopen (2, 49, 11, 78, 5, CYAN|_BLACK, CYAN|_BLACK)) != 0)
  40.          wprintf ("\n\n\n\n\n\n\n\n\n\n\n\n");
  41.       videoupdate ();
  42.    }
  43. #endif
  44. }
  45.  
  46. TModem::~TModem (void)
  47. {
  48.    if (Serial != NULL) {
  49.       Serial->SetDTR (FALSE);
  50.       Serial->SetRTS (FALSE);
  51.       delete Serial;
  52.    }
  53. #if defined(__DOS__) || defined(__LINUX__)
  54.    if (Daemon == FALSE) {
  55.       if (window != 0) {
  56.          wactiv (window);
  57.          wclose ();
  58.       }
  59.       videoupdate ();
  60.    }
  61. #endif
  62. }
  63.  
  64. USHORT TModem::GetResponse (VOID)
  65. {
  66.    USHORT RetVal = NO_RESPONSE, IsUpper;
  67.    CHAR c, *p;
  68.  
  69.    while (Serial->BytesReady () == TRUE && RetVal == NO_RESPONSE) {
  70.       c = (CHAR)Serial->ReadByte ();
  71.       if (Terminal == TRUE)
  72.          Serial->SendByte (c);
  73.       if (c == '\r' || Position >= sizeof (Response) - 1) {
  74.          Response[Position] = '\0';
  75.          if (Position > 0) {
  76.             if (!stricmp (Response, "OK"))
  77.                RetVal = OK;
  78.             else if (!stricmp (Response, "ERROR"))
  79.                RetVal = ERROR;
  80.             else if (!stricmp (Response, Ring))
  81.                RetVal = RING;
  82.             else if (!stricmp (Response, "FAX") || !stricmp (Response, "+FCON"))
  83.                RetVal = FAX;
  84.             else if (!strnicmp (Response, "CONNECT", 7)) {
  85.                if ((Speed = atol (&Response[7])) == 0L)
  86.                   Speed = 300L;
  87.                RetVal = CONNECT;
  88.             }
  89.             else if (!stricmp (Response, "NO CARRIER") || !stricmp (Response, "NO ANSWER") || !stricmp (Response, "BUSY"))
  90.                RetVal = NO_CARRIER;
  91.  
  92.             if (Terminal == TRUE) {
  93.                if (!strnicmp (Response, "ATD", 3) || !stricmp (Response, "ATA")) {
  94. #if defined(__OS2__) || defined(__NT__)
  95.                   if (hwndWindow != NULL) {
  96. #elif defined(__DOS__) || defined(__LINUX__)
  97.                   if (window != 0 && Daemon == FALSE) {
  98.                      wactiv (window);
  99. #endif
  100. #if defined(__OS2__)
  101.                      WinSendMsg (hwndWindow, WM_USER, MPFROMSHORT (WMU_ADDMODEMITEM), MPFROMP (Response));
  102. #elif defined(__NT__)
  103.                      SendMessage (hwndWindow, WM_USER, (WPARAM)WMU_ADDMODEMITEM, (LPARAM)Response);
  104. #elif defined(__DOS__) || defined(__LINUX__)
  105.                      wprintf ("\n%.28s", Response);
  106.                      videoupdate ();
  107. #endif
  108.                   }
  109.  
  110.                   Pause (10);
  111.                   Serial->SendBytes ((UCHAR *)"\r\n", 2);
  112.                   sprintf (Response, "CONNECT %lu", Serial->Speed);
  113.                   Serial->SendBytes ((UCHAR *)Response, (USHORT)strlen (Response));
  114.                   Serial->SendBytes ((UCHAR *)"\r\n", 2);
  115.                   Serial->SetDTR (TRUE);
  116.                   Speed = Serial->Speed;
  117.                   RetVal = CONNECT;
  118.                }
  119.                else if (!strnicmp (Response, "AT", 2)) {
  120.                   Serial->SendBytes ((UCHAR *)"\r\n", 2);
  121.                   Serial->SendBytes ((UCHAR *)"OK\r\n", 4);
  122.                }
  123.             }
  124.  
  125. #if defined(__OS2__) || defined(__NT__)
  126.             if (hwndWindow != NULL) {
  127. #elif defined(__DOS__) || defined(__LINUX__)
  128.             if (window != 0 && Daemon == FALSE) {
  129.                wactiv (window);
  130. #endif
  131. #if defined(__OS2__)
  132.                WinSendMsg (hwndWindow, WM_USER, MPFROMSHORT (WMU_ADDMODEMITEM), MPFROMP (Response));
  133. #elif defined(__NT__)
  134.                SendMessage (hwndWindow, WM_USER, (WPARAM)WMU_ADDMODEMITEM, (LPARAM)Response);
  135. #elif defined(__DOS__) || defined(__LINUX__)
  136.                wprintf ("\n%.28s", Response);
  137.                videoupdate ();
  138. #endif
  139.             }
  140.  
  141.             if (Log != NULL && RetVal != NO_RESPONSE && RetVal != OK && RetVal != ERROR) {
  142. //            if (Log != NULL && RetVal != NO_RESPONSE) {
  143.                p = Response;
  144.                IsUpper = TRUE;
  145.                while (*p != '\0') {
  146.                   if (IsUpper == TRUE) {
  147.                      *p = (CHAR)toupper (*p);
  148.                      IsUpper = FALSE;
  149.                   }
  150.                   else if (*p == ' ' || *p == '/')
  151.                      IsUpper = TRUE;
  152.                   else if (IsUpper == FALSE) {
  153.                      *p = (CHAR)tolower (*p);
  154.                      IsUpper = FALSE;
  155.                   }
  156.                   p++;
  157.                }
  158.                Log->Write ("+%s", Response);
  159.             }
  160.          }
  161.          Position = 0;
  162.       }
  163.       else if (c >= ' ')
  164.          Response[Position++] = c;
  165.    }
  166.  
  167.    if (RetVal == CONNECT && LockSpeed == FALSE)
  168.       Serial->SetParameters (Speed, 8, 'N', 1);
  169.  
  170.    return (RetVal);
  171. }
  172.  
  173. USHORT TModem::Initialize (ULONG comHandle)
  174. {
  175.    USHORT RetVal = FALSE;
  176.  
  177.    if (Serial != NULL) {
  178.       delete Serial;
  179.       Serial = NULL;
  180.    }
  181.    if (Serial == NULL)
  182.       Serial = new TSerial;
  183.  
  184.    if (Serial != NULL) {
  185. #if defined(__OS2__) || defined(__NT__) || defined(__LINUX__)
  186.       strcpy (Serial->Device, Device);
  187. #else
  188.       Serial->Com = (USHORT)atoi (&Device[3]);
  189. #endif
  190.       Serial->Speed = Speed;
  191.       if (comHandle == 0L) {
  192.          if (Serial->Initialize () == TRUE) {
  193.             if (Serial->Carrier () == FALSE) {
  194.                Serial->SetDTR (FALSE);
  195.                Serial->SetRTS (TRUE);
  196.             }
  197.             RetVal = TRUE;
  198.          }
  199.       }
  200.       else {
  201. #if defined(__OS2__)
  202.          Serial->hFile = (HFILE)comHandle;
  203. #elif defined(__NT__)
  204.          Serial->hFile = (HANDLE)comHandle;
  205. #endif
  206.          Serial->SetParameters (Serial->Speed, Serial->DataBits, Serial->Parity, Serial->StopBits);
  207.          if (Serial->Carrier () == FALSE) {
  208.             Serial->SetDTR (FALSE);
  209.             Serial->SetRTS (TRUE);
  210.          }
  211.          RetVal = TRUE;
  212.       }
  213.    }
  214.  
  215.    return (RetVal);
  216. }
  217.  
  218. VOID TModem::Poll (PSZ pszNode)
  219. {
  220.    FILE *fp;
  221.    CHAR Number[64], Temp[128], Traslated[128], *p;
  222.    class TAddress Addr;
  223.    class TNodes *Nodes;
  224.    class TNodeFlags *Flags;
  225.  
  226.    strcpy (DialCmd, Cfg->Dial);
  227.    strcpy (Number, pszNode);
  228.  
  229.    if (strchr (pszNode, '/') != NULL || strchr (pszNode, ':') != NULL) {
  230.       if ((Nodes = new TNodes (NodelistPath)) != NULL) {
  231.          Addr.Parse (pszNode);
  232.          if (Nodes->Read (Addr) == TRUE) {
  233.             if (Log != NULL)
  234.                Log->Write ("*Processing %s - %s", Nodes->Address, Nodes->SystemName);
  235.             strcpy (Number, Nodes->Phone);
  236.             if (Nodes->DialCmd[0] != '\0')
  237.                strcpy (DialCmd, Nodes->DialCmd);
  238.             else {
  239.                if ((Flags = new TNodeFlags (Cfg->SystemPath)) != NULL) {
  240.                   if (Flags->Read (Nodes->Flags) == TRUE) {
  241.                      if (Flags->Cmd[0] != '\0')
  242.                         strcpy (DialCmd, Flags->Cmd);
  243.                   }
  244.                   delete Flags;
  245.                }
  246.             }
  247.          }
  248.          delete Nodes;
  249.       }
  250.    }
  251.  
  252.    if (Number[0] == '+')
  253.       strcpy (Number, &Number[1]);
  254.  
  255.    sprintf (Temp, "%scost.cfg", NodelistPath);
  256.    if ((fp = _fsopen (Temp, "rt", SH_DENYNO)) != NULL) {
  257.       while (fgets (Temp, sizeof (Temp) - 1, fp) != NULL) {
  258.          if ((p = strchr (Temp, '\n')) != NULL)
  259.             *p = '\0';
  260.          if ((p = strchr (Temp, '\r')) != NULL)
  261.             *p = '\0';
  262.          if ((p = strtok (Temp, " ")) != NULL) {
  263.             if (!strcmp (p, "Prefix")) {
  264.                if ((p = strtok (NULL, " ")) != NULL) {
  265.                   if (!strncmp (Number, p, strlen (p)) || !strcmp (p, "-")) {
  266.                      if (!strcmp (p, "-"))
  267.                         p = "";
  268.                      strcpy (Traslated, &Number[strlen (p)]);
  269.                      if ((p = strtok (NULL, " ")) != NULL) {
  270.                         if (!strcmp (p, "/"))
  271.                            p = "";
  272.                         strcpy (Number, p);
  273.                         strcat (Number, Traslated);
  274.                      }
  275.                      break;
  276.                   }
  277.                }
  278.             }
  279.          }
  280.       }
  281.       fclose (fp);
  282.    }
  283.  
  284.    if (Log != NULL)
  285.       Log->Write (":Dialing %s", Number);
  286.  
  287.    if (Terminal == TRUE)
  288.       SendCommand ("RING");
  289.    else {
  290.       sprintf (Temp, DialCmd, Number);
  291.       SendCommand (Temp);
  292.    }
  293. }
  294.  
  295. VOID TModem::SendCommand (PSZ pszCmd)
  296. {
  297.    if (Terminal == FALSE) {
  298.       Serial->SetDTR (TRUE);
  299.       Pause (10);
  300.    }
  301.  
  302.    while (*pszCmd) {
  303.       switch (*pszCmd) {
  304.          case '|':
  305.             Serial->SendByte ((char)13);
  306.             Pause (10);
  307.             break;
  308.  
  309.          case 'v':
  310.             Serial->SetDTR (FALSE);
  311.             Pause (10);
  312.             break;
  313.  
  314.          case '^':
  315.             Serial->SetDTR (TRUE);
  316.             Pause (10);
  317.             break;
  318.  
  319.          case '~':
  320.             Pause (50);
  321.             break;
  322.  
  323.          case '`':
  324.             Pause (10);
  325.             break;
  326.  
  327.          default:
  328.             Serial->SendByte (*pszCmd);
  329.             break;
  330.       }
  331.       pszCmd++;
  332.    }
  333.  
  334.    Serial->SendByte ((char)13);
  335.    Pause (10);
  336. }
  337.  
  338. /*
  339. USHORT TModem::ReceiveFax (PSZ path)
  340. {
  341.    int i, j;
  342.  
  343.    for (i = 0; i < 256; i++)
  344.       j = (((i & 0x01) << 7) |
  345.            ((i & 0x02) << 5) |
  346.            ((i & 0x04) << 3) |
  347.            ((i & 0x08) << 1) |
  348.            ((i & 0x10) >> 1) |
  349.            ((i & 0x20) >> 3) |
  350.            ((i & 0x40) >> 5) |
  351.            ((i & 0x80) >> 7));
  352.       swaptable[i] = (unsigned char)j;
  353.    }
  354. }
  355.  
  356. #define ETX    0x03
  357. #define DLE    0x10
  358. #define DC2    0x12
  359.  
  360. USHORT TModem::ReadG3Stream (VOID)
  361. {
  362.    USHORT RetVal = FALSE, c, faxsize = 0;
  363.    CHAR e_input_buf[11];
  364.    UCHAR *secbuf, *p;
  365.    ULONG ltimer = 0L;
  366.  
  367.    Serial->ClearInbound ();
  368.  
  369.    if ((secbuf = (UCHAR *)malloc (1024)) != NULL) {
  370.       p = secbuf;
  371.       Serial->SendByte (DC2);
  372.  
  373.       while (Serial->Carrier () == TRUE) {
  374.          if (Serial->BytesReady () == TRUE) {
  375.             c = Serial->ReadByte ();
  376.             if (c == DLE) {
  377.                while (Serial->BytesReady () == FALSE)
  378.                   ;
  379.                if ((c = Serial->ReadByte ()) == ETX)
  380.                   break;
  381.             }
  382.  
  383.             *p++ = swaptable[(unsigned char)c];
  384.             faxsize++;
  385.  
  386.             if ((faxsize % 1024) == 0) {
  387.                if (fax_fp != NULL)
  388.                   fwrite (secbuf, 1, 1024, fax_fp);
  389.                p = secbuf;
  390.             }
  391.          }
  392.       }
  393.  
  394.       if ((faxsize % 1024) != 0)
  395.          fwrite (secbuf, 1, faxsize % 1024, fp);
  396.  
  397.       free (secbuf);
  398.  
  399.       c = 0;
  400.       post_page_code = -1;
  401.       RetVal = TRUE;
  402.  
  403.       do {
  404.          switch (GetResponse ()) {
  405.             case NO_CARRIER:
  406.             case ERROR:
  407.             case FHNG:
  408.             case FHS:
  409.                RetVal = FALSE;
  410.                break;
  411.             case
  412.          }
  413.       } while (post_page_code == -1);
  414.    }
  415.  
  416.    return (RetVal);
  417. }
  418. */
  419.