1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/* macnsmtp.c -- simple async SMTP client
*/
/* (C) Copyright 1995 by Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of Carnegie
* Mellon University not be used in advertising or publicity
* pertaining to distribution of the software without specific,
* written prior permission. Carnegie Mellon University makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied
* warranty.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/* (C) Copyright 1994-1995 by Christopher J. Newman
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Christopher J. Newman not be used in
* advertising or publicity pertaining to distribution of the software without
* specific, written prior permission. Christopher J. Newman makes no
* representations about the suitability of this software for any purpose. It
* is provided "as is" without express or implied warranty.
*
* CHRISTOPHER J. NEWMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
* SHALL CHRISTOPHER J. NEWMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
* Author: Christopher J. Newman
* Message: This is a nifty program.
*/
#include "macnapp.h"
#define SMTP_PORT 25
typedef struct {
na_win w;
void *context; /* user context */
short num, rcpt; /* recipient count (num), and sent (rcpt) */
na_smtpstat statf; /* callback */
na_tcp tcpid; /* TCP id */
short state; /* SMTP state (see below) */
short count; /* bytes used in linebuf */
short refnum; /* input file */
short crfound:1; /* found a CR in SMTP server data */
short crlf:1; /* found a CRLF in SMTP server data */
short crtrans:1; /* translate CR to CRLF in file */
long headsize; /* size of extra headers starting at data */
long fsize, fdone; /* file size & amount written */
long bufsize; /* usable buffer size */
Ptr buf; /* output buffer */
char linebuf[1024]; /* input line buffer */
char data[1]; /* header & envelope */
} na_smtpwin;
#define sw ((na_smtpwin *) w)
/* states: */
#define S_conn 0 /* connecting to server */
#define S_greet 1 /* waiting for greeting */
#define S_hello 2 /* waiting for local host lookup and HELO reply */
#define S_mailf 3 /* waiting for MAIL FROM reply */
#define S_rcpt 4 /* waiting for RCPT TO reply */
#define S_data 5 /* waiting for DATA continue reply */
#define S_send 6 /* transmitting data */
#define S_done 7 /* waiting for DATA success reply */
#define S_quit 8 /* waiting for QUIT reply */
#define S_wait 9 /* waiting for connection close */
#define S_close 10 /* closed */
/* generate and submit SMTP command line (put command & data together with CRLF ending)
* returns NATCPwrite result code
*/
static int SMTPsend(na_win *w, char *com, char *data)
{
char buf[512];
char *dest = buf;
int result = 0;
while ((*dest = *com) != '\0') ++dest, ++com;
if (data) {
while ((*dest = *data++) != '\0') ++dest;
if (com[-1] == '<') *dest++ = '>';
}
*dest++ = '\r';
*dest++ = '\n';
result = NATCPwrite(sw->tcpid, buf, dest - buf, -1);
return (result);
}
/* do final callback
*/
static void smtpclose(na_win *w, short code, short err, long size, char *data)
{
if (sw->state < S_wait) {
NATCPclose(sw->tcpid);
FSClose(sw->refnum);
sw->state = S_wait;
(*sw->statf)(sw->context, code, err, size, data);
}
}
/* TCP read/write callback
*/
static void readp(void *wh, na_tcp s, short status, long size, char *data)
{
na_win *w, **taskh;
char *dest;
short major, count, smtpcode;
/* make sure our SMTP task still exists */
for (taskh = NAtask; taskh && taskh != wh; taskh = (*taskh)->task);
if (!taskh) return;
/* handle TCP result */
w = NAlockWindow((na_win **) wh);
if (status == NATCP_connect) {
/* deal with new connection */
sw->state = S_greet;
} else if (status < 0) {
/* deal with TCP errors */
smtpclose(w, NASMTP_tcpfail, status, size, NULL);
if (status == NATCP_closed) sw->state = S_close;
} else if (status & NATCP_closing) {
/* deal with a closed connection */
if (sw->state < S_wait) {
smtpclose(w, NASMTP_conclosed, 0, 0, NULL);
}
} else if (status & NATCP_data) {
do {
/* buffer SMTP line */
dest = sw->linebuf + sw->count;
while (size && sw->count < sizeof (sw->linebuf)) {
--size, ++sw->count;
if (sw->crfound && *data == '\n') {
*--dest = '\0';
--sw->count;
++data;
sw->crfound = 0;
sw->crlf = 1;
break;
}
sw->crfound = (*dest++ = *data++) == '\r';
}
if (!sw->crlf) {
if (sw->count == sizeof (sw->linebuf)) {
sw->linebuf[sw->count - 1] = '\0';
smtpclose(w, NASMTP_badprot, 0, 0, sw->linebuf);
}
break;
}
sw->crlf = 0;
/* parse SMTP result code */
dest = sw->linebuf;
if (sw->count < 3 || !isdigit(dest[0])
|| !isdigit(dest[1]) || !isdigit(dest[2])) {
smtpclose(w, NASMTP_badprot, 0, 0, dest);
break;
}
sw->count = 0;
major = dest[0] - '0';
smtpcode = major * 100 + (dest[1] - '0') * 10 + (dest[2] - '0');
/* handle reply continuation */
if (dest[3] == '-') continue;
/* handle major errors */
if (major > 3) {
if (sw->state != S_rcpt) {
smtpclose(w, major == 4 ? NASMTP_temperr : NASMTP_permerr,
smtpcode, sw->state, dest + 3);
break;
}
(*sw->statf)(sw->context, NASMTP_badaddr, smtpcode, 0, sw->linebuf + 3);
}
dest = sw->data + sw->headsize;
/* state changes */
switch (sw->state) {
case S_greet:
if (sw->buf) {
SMTPsend(w, "HELO ", sw->buf);
if (sw->buf) DisposPtr(sw->buf);
}
sw->state = S_hello;
break;
case S_hello:
SMTPsend(w, "MAIL FROM:<", dest);
(*sw->statf)(sw->context, NASMTP_progress, 5, 0, 0);
sw->state = S_mailf;
break;
case S_mailf:
case S_rcpt:
count = ++sw->rcpt;
if (count < sw->num + 1) {
while (count--) {
while (*dest++);
}
SMTPsend(w, "RCPT TO:<", dest);
(*sw->statf)(sw->context, NASMTP_progress, 5 + 10 * sw->rcpt / sw->num, 0, 0);
} else {
sw->state = S_data;
SMTPsend(w, "DATA", 0);
(*sw->statf)(sw->context, NASMTP_progress, 20, 0, 0);
}
break;
case S_data:
if (major != 3) {
smtpclose(w, NASMTP_badprot, 0, 0, dest);
break;
}
sw->state = S_send;
if (sw->headsize) {
sw->buf = NewPtr(sw->bufsize = sw->headsize);
if (!sw->buf) {
smtpclose(w, NASMTP_nomem, 0, 0, NULL);
break;
}
memcpy(sw->buf, sw->data, sw->headsize);
}
case S_send:
/* NOTE: this case should never happen */
break;
case S_done:
sw->state = S_quit;
SMTPsend(w, "QUIT", NULL);
(*sw->statf)(sw->context, NASMTP_progress, 95, 0, 0);
break;
case S_quit:
smtpclose(w, NASMTP_completed, 0, 0, 0);
break;
}
} while (size);
}
NAunlockWindowh((na_win **) wh, w)
}
/* TCP gethost callback
*/
static void hostp(void *wh, na_tcp s, short status, long size, char *data)
{
na_win *w, **taskh;
/* make sure our task still exists */
for (taskh = NAtask; taskh && taskh != wh; taskh = (*taskh)->task);
if (!taskh) return;
/* store host/error */
w = NAlockWindow((na_win **) wh);
if (status == NATCP_connect) {
if (sw->state == S_hello) {
SMTPsend(w, "HELO ", data);
} else {
sw->buf = NewPtr(size + 1);
if (sw->buf == NULL) {
smtpclose(w, NASMTP_nomem, 0, 0, NULL);
} else {
memcpy(sw->buf, data, size + 1);
}
}
} else {
smtpclose(w, NASMTP_tcpfail, status, size, NULL);
}
NAunlockWindowh((na_win **) wh, w);
}
/* translate CR to CRLF
*/
static void crtocrlf(char *buf, long *size)
{
long crcount = 0;
char *src, *dst, *end = buf + *size;
for (src = buf; src < end; ++src) {
if (src[0] == '\r') ++crcount;
}
src = end - 1;
for (dst = src + crcount; dst > src; *dst-- = *src--) {
if (*src == '\r') *dst-- = '\n';
}
*size += crcount;
}
/* SMTP task
*/
static short taskp(na_win *w)
{
OSErr oserr;
if (sw->state == S_send || sw->state == S_done) {
/*XXX: could be generous with NewPtr() failure if a NATCPwritePending() */
if (!sw->bufsize) {
if ((sw->buf = NewPtr(8192)) == NULL) {
smtpclose(w, NASMTP_nomem, 0, 0, NULL);
return (NA_NOTPROCESSED);
}
sw->bufsize = sw->crtrans ? 4096 : 8192;
oserr = FSRead(sw->refnum, &sw->bufsize, sw->buf);
if (oserr != noErr && oserr != eofErr) sw->bufsize = 0;
if (!sw->bufsize) {
if (oserr == eofErr && sw->state == S_send) {
memcpy(sw->buf, "\r\n.\r\n", 5);
sw->bufsize = 5;
sw->state = S_done;
} else {
DisposPtr(sw->buf);
}
} else {
if (sw->crtrans) {
crtocrlf(sw->buf, &sw->bufsize);
}
(*sw->statf)(sw->context, NASMTP_progress, 20
+ 70 * (sw->fdone += sw->bufsize) / sw->fsize, 0, 0);
}
}
if (sw->bufsize && NATCPwrite(sw->tcpid, sw->buf, sw->bufsize, 1) == NATCP_data) {
sw->bufsize = 0;
}
}
return (sw->state == S_close ? NA_REQCLOSE : NA_NOTPROCESSED);
}
/* SMTP close procedure
* IMPORTANT: if the user quits during mail transmission, we want to
* warn the user that mail will be lost.
*/
static short closep(na_win *w)
{
if (sw->state < S_wait) {
/*XXX: put modal dialog here, allow abort of close */
if (sw->tcpid >= 0) NATCPclose(sw->tcpid);
FSClose(sw->refnum);
}
return (NA_CLOSED);
}
/* submit file to SMTP:
* creates SMTP task, initializes data, starts TCP connection
* copies from & dest addresses, so they don't need to persist
* task is not completed until statf() is called
*/
void NASMTPsubmit(na_smtpstat statf, char *server, FSSpec *fspec, Handle headers,
Handle envelope, short flags, void *context)
{
long size;
na_win **wh, *w;
char *src, *dst;
OSErr oserr;
/* create task */
size = sizeof (na_smtpwin);
if (headers) size += GetHandleSize(headers);
size += GetHandleSize(envelope);
wh = NAaddtask(taskp, size);
if (!wh) {
(*statf)(context, NASMTP_nomem, 0, 0, 0);
return;
}
/* init task */
w = NAlockWindow(wh);
w->type = NA_SMTPTYPE;
w->closep = closep;
sw->context = context;
sw->statf = statf;
if (headers && (sw->headsize = GetHandleSize(headers))) {
memcpy(sw->data, (char *) *headers, sw->headsize);
}
size = GetHandleSize(envelope);
sw->num = -1;
dst = sw->data + sw->headsize;
for (src = (char *) *envelope; size; --size) {
if ((*dst++ = *src++) == '\0') ++sw->num;
}
if (flags & NASMTP_crtrans) sw->crtrans = 1;
/* open file */
if ((oserr = HOpen(fspec->vRefNum, fspec->parID, fspec->name,
fsRdPerm, &sw->refnum)) != noErr) {
(*statf)(context, NASMTP_oserr, 0, oserr, 0);
NAcloseWindow(w, NA_CLOSED);
return;
}
GetEOF(sw->refnum, &sw->fsize);
/* open MacTCP */
sw->tcpid = NATCPopen(readp, (void *) wh, server, SMTP_PORT, 0);
if (sw->tcpid != -1) NATCPgethost(hostp, (void *) wh);
NAunlockWindowh(wh, w);
}
|