home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / UWPC201.ZIP / UW-SRC.ZIP / LOGIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-27  |  4.7 KB  |  169 lines

  1. //-------------------------------------------------------------------------
  2. //
  3. // LOGIN.CPP - Declarations for the DOS login facility to allow a user to
  4. //           login to the PC remotely.
  5. // 
  6. //  This file is part of UW/PC - a multi-window comms package for the PC.
  7. //  Copyright (C) 1990-1991  Rhys Weatherley
  8. //
  9. //  This program is free software; you can redistribute it and/or modify
  10. //  it under the terms of the GNU General Public License as published by
  11. //  the Free Software Foundation; either version 1, or (at your option)
  12. //  any later version.
  13. //
  14. //  This program is distributed in the hope that it will be useful,
  15. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. //  GNU General Public License for more details.
  18. //
  19. //  You should have received a copy of the GNU General Public License
  20. //  along with this program; if not, write to the Free Software
  21. //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. //
  23. // Revision History:
  24. // ================
  25. //
  26. //  Version  DD/MM/YY  By  Description
  27. //  -------  --------  --  --------------------------------------
  28. //    1.0    26/07/91  RW  Original Version of LOGIN.CPP
  29. //
  30. // You may contact the author by:
  31. // =============================
  32. //
  33. //  e-mail: rhys@cs.uq.oz.au
  34. //    mail: Rhys Weatherley
  35. //          5 Horizon Drive
  36. //          Jamboree Heights
  37. //          Queensland 4074
  38. //        Australia
  39. //
  40. //-------------------------------------------------------------------------
  41.  
  42. #include "login.h"        // Declarations for this module.
  43. #include "client.h"        // Client handling declarations.
  44. #include "uw.h"            // UW protocol declarations.
  45. #include "config.h"        // Configuration routines.
  46. #include <string.h>        // String handling routines.
  47. #include <dir.h>        // Directory handling routines.
  48. #include <ctype.h>        // Character typing macros.
  49.  
  50. #define    MODE_PASSWORD    0
  51. #define    MODE_COMMAND    1
  52.  
  53. // Send a string of characters to the remote host.
  54. void    UWLoginTool::sendstr (char *str)
  55. {
  56.   while (*str)
  57.     send (*str++);
  58. } // UWLoginTool::sendstr //
  59.  
  60. // Process a character meant for the buffer.  Returns
  61. // zero if OK, 1 if '\r' pressed, -1 if ESC pressed.
  62. int    UWLoginTool::charbuf (int ch)
  63. {
  64.   if (ch == '\r' || ch == '\n')
  65.     {
  66.       buffer[posn] = '\0';
  67.       return (1);
  68.     }
  69.    else if (ch == '\033')
  70.     return (-1);
  71.    else if (ch == '\b' && posn > 0)
  72.     {
  73.       --posn;
  74.       if (mode != MODE_PASSWORD)
  75.         sendstr ("\b \b");    // Backspace over last character.
  76.     }
  77.    else if (ch >= ' ' && posn < 79)
  78.     {
  79.       buffer[posn++] = ch;
  80.       if (mode != MODE_PASSWORD)
  81.         send (ch);            // Echo the character.
  82.     }
  83.   return (0);
  84. } // UWLoginTool::charbuf //
  85.  
  86. // Output the main login menu.
  87. void    UWLoginTool::menu (void)
  88. {
  89.   static char dir[100];
  90.   getcwd (dir,100);
  91.   sendstr ("\r\n");
  92.   sendstr (dir);
  93.   sendstr ("> ");
  94. } // UWLoginTool::menu //
  95.  
  96. // Execute the current command.
  97. int    UWLoginTool::execute (void)
  98. {
  99.   int index=0;
  100.   while (buffer[index] && isspace (buffer[index]))
  101.     ++index;
  102.   return (1);
  103. } // UWLoginTool::execute //
  104.  
  105. UWLoginTool::UWLoginTool (UWDisplay *wind)
  106.     : UWClient (wind)
  107. {
  108.   UWMaster.install (this);
  109.   sendstr ("\032Welcome to the UW/PC remote login client\r\n\r\nPassword: ");
  110.   clrbuf ();
  111.   mode = MODE_PASSWORD;
  112. } // UWLoginTool::UWLoginTool //
  113.  
  114. void    UWLoginTool::key (int keypress)
  115. {
  116.   if (keypress == 033)
  117.     {
  118.       send ('X' & 0x1F);    // Cancel the login session.
  119.       UWMaster.remove ();    // Remove the login client.
  120.     } /* then */
  121.    else
  122.     defkey (keypress);        // Handle the default keys.
  123. } // UWLoginTool::key //
  124.  
  125. void    UWLoginTool::remote (int ch)
  126. {
  127.   int value;
  128.   switch (mode)
  129.     {
  130.       case MODE_PASSWORD:
  131.               if ((value = charbuf (ch)) == 0)
  132.           break;
  133.          else if (value == 1 && UWConfig.Password[0] &&
  134.                !strcmp (UWConfig.Password,buffer))
  135.           {
  136.             sendstr ("\r\nLogin succeeded.\r\n");
  137.             mode = MODE_COMMAND;
  138.             menu ();        // Send the login menu prompt.
  139.             clrbuf ();        // Clear the input buffer.
  140.           } /* if */
  141.          else
  142.           {
  143.             sendstr ("\r\nInvalid password - goobye!\r\n");
  144.             send ('X' & 0x1F);
  145.             UWMaster.remove ();
  146.           } /* if */
  147.         break;
  148.       case MODE_COMMAND:
  149.               if ((value = charbuf (ch)) == 0)
  150.           break;
  151.          else if (value == 1)
  152.           {
  153.             if (!execute ())    // Execute the command.
  154.               break;
  155.             menu ();
  156.             clrbuf ();
  157.           }
  158.          else
  159.           {
  160.             // Clear the current line when ESC pressed.
  161.             while (posn > 0)
  162.               sendstr ("\b \b");
  163.             clrbuf ();
  164.           }
  165.         break;
  166.       default:    break;
  167.     } /* switch */
  168. } // UWLoginTool::remote //
  169.