home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / POP3.CPP < prev    next >
C/C++ Source or Header  |  1996-07-24  |  12KB  |  303 lines

  1.  
  2. // ----------------------------------------------------------------------
  3. // LoraBBS Professional Edition - Version 3.00.6
  4. // Copyright (c) 1996 by Marco Maccaferri. All rights reserved.
  5. //
  6. // History:
  7. //    02/07/96 - Initial coding.
  8. // ----------------------------------------------------------------------
  9.  
  10. #include "_ldefs.h"
  11. #include "lserver.h"
  12.  
  13. static CHAR *WeekDays[] = {
  14.    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  15. };
  16.  
  17. static CHAR *Months[] = {
  18.    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  19.    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  20. };
  21.  
  22. TPOP3::TPOP3 (void)
  23. {
  24.    Tcp = NULL;
  25.    User = NULL;
  26.    Msg = NULL;
  27.    Log = NULL;
  28. }
  29.  
  30. TPOP3::~TPOP3 (void)
  31. {
  32.    if (Tcp != NULL)
  33.       delete Tcp;
  34.    if (User != NULL)
  35.       delete User;
  36.    if (Msg != NULL)
  37.       delete Msg;
  38. }
  39.  
  40. VOID TPOP3::GetCommand (VOID)
  41. {
  42.    USHORT len = 0, MaxLen = sizeof (Response);
  43.    CHAR c, *pszResp = Response;
  44.  
  45.    do {
  46.       if (Tcp->BytesReady () == TRUE) {
  47.          do {
  48.             if ((c = (CHAR)Tcp->ReadByte ()) != '\r') {
  49.                if (c != '\n') {
  50.                   *pszResp++ = c;
  51.                   if (++len >= MaxLen)
  52.                      c = '\r';
  53.                }
  54.             }
  55.          } while (Tcp->RxBytes > 0 && c != '\r');
  56.       }
  57. #if defined(__OS2__)
  58.       DosSleep (1L);
  59. #elif defined(__NT__)
  60.       Sleep (1L);
  61. #endif
  62.    } while (c != '\r' && Tcp->Carrier () == TRUE);
  63.    *pszResp = '\0';
  64. }
  65.  
  66. VOID TPOP3::Run (VOID)
  67. {
  68.    USHORT EndRun, Validated, Index, Found;
  69.    CHAR *p;
  70.    ULONG Number, Size;
  71.    MAILINDEX *MailIdx;
  72.    time_t mtime;
  73.    struct tm ftm;
  74.  
  75.    EndRun = FALSE;
  76.    Validated = FALSE;
  77.  
  78.    sprintf (Temp, "+OK LoraBBS POP3 server ready\r\n");
  79.    Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  80.  
  81.    User = new TUser (Cfg->UserFile);
  82.  
  83.    while (Tcp->Carrier () == TRUE && EndRun == FALSE) {
  84.       GetCommand ();
  85.  
  86.       if ((p = strtok (Response, " ")) != NULL) {
  87.          if (!stricmp (p, "QUIT")) {
  88.             if (Validated == TRUE && Msg != NULL) {
  89.                if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
  90.                   do {
  91.                      if (MailIdx->Deleted == TRUE)
  92.                         Msg->Delete (MailIdx->Number);
  93.                   } while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
  94.             }
  95.             sprintf (Temp, "+OK LoraBBS POP3 server signing off\r\n");
  96.             Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  97.             EndRun = TRUE;
  98.          }
  99.          else if (!stricmp (p, "USER")) {
  100.             Validated = FALSE;
  101.             if ((p = strtok (NULL, "")) != NULL) {
  102.                while (*p == ' ')
  103.                   p++;
  104.                strcpy (Response, strlwr (p));
  105.                Response[0] = (CHAR)toupper (Response[0]);
  106.                while ((p = strchr (Response, '.')) != NULL) {
  107.                   *p++ = ' ';
  108.                   *p = (CHAR)toupper (*p);
  109.                }
  110.                if (User->GetData (Response) == TRUE)
  111.                   sprintf (Temp, "+OK %s is welcome here\r\n", Response);
  112.                else
  113.                   sprintf (Temp, "-ERR never heard of %s\r\n", Response);
  114.                Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  115.             }
  116.          }
  117.          else if (!stricmp (p, "PASS")) {
  118.             Validated = FALSE;
  119.             if ((p = strtok (NULL, " ")) != NULL) {
  120.                if (User->CheckPassword (p) == TRUE) {
  121.                   MailDrop.Clear ();
  122.  
  123.                   if (Cfg->MailStorage == ST_JAM)
  124.                      Msg = new JAM (Cfg->MailPath);
  125.                   else if (Cfg->MailStorage == ST_SQUISH)
  126.                      Msg = new SQUISH (Cfg->MailPath);
  127.                   else if (Cfg->MailStorage == ST_FIDO)
  128.                      Msg = new FIDOSDM (Cfg->MailPath);
  129.                   else if (Cfg->MailStorage == ST_ADEPT)
  130.                      Msg = new ADEPT (Cfg->MailPath);
  131.  
  132.                   Index = 0;
  133.                   Size = 0L;
  134.  
  135.                   if (Msg != NULL) {
  136.                      Number = Msg->Lowest ();
  137.                      do {
  138.                         if (Msg->Read (Number) == TRUE && !stricmp (User->Name, Msg->To)) {
  139.                            Idx.Index = ++Index;
  140.                            Idx.Number = Number;
  141.                            Idx.Deleted = FALSE;
  142.                            Idx.Size = 0L;
  143.                            Idx.Size += strlen (Msg->From);
  144.                            Idx.Size += strlen (Msg->To);
  145.                            Idx.Size += strlen (Msg->Subject);
  146.                            if ((p = (CHAR *)Msg->Text.First ()) != NULL)
  147.                               do {
  148.                                  Idx.Size += strlen (p) + 1;
  149.                               } while ((p = (CHAR *)Msg->Text.Next ()) != NULL);
  150.                            Size += Idx.Size;
  151.                            MailDrop.Add (&Idx, sizeof (Idx));
  152.                         }
  153.                      } while (Msg->Next (Number) == TRUE);
  154.                   }
  155.  
  156.                   sprintf (Temp, "+OK maildrop locked and ready\r\n");
  157.                   Validated = TRUE;
  158.                }
  159.                else
  160.                   sprintf (Temp, "-ERR invalid password\r\n");
  161.             }
  162.             else
  163.                sprintf (Temp, "-ERR invalid password\r\n");
  164.             Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  165.          }
  166.          else if (!stricmp (p, "NOOP")) {
  167.             sprintf (Temp, "+OK\r\n");
  168.             Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  169.          }
  170.          else if (Validated == TRUE) {
  171.             if (!stricmp (p, "STAT")) {
  172.                sprintf (Temp, "+OK %u %lu\r\n", Index, Size);
  173.                Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  174.             }
  175.             else if (!stricmp (p, "LIST")) {
  176.                if ((p = strtok (NULL, " ")) != NULL) {
  177.                   Found = FALSE;
  178.                   if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
  179.                      do {
  180.                         if (MailIdx->Index == (USHORT)atoi (p)) {
  181.                            sprintf (Temp, "+OK %u %lu\r\n", MailIdx->Index, MailIdx->Size);
  182.                            Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  183.                            Found = TRUE;
  184.                            break;
  185.                         }
  186.                      } while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
  187.                   if (Found == FALSE) {
  188.                      sprintf (Temp, "-ERR no such message\r\n");
  189.                      Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  190.                   }
  191.                }
  192.                else {
  193.                   sprintf (Temp, "+OK %u messages (%lu octects)\r\n", Index, Size);
  194.                   Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  195.                   if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
  196.                      do {
  197.                         sprintf (Temp, "%u %lu\r\n", MailIdx->Index, MailIdx->Size);
  198.                         Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  199.                      } while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
  200.                   Tcp->BufferBytes ((UCHAR *)".\r\n", 3);
  201.                   Tcp->UnbufferBytes ();
  202.                }
  203.             }
  204.             else if (!stricmp (p, "RETR")) {
  205.                Found = FALSE;
  206.                if ((p = strtok (NULL, " ")) != NULL) {
  207.                   if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
  208.                      do {
  209.                         if (MailIdx->Index == (USHORT)atoi (p)) {
  210.                            sprintf (Temp, "+OK %lu octects\r\n", MailIdx->Index, MailIdx->Size);
  211.                            Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  212.                            Found = TRUE;
  213.                            break;
  214.                         }
  215.                      } while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
  216.                }
  217.                if (Found == FALSE) {
  218.                   sprintf (Temp, "-ERR no such message\r\n");
  219.                   Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  220.                }
  221.                else {
  222.                   Msg->Read (MailIdx->Number);
  223.  
  224.                   memset (&ftm, 0, sizeof (ftm));
  225.                   ftm.tm_mday = Msg->Written.Day;
  226.                   ftm.tm_mon = Msg->Written.Month - 1;
  227.                   ftm.tm_year = Msg->Written.Year - 1900;
  228.                   ftm.tm_hour = Msg->Written.Hour;
  229.                   ftm.tm_min = Msg->Written.Minute;
  230.                   ftm.tm_sec = Msg->Written.Second;
  231.                   mtime = mktime (&ftm);
  232.                   memcpy (&ftm, gmtime (&mtime), sizeof (ftm));
  233.                   sprintf (Temp, "Date: %s %d %s %d %d:%02d:%02d GMT\r\n", WeekDays[ftm.tm_wday], ftm.tm_mday, Months[ftm.tm_mon], ftm.tm_year % 100, ftm.tm_hour, ftm.tm_min, ftm.tm_sec);
  234.                   Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  235.                   sprintf (Temp, "From: %s <%s>\r\n", Msg->From, Msg->FromAddress);
  236.                   Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  237.                   sprintf (Temp, "To: %s <%s>\r\n", Msg->To, Msg->ToAddress);
  238.                   Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  239.                   sprintf (Temp, "Subject: %s\r\n", Msg->Subject);
  240.                   Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  241.  
  242.                   sprintf (Temp, "\r\n");
  243.                   Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  244.  
  245.                   sprintf (Temp, "\r\n");
  246.                   if ((p = (CHAR *)Msg->Text.First ()) != NULL)
  247.                      do {
  248.                         if (!strcmp (p, "."))
  249.                            Tcp->BufferBytes ((UCHAR *)"..", 2);
  250.                         else
  251.                            Tcp->BufferBytes ((UCHAR *)p, (USHORT)strlen (p));
  252.                         Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  253.                      } while ((p = (CHAR *)Msg->Text.Next ()) != NULL);
  254.  
  255.                   Tcp->BufferBytes ((UCHAR *)".\r\n", 3);
  256.  
  257.                   Tcp->UnbufferBytes ();
  258.                }
  259.             }
  260.             else if (!stricmp (p, "DELE")) {
  261.                Found = FALSE;
  262.                if ((p = strtok (NULL, " ")) != NULL) {
  263.                   if ((MailIdx = (MAILINDEX *)MailDrop.First ()) != NULL)
  264.                      do {
  265.                         if (MailIdx->Index == (USHORT)atoi (p)) {
  266.                            if (MailIdx->Deleted == TRUE)
  267.                               sprintf (Temp, "-ERR message %lu already deleted\r\n", MailIdx->Index);
  268.                            else
  269.                               sprintf (Temp, "+OK message %lu deleted\r\n", MailIdx->Index);
  270.                            Tcp->BufferBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  271.                            MailIdx->Deleted = TRUE;
  272.                            Found = TRUE;
  273.                            break;
  274.                         }
  275.                      } while ((MailIdx = (MAILINDEX *)MailDrop.Next ()) != NULL);
  276.                }
  277.                if (Found == FALSE)
  278.                   sprintf (Temp, "-ERR no such message\r\n");
  279.                else
  280.                   sprintf (Temp, "+OK\r\n");
  281.                Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  282.             }
  283.             else {
  284.                sprintf (Temp, "-ERR command not implemented.\r\n");
  285.                Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  286.             }
  287.          }
  288.          else {
  289.             sprintf (Temp, "-ERR command not implemented.\r\n");
  290.             Tcp->SendBytes ((UCHAR *)Temp, (USHORT)strlen (Temp));
  291.          }
  292.       }
  293.  
  294. #if defined(__OS2__)
  295.       DosSleep (1L);
  296. #elif defined(__NT__)
  297.       Sleep (1L);
  298. #endif
  299.    }
  300. }
  301.  
  302.  
  303.