home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / INTERNET.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  59KB  |  1,257 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.h"
  21.  
  22. #define SE                 240
  23. #define NOP                241
  24. #define DM                 242
  25. #define BREAK              243
  26. #define IP                 244
  27. #define AO                 245
  28. #define AYT                246
  29. #define EC                 247
  30. #define EL                 248
  31. #define GOAHEAD            249
  32. #define SB                 250
  33. #define WILLTEL            251
  34. #define WONTTEL            252
  35. #define DOTEL              253
  36. #define DONTTEL            254
  37. #define IAC                255
  38.  
  39. #define BINARY             0
  40. #define IECHO              1
  41. #define RECONNECT          2
  42. #define SGA                3
  43. #define AMSN               4
  44. #define STATUS             5
  45. #define TIMING             6
  46. #define RCTAN              7
  47. #define OLW                8
  48. #define OPS                9
  49. #define OCRD               10
  50. #define OHTS               11
  51. #define OHTD               12
  52. #define OFFD               13
  53. #define OVTS               14
  54. #define OVTD               15
  55. #define OLFD               16
  56. #define XASCII             17
  57. #define LOGOUT             18
  58. #define BYTEM              19
  59. #define DET                20
  60. #define SUPDUP             21
  61. #define SUPDUPOUT          22
  62. #define SENDLOC            23
  63. #define TERMTYPE           24
  64. #define EOR                25
  65. #define TACACSUID          26
  66. #define OUTPUTMARK         27
  67. #define TERMLOCNUM         28
  68. #define REGIME3270         29
  69. #define X3PAD              30
  70. #define NAWS               31
  71. #define TERMSPEED          32
  72. #define TFLOWCNTRL         33
  73. #define LINEMODE           34
  74. #define XDISPLOC           35
  75. #define ENVIRONMENT        36
  76. #define AUTHENTICATION     37
  77. #define DATA_ENCRYPTION    38
  78.  
  79. TInternet::TInternet (void)
  80. {
  81.    Cfg = NULL;
  82.    Com = Snoop = NULL;
  83.    Log = NULL;
  84.    Embedded = NULL;
  85.    User = NULL;
  86. }
  87.  
  88. TInternet::~TInternet (void)
  89. {
  90. }
  91.  
  92. VOID TInternet::Telnet (PSZ pszServer, USHORT usPort)
  93. {
  94.    USHORT c, Exit;
  95.    CHAR Temp[30];
  96.  
  97.    if (pszServer == NULL || pszServer[0] == '\0') {
  98.       Embedded->Printf ("\n\026\001\017Host name? ");
  99.       Embedded->GetString (Host, (USHORT)(sizeof (Host) - 1), 0);
  100.       pszServer = Host;
  101.    }
  102.  
  103.    if (Embedded->AbortSession () == FALSE && Host[0] != '\0') {
  104.       Embedded->Printf ("\nTrying...");
  105.  
  106.       if ((Tcp = new TTcpip) != NULL) {
  107.          if (Tcp->ConnectServer (pszServer, usPort) == TRUE) {
  108.             Log->Write ("+Telnet to %s", pszServer);
  109.  
  110.             Embedded->Printf ("\014Connected to %s.\nEscape character is '^]'\n\n", pszServer);
  111.  
  112.             Tcp->BufferByte (IAC);
  113.             Tcp->BufferByte (DOTEL);
  114.             Tcp->BufferByte (IECHO);
  115.             Tcp->BufferByte (IAC);
  116.             Tcp->BufferByte (DOTEL);
  117.             Tcp->BufferByte (BINARY);
  118.             Tcp->UnbufferBytes ();
  119.  
  120.             Exit = FALSE;
  121.  
  122.             do {
  123.                if (Tcp->BytesReady () == TRUE) {
  124.                   do {
  125.                      if ((c = Tcp->ReadByte ()) == IAC) {
  126.                         if ((c = Tcp->ReadByte ()) != IAC) {
  127.                            if (c == WILLTEL) {
  128.                               c = Tcp->ReadByte ();
  129.                               Tcp->BufferByte (IAC);
  130.                               if (c == BINARY || c == IECHO && c != SGA)
  131.                                  Tcp->BufferByte (DOTEL);
  132.                               else
  133.                                  Tcp->BufferByte (DONTTEL);
  134.                               Tcp->BufferByte ((UCHAR)c);
  135.                               Tcp->UnbufferBytes ();
  136.                            }
  137.                            else if (c == WONTTEL) {
  138.                               c = Tcp->ReadByte ();
  139.                               if (c != BINARY && c != IECHO && c != SGA) {
  140.                                  Tcp->BufferByte (IAC);
  141.                                  Tcp->BufferByte (DONTTEL);
  142.                                  Tcp->BufferByte ((UCHAR)c);
  143.                                  Tcp->UnbufferBytes ();
  144.                               }
  145.                            }
  146.                            else if (c == DOTEL) {
  147.                               c = Tcp->ReadByte ();
  148.                               if (c == TERMTYPE) {
  149.                                  Tcp->BufferByte (IAC);
  150.                                  Tcp->BufferByte (WILLTEL);
  151.                                  Tcp->BufferByte (TERMTYPE);
  152.                               }
  153.                               else if (c != BINARY && c != IECHO && c != SGA) {
  154.                                  Tcp->BufferByte (IAC);
  155.                                  Tcp->BufferByte (WONTTEL);
  156.                                  Tcp->BufferByte ((UCHAR)c);
  157.                               }
  158.                               Tcp->UnbufferBytes ();
  159.                            }
  160.                            else if (c == DONTTEL) {
  161.                               c = Tcp->ReadByte ();
  162.                               if (c != BINARY && c != IECHO && c != SGA) {
  163.                                  Tcp->BufferByte (IAC);
  164.                                  Tcp->BufferByte (WONTTEL);
  165.                                  Tcp->BufferByte ((UCHAR)c);
  166.                                  Tcp->UnbufferBytes ();
  167.                               }
  168.                            }
  169.                            else if (c == SB) {
  170.                               if ((c = Tcp->ReadByte ()) == TERMTYPE) {
  171.                                  if ((c = Tcp->ReadByte ()) == 1) {
  172.                                     Tcp->BufferByte (IAC);
  173.                                     Tcp->BufferByte (SB);
  174.                                     Tcp->BufferByte (TERMTYPE);
  175.                                     Tcp->BufferByte (0);
  176.                                     Tcp->BufferBytes ((UCHAR *)"VT100", 5);
  177.                                     Tcp->BufferByte (IAC);
  178.                                     Tcp->BufferByte (SE);
  179.                                     Tcp->UnbufferBytes ();
  180.                                  }
  181.                               }
  182.                            }
  183.                         }
  184.                         else {
  185.                            Com->BufferByte ((UCHAR)c);
  186.                            if (Snoop != NULL)
  187.                               Snoop->BufferByte ((UCHAR)c);
  188.                         }
  189.                      }
  190.                      else {
  191.                         Com->BufferByte ((UCHAR)c);
  192.                         if (Snoop != NULL)
  193.                            Snoop->BufferByte ((UCHAR)c);
  194.                         if (c == 0x0A) {
  195.                            Com->BufferByte (13);
  196.                            if (Snoop != NULL)
  197.                               Snoop->BufferByte (13);
  198.                         }
  199.                      }
  200.                   } while (Tcp->RxBytes > 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE && Exit == FALSE);
  201.  
  202.                   Com->UnbufferBytes ();
  203.                   if (Snoop != NULL)
  204.                      Snoop->UnbufferBytes ();
  205.                }
  206.  
  207.                if (Com->BytesReady () == TRUE) {
  208.                   do {
  209.                      c = Com->ReadByte ();
  210.                      if (c == 0x1D) {
  211.                         Tcp->UnbufferBytes ();
  212.                         Embedded->Printf ("\n");
  213.                         do {
  214.                            Embedded->Printf ("\x16\x01\013telnet> ");
  215.                            Embedded->Input (Temp, (USHORT)(sizeof (Temp) - 1), 0);
  216.                            if (!stricmp (Temp, "quit")) {
  217.                               if (Tcp->Carrier () == TRUE)
  218.                                  Tcp->ClosePort ();
  219.                               Exit = TRUE;
  220.                            }
  221.                            else if (!stricmp (Temp, "close")) {
  222.                               Tcp->ClosePort ();
  223.                               Embedded->Printf ("Connection closed.\n");
  224.                            }
  225.                         } while (Temp[0] != '\0' && Embedded->AbortSession () == FALSE);
  226.                      }
  227.                      else {
  228.                         Tcp->BufferByte ((UCHAR)c);
  229.                         if (c == IAC)
  230.                            Tcp->BufferByte ((UCHAR)c);
  231.                      }
  232.                   } while (Com->RxBytes > 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE && Exit == FALSE);
  233.  
  234.                   Tcp->UnbufferBytes ();
  235.                }
  236.             } while (Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE && Exit == FALSE);
  237.  
  238.             Log->Write ("+End Telnet");
  239.  
  240.             Embedded->Printf ("\n");
  241.             Embedded->Printf ("\026\001\007Connection closed.\n");
  242.          }
  243.          Tcp->ClosePort ();
  244.          delete Tcp;
  245.       }
  246.    }
  247. }
  248.  
  249. VOID TInternet::Finger (PSZ pszServer, USHORT usPort)
  250. {
  251.    CHAR String[24], c;
  252.  
  253.    if (pszServer == NULL || pszServer[0] == '\0') {
  254.       Embedded->Printf ("\n\026\001\017Host name? ");
  255.       Embedded->GetString (Host, (USHORT)(sizeof (Host) - 1), 0);
  256.       pszServer = Host;
  257.    }
  258.  
  259.    if (Embedded->AbortSession () == FALSE && Host[0] != '\0') {
  260.       if ((Tcp = new TTcpip) != NULL) {
  261.          if (Tcp->ConnectServer (pszServer, usPort) == TRUE) {
  262.             Log->Write ("+Finger => %s", pszServer);
  263.  
  264.             Embedded->Printf ("\n\026\001\017User name? ");
  265.             Embedded->GetString (String, (USHORT)(sizeof (String) - 1), 0);
  266.  
  267.             Tcp->SendBytes ((UCHAR *)String, (USHORT)strlen (String));
  268.             Tcp->SendBytes ((UCHAR *)"\r\n", 2);
  269.  
  270.             Embedded->Printf ("\n\x16\x01\x07");
  271.  
  272.             while (Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE) {
  273.                if (Tcp->BytesReady () == TRUE) {
  274.                   c = (CHAR)Tcp->ReadByte ();
  275.                   if (Com != NULL)
  276.                      Com->BufferByte ((UCHAR)c);
  277.                   if (Snoop != NULL)
  278.                      Snoop->BufferByte ((UCHAR)c);
  279.                }
  280.             }
  281.  
  282.             if (Com != NULL)
  283.                Com->UnbufferBytes ();
  284.             if (Snoop != NULL)
  285.                Snoop->UnbufferBytes ();
  286.          }
  287.  
  288.          Tcp->ClosePort ();
  289.          delete Tcp;
  290.       }
  291.    }
  292. }
  293.  
  294. USHORT TInternet::GetResponse (PSZ pszResponse, USHORT usMaxLen)
  295. {
  296.    USHORT retVal = FALSE, len = 0;
  297.    CHAR c, *pszResp = pszResponse;
  298.  
  299.    do {
  300.       if (Tcp->BytesReady () == TRUE)
  301.          do {
  302.             if ((c = (CHAR)Tcp->ReadByte ()) != '\r') {
  303.                if (c != '\n') {
  304.                   *pszResp++ = c;
  305.                   if (++len >= usMaxLen)
  306.                      c = '\r';
  307.                }
  308.             }
  309.          } while (Tcp->BytesReady () == TRUE && c != '\r');
  310.    } while (c != '\r' && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  311.  
  312.    *pszResp = '\0';
  313.    if (pszResponse[3] == ' ')
  314.       retVal = (USHORT)atoi (pszResponse);
  315.  
  316.    return (retVal);
  317. }
  318.  
  319. // ----------------------------------------------------------------------
  320. // Waits for an incoming connection for the FTP file transfer. This
  321. // routine is used to receive both ASCII and BINARY files for real files
  322. // and directory listings.
  323. // ----------------------------------------------------------------------
  324. VOID TInternet::FTP_GET (PSZ pszFile, PSZ pszName, USHORT fHash)
  325. {
  326.    FILE *fp;
  327.    USHORT Len, Counter;
  328.    CHAR Name[64], *p;
  329.    ULONG Size, Elapsed;
  330.  
  331.    while (Data->WaitClient () == 0) {
  332.       if (Embedded->AbortSession () == TRUE || Tcp->Carrier () == FALSE)
  333.          return;
  334.    }
  335.  
  336.    if (pszFile != NULL) {
  337.       if (Log != NULL)
  338.          Log->Write (" Receiving %s", pszFile);
  339.       if ((fp = _fsopen (pszFile, "wb", SH_DENYNO)) == NULL) {
  340.          if ((p = strchr (pszFile, '\0')) != NULL) {
  341.             while (p > pszFile && *p != '\\' && *p != ':' && *p != '/')
  342.                p--;
  343.             strcpy (Name, ++p);
  344.             Name[12] = '\0';
  345.             while ((p = strchr (Name, '.')) != NULL)
  346.                *p = '_';
  347.             if (strlen (Name) > 8)
  348.                Name[8] = '.';
  349.             strcpy (p, Name);
  350.          }
  351.       }
  352.    }
  353.  
  354.    Size = 0;
  355.    Counter = 0;
  356.    Elapsed = time (NULL);
  357.  
  358. // ----------------------------------------------------------------------
  359. // Bytes receiving loop. This loop runs until the remote host drops the
  360. // connection, signalling that the transmission is ended.
  361. // ----------------------------------------------------------------------
  362.    do {
  363.       while (Data->BytesReady () == TRUE && Embedded->AbortSession () == FALSE) {
  364.          Len = Data->ReadBytes (Buffer, sizeof (Buffer));
  365.          Size += Len;
  366. // ----------------------------------------------------------------------
  367. // This is a failsafe routine, if a file was request but the fopen fails,
  368. // each byte is received but nothing is saved (I think that's should be
  369. // called failsafe...).
  370. // ----------------------------------------------------------------------
  371.          if (pszFile == NULL) {
  372.             Com->BufferBytes (Buffer, Len);
  373.             if (Snoop != NULL)
  374.                Snoop->BufferBytes (Buffer, Len);
  375.          }
  376.          else if (fp != NULL)
  377.             fwrite (Buffer, Len, 1, fp);
  378. // ----------------------------------------------------------------------
  379. // Hash mark printing after each 1024 bytes packet received.
  380. // ----------------------------------------------------------------------
  381.          Counter += Len;
  382.          if (fHash == TRUE && Counter >= 1024) {
  383.             Embedded->Putch ((UCHAR)'#');
  384.             Counter -= 1024;
  385.          }
  386.       }
  387.    } while (Data->Carrier () == TRUE && Embedded->AbortSession () == FALSE);
  388.  
  389.    Com->UnbufferBytes ();
  390.    if (Snoop != NULL)
  391.       Snoop->UnbufferBytes ();
  392.  
  393.    if (fHash == TRUE && Size >= 1024L)
  394.       Embedded->Printf ("\n");
  395.    if (pszFile != NULL && fp != NULL)
  396.       fclose (fp);
  397.  
  398.    if ((Elapsed = time (NULL) - Elapsed) == 0L)
  399.       Elapsed = 1L;
  400.    Embedded->Printf ("%lu bytes received in %lu seconds (%lu bytes/sec.).\n", Size, Elapsed, Size / Elapsed);
  401.    if (pszFile != NULL && Log != NULL)
  402.       Log->Write ("+Received %s, %lu bytes", pszFile, Size);
  403.  
  404.    if (pszFile != NULL && pszName != NULL && User != NULL) {
  405.       User->FileTag->New ();
  406.       strcpy (User->FileTag->Name, pszName);
  407.       strcpy (User->FileTag->Area, "USER");
  408.       strcpy (User->FileTag->Complete, pszFile);
  409.       User->FileTag->Size = Size;
  410.       User->FileTag->DeleteAfter = TRUE;
  411.       User->FileTag->Add ();
  412.       Embedded->Printf ("\x16\x01\x0AThe file %s is tagged for download\n", pszName);
  413.       if (Log != NULL)
  414.          Log->Write (":Tagged file %s, library %s", pszName, User->FileTag->Area);
  415.    }
  416. }
  417.  
  418. VOID TInternet::FTP_MGET (PSZ pszFile)
  419. {
  420.    FILE *fp;
  421.    USHORT i;
  422.    CHAR File[128], *p;
  423.  
  424. // ----------------------------------------------------------------------
  425. // Delete the existing nlst.tmp file in order to prevent wrong requests
  426. // in case the directory listings is not retrived correctly.
  427. // ----------------------------------------------------------------------
  428.    sprintf (Temp, "%s%s\\", Cfg->UsersHomePath, User->MailBox);
  429.    BuildPath (Temp);
  430.    strcat (Temp, "nlst.tmp");
  431.    unlink (Temp);
  432.  
  433. // ----------------------------------------------------------------------
  434. // Set the data type to ASCII in order to receive the list of file
  435. // names to receive in the Multiple GET command.
  436. // ----------------------------------------------------------------------
  437.    Tcp->SendBytes ((UCHAR *)"TYPE A\r\n", 8);
  438.    do {
  439.       i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  440.    } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  441.  
  442.    if (Embedded->AbortSession () == FALSE && (Data = new TTcpip) != NULL) {
  443. // ----------------------------------------------------------------------
  444. // Retrive the file names that matches the name requested by the user.
  445. // The NSLT command is used because we only need the name of the files.
  446. // ----------------------------------------------------------------------
  447.       Data->Initialize (DataPort);
  448.  
  449.       sprintf (Temp, "PORT %lu,%lu,%lu,%lu,%u,%u\r\n", (Data->HostID & 0xFF000000L) >> 24, (Data->HostID & 0xFF0000L) >> 16, (Data->HostID & 0xFF00L) >> 8, Data->HostID & 0xFFL, (DataPort & 0xFF00) >> 8, DataPort & 0xFF);
  450.       Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  451.       do {
  452.          i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  453.       } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  454.       if (i == 200) {
  455.          if ((p = strtok (pszFile, " ")) != NULL) {
  456.             sprintf (Temp, "NLST %s\r\n", p);
  457.             Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  458.          }
  459.          else
  460.             Tcp->SendBytes ((UCHAR *)"NLST\r\n", 6);
  461.          do {
  462.             i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  463.          } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  464.          if (i == 150) {
  465. #if defined(__LINUX__)
  466.             sprintf (Temp, "%s%s/nlst.tmp", Cfg->UsersHomePath, User->MailBox);
  467. #else
  468.             sprintf (Temp, "%s%s\\nlst.tmp", Cfg->UsersHomePath, User->MailBox);
  469. #endif
  470.             FTP_GET (Temp, NULL, FALSE);
  471.             do {
  472.                i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  473.             } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  474.          }
  475.       }
  476.  
  477.       DataPort += 2;
  478.       if (DataPort > FTPDATA_PORT + 1024)
  479.          DataPort = FTPDATA_PORT;
  480.  
  481.       Data->ClosePort ();
  482.       delete Data;
  483.    }
  484.  
  485. #if defined(__LINUX__)
  486.    sprintf (Temp, "%s%s/nlst.tmp", Cfg->UsersHomePath, User->MailBox);
  487. #else
  488.    sprintf (Temp, "%s%s\\nlst.tmp", Cfg->UsersHomePath, User->MailBox);
  489. #endif
  490.    if ((fp = _fsopen (Temp, "rt", SH_DENYNO)) != NULL) {
  491. // ----------------------------------------------------------------------
  492. // If the file names list was successfully retrived we can read one line
  493. // at a time and request the corresponding file from the remote host.
  494. // ----------------------------------------------------------------------
  495.       if (Binary == TRUE)
  496.          Tcp->SendBytes ((UCHAR *)"TYPE I\r\n", 8);
  497.       else
  498.          Tcp->SendBytes ((UCHAR *)"TYPE A\r\n", 8);
  499.       do {
  500.          i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  501.       } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  502.  
  503.       while (Embedded->AbortSession () == FALSE && fgets (File, sizeof (File) - 1, fp) != NULL) {
  504. // ----------------------------------------------------------------------
  505. // This is the file request/receive loop that run until every file was
  506. // retrived or the user drops the carrier (why wasting time, network
  507. // resources and disk space if the user is gone ???).
  508. // ----------------------------------------------------------------------
  509.          if ((p = strchr (File, 0x0A)) != NULL)
  510.             *p = '\0';
  511.  
  512.          if ((Data = new TTcpip) != NULL) {
  513.             Data->Initialize (DataPort);
  514.  
  515.             sprintf (Temp, "PORT %lu,%lu,%lu,%lu,%u,%u\r\n", (Data->HostID & 0xFF000000L) >> 24, (Data->HostID & 0xFF0000L) >> 16, (Data->HostID & 0xFF00L) >> 8, Data->HostID & 0xFFL, (DataPort & 0xFF00) >> 8, DataPort & 0xFF);
  516.             Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  517.             do {
  518.                i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  519.                Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  520.             } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  521.             if (i == 200) {
  522.                sprintf (Temp, "RETR %s\r\n", File);
  523.                Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  524.                do {
  525.                   i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  526.                   Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  527.                } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  528.                if (i == 150) {
  529.                   Log->Write ("+%s", &Temp[4]);
  530. #if defined(__LINUX__)
  531.                   sprintf (Temp, "%s%s/%s", Cfg->UsersHomePath, User->MailBox, File);
  532. #else
  533.                   sprintf (Temp, "%s%s\\%s", Cfg->UsersHomePath, User->MailBox, File);
  534. #endif
  535.                   FTP_GET (Temp, File, Hash);
  536.                   do {
  537.                      i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  538.                      Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  539.                   } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  540.                }
  541.             }
  542.  
  543.             DataPort += 2;
  544.             if (DataPort > FTPDATA_PORT + 1024)
  545.                DataPort = FTPDATA_PORT;
  546.  
  547.             Data->ClosePort ();
  548.             delete Data;
  549.          }
  550.       }
  551.  
  552.       fclose (fp);
  553.  
  554. #if defined(__LINUX__)
  555.       sprintf (Temp, "%s%s/nlst.tmp", Cfg->UsersHomePath, User->MailBox);
  556. #else
  557.       sprintf (Temp, "%s%s\\nlst.tmp", Cfg->UsersHomePath, User->MailBox);
  558. #endif
  559.       unlink (Temp);
  560.    }
  561. }
  562.  
  563. VOID TInternet::FTP_PUT (PSZ pszFile, USHORT fHash, USHORT fBinary)
  564. {
  565.    FILE *fp;
  566.    USHORT i;
  567.    ULONG Size, Elapsed;
  568.  
  569.    while (Data->WaitClient () == 0) {
  570.       if (Embedded->AbortSession () == TRUE || Tcp->Carrier () == FALSE)
  571.          return;
  572.    }
  573.  
  574.    if (pszFile != NULL)
  575.       fp = _fsopen (pszFile, (fBinary == TRUE) ? "rb" : "rt", SH_DENYNO);
  576.  
  577.    Size = 0;
  578.    Elapsed = time (NULL);
  579.  
  580.    do {
  581.       i = (USHORT)fread (Buffer, 1, sizeof (Buffer), fp);
  582.       Data->BufferBytes (Buffer, i);
  583.       Size += i;
  584.       if (fHash == TRUE && Size != 0L && (Size % 1024L) == 0L)
  585.          Embedded->Putch ((UCHAR)'#');
  586.    } while (Data->Carrier () == TRUE && i == sizeof (Buffer) && Embedded->AbortSession () == FALSE);
  587.  
  588.    if (Embedded->AbortSession () == FALSE) {
  589.       if (Data->Carrier () == TRUE)
  590.          Data->UnbufferBytes ();
  591.  
  592.       if (fHash == TRUE && Size >= 1024L)
  593.          Embedded->Printf ("\n");
  594.       if (pszFile != NULL && fp != NULL)
  595.          fclose (fp);
  596.  
  597.       if ((Elapsed = time (NULL) - Elapsed) == 0L)
  598.          Elapsed = 1L;
  599.       Embedded->Printf ("%lu bytes sent in %lu seconds (%lu bytes/sec.).\n", Size, Elapsed, Size / Elapsed);
  600.    }
  601.  
  602.    Data->ClosePort ();
  603. }
  604.  
  605. VOID TInternet::FTP (PSZ pszServer, USHORT usPort)
  606. {
  607.    USHORT i;
  608.    CHAR *p, *Local, *Old, *New;
  609.  
  610.    Binary = Hash = TRUE;
  611.    DataPort = FTPDATA_PORT;
  612.  
  613.    if (pszServer == NULL || pszServer[0] == '\0') {
  614.       Embedded->Printf ("\n\026\001\017Host name? ");
  615.       Embedded->GetString (Host, (USHORT)(sizeof (Host) - 1), 0);
  616.       pszServer = Host;
  617.    }
  618.  
  619.    if (Embedded->AbortSession () == FALSE && Host[0] != '\0') {
  620.       if ((Tcp = new TTcpip) != NULL) {
  621.          if (Tcp->ConnectServer (pszServer, usPort) == TRUE) {
  622.             if (Log != NULL)
  623.                Log->Write ("+Opening FTP connection to %s", pszServer);
  624.  
  625.             do {
  626.                i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  627.                Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  628.             } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  629.  
  630.             if (i == 220) {
  631.                Embedded->Printf ("\x16\x01\013Username: ");
  632.                Embedded->GetString (Cmd, (USHORT)(sizeof (Cmd) - 1), 0);
  633.                sprintf (Temp, "USER %s\r\n", Cmd);
  634.                Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  635.                if (Log != NULL)
  636.                   Log->Write ("   %s", Temp);
  637.                if (Embedded->AbortSession () == FALSE)
  638.                   do {
  639.                      i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  640.                      Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  641.                   } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  642.  
  643.                if (i == 331) {
  644.                   Embedded->Printf ("\x16\x01\013Password: ");
  645.                   Embedded->GetString (Cmd, (USHORT)(sizeof (Cmd) - 1), INP_PWD);
  646.                   sprintf (Temp, "PASS %s\r\n", Cmd);
  647.                   Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  648.                   if (Log != NULL)
  649.                      Log->Write ("   %s", Temp);
  650.                   if (Embedded->AbortSession () == FALSE)
  651.                      do {
  652.                         i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  653.                         Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  654.                      } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  655.                }
  656.             }
  657.  
  658.             if (i == 230) {
  659.                while (Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE) {
  660.                   Embedded->Printf ("\x16\x01\013ftp> ");
  661.                   Embedded->GetString (Cmd, (USHORT)(sizeof (Cmd) - 1), 0);
  662.  
  663.                   if ((p = strtok (Cmd, " ")) != NULL) {
  664.                      if (!stricmp (p, "?") || !stricmp (p, "help"))
  665.                         Embedded->DisplayFile ("ftphelp");
  666.                      else if (!stricmp (p, "account")) {
  667.                         if ((p = strtok (NULL, " ")) != NULL) {
  668.                            sprintf (Temp, "ACCT %s\r\n", p);
  669.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  670.                            do {
  671.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  672.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  673.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  674.                         }
  675.                      }
  676.                      else if (!stricmp (p, "ascii")) {
  677.                         Binary = FALSE;
  678.                         Embedded->Printf ("\x16\x01\007200 Type set to A.\n");
  679.                      }
  680.                      else if (!strnicmp (p, "bin", 3) || !stricmp (p, "image")) {
  681.                         Binary = TRUE;
  682.                         Embedded->Printf ("\x16\x01\007200 Type set to I.\n");
  683.                      }
  684.                      else if (!stricmp (p, "cd")) {
  685.                         if ((p = strtok (NULL, " ")) != NULL) {
  686.                            sprintf (Temp, "CWD %s\r\n", p);
  687.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  688.                            do {
  689.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  690.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  691.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  692.                         }
  693.                      }
  694.                      else if (!stricmp (p, "cdup")) {
  695.                         Tcp->SendBytes ((UCHAR *)"CDUP\r\n", 6);
  696.                         do {
  697.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  698.                            Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  699.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  700.                      }
  701.                      else if (!strnicmp (p, "del", 3)) {
  702.                         if ((p = strtok (NULL, " ")) != NULL) {
  703.                            sprintf (Temp, "DELE %s\r\n", p);
  704.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  705.                            do {
  706.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  707.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  708.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  709.                         }
  710.                      }
  711.                      else if (!stricmp (p, "dir") || !stricmp (p, "ls")) {
  712.                         Tcp->SendBytes ((UCHAR *)"TYPE A\r\n", 8);
  713.                         do {
  714.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  715.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  716.  
  717.                         if ((Data = new TTcpip) != NULL) {
  718.                            Data->Initialize (DataPort);
  719.  
  720.                            sprintf (Temp, "PORT %lu,%lu,%lu,%lu,%u,%u\r\n", (Data->HostID & 0xFF000000L) >> 24, (Data->HostID & 0xFF0000L) >> 16, (Data->HostID & 0xFF00L) >> 8, Data->HostID & 0xFFL, (DataPort & 0xFF00) >> 8, DataPort & 0xFF);
  721.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  722.                            do {
  723.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  724.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  725.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  726.                            if (i == 200) {
  727.                               if ((p = strtok (NULL, " ")) != NULL) {
  728.                                  sprintf (Temp, "LIST %s\r\n", p);
  729.                                  Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  730.                               }
  731.                               else
  732.                                  Tcp->SendBytes ((UCHAR *)"LIST\r\n", 6);
  733.                               do {
  734.                                  i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  735.                                  Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  736.                               } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  737.                               if (i == 150) {
  738.                                  FTP_GET (NULL, NULL, FALSE);
  739.                                  do {
  740.                                     i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  741.                                     Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  742.                                  } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  743.                               }
  744.                            }
  745.  
  746.                            DataPort += 2;
  747.                            if (DataPort > FTPDATA_PORT + 1024)
  748.                               DataPort = FTPDATA_PORT;
  749.  
  750.                            Data->ClosePort ();
  751.                            delete Data;
  752.                         }
  753.                      }
  754.                      else if (!stricmp (p, "get") || !stricmp (p, "retr")) {
  755.                         if (Binary == TRUE)
  756.                            Tcp->SendBytes ((UCHAR *)"TYPE I\r\n", 8);
  757.                         else
  758.                            Tcp->SendBytes ((UCHAR *)"TYPE A\r\n", 8);
  759.                         do {
  760.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  761.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  762.  
  763.                         if ((Data = new TTcpip) != NULL) {
  764.                            Data->Initialize (DataPort);
  765.  
  766.                            sprintf (Temp, "PORT %lu,%lu,%lu,%lu,%u,%u\r\n", (Data->HostID & 0xFF000000L) >> 24, (Data->HostID & 0xFF0000L) >> 16, (Data->HostID & 0xFF00L) >> 8, Data->HostID & 0xFFL, (DataPort & 0xFF00) >> 8, DataPort & 0xFF);
  767.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  768.                            do {
  769.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  770.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  771.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  772.                            if (i == 200 && (p = strtok (NULL, " ")) != NULL) {
  773.                               sprintf (Temp, "RETR %s\r\n", p);
  774.                               Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  775.                               do {
  776.                                  i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  777.                                  Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  778.                               } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  779.                               if (i == 150) {
  780.                                  Local = p;
  781.                                  if ((p = strtok (NULL, " ")) != NULL)
  782.                                     Local = p;
  783. #if defined(__LINUX__)
  784.                                  sprintf (Temp, "%s%s/", Cfg->UsersHomePath, User->MailBox);
  785. #else
  786.                                  sprintf (Temp, "%s%s\\", Cfg->UsersHomePath, User->MailBox);
  787. #endif
  788.                                  BuildPath (Temp);
  789.                                  strcat (Temp, Local);
  790.                                  FTP_GET (Temp, Local, Hash);
  791.                                  do {
  792.                                     i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  793.                                     Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  794.                                  } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  795.                               }
  796.                            }
  797.  
  798.                            DataPort += 2;
  799.                            if (DataPort > FTPDATA_PORT + 1024)
  800.                               DataPort = FTPDATA_PORT;
  801.  
  802.                            Data->ClosePort ();
  803.                            delete Data;
  804.                         }
  805.                      }
  806.                      else if (!stricmp (p, "hash")) {
  807.                         if (Hash == TRUE) {
  808.                            Hash = FALSE;
  809.                            Embedded->Printf ("\x16\x01\x07Hash printing off.\n");
  810.                         }
  811.                         else {
  812.                            Hash = TRUE;
  813.                            Embedded->Printf ("\x16\x01\x07Hash mark printing on (1024 bytes/hash mark).\n");
  814.                         }
  815.                      }
  816.                      else if (!stricmp (p, "mget")) {
  817.                         if ((p = strtok (NULL, " ")) != NULL)
  818.                            FTP_MGET (p);
  819.                      }
  820.                      else if (!stricmp (p, "mkdir") || !stricmp (p, "md")) {
  821.                         if ((p = strtok (NULL, " ")) != NULL) {
  822.                            sprintf (Temp, "MKD %s\r\n", p);
  823.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  824.                            do {
  825.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  826.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  827.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  828.                         }
  829.                      }
  830.                      else if (!stricmp (p, "put") || !stricmp (p, "stor")) {
  831.                         if (Binary == TRUE)
  832.                            Tcp->SendBytes ((UCHAR *)"TYPE I\r\n", 8);
  833.                         else
  834.                            Tcp->SendBytes ((UCHAR *)"TYPE A\r\n", 8);
  835.                         do {
  836.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  837.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  838.  
  839.                         if ((Data = new TTcpip) != NULL) {
  840.                            Data->Initialize (DataPort);
  841.  
  842.                            sprintf (Temp, "PORT %lu,%lu,%lu,%lu,%u,%u\r\n", (Data->HostID & 0xFF000000L) >> 24, (Data->HostID & 0xFF0000L) >> 16, (Data->HostID & 0xFF00L) >> 8, Data->HostID & 0xFFL, (DataPort & 0xFF00) >> 8, DataPort & 0xFF);
  843.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  844.                            do {
  845.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  846.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  847.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  848.                            if (i == 200 && (p = strtok (NULL, " ")) != NULL) {
  849.                               sprintf (Temp, "STOR %s\r\n", p);
  850.                               Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  851.                               do {
  852.                                  i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  853.                                  Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  854.                               } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  855.                               if (i == 150) {
  856.                                  Local = p;
  857.                                  if ((p = strtok (NULL, " ")) != NULL)
  858.                                     Local = p;
  859.                                  sprintf (Temp, "%s%s\\%s", Cfg->UsersHomePath, User->MailBox, Local);
  860.                                  FTP_PUT (Temp, Hash, Binary);
  861.                                  do {
  862.                                     i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  863.                                     Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  864.                                  } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  865.                               }
  866.                            }
  867.  
  868.                            DataPort += 2;
  869.                            if (DataPort > FTPDATA_PORT + 1024)
  870.                               DataPort = FTPDATA_PORT;
  871.  
  872.                            Data->ClosePort ();
  873.                            delete Data;
  874.                         }
  875.                      }
  876.                      else if (!stricmp (p, "pwd")) {
  877.                         Tcp->SendBytes ((UCHAR *)"PWD\r\n", 5);
  878.                         do {
  879.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  880.                            Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  881.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  882.                      }
  883.                      else if (!stricmp (p, "quit") || !stricmp (p, "bye")) {
  884.                         Tcp->SendBytes ((UCHAR *)"QUIT\r\n", 6);
  885.                         do {
  886.                            GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  887.                            Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  888.                         } while (Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  889.                      }
  890.                      else if (!stricmp (p, "remotehelp")) {
  891.                         Tcp->SendBytes ((UCHAR *)"HELP\r\n", 6);
  892.                         do {
  893.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  894.                            Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  895.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  896.                      }
  897.                      else if (!strnicmp (p, "ren", 3)) {
  898.                         if ((Old = strtok (NULL, " ")) != NULL) {
  899.                            if ((New = strtok (NULL, " ")) != NULL) {
  900.                               sprintf (Temp, "RNFR %s\r\n", Old);
  901.                               Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  902.                               do {
  903.                                  i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  904.                                  Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  905.                               } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  906.                               sprintf (Temp, "RNTO %s\r\n", New);
  907.                               Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  908.                               do {
  909.                                  i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  910.                                  Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  911.                               } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  912.                            }
  913.                         }
  914.                      }
  915.                      else if (!stricmp (p, "rmdir") || !stricmp (p, "rd")) {
  916.                         if ((p = strtok (NULL, " ")) != NULL) {
  917.                            sprintf (Temp, "RMD %s\r\n", p);
  918.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  919.                            do {
  920.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  921.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  922.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  923.                         }
  924.                      }
  925.                      else if (!stricmp (p, "site")) {
  926.                         if ((p = strtok (NULL, "")) != NULL) {
  927.                            while (*p == ' ')
  928.                               p++;
  929.                            sprintf (Temp, "SITE %s\r\n", p);
  930.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  931.                            do {
  932.                               i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  933.                               Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  934.                            } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  935.                         }
  936.                      }
  937.                      else if (!stricmp (p, "SYST")) {
  938.                         Tcp->SendBytes ((UCHAR *)"SYST\r\n", 6);
  939.                         do {
  940.                            i = GetResponse (Temp, (USHORT)(sizeof (Temp) - 1));
  941.                            Embedded->Printf ("\x16\x01\x07%s\n", Temp);
  942.                         } while (i == 0 && Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE);
  943.                      }
  944.                   }
  945.                }
  946.             }
  947.             Log->Write (":Closing FTP connection");
  948.          }
  949.  
  950.          Tcp->ClosePort ();
  951.          delete Tcp;
  952.       }
  953.    }
  954.  
  955.    sprintf (Temp, "%s%s", Cfg->UsersHomePath, User->MailBox);
  956.    rmdir (Temp);
  957. }
  958.  
  959. VOID TInternet::IRC (PSZ pszServer, PSZ pszNick, USHORT usPort)
  960. {
  961.    int i;
  962.    USHORT Refresh, noctcp, Loop;
  963.    CHAR Temp[128], Nick[32], Object[64], *tempptr;
  964.    CHAR InLine[128], OutLine[128], *inptr, c, *p;
  965.    CHAR *FromHost;
  966.  
  967.    if (pszServer == NULL || pszServer[0] == '\0') {
  968.       Embedded->Printf ("\n\026\001\017Host name? ");
  969.       Embedded->GetString (Host, (USHORT)(sizeof (Host) - 1), 0);
  970.       pszServer = Host;
  971.    }
  972.    if (pszServer[0] != '\0' && (pszNick == NULL || pszNick[0] == '\0')) {
  973.       Embedded->Printf ("\026\001\017Nickname? ");
  974.       Embedded->GetString (Nick, (USHORT)(sizeof (Nick) - 1), 0);
  975.       pszNick = Nick;
  976.    }
  977.  
  978.    if ((p = strchr (pszServer, ':')) != NULL) {
  979.       *p++ = '\0';
  980.       usPort = (USHORT)atoi (p);
  981.    }
  982.  
  983.    Loop = TRUE;
  984.    if (Loop == TRUE && Embedded->AbortSession () == FALSE && pszServer[0] != '\0' && pszNick[0] != '\0') {
  985.       if ((Tcp = new TTcpip) != NULL) {
  986.          if (Log != NULL)
  987.             Log->Write ("+Opening IRC connection to %s", pszServer);
  988.  
  989.          if (Tcp->ConnectServer (pszServer, usPort) == TRUE) {
  990.             sprintf (OutLine, "NICK %s\r\n", pszNick);
  991.             Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  992.             sprintf (OutLine, "USER %s %s %s :%s\r\n", User->MailBox, Cfg->HostName, pszServer, pszNick);
  993.             Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  994.  
  995.             inptr = InLine;
  996.             strcpy (Object, "*");
  997.             FromHost = "";
  998.             tempptr = Temp;
  999.             Temp[0] = '\0';
  1000.             Refresh = TRUE;
  1001.  
  1002.             while (Embedded->AbortSession () == FALSE && Tcp->Carrier () == TRUE) {
  1003.                while (Tcp->BytesReady () == TRUE) {
  1004.                   c = (CHAR)Tcp->ReadByte ();
  1005.                   if (c == '\n' || c == '\r') {
  1006.                      *inptr = '\0';
  1007.                      Refresh = TRUE;
  1008.                      inptr = InLine;
  1009.  
  1010.                      if (Log != NULL)
  1011.                         Log->Write (">  %s", InLine);
  1012.                      if (InLine[0] != '\0')
  1013.                         Embedded->Printf ("\r\026\001\x07\026\007");
  1014.  
  1015.                      // Risponde al comando ping con un pong.
  1016.                      if (!strncmp (InLine, "PI", 2)) {
  1017.                         memcpy (InLine, "PO", 2);
  1018.                         strcat (InLine, "\r\n");
  1019.                         Tcp->SendBytes ((UCHAR *)InLine, (USHORT)strlen (InLine));
  1020.                      }
  1021.                      // Messaggio normale.
  1022.                      else if (InLine[0] != ':' && InLine[0] != '\0')
  1023.                         Embedded->Printf ("\r%s\n", InLine);
  1024.                      // Parsing dei messaggi del server.
  1025.                      else {
  1026.                         i = 0;
  1027.                         if ((p = strtok (InLine, " ")) != NULL)
  1028.                            do {
  1029.                               Tokens[i++] = p;
  1030.                            } while ((p = strtok (NULL, " ")) != NULL);
  1031.                         Tokens[i] = NULL;
  1032.  
  1033.                         if (i > 1) {
  1034.                         if ((p = strchr (Tokens[0], '!')) != NULL) {
  1035.                            *p = '\0';
  1036.                            FromHost = p + 1;
  1037.                         }
  1038.                         if (atoi (Tokens[1]) != 0) {
  1039.                            switch (atoi (Tokens[1])) {
  1040.                               case 352:
  1041.                                  Embedded->BufferedPrintf ("\r%-15s %-10s %3s %s@%s", Tokens[3], Tokens[7], Tokens[8], Tokens[4], Tokens[5]);
  1042.                                  for (i = 9; Tokens[i] != NULL; i++)
  1043.                                     Embedded->BufferedPrintf (" %s", Tokens[i]);
  1044.                                  Embedded->Printf ("\n");
  1045.                                  break;
  1046.                               default:
  1047.                                  Embedded->BufferedPrintf ("\r\026\001\007%s", &Tokens[3][1]);
  1048.                                  for (i = 4; Tokens[i] != NULL; i++)
  1049.                                     Embedded->BufferedPrintf (" %s", Tokens[i]);
  1050.                                  Embedded->Printf ("\n");
  1051.                                  break;
  1052.                            }
  1053.                         }
  1054.                         else if (Tokens[1][0] == 'P') {
  1055.                            if (Tokens[1][1] == 'R') {
  1056.                               i = 4;
  1057.                               noctcp = 0;
  1058.                               if (Tokens[3][1] == '\001') {
  1059.                                  while ((p = strchr (Tokens[0], '\001')) != NULL)
  1060.                                     *p = ' ';
  1061.                                  if (Tokens[3][2] == 'A') {
  1062.                                     Embedded->BufferedPrintf ("\r*** ACTION: %s", Tokens[0]);
  1063.                                     noctcp = 1;
  1064.                                  }
  1065. //                                 else {
  1066. //                                    while ((p = strchr (Tokens[3], '\001')) != NULL)
  1067. //                                       *p = ' ';
  1068. //                                    Embedded->BufferedPrintf ("\r*** got CTCP %s from %s", &Tokens[3][2], Tokens[0]);
  1069. //                                 }
  1070.                               }
  1071.                               else {
  1072.                                  if (Tokens[2][0] != '#') {
  1073.                                     Embedded->BufferedPrintf ("\r*%s*", &Tokens[0][1]);
  1074.                                     i = 3;
  1075.                                     noctcp = 1;
  1076.                                  }
  1077.                                  else {
  1078.                                     Log->Write (">%s - %s", Object, Tokens[2]);
  1079.                                     if (stricmp (Object, Tokens[2]))
  1080.                                        Embedded->BufferedPrintf ("\r<%s:%s>", &Tokens[0][1], Tokens[2]);
  1081.                                     else
  1082.                                        Embedded->BufferedPrintf ("\r<%s>", &Tokens[0][1]);
  1083.                                     Embedded->BufferedPrintf ("%s", &Tokens[3][1]);
  1084.                                     i = 4;
  1085.                                     noctcp = 1;
  1086.                                  }
  1087.                               }
  1088.                               for (; Tokens[i] != NULL; i++)
  1089.                                  Embedded->BufferedPrintf (" %s", Tokens[i]);
  1090.                               Embedded->Printf ("\n");
  1091.  
  1092.                               if (noctcp == 0) {
  1093.                                  if (Tokens[3][2] == 'V') {
  1094.                                     sprintf (OutLine, "NOTICE %s \001VERSION %s %s *IX :1024 bytes\001\r\n", Tokens[0], NAME, VERSION);
  1095.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1096.                                  }
  1097.                                  else if (Tokens[3][2] == 'P') {
  1098.                                     sprintf (OutLine, "NOTICE %s \001PID %d\001\r\n", Tokens[0], 1);
  1099.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1100.                                  }
  1101.                                  else if (Tokens[3][2] == 'F') {
  1102.                                     sprintf (OutLine, "NOTICE %s \001FINGER %s (%s@%s) Idle %d seconds\001\r\n", Tokens[0], "Unix", User->MailBox, Cfg->HostName, 0);
  1103.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1104.                                  }
  1105.                                  else if (Tokens[3][2] == 'C') {
  1106.                                     sprintf (OutLine, "NOTICE %s \001CLIENTINFO VERSION FINGER PID ERRMSG\001\r\n", Tokens[0]);
  1107.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1108.                                  }
  1109.                                  else if (Tokens[3][2] == 'E') {
  1110.                                     sprintf (OutLine, "NOTICE %s \001%s %s %s %s\001\r\n", Tokens[0], Tokens[3], Tokens[4], Tokens[5], Tokens[6]);
  1111.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1112.                                  }
  1113.                                  else if (Tokens[3][2] == 'D') {
  1114.                                     sprintf (OutLine, "NOTICE %s \001DCC not supported\001\r\n", Tokens[0]);
  1115.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1116.                                  }
  1117.                                  else {
  1118.                                     sprintf (OutLine, "NOTICE %s \001ERRMSG I'm sorry dave, I'm afraid I can't do that\001\r\n", Tokens[0]);
  1119.                                     Tcp->SendBytes ((UCHAR *)OutLine, (USHORT)strlen (OutLine));
  1120.                                  }
  1121.                               }
  1122.                            }
  1123.                            else {
  1124.                               if (!strcmp (Tokens[0], pszNick)) {
  1125.                                  if (!strcmp (Object, Tokens[2]))
  1126.                                     strcpy (Object, "*");
  1127.                                  Embedded->Printf ("\r*** You have left %s\n", Tokens[2]);
  1128.                               }
  1129.                               else
  1130.                                  Embedded->Printf ("\r*** %s (%s) has left channel %s\n", Tokens[0], FromHost, Tokens[2]);
  1131.                            }
  1132.                         }
  1133.                         else if (Tokens[1][0] == 'N') {
  1134.                            if (Tokens[1][1] == 'O') {
  1135.                               i = 3;
  1136.                               if (Tokens[3][1] == '\01')
  1137.                                  Embedded->BufferedPrintf ("\r*** CTCP %s reply from %s:", &Tokens[i++][2], Tokens[0]);
  1138.                               else if (strchr (Tokens[0], '.') == NULL)
  1139.                                  Embedded->BufferedPrintf ("\r\026\001\004-%s-", &Tokens[0][1]);
  1140.                               for (; Tokens[i] != NULL; i++)
  1141.                                  Embedded->BufferedPrintf (" %s", Tokens[i]);
  1142.                               Embedded->Printf ("\n");
  1143.                            }
  1144.                            else {
  1145.                               if (!strcmp (&Tokens[0][1], pszNick)) {
  1146.                                  strcpy (Nick, &Tokens[2][1]);
  1147.                                  pszNick = Nick;
  1148.                                  Embedded->Printf ("\r\026\001\002*** You have changed your nickname to %s\n", pszNick);
  1149.                               }
  1150.                               else
  1151.                                  Embedded->Printf ("\r\026\001\002*** %s is now known as %s\n", &Tokens[0][1], &Tokens[2][1]);
  1152.                            }
  1153.                         }
  1154.                         else if (Tokens[1][0] == 'J') {
  1155.                            if (!strcmp (&Tokens[0][1], pszNick)) {
  1156.                               strcpy (Object, &Tokens[2][1]);
  1157.                               Embedded->Printf ("\r\026\001\002*** Current channel is %s\n", Object);
  1158.                               if (Log != NULL)
  1159.                                  Log->Write ("+ Current channel is %s\n", Object);
  1160.                            }
  1161.                            else
  1162.                               Embedded->Printf ("\r\026\001\002*** %s (%s) has joined %s\n", &Tokens[0][1], FromHost, &Tokens[2][1]);
  1163.                         }
  1164.                         else if (Tokens[1][0] == 'Q') {
  1165.                            Embedded->BufferedPrintf ("\r*** %s quit", Tokens[0]);
  1166.                            for (i = 2; Tokens[i] != NULL; i++)
  1167.                               Embedded->BufferedPrintf (" %s", Tokens[i]);
  1168.                            Embedded->Printf ("\n");
  1169.                         }
  1170.                         else if (Tokens[1][0] == 'T') {
  1171.                            Embedded->BufferedPrintf ("\r\026\001\001*** %s changed the %s topic to", &Tokens[0][1], Tokens[2]);
  1172.                            for (i = 3; Tokens[i] != NULL; i++)
  1173.                               Embedded->BufferedPrintf (" %s", Tokens[i]);
  1174.                            Embedded->Printf ("\n");
  1175.                         }
  1176.                         else if (Tokens[1][0] == 'I')
  1177.                            Embedded->BufferedPrintf ("\r*** %s invited you to %s\n", Tokens[0], Tokens[3]);
  1178.                         else if (Tokens[1][0] == 'M') {
  1179.                            Embedded->BufferedPrintf ("\r*** %s changed %s to \"%s", Tokens[0], Tokens[2], Tokens[3]);
  1180.                            for (i = 4; Tokens[i] != NULL; i++)
  1181.                               Embedded->BufferedPrintf (" %s", Tokens[i]);
  1182.                            Embedded->Printf ("\"\n");
  1183.                         }
  1184.                         else if (Tokens[1][0] == 'K')
  1185.                            Embedded->BufferedPrintf ("\r*** %s kicked %s from %s\n", Tokens[0], Tokens[3], Tokens[2]);
  1186.                         else if (Tokens[1][0] == 'E') {
  1187.                            Embedded->BufferedPrintf ("\r*** ERROR:");
  1188.                            for (i = 2; Tokens[i] != NULL; i++)
  1189.                               Embedded->BufferedPrintf (" %s", Tokens[i]);
  1190.                            Embedded->Printf ("\n");
  1191.                         }
  1192.                         else
  1193.                            Refresh = FALSE;
  1194.                         }
  1195.                      }
  1196.                   }
  1197.                   else if (c != '\r' && c != '\n')
  1198.                      *inptr++ = c;
  1199.                }
  1200.  
  1201.                if (Refresh == TRUE) {
  1202.                   Embedded->Printf ("\r\026\001\x70\026\007> %s", Temp);
  1203.                   Refresh = FALSE;
  1204.                }
  1205.  
  1206.                if (Embedded->KBHit () == TRUE) {
  1207.                   c = (CHAR)Embedded->Getch ();
  1208.                   if (c == '\r') {
  1209.                      if (Temp[0] == '/') {
  1210.                         Embedded->Printf ("\r\026\001\007\026\007%s\n", Temp);
  1211.                         if (toupper (Temp[1]) == 'Q') {
  1212.                            strcat (Temp, " :lorabbs - exit");
  1213.                            Loop = FALSE;
  1214.                         }
  1215.                         Tcp->BufferBytes ((UCHAR *)&Temp[1], (USHORT)strlen (&Temp[1]));
  1216.                      }
  1217.                      else {
  1218.                         Embedded->Printf ("\r\026\001\007\026\007<%s> %s\n", pszNick, Temp);
  1219.                         Tcp->BufferBytes ((UCHAR *)"PRIVMSG ", 8);
  1220.                         Tcp->BufferBytes ((UCHAR *)Object, (USHORT)strlen (Object));
  1221.                         Tcp->BufferBytes ((UCHAR *)" :", 2);
  1222.                         Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  1223.                      }
  1224.                      Tcp->BufferBytes ((UCHAR *)"\r\n", 2);
  1225.                      Tcp->UnbufferBytes ();
  1226.                      tempptr = Temp;
  1227.                      *tempptr = '\0';
  1228.                      Refresh = TRUE;
  1229.                   }
  1230.                   else if (c == 0x08) {
  1231.                      if (tempptr > Temp) {
  1232.                         Embedded->Printf ("\x08 \x08");
  1233.                         tempptr--;
  1234.                         *tempptr = '\0';
  1235.                      }
  1236.                   }
  1237.                   else if (c >= ' ') {
  1238.                      *tempptr++ = c;
  1239.                      *tempptr = '\0';
  1240.                      Embedded->Putch (c);
  1241.                   }
  1242.                }
  1243.             }
  1244.          }
  1245.          else {
  1246.             Embedded->Printf ("\n\026\001\014Connection refused.");
  1247.             if (Log != NULL)
  1248.                Log->Write ("!Connection refused");
  1249.          }
  1250.  
  1251.          delete Tcp;
  1252.       }
  1253.    }
  1254. }
  1255.  
  1256.  
  1257.