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

  1. //-------------------------------------------------------------------------
  2. //
  3. // MAIL.CPP - Declarations for the mail handling tool of UW/PC.
  4. // 
  5. //  This file is part of UW/PC - a multi-window comms package for the PC.
  6. //  Copyright (C) 1990-1991  Rhys Weatherley
  7. //
  8. //  This program is free software; you can redistribute it and/or modify
  9. //  it under the terms of the GNU General Public License as published by
  10. //  the Free Software Foundation; either version 1, or (at your option)
  11. //  any later version.
  12. //
  13. //  This program is distributed in the hope that it will be useful,
  14. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. //  GNU General Public License for more details.
  17. //
  18. //  You should have received a copy of the GNU General Public License
  19. //  along with this program; if not, write to the Free Software
  20. //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. //
  22. // Revision History:
  23. // ================
  24. //
  25. //  Version  DD/MM/YY  By  Description
  26. //  -------  --------  --  --------------------------------------
  27. //    1.0    25/07/91  RW  Original Version of MAIL.CPP
  28. //
  29. // You may contact the author by:
  30. // =============================
  31. //
  32. //  e-mail: rhys@cs.uq.oz.au
  33. //    mail: Rhys Weatherley
  34. //          5 Horizon Drive
  35. //          Jamboree Heights
  36. //          Queensland 4074
  37. //        Australia
  38. //
  39. //-------------------------------------------------------------------------
  40.  
  41. #include "mail.h"        // Declarations for this module.
  42. #include "client.h"        // Client handling declarations.
  43. #include "display.h"        // Display handling declarations.
  44. #include "uw.h"            // UW protocol declarations.
  45. #include "config.h"        // Configuration declarations.
  46. #include "screen.h"        // Screen handling routines.
  47. #include <string.h>        // String handling routines.
  48.  
  49. //
  50. // Define the mail tool modes.
  51. //
  52. #define    MODE_NORMAL    0    // Normal operation.
  53. #define    MODE_EDITING    1    // Editing a file on the remote host.
  54. #define    MODE_READING    2    // Reading headers from a mailbox.
  55. #define    MODE_BROWSING    3    // Browsing a mail message.
  56.  
  57. UWMailTool::UWMailTool (UWDisplay *wind)
  58.     : UWClient (wind)
  59. {
  60.   mode = MODE_READING;        // Enter the "reading headers" mode.
  61.   UWMaster.install (this);    // Install this client.
  62.   window -> clear ();        // Clear the screen.
  63.   window -> move (0,0);        // Home the cursor.
  64.   resposn = 0;            // Clear the response buffer.
  65.   numheaders = 0;        // No headers in mailbox at the moment.
  66.   write ("Reading mailbox...");    // Inform the user that something is happening.
  67.   send ('R');            // Begin a "read mailbox command".
  68.   command (UWConfig.MailBoxName);
  69. } // UWMailTool::UWMailTool //
  70.  
  71. // Send a mail server command to the remote host.
  72. void    UWMailTool::command (char *str)
  73. {
  74.   while (*str)
  75.     send (*str++);        // Send the text of the command.
  76.   send ('\r');            // Terminate the command line.
  77. } // UWMailTool::command //
  78.  
  79. // Write a string to the screen.
  80. void    UWMailTool::write (char *str)
  81. {
  82.   while (*str)
  83.     window -> send (*str++);
  84. } // UWMailTool::write //
  85.  
  86. // Print a string field up to a maximum number of characters //
  87. static    int    printstring (UWDisplay *wind,char *header,int index,int width)
  88. {
  89.   while (width > 0 && header[index] != '|' && header[index] != '\0')
  90.     {
  91.       wind -> send (header[index++]);
  92.       --width;
  93.     }
  94.   while (header[index] != '|' && header[index] != '\0')
  95.     ++index;
  96.   if (header[index] == '\0')
  97.     return (index);            // Quick hack for end of subject.
  98.   ++index;                // Skip past next '|'.
  99.   while (width > 0)
  100.     {
  101.       wind -> send (' ');        // Pad to the field width.
  102.       --width;
  103.     }
  104.   return (index);
  105. } // printstring //
  106.  
  107. // Display mail header information in a window.
  108. static    void    displayheader (UWDisplay *wind,char *header,int inverse)
  109. {
  110.   int index;
  111.  
  112.   // Set the printing attribute to use for the header //
  113.   if (inverse)
  114.     wind -> setattr (HardwareScreen.attributes[ATTR_INVERSE]);
  115.    else
  116.     wind -> setattr (HardwareScreen.attributes[ATTR_NORMAL]);
  117.  
  118.   // Print the header status information //
  119.   wind -> send (' ');
  120.   wind -> send (header[0]);
  121.   wind -> send (' ');
  122.  
  123.   // Print the sender, lines and subject information //
  124.   index = 2;
  125.   while (header[index] != '|')
  126.     ++index;            /* Skip past the Message-Id */
  127.   ++index;            /* Skip the last '|' character */
  128.   index = printstring (wind,header,index,20);
  129.   wind -> send (' ');
  130.   index = printstring (wind,header,index,5);
  131.   wind -> send (' ');
  132.   printstring (wind,header,index,(wind -> width) - 31);
  133.  
  134.   // Clear the rest of the line with the header information //
  135.   wind -> clear (CLR_END_LINE);
  136.  
  137.   // Return the printing attribute to normal //
  138.   if (inverse)
  139.     wind -> setattr (HardwareScreen.attributes[ATTR_NORMAL]);
  140. } // displayheader //
  141.  
  142. // Show the header information on the screen.
  143. void    UWMailTool::showheaders (void)
  144. {
  145.   int y=0;
  146.   while (y < window -> height && (topheader + y) < numheaders)
  147.     {
  148.       window -> move (0,y);
  149.       displayheader (window,headers[topheader + y],
  150.                    currheader == (topheader + y));
  151.     }
  152. } // UWMailTool::showheaders //
  153.  
  154. // Handle the current buffer of information from the remote host.
  155. void    UWMailTool::handle (void)
  156. {
  157.   switch (respbuf[0])        // Determine the response type.
  158.     {
  159.       case 'H': if (numheaders >= MAIL_MAX_HDRS ||
  160.                   !(headers[numheaders] = new char [resposn]))
  161.                 break;    // Not enough memory - ignore.
  162.         strncpy (headers[numheaders],respbuf + 1,resposn - 1);
  163.         headers[numheaders++][resposn - 1] = '\0';
  164.         break;
  165.       case 'h':    mode = MODE_NORMAL;
  166.               topheader = 0;        // Setup for the first page display.
  167.         currheader = 0;
  168.         UWMaster.status ();    // Update the status line.
  169.         window -> clear ();
  170.               showheaders ();
  171.               break;
  172.       default:    break;
  173.     } /* switch */
  174. } // UWMailTool::handle //
  175.  
  176. void    UWMailTool::key (int keypress)
  177. {
  178.   if (mode == MODE_EDITING)
  179.     {
  180.       // If we are editing - pass characters straight through //
  181.       if (underneath)
  182.         underneath -> key (keypress);
  183.       return;
  184.     }
  185.   switch (keypress)
  186.     {
  187.       case 033:    command ("Q");        // Quit the mail tool handling.
  188.               window -> clear ();    // Clear screen for the return
  189.         UWMaster.remove ();    // to terminal emulation mode.
  190.         disposeheaders ();    // Clean up the memory.
  191.               break;
  192.       default:    defkey (keypress);    // Do the default key operation.
  193.               break;
  194.     } /* switch */
  195. } // UWMailTool::key //
  196.  
  197. void    UWMailTool::remote (int ch)
  198. {
  199.   if (mode == MODE_EDITING)
  200.     {
  201.       // If an edit is in progress, then pass characters straight through //
  202.       if (ch == ('X' & 0x1F))    // Abort editing mode if ^X received.
  203.         {
  204.       mode = MODE_NORMAL;
  205.       UWMaster.status ();
  206.     }
  207.        else if (underneath)
  208.         underneath -> remote (ch);
  209.       return;            // Finished with this character.
  210.     }
  211.   if (ch == '\r' || ch == '\n')
  212.     {
  213.       if (resposn > 0)
  214.         handle ();        // Handle the incoming buffer.
  215.       resposn = 0;        // Clear buffer for next line.
  216.     }
  217.    else if (resposn < MAIL_BUF_LEN)
  218.     respbuf[resposn++] = ch;    // Store character in the response buffer.
  219. } // UWMailTool::remote //
  220.  
  221. // Dispose the current mail headers.
  222. void    UWMailTool::disposeheaders (void)
  223. {
  224.   int temp;
  225.   for (temp = 0;temp < numheaders;++temp)
  226.     delete headers[temp];
  227.   numheaders = 0;
  228. } // UWMailTool::disposeheaders //
  229.  
  230. char    *UWMailTool::getstatus (void)
  231. {
  232.   if (mode == MODE_EDITING)
  233.     return ("Edit mail message");
  234.    else if (mode == MODE_READING)
  235.     return ("Reading mailbox - please wait ...");
  236.    else if (mode == MODE_BROWSING)
  237.     return ("%h\033\030\031\032 PgUp PgDn Home End Space Enter Esc%i "
  238.             "%hH%ieader");
  239.    else
  240.     return ("%h\030 \031 PgUp PgDn Enter%i Mail%hb%iox %hD%ielete %hM%iail "
  241.           "%hN%iew %hR%ieply %hS%iave %hU%indelete %hW%irite "
  242.         "%hQ%iuit E%hx%iit");
  243. } // UWMailTool::getstatus //
  244.