home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************
- * *
- * Mail.c *
- * *
- * Line Printer Daemon using TCP/IP printer protocol *
- * *
- * -------------- The smtp routines -------------- *
- * *
- * Written by Casper Boon, November, 1992. *
- * *
- * © 1992 Casper Boon. *
- * *
- * SendMail sends mail to a user at a specified host using the SMTP *
- * protocol. The mail is usually a handle, but in the case of the log *
- * file, may also be a file if the handle is NIL. *
- * *
- ************************************************************************/
-
- #include "LPD.H"
- #include "TCPStream.H"
- #include "BackGrounder.h"
- #include "lpdProtos.h"
-
-
- static integer SendString(integer crefnum, StringPtr s);
- static integer GetResponse(integer crefnum, Byte which);
-
-
- void SendMail(char * who, char * host, Handle mail, integer fRef)
- {
- integer cRef, state, size, eof;
- LongInt handSize;
- Byte buff[256];
- Ptr mailing;
- char who2[12], where[80];
-
- /* convert the who and where if they were aliased */
- ResolveAlias(who, host, who2, where);
- who = who2; host = where;
-
- /* open a connection to the SMTP port */
- if ( TCPSOpen( &cRef, host, 0, SMTP_PORT, FALSE, &state) != noErr )
- { /* it failed, fall out now */
- if DEBUGGING
- log_printf("Cannot connect to SMTP port on %s\n", where);
- return;
- }
-
- WaitForState(&state);
-
- if (state < 0)
- {
- TCPSClose(cRef, &state);
- WaitForState(&state);
- if DEBUGGING
- log_printf("Cannot connect to SMTP port on %s\n", where);
- return ; /* couldn't open */
- }
-
- if ( !GetResponse(cRef, '2') )
- return;
-
- if ( !SendString(cRef, "\pMAIL FROM: <lpDaemon>\015\012") )
- return;
-
- if ( !GetResponse(cRef, '2') )
- return;
-
- psprintf(buff, "RCPT TO: <%s@%s>\015\012", who, host);
- if ( !SendString(cRef, buff) )
- return;
-
- if ( !GetResponse(cRef, '2') )
- return;
-
- if ( !SendString(cRef, "\pDATA\015\012") )
- return;
-
- if ( !GetResponse(cRef, '3') )
- return;
-
- if ( !SendString(cRef, "\pFrom: <lpDaemon>\015\012") )
- return;
-
- psprintf(buff, "To: <%s@%s>\015\012", who, host);
- if ( !SendString(cRef, buff) )
- return;
-
- if ( mail )
- {
- if ( !SendString(cRef, "\pSubject: PostScript Errors\015\012") )
- return;
-
- handSize = GetHandleSize(mail);
-
- HLock(mail);
- mailing = *mail;
- }
- else
- {
- if ( !SendString(cRef, "\pSubject: lpDaemon Log File\015\012") )
- return;
-
- mailing = NewPtr(1024);
-
- if (GetEOF(fRef, &handSize) != noErr)
- handSize = 0;
- }
-
-
- while (handSize)
- {
- if (handSize > 1024)
- size = 1024;
- else
- size = handSize;
- handSize -= size;
-
- if ( !mail )
- { /* Read a buffer load from the log file and convert CR to LF */
- Ptr tptr; LongInt lf_size = size;
-
- FSRead(fRef, &lf_size, mailing);
-
- for (size=0, tptr = mailing; size < lf_size; size++, tptr++)
- if (*tptr == '\015') *tptr = '\012';
- }
-
- TCPSWrite(cRef, mailing, size, &state);
- WaitForState(&state);
-
- if (state < 0) break;
-
- if ( mail )
- mailing += size;
- }
-
- if ( mail )
- HUnlock(mail);
- else
- DisposPtr(mailing);
-
- if (state < 0) /* mail failed */
- {
- TCPSClose(cRef, &state);
- WaitForState(&state);
- return; /* read failed */
- }
-
- if ( !SendString(cRef, "\p\015\012.\015\012") )
- return;
-
- if ( !GetResponse(cRef, '2') )
- return;
-
- if ( !SendString(cRef, "\pQUIT\015\012") )
- return;
-
- if ( !GetResponse(cRef, '2') )
- return;
-
- TCPSClose(cRef, &state);
- WaitForState(&state);
- }
-
- /********************************************************************/
- /* Sends a string to the remote host. */
- /********************************************************************/
- static integer SendString(integer cRef, StringPtr s)
- {
- Word slen;
- integer state;
-
- slen = *s++;
-
- if ( TCPSWrite(cRef, (Ptr)s, slen, &state) == noErr )
- WaitForState(&state);
-
- if (state < 0)
- {
- if DEBUGGING log_printf("sendstring failed in SMTP\n");
- TCPSClose(cRef, &state);
- WaitForState(&state);
- return 0; /* write failed */
- }
- return 1;
- }
-
- /********************************************************************/
- /* Get a response from the remote host. */
- /********************************************************************/
- static integer GetResponse(integer cRef, Byte which)
- {
- integer state, size, eof;
- Byte buff[256], *s=buff;
-
- size = 256;
-
- if ( TCPSRead(cRef, (Ptr)buff, &size, &eof, &state) == noErr )
- WaitForState(&state);
-
- buff[size] = 0;
- if (state < 0 || buff[0] != which)
- {
- if DEBUGGING
- {
- if (state < 0) log_printf("response failed in SMTP\n");
- else log_printf("NAK received in SMTP, (%s)\n", buff);
- }
- TCPSClose(cRef, &state);
- WaitForState(&state);
- return 0; /* read failed */
- }
-
- return 1;
- }
-