home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / comm / news102.sit / NewsWatcher / source / smtplow.c < prev    next >
Text File  |  1991-04-03  |  5KB  |  244 lines

  1. /*----------------------------------------------------------
  2. #
  3. #    NewsWatcher    - Macintosh NNTP Client Application
  4. #
  5. #    Written by Steven Falkenburg
  6. #    ⌐1990 Apple Computer, Inc.
  7. #
  8. #-----------------------------------------------------------
  9. #
  10. #    smtplow.c
  11. #
  12. #    This module contains a routine which is called to send
  13. #    mail to a remote host using the Simple Mail Transfer Protocol
  14. #
  15. #-----------------------------------------------------------*/
  16.  
  17. #pragma segment netstuff
  18.  
  19. #include "compat.h"
  20. #include <String.h>
  21.  
  22. #ifdef PROTOS
  23. #include <Types.h>
  24. #include <QuickDraw.h>
  25. #include <Events.h>
  26. #include <Controls.h>
  27. #include <Windows.h>
  28. #include <TextEdit.h>
  29. #include <Dialogs.h>
  30. #include <Lists.h>
  31. #endif
  32.  
  33. #include "nntp.h"
  34. #include "miscstuff.h"
  35. #include "MacTCPCommonTypes.h"
  36. #include "AddressXLation.h"
  37. #include "TCPPB.h"
  38. #include "TCPHi.h"
  39. #include "SMTPLow.h"
  40.  
  41. /* forward declaration */
  42.  
  43. void RcptMsg(char *text,unsigned long stream,char *header);
  44.  
  45.  
  46. /*    SendSMTP sends a message through e-mail by contacting the local
  47.     SMTP server and sending the mail.
  48. */
  49.  
  50. Boolean SendSMTP(char *text,unsigned short tLength)
  51. {
  52.     extern TPrefRec gPrefs;
  53.     extern unsigned long gSMTPAddress;
  54.     unsigned long stream;
  55.     Ptr data;
  56.     unsigned short length;
  57.     Str255 sendData[6];
  58.     unsigned long hostAddr;
  59.     char commStr[256];
  60.     
  61.     *(text+tLength) = '\0';
  62.  
  63.     hostAddr = gSMTPAddress;
  64.         
  65.     if (CreateStream(&stream,kBufLen) != noErr)
  66.         return false;
  67.     
  68.     if (OpenConnection(stream,hostAddr,kSMTPPort,10) != noErr) {
  69.         AbortConnection(stream);
  70.         ReleaseStream(stream);
  71.         return false;
  72.     }
  73.     
  74.     data = NewPtr(kBufLen);
  75.     if (MyMemErr() != noErr) {
  76.         AbortConnection(stream);
  77.         ReleaseStream(stream);
  78.         return false;
  79.     }
  80.         
  81.     length = kBufLen;
  82.     if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
  83.         AbortConnection(stream);
  84.         ReleaseStream(stream);
  85.         MyDisposPtr(data);
  86.         return false;
  87.     }
  88.     
  89.     strcpy(sendData[0],"MAIL FROM:<");
  90.     strcpy(sendData[1],gPrefs.address);
  91.     strcpy(sendData[2],">");
  92.     strcpy(sendData[3],CRLF);
  93.     
  94.     if (SendMultiData(stream,sendData,4,false) != noErr) {
  95.         AbortConnection(stream);
  96.         ReleaseStream(stream);
  97.         MyDisposPtr(data);
  98.         return false;
  99.     }
  100.     
  101.     length = kBufLen;
  102.     if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
  103.         AbortConnection(stream);
  104.         ReleaseStream(stream);
  105.         MyDisposPtr(data);
  106.         return false;
  107.     }
  108.  
  109.     RcptMsg(text,stream,"To: ");
  110.     RcptMsg(text,stream,"Cc: ");
  111.     RcptMsg(text,stream,"Bcc: ");
  112.     
  113.     /* send DATA command */
  114.     
  115.     strcpy(commStr,"DATA");
  116.     strcat(commStr,CRLF);
  117.     if (SendData(stream,commStr,6,false) != noErr) {
  118.         AbortConnection(stream);
  119.         ReleaseStream(stream);
  120.         MyDisposPtr(data);
  121.         return false;
  122.     }
  123.     length = kBufLen;
  124.     if (RecvData(stream,data,&length,false) != noErr || *data != '3') {
  125.         AbortConnection(stream);
  126.         ReleaseStream(stream);
  127.         MyDisposPtr(data);
  128.         return false;
  129.     }
  130.  
  131.     /* send message */
  132.  
  133.     if (SendData(stream,text,tLength,false) != noErr) {
  134.         AbortConnection(stream);
  135.         ReleaseStream(stream);
  136.         MyDisposPtr(data);
  137.         return false;
  138.     }
  139.  
  140.     strcpy(commStr,CRLF);
  141.     strcat(commStr,".");
  142.     strcat(commStr,CRLF);
  143.     if (SendData(stream,commStr,5,false) != noErr) {
  144.         AbortConnection(stream);
  145.         ReleaseStream(stream);
  146.         MyDisposPtr(data);
  147.         return false;
  148.     }
  149.     
  150.     length = kBufLen;
  151.     if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
  152.         AbortConnection(stream);
  153.         ReleaseStream(stream);
  154.         MyDisposPtr(data);
  155.         return false;
  156.     }
  157.  
  158.     /* send QUIT */
  159.     
  160.     strcpy(commStr,"QUIT");
  161.     strcat(commStr,CRLF);
  162.     if (SendData(stream,commStr,6,false) != noErr) {
  163.         AbortConnection(stream);
  164.         ReleaseStream(stream);
  165.         MyDisposPtr(data);
  166.         return false;
  167.     }
  168.     length = kBufLen;
  169.     if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
  170.         AbortConnection(stream);
  171.         ReleaseStream(stream);
  172.         MyDisposPtr(data);
  173.         return false;
  174.     }
  175.     
  176.     CloseConnection(stream);
  177.     ReleaseStream(stream);
  178.     MyDisposPtr(data);
  179.     return true;
  180. }
  181.  
  182.  
  183. /*    RcptMsg determines the recipients of the message and sends commands
  184.     to the SMTP server specifying these people as recipients.
  185. */
  186.  
  187. void RcptMsg(char *text,unsigned long stream,char *header)
  188. {
  189.     Ptr current,current2,lineEnd,hdrEnd;
  190.     char toStr[256];
  191.     Str255 sendData[6];
  192.     unsigned short length;
  193.     Ptr data;
  194.     char commStr[256];
  195.     
  196.     strcpy(sendData[0],"RCPT TO:<");
  197.     strcpy(sendData[2],">");
  198.     strcpy(sendData[3],CRLF);
  199.  
  200.     data = MyNewPtr(kBufLen);
  201.     if (MyMemErr() != noErr)
  202.         return;
  203.     
  204.     strcpy(commStr,CRLF);
  205.     strcat(commStr,CRLF);
  206.     hdrEnd = (Ptr) strstr(text,commStr);        
  207.     current = (Ptr) strstr(text, header);
  208.     lineEnd = (Ptr) strstr(current,CRSTR);
  209.     
  210.     if (!hdrEnd || !current || !lineEnd || current >= hdrEnd) {
  211.         MyDisposPtr(data);
  212.         return;
  213.     }
  214.  
  215.     current += strlen(header);
  216.     current2 = current;
  217.  
  218.     while (current && current < lineEnd) {
  219.         current2 = (Ptr) strstr(current,",");
  220.         if (!current2 || current2 > lineEnd)
  221.             current2 = lineEnd;
  222.         strncpy(toStr,(char *)current,current2-current);
  223.         toStr[current2-current] = '\0';
  224.         
  225.         strcpy(sendData[1],toStr);
  226.         
  227.         if (SendMultiData(stream,sendData,4,false) != noErr) {
  228.             AbortConnection(stream);
  229.             ReleaseStream(stream);
  230.             MyDisposPtr(data);
  231.             return;
  232.         }
  233.         length = kBufLen;
  234.         if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
  235.             AbortConnection(stream);
  236.             ReleaseStream(stream);
  237.             MyDisposPtr(data);
  238.             return;
  239.         }
  240.         current = current2+1;
  241.     }
  242.     MyDisposPtr(data);
  243. }
  244.