home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / lpd Sources / Mail.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-23  |  4.7 KB  |  217 lines  |  [TEXT/KAHL]

  1. /************************************************************************
  2.  *                                                                        *
  3.  *    Mail.c                                                                *
  4.  *                                                                        *
  5.  *  Line Printer Daemon using TCP/IP printer protocol                    *
  6.  *                                                                        *
  7.  *              -------------- The smtp routines --------------            *
  8.  *                                                                        *
  9.  *  Written by Casper Boon, November, 1992.                                *
  10.  *                                                                        *
  11.  *    © 1992 Casper Boon.                                                    *
  12.  *                                                                        *
  13.  * SendMail sends mail to a user at a specified host using the SMTP        *
  14.  * protocol.  The mail is usually a handle, but in the case of the log    *
  15.  * file, may also be a file if the handle is NIL.                        * 
  16.  *                                                                        *
  17.  ************************************************************************/
  18.  
  19. #include "LPD.H"
  20. #include "TCPStream.H"
  21. #include "BackGrounder.h"
  22. #include "lpdProtos.h"
  23.  
  24.  
  25. static integer SendString(integer crefnum, StringPtr s);
  26. static integer GetResponse(integer crefnum, Byte which);
  27.  
  28.  
  29. void SendMail(char * who, char * host, Handle mail, integer fRef)
  30. {
  31.     integer    cRef, state, size, eof;
  32.     LongInt    handSize;
  33.     Byte    buff[256];
  34.     Ptr        mailing;
  35.     char    who2[12], where[80];
  36.  
  37.     /* convert the who and where if they were aliased */
  38.     ResolveAlias(who, host, who2, where);
  39.     who = who2; host = where;
  40.  
  41.     /* open a connection to the SMTP port */
  42.     if ( TCPSOpen( &cRef, host, 0, SMTP_PORT, FALSE, &state) != noErr )
  43.         {    /* it failed, fall out now */
  44.         if DEBUGGING
  45.             log_printf("Cannot connect to SMTP port on %s\n", where);
  46.         return;
  47.         }
  48.  
  49.     WaitForState(&state);
  50.  
  51.     if (state < 0)
  52.         {
  53.         TCPSClose(cRef, &state);
  54.         WaitForState(&state);
  55.         if DEBUGGING
  56.             log_printf("Cannot connect to SMTP port on %s\n", where);
  57.         return ;    /* couldn't open */
  58.         }
  59.  
  60.     if ( !GetResponse(cRef, '2') )
  61.         return;
  62.  
  63.     if ( !SendString(cRef, "\pMAIL FROM: <lpDaemon>\015\012") )
  64.         return;
  65.  
  66.     if ( !GetResponse(cRef, '2') )
  67.         return;
  68.  
  69.     psprintf(buff, "RCPT TO: <%s@%s>\015\012", who, host);
  70.     if ( !SendString(cRef, buff) )
  71.         return;
  72.  
  73.     if ( !GetResponse(cRef, '2') )
  74.         return;
  75.  
  76.     if ( !SendString(cRef, "\pDATA\015\012") )
  77.         return;
  78.  
  79.     if ( !GetResponse(cRef, '3') )
  80.         return;
  81.  
  82.     if ( !SendString(cRef, "\pFrom: <lpDaemon>\015\012") )
  83.         return;
  84.  
  85.     psprintf(buff, "To: <%s@%s>\015\012", who, host);
  86.     if ( !SendString(cRef, buff) )
  87.         return;
  88.  
  89.     if ( mail )
  90.         {
  91.         if ( !SendString(cRef, "\pSubject: PostScript Errors\015\012") )
  92.             return;
  93.  
  94.         handSize = GetHandleSize(mail);
  95.  
  96.         HLock(mail);
  97.         mailing = *mail;
  98.         }
  99.     else
  100.         {
  101.         if ( !SendString(cRef, "\pSubject: lpDaemon Log File\015\012") )
  102.             return;
  103.  
  104.         mailing = NewPtr(1024);
  105.  
  106.         if (GetEOF(fRef, &handSize) != noErr)
  107.             handSize = 0;
  108.         }
  109.  
  110.  
  111.     while (handSize)
  112.         {
  113.         if (handSize > 1024)
  114.             size = 1024;
  115.         else
  116.             size = handSize;
  117.         handSize -= size;
  118.  
  119.         if ( !mail )
  120.             {    /* Read a buffer load from the log file and convert CR to LF */
  121.             Ptr tptr; LongInt lf_size = size;
  122.  
  123.             FSRead(fRef, &lf_size, mailing);
  124.  
  125.             for (size=0, tptr = mailing; size < lf_size; size++, tptr++)
  126.                 if (*tptr == '\015') *tptr = '\012';
  127.             }
  128.  
  129.         TCPSWrite(cRef, mailing, size, &state);
  130.         WaitForState(&state);
  131.  
  132.         if (state < 0) break;
  133.  
  134.         if ( mail )
  135.             mailing += size;
  136.         }
  137.  
  138.     if ( mail )
  139.         HUnlock(mail);
  140.     else
  141.         DisposPtr(mailing);
  142.  
  143.     if (state < 0)    /* mail failed */
  144.         {
  145.         TCPSClose(cRef, &state);
  146.         WaitForState(&state);
  147.         return;    /* read failed */
  148.         }
  149.  
  150.     if ( !SendString(cRef, "\p\015\012.\015\012") )
  151.         return;
  152.  
  153.     if ( !GetResponse(cRef, '2') )
  154.         return;
  155.  
  156.     if ( !SendString(cRef, "\pQUIT\015\012") )
  157.         return;
  158.  
  159.     if ( !GetResponse(cRef, '2') )
  160.         return;
  161.  
  162.     TCPSClose(cRef, &state);
  163.     WaitForState(&state);
  164. }
  165.  
  166. /********************************************************************/
  167. /*                 Sends a string to the remote host.                    */
  168. /********************************************************************/
  169. static integer SendString(integer cRef, StringPtr s)
  170. {
  171.     Word    slen;
  172.     integer state;
  173.  
  174.     slen = *s++;
  175.  
  176.     if ( TCPSWrite(cRef, (Ptr)s, slen, &state) == noErr )
  177.         WaitForState(&state);
  178.  
  179.     if (state < 0)
  180.         {
  181.         if DEBUGGING log_printf("sendstring failed in SMTP\n");
  182.         TCPSClose(cRef, &state);
  183.         WaitForState(&state);
  184.         return 0;    /* write failed */
  185.         }
  186.     return 1;
  187. }
  188.  
  189. /********************************************************************/
  190. /*                 Get a response from the remote host.                */
  191. /********************************************************************/
  192. static integer GetResponse(integer cRef, Byte which)
  193. {
  194.     integer    state, size, eof;
  195.     Byte    buff[256], *s=buff;
  196.  
  197.     size = 256;
  198.  
  199.     if ( TCPSRead(cRef, (Ptr)buff, &size, &eof, &state) == noErr )
  200.         WaitForState(&state);
  201.  
  202.     buff[size] = 0;
  203.     if (state < 0 || buff[0] != which)
  204.         {
  205.         if DEBUGGING
  206.             {
  207.             if (state < 0)  log_printf("response failed in SMTP\n");
  208.             else            log_printf("NAK received in SMTP, (%s)\n", buff);
  209.             }
  210.         TCPSClose(cRef, &state);
  211.         WaitForState(&state);
  212.         return 0;    /* read failed */
  213.         }
  214.  
  215.     return 1;
  216. }
  217.