
static char rcsid[] = "@(#)$Id: expires.c,v 1.2 1995/09/29 17:42:07 wfp5p Exp $";

/*******************************************************************************
 *  The Elm Mail System  -  $Revision: 1.2 $   $State: Exp $
 *
 *                      Copyright (c) 1988-1995 USENET Community Trust
 *			Copyright (c) 1986,1987 Dave Taylor
 *******************************************************************************
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *      Bill Pemberton, Elm Coordinator
 *      flash@virginia.edu
 *
 *******************************************************************************
 * $Log: expires.c,v $
 * Revision 1.2  1995/09/29  17:42:07  wfp5p
 * Alpha 8 (Chip's big changes)
 *
 * Revision 1.1.1.1  1995/04/19  20:38:36  wfp5p
 * Initial import of elm 2.4 PL0 as base for elm 2.5.
 *
 ******************************************************************************/

/** This routine is written to deal with the Expires: header on the
    individual mail coming in.  What it does is to look at the date,
    compare it to todays date, then set the EXPIRED flag on the
    current message if it is true...
**/

#include "elm_defs.h"
#include "elm_globals.h"

#ifdef I_TIME
#  include <time.h>
#endif
#ifdef I_SYSTIME
#  include <sys/time.h>
#endif

process_expiration_date(date, message_status)
char *date;
int  *message_status;
{
	struct tm *timestruct;
	time_t thetime;
	char word1[WLEN], word2[WLEN], word3[WLEN], word4[WLEN], word5[WLEN];
	int  month = 0, day = 0, year = 0, hour = 0, minute = 0, items;
#ifndef	_POSIX_SOURCE
	struct tm *localtime();
	time_t time();
#endif

	/** first step is to break down the date given into MM DD YY HH MM
	    format:  The possible formats for this field are, by example:
	
		(1) Mon, Jun 11, 87
		(2) Mon, 11 Jun 87
		(3) Jun 11, 87
		(4) 11 Jun 87
		(5) 11/06/87	<- ambiguous - will be ignored!!
		(6) 8711061248GMT
		(7) Mon, Jun 11, 87 12:48:35 GMT

	    The reason #5 is considered ambiguous will be made clear
	    if we consider a message to be expired on Jan 4, 88:
			01/04/88	in the United States
			04/01/88	in Europe
	    so is the first field the month or the day?  Standard prob.
	**/

	items = sscanf(date, "%s %s %s %s %s",
		       word1, word2, word3, word4, word5);

	if (items < 5)
	  word5[0] = '\0';
	if (items < 4)
	  word4[0] = '\0';
	if (items < 3)
	  word3[0] = '\0';
	if (items < 2)
	  word2[0] = '\0';
	if (items < 1)
	  word1[0] = '\0';

	if (strlen(word5) != 0) {	/* we have form #7 */
	  day   = atoi(word1);
	  month = month_number(word2);
	  year  = atoi(word3);
	  sscanf(word4, "%02d%*c%02d",
	       &hour, &minute);
	}
	else if (strlen(word2) == 0) {	/* we have form #6 or form #5 */
	  if (isdigit(word1[1]) && isdigit(word1[2]))	  /* form #6 */
	    sscanf(word1, "%02d%02d%02d%02d%02d%*c",
		 &year, &month, &day, &hour, &minute);
	}
	else if (strlen(word4) != 0) {		   /* form #1 or form #2 */
	  if(isdigit(word2[0])) {		   /* form #2 */
	      month = month_number(word3);
	      day   = atoi(word2);
	      year  = atoi(word4);
	  } else {				   /* form #1 */
	      month = month_number(word2);
	      day   = atoi(word3);
	      year  = atoi(word4);
	  }
	}
	else if (! isdigit(word1[0])) {		   /* form #3 */
	  month = month_number(word1);
	  day   = atoi(word2);
	  year  = atoi(word3);
	}
	else {					   /* form #4 */
	  day   = atoi(word1);
	  month = month_number(word2);
	  year  = atoi(word3);
	}

	if (day == 0 || year == 0)
	  return;			/* we didn't get a valid date */

	/** next let's get the current time and date, please **/

	thetime = time((time_t *) 0);

	timestruct = localtime(&thetime);

	/** and compare 'em **/

	if (year > timestruct->tm_year)
	  return;
	else if (year < timestruct->tm_year)
	  goto expire_message;

	if (month > timestruct->tm_mon)
	  return;
	else if (month < timestruct->tm_mon)
	  goto expire_message;

	if (day > timestruct->tm_mday)
	  return;
	else if (day < timestruct->tm_mday)
	  goto expire_message;

	if (hour > timestruct->tm_hour)
	  return;
	else if (hour < timestruct->tm_hour)
	  goto expire_message;

	if (minute > timestruct->tm_min)
	  return;

expire_message:

	/** it's EXPIRED!  Yow!! **/

	(*message_status) |= EXPIRED;
}
