home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Otherware
/
Otherware_1_SB_Development.iso
/
mac
/
util
/
comm
/
news102.sit
/
NewsWatcher
/
source
/
smtplow.c
< prev
next >
Wrap
Text File
|
1991-04-03
|
5KB
|
244 lines
/*----------------------------------------------------------
#
# NewsWatcher - Macintosh NNTP Client Application
#
# Written by Steven Falkenburg
# ⌐1990 Apple Computer, Inc.
#
#-----------------------------------------------------------
#
# smtplow.c
#
# This module contains a routine which is called to send
# mail to a remote host using the Simple Mail Transfer Protocol
#
#-----------------------------------------------------------*/
#pragma segment netstuff
#include "compat.h"
#include <String.h>
#ifdef PROTOS
#include <Types.h>
#include <QuickDraw.h>
#include <Events.h>
#include <Controls.h>
#include <Windows.h>
#include <TextEdit.h>
#include <Dialogs.h>
#include <Lists.h>
#endif
#include "nntp.h"
#include "miscstuff.h"
#include "MacTCPCommonTypes.h"
#include "AddressXLation.h"
#include "TCPPB.h"
#include "TCPHi.h"
#include "SMTPLow.h"
/* forward declaration */
void RcptMsg(char *text,unsigned long stream,char *header);
/* SendSMTP sends a message through e-mail by contacting the local
SMTP server and sending the mail.
*/
Boolean SendSMTP(char *text,unsigned short tLength)
{
extern TPrefRec gPrefs;
extern unsigned long gSMTPAddress;
unsigned long stream;
Ptr data;
unsigned short length;
Str255 sendData[6];
unsigned long hostAddr;
char commStr[256];
*(text+tLength) = '\0';
hostAddr = gSMTPAddress;
if (CreateStream(&stream,kBufLen) != noErr)
return false;
if (OpenConnection(stream,hostAddr,kSMTPPort,10) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
return false;
}
data = NewPtr(kBufLen);
if (MyMemErr() != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
return false;
}
length = kBufLen;
if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
strcpy(sendData[0],"MAIL FROM:<");
strcpy(sendData[1],gPrefs.address);
strcpy(sendData[2],">");
strcpy(sendData[3],CRLF);
if (SendMultiData(stream,sendData,4,false) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
length = kBufLen;
if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
RcptMsg(text,stream,"To: ");
RcptMsg(text,stream,"Cc: ");
RcptMsg(text,stream,"Bcc: ");
/* send DATA command */
strcpy(commStr,"DATA");
strcat(commStr,CRLF);
if (SendData(stream,commStr,6,false) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
length = kBufLen;
if (RecvData(stream,data,&length,false) != noErr || *data != '3') {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
/* send message */
if (SendData(stream,text,tLength,false) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
strcpy(commStr,CRLF);
strcat(commStr,".");
strcat(commStr,CRLF);
if (SendData(stream,commStr,5,false) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
length = kBufLen;
if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
/* send QUIT */
strcpy(commStr,"QUIT");
strcat(commStr,CRLF);
if (SendData(stream,commStr,6,false) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
length = kBufLen;
if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return false;
}
CloseConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return true;
}
/* RcptMsg determines the recipients of the message and sends commands
to the SMTP server specifying these people as recipients.
*/
void RcptMsg(char *text,unsigned long stream,char *header)
{
Ptr current,current2,lineEnd,hdrEnd;
char toStr[256];
Str255 sendData[6];
unsigned short length;
Ptr data;
char commStr[256];
strcpy(sendData[0],"RCPT TO:<");
strcpy(sendData[2],">");
strcpy(sendData[3],CRLF);
data = MyNewPtr(kBufLen);
if (MyMemErr() != noErr)
return;
strcpy(commStr,CRLF);
strcat(commStr,CRLF);
hdrEnd = (Ptr) strstr(text,commStr);
current = (Ptr) strstr(text, header);
lineEnd = (Ptr) strstr(current,CRSTR);
if (!hdrEnd || !current || !lineEnd || current >= hdrEnd) {
MyDisposPtr(data);
return;
}
current += strlen(header);
current2 = current;
while (current && current < lineEnd) {
current2 = (Ptr) strstr(current,",");
if (!current2 || current2 > lineEnd)
current2 = lineEnd;
strncpy(toStr,(char *)current,current2-current);
toStr[current2-current] = '\0';
strcpy(sendData[1],toStr);
if (SendMultiData(stream,sendData,4,false) != noErr) {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return;
}
length = kBufLen;
if (RecvData(stream,data,&length,false) != noErr || *data != '2') {
AbortConnection(stream);
ReleaseStream(stream);
MyDisposPtr(data);
return;
}
current = current2+1;
}
MyDisposPtr(data);
}