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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
|
/*
** Routines to read and write mail and news headers. The code here
** is gross and complicated.
*/
#include "gate.h"
#include <time.h>
#if defined(RCSID)
static char RCS[] =
"$Header: /nfs/papaya/u2/rsalz/src/newsgate/RCS/rfc822.c,v 1.11 91/07/22 10:26:51 rsalz Exp Locker: rsalz $";
#endif /* defined(RCSID) */
extern time_t time();
extern char *asctime();
extern struct tm *gmtime();
#define QUESTIONABLE(c) \
((c) == '"' || (c) == '(' || (c) == ')' || (c) == '\\')
#define HDR_OTHER -1
#define HDR_END FALSE
#define HDR_APPROVED 1
#define HDR_CONTROL 2
#define HDR_DATE 3
#define HDR_DISTRIBUTION 4
#define HDR_EXPIRE 5
#define HDR_FOLLOWTO 6
#define HDR_FROM 7
#define HDR_KEYWORDS 8
#define HDR_MESSAGEID 9
#define HDR_NEWSGROUP 10
#define HDR_ORGANIZATION 11
#define HDR_REFERENCES 12
#define HDR_REPLYTO 13
#define HDR_SENDER 14
#define HDR_SUMMARY 15
#define HDR_TITLE 16
#define HDR_MIMEVERS 17
#define HDR_MIMEXFER 18
#define HDR_MIMETYPE 19
#define HDR_EXT 20
/*
** The list of headers we recognize; all others are stripped.
*/
typedef struct _HTYPE {
STRING Name;
int Type;
} HTYPE;
STATIC HTYPE HeaderList[] = {
{ "Approved:", HDR_APPROVED },
{ "Control:", HDR_CONTROL },
{ "Date:", HDR_DATE },
{ "Posted:", HDR_DATE },
{ "Distribution:", HDR_DISTRIBUTION },
{ "Expires:", HDR_EXPIRE },
{ "Followup-To:", HDR_FOLLOWTO },
{ "From:", HDR_FROM },
{ "Keywords:", HDR_KEYWORDS },
{ "MIME-Version:", HDR_MIMEVERS },
{ "Content-Type:", HDR_MIMETYPE },
{ "Content-Transfer-Encoding", HDR_MIMEXFER },
{ "Message-ID:", HDR_MESSAGEID },
{ "Newsgroups:", HDR_NEWSGROUP },
{ "Organization:", HDR_ORGANIZATION },
{ "In-Reply-To:", HDR_REFERENCES },
{ "References:", HDR_REFERENCES },
{ "Reply-To:", HDR_REPLYTO },
{ "Sender:", HDR_SENDER },
{ "Summary:", HDR_SUMMARY },
{ "Subject:", HDR_TITLE },
{ "Title:", HDR_TITLE },
{ "X-", HDR_EXT }
};
/*
** Getline is like fgets, but deals with continuation lines. It also
** ensures that even if a line that is too long is received, the
** remainder of the line is thrown away instead of treated like a second
** line.
*/
STATIC char *
Getline(buf, len, fp)
char *buf;
int len;
FILE *fp;
{
register char *cp;
register int c;
register int n;
for (n = 0, cp = buf; (c = getc(fp)) != EOF && c != '\n' && n < len; )
if (!iscntrl(c) || c == '\b' || c == '\t') {
*cp++ = c;
n++;
}
if (c == EOF && cp == buf)
return NULL;
*cp = '\0';
if (c != '\n')
/* Line too long - read part didn't fit into a newline */
while ((c = getc(fp)) != '\n' && c != EOF)
continue;
else if (cp == buf) {
/* Don't look for continuation of blank lines */
*cp++ = '\n';
*cp = '\0';
return buf;
}
while ((c = getc(fp)) == ' ' || c == '\t') {
/* Continuation line. */
if ((n += 2) < len) {
*cp++ = '\n';
*cp++ = c;
}
while ((c = getc(fp)) != '\n' && c != EOF)
if ((!iscntrl(c) || c == '\b' || c == '\t') && n++ < len)
*cp++ = c;
}
if (n >= len - 1)
cp = buf + len - 2;
*cp++ = '\n';
*cp = '\0';
if (c != EOF)
/* push back first char of next header */
(void)ungetc(c, fp);
return buf;
}
/*
** I guess this is basically strncasecmp
*/
STATIC int
prefix(full, pref)
register char *full;
register char *pref;
{
register char fc;
register char pc;
while ((pc = *pref++) != '\0') {
fc = *full++;
if (isupper(fc))
fc = tolower(fc);
if (isupper(pc))
pc = tolower(pc);
if (fc != pc)
return FALSE;
}
return TRUE;
}
STATIC int
HeaderType(p)
register char *p;
{
static int save = HDR_END;
register HTYPE *hp;
char *colon;
char *space;
/* some consistency checks (i.e. is this really a header line?) */
if (p == NULL || !isascii(*p) || *p == '\n')
return save = HDR_END;
if (WHITE(*p))
/* Continuation line. */
return save;
/* If we don't get a "<no space> <colon> <space>", it's not a header. */
if ((colon = IDX(p, ':')) == NULL
|| ((space = IDX(p, ' ')) && space < colon))
return save = HDR_END;
for (hp = HeaderList; hp < ENDOF(HeaderList); hp++)
if (prefix(p, hp->Name))
return save = hp->Type;
return save = HDR_OTHER;
}
/*
** Get the contents of the field of the header line, appending it, with a
** space delimeter if it's a continuation line. If there is already
** something in the header storage, skip this header line and the
** continuations.
*/
STATIC void
getfield(src, dest, size)
register char *src;
register char *dest;
register int size;
{
static int skip = FALSE;
register char *p;
if (src == NULL || dest == NULL)
return;
if (WHITE(*src)) {
/* Continuation line. If skipping or no room, ignore. */
if (skip || (size -= strlen(dest)) <= 0)
return;
/* Munch all but one whitespace, append it to header. */
while (*src && WHITE(*src))
src++;
*--src = ' ';
(void)strncat(dest, src, size - 1);
}
else {
skip = FALSE;
if (*dest) {
/* Already got a value, so mark this as one to skip. */
skip = TRUE;
return;
}
if ((src = IDX(src, ':')) == NULL)
/* Can't happen! */
return;
/* Skip colon, eat whitespace. */
for (src++; *src && WHITE(*src); )
src++;
(void)strncpy(dest, src, size - 1);
}
/* Munch trailing whitespace. */
for (p = dest + strlen(dest); --p >= dest && (*p == '\n' || WHITE(*p)); )
continue;
p[1] = '\0';
}
STATIC time_t
cgtdate(datestr)
char *datestr;
{
static time_t lasttime;
static char save[SM_SIZE];
char junk[40];
char month[40];
char day[30];
char tod[60];
char year[50];
char buf[SM_SIZE];
#if defined(USE_GETABSDATE)
extern time_t getabsdate();
#else
#if defined(USE_PARSEDATE)
extern time_t parse();
#else
extern time_t getdate();
#endif /* defined(USE_PARSEDATE) */
#endif /* defined(USE_GETABSDATE) */
if (save[0] && EQ(datestr, save))
return lasttime;
#if defined(USE_GETABSDATE)
lasttime = getabsdate(datestr, (struct timeb *)NULL);
#else
#if defined(USE_PARSEDATE)
lasttime = parsedate(datestr, (struct _TIMEINFO *)NULL);
#else
lasttime = getdate(datestr, (struct timeb *)NULL);
#endif /* defined(USE_PARSEDATE) */
#endif /* defined(USE_GETABSDATE) */
if (lasttime < 0 &&
sscanf(datestr, "%s %s %s %s %s", junk, month, day, tod, year) == 5) {
(void)sprintf(buf, "%s %s, %s %s", month, day, year, tod);
#if defined(USE_GETABSDATE)
lasttime = getabsdate(buf, (struct timeb *)NULL);
#else
#if defined(USE_PARSEDATE)
lasttime = parsedate(buf, (struct _TIMEINFO *)NULL);
#else
lasttime = getdate(buf, (struct timeb *)NULL);
#endif /* defined(USE_PARSEDATE) */
#endif /* defined(USE_GETABSDATE) */
}
(void)strncpy(save, datestr, sizeof save);
return lasttime;
}
/*
** Print the date in ARPA format, not ctime(3) format.
*/
STATIC void
DoDate(fp, ud)
FILE *fp;
register char *ud;
{
register char *p;
register char *q;
register int i;
char buff[SM_SIZE];
q = buff;
/* "16" */
p = &ud[8];
if (*p == ' ')
p++;
else
*q++ = *p++;
*q++ = *p++; *q++ = ' ';
/* "Sep " */
p = &ud[4]; *q++ = *p++; *q++ = *p++; *q++ = *p++; *q++ = ' ';
/* "79 " */
p = &ud[22]; *q++ = *p++; *q++ = *p++; *q++ = ' ';
/* "01:03:52" */
for (p = &ud[11], i = 8; i > 0; i--)
*q++ = *p++;
/* " GMT" */
*q++ = ' '; *q++ = 'G'; *q++ = 'M'; *q++ = 'T'; *q = '\0';
Fprintf(fp, "Date: %s\n", buff);
}
/*
** Crack an RFC822 from header field into address and fullname. We do
** this to make sure we write things out in official form. "Be liberal
** in what you accept, conservative in what you generate." Anyhow, we
** read things into three buffers, one for all <...> text, one for all
** (...) text, and a third for stuff not in either. Either the first or
** third buffer will be the real address, depending on whether there is
** anything in buffer two or not.
*/
int
CrackFrom(addr, name, p)
ADDRCHAR *addr;
char *name;
char *p;
{
register ADDRCHAR *adp;
register char *bp;
register char *ap;
register ADDRCHAR *cp;
register ADDRCHAR CurrChar;
register int comment;
register int address;
register int addrfound;
register int QuoteNext;
register int InQuotes;
ADDRCHAR *xp;
ADDRCHAR comm[LG_SIZE];
ADDRCHAR commbuf[LG_SIZE];
ADDRCHAR addrbuf[LG_SIZE];
/* Just to make sure. */
*name = '\0';
*addr = '\0';
if (p == NULL)
return FALSE;
/* Eat leading white space. */
while (*p && isspace(*p))
p++;
/* Set defaults. */
comm[0] = '\0';
commbuf[0] = '\0';
addrbuf[0] = '\0';
adp = addrbuf;
comment = 0;
addrfound = 0;
address = 0;
QuoteNext = 0;
InQuotes = 0;
for (; *p; p++) {
CurrChar = (*p & UNQUOTE_MASK) | QuoteNext;
QuoteNext = CurrChar == '\\' ? QUOTE_MASK : 0;
if (!comment && CurrChar == '"')
InQuotes = InQuotes ? 0 : QUOTE_MASK;
else
CurrChar |= InQuotes;
switch (CurrChar) {
case '(':
if (comment == 0) {
cp = commbuf;
*cp = '\0';
}
comment++;
break;
case ')':
if (comment > 0 && --comment == 0) {
*cp = '\0';
xp = comm;
if (*xp) {
while (*xp)
xp++;
*xp++ = ',';
*xp++ = ' ';
}
for (cp = &commbuf[1]; (*xp++ = *cp) != '\0'; cp++)
continue;
cp = NULL;
continue;
}
break;
case '<':
if (address)
return FALSE; /* AWK! Abort! */
if (!comment) {
address++;
*adp = '\0';
adp = addr;
}
break;
case '>':
if (!comment && address) {
address--;
addrfound++;
for (*adp = '\0', adp = addrbuf; *adp; )
adp++;
continue;
}
break;
}
if (comment)
*cp++ = CurrChar;
else if (!address || CurrChar != '<')
*adp++ = CurrChar;
if (*p == '\0')
break;
}
*adp++ = '\0';
if (addrfound) {
for (ap = name, xp = addrbuf; (*ap++ = (*xp & UNQUOTE_MASK)) != 0; xp++)
continue;
}
else {
for (cp = addr, xp = addrbuf; (*cp++ = *xp) != 0; xp++)
continue;
*name = '\0';
}
/* Just to be sure that we got the full name, we'll take all of
* the comments. */
if (*comm) {
ap = name;
if (*ap) {
while (*ap)
ap++;
*ap++ = ',';
*ap++ = ' ';
}
for (xp = comm; (*ap = (*xp++ & UNQUOTE_MASK)) != 0; ap++)
continue;
}
/* Copy back, skipping leading spaces and trailing spaces. */
for (cp = addr; isspace(*cp); cp++)
continue;
for (xp = addr; (*xp = *cp++) != 0; )
xp++;
while (*addr && isspace(*--xp))
*xp = '\0';
/* Since characters in (comments) are interpreted differently from
* those in "phrase" <address>, play it safe and remove all
* questionable characters. */
for (ap = name, bp = name; isspace(*bp) || QUESTIONABLE(*bp); bp++)
continue;
while ((*ap = *bp++) != '\0')
if (!QUESTIONABLE(*ap))
ap++;
while (*name && isspace(*--ap))
*ap = '\0';
return TRUE;
}
/*
** Write out an RFC822 header, paying no attention to line limits.
** Ideally, we should do continuations in here...
*/
int
rfc822write(hp, fp)
register HBUF *hp;
register FILE *fp;
{
time_t t;
struct xlist *p;
if (hp->path[0])
Fprintf(fp, "Path: %s\n", hp->path);
if (hp->from[0])
Fprintf(fp, "From: %s\n", hp->from);
if (hp->nbuf[0])
Fprintf(fp, "Newsgroups: %s\n", hp->nbuf);
if (hp->title[0])
Fprintf(fp, "Subject: %s\n", hp->title);
if (hp->ident[0])
Fprintf(fp, "Message-ID: %s\n", hp->ident);
/* Get current time. This will be used resolve the timezone. */
if (hp->subdate[0] == '\0')
(void)time(&t);
else if ((t = cgtdate(hp->subdate)) < 0) {
Fprintf(fp, "X-Unparseable-Date: %s\n", hp->subdate);
(void)time(&t);
}
DoDate(fp, asctime(gmtime(&t)));
if (hp->expdate[0])
Fprintf(fp, "Expires: %s\n", hp->expdate);
if (hp->followid[0])
Fprintf(fp, "References: %s\n", hp->followid);
if (hp->ctlmsg[0])
Fprintf(fp, "Control: %s\n", hp->ctlmsg);
if (hp->sender[0])
Fprintf(fp, "Sender: %s\n", hp->sender);
if (hp->mimeversion[0])
Fprintf(fp, "MIME-Version: %s\n", hp->mimeversion);
if (hp->mimexfer[0])
Fprintf(fp, "Content-Transfer-Encoding: %s\n", hp->mimexfer);
if (hp->mimetype[0])
Fprintf(fp, "Content-Type: %s\n", hp->mimetype);
if (hp->replyto[0])
Fprintf(fp, "Reply-To: %s\n", hp->replyto);
if (hp->followto[0])
Fprintf(fp, "Followup-To: %s\n", hp->followto);
if (hp->distribution[0])
Fprintf(fp, "Distribution: %s\n", hp->distribution);
if (hp->organization[0])
Fprintf(fp, "Organization: %s\n", hp->organization);
if (hp->keywords[0])
Fprintf(fp, "Keywords: %s\n", hp->keywords);
if (hp->summary[0])
Fprintf(fp, "Summary: %s\n", hp->summary);
if (hp->approved[0])
Fprintf(fp, "Approved: %s\n", hp->approved);
for (p=hp->xhead; p; p=p->next)
Fprintf(fp, "%s", p->header);
Fprintf(fp, "\n");
return !ferror(fp);
}
void
rfc822read(hp, fp, buff, buffsize)
register HBUF *hp;
register FILE *fp;
char *buff;
int buffsize;
{
register int i;
long curpos;
/* Zap out the headers. */
hp->approved[0] = '\0';
hp->ctlmsg[0] = '\0';
hp->subdate[0] = '\0';
hp->distribution[0] = '\0';
hp->expdate[0] = '\0';
hp->followto[0] = '\0';
hp->from[0] = '\0';
hp->followid[0] = '\0';
hp->keywords[0] = '\0';
hp->ident[0] = '\0';
hp->nbuf[0] = '\0';
hp->organization[0] = '\0';
hp->title[0] = '\0';
hp->replyto[0] = '\0';
hp->summary[0] = '\0';
hp->path[0] = '\0';
hp->sender[0] = '\0';
hp->mimeversion[0] = '\0';
hp->mimexfer[0] = '\0';
hp->mimetype[0] = '\0';
hp->xhead = hp->xtail = 0;
i = HeaderType(buff);
do {
curpos = ftell(fp);
switch (i) {
case HDR_APPROVED:
getfield(buff, hp->approved, sizeof hp->approved);
break;
case HDR_CONTROL:
getfield(buff, hp->ctlmsg, sizeof hp->ctlmsg);
break;
case HDR_DATE:
getfield(buff, hp->subdate, sizeof hp->subdate);
break;
case HDR_DISTRIBUTION:
getfield(buff, hp->distribution, sizeof hp->distribution);
break;
case HDR_EXPIRE:
getfield(buff, hp->expdate, sizeof hp->expdate);
break;
case HDR_FOLLOWTO:
getfield(buff, hp->followto, sizeof hp->followto);
break;
case HDR_FROM:
getfield(buff, hp->from, sizeof hp->from);
break;
case HDR_KEYWORDS:
getfield(buff, hp->keywords, sizeof hp->keywords);
break;
case HDR_MESSAGEID:
getfield(buff, hp->ident, sizeof hp->ident);
break;
case HDR_NEWSGROUP:
getfield(buff, hp->nbuf, sizeof hp->nbuf);
break;
case HDR_ORGANIZATION:
getfield(buff, hp->organization, sizeof hp->organization);
break;
case HDR_REFERENCES:
getfield(buff, hp->followid, sizeof hp->followid);
break;
case HDR_REPLYTO:
getfield(buff, hp->replyto, sizeof hp->replyto);
break;
case HDR_SENDER:
getfield(buff, hp->sender, sizeof hp->sender);
break;
case HDR_MIMEVERS:
getfield(buff, hp->mimeversion, sizeof hp->mimeversion);
break;
case HDR_MIMEXFER:
getfield(buff, hp->mimexfer, sizeof hp->mimexfer);
break;
case HDR_MIMETYPE:
getfield(buff, hp->mimetype, sizeof hp->mimetype);
break;
case HDR_SUMMARY:
getfield(buff, hp->summary, sizeof hp->summary);
break;
case HDR_TITLE:
getfield(buff, hp->title, sizeof hp->title);
break;
case HDR_EXT: {
struct xlist *node = NEW(struct xlist, 1);
node->header = strdup(buff);
node->next = 0;
if (hp->xtail) hp->xtail->next = node;
else hp->xhead = node;
hp->xtail = node;
}
break;
}
} while ((i = HeaderType(Getline(buff, buffsize, fp))) != HDR_END);
if (*buff != '\n')
(void)fseek(fp, curpos, 0);
}
|