/**********************************************************************
 * lucymail.c -- mail hook for lucy
 *
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/lucymail.c,v $
 * $Author: brlewis $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/lucymail.c,v 1.2 92/04/01 14:00:49 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 * For copying and distribution information, see the file
 * "mit-copyright.h".
 **********************************************************************/
#include "mit-copyright.h"

#ifndef lint
static char rcsid_lucymail_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/lucymail.c,v 1.2 92/04/01 14:00:49 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include "lucy/lucy.h"
#define LUCYM_LINE 256
#define LUCYM_RETM1 "Returned mail"
#define LUCYM_RETM2 "mail delivery error"
main(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  char buf[LUCYM_LINE+1], subject[LUCYM_LINE+1],
       from[LUCYM_LINE+1], replyto[LUCYM_LINE+1];
  char *sleft, *sright, *s;
  int inreplyto=0, returned_mail=0;
  char tmpfile[64];
  FILE *fp;
  int j, flags=0, flagstoclr;
  ltrn *info;
  tfile tf;

  /* Initialize */
  code = luc_init();
  if (code) {
    com_err(argv[0], code, "(%s)", luv_context);
    exit(1);
  }
  from[0] = '\0';
  strcpy(replyto, "somebody");
  umask(077);
#ifdef LOG_USAGE
  log_startup((strcat(strcpy(subject, "lucymail "), luv_qdirname)));
#endif
  subject[0] = '\0';

  /* Read mail header */
  while(fgets(buf, LUCYM_LINE, stdin)) {
    /* Stop if end of headers*/
    if (buf[0] == '\n' || buf[0] == '-') break;
    if (strncasecmp(buf, "From: ", 6) == 0) {
      strncpy(from, buf+6, LUCYM_LINE);
      rm_whitespace(from);
      continue;
    }
    if (strncasecmp(buf, "Subject: ", 9) == 0) {
      strncpy(subject, buf+9, LUCYM_LINE);
      rm_whitespace(subject);

      /* returned mail? */
      if (strncasecmp(subject, LUCYM_RETM1, strlen(LUCYM_RETM1)) == 0 ||
	  strncasecmp(subject, LUCYM_RETM2, strlen(LUCYM_RETM2)) == 0)
	returned_mail=1;

      /* Find [nnnn] reference */
      sleft = subject;
      while (sleft = index(sleft, '[')) {
	sleft++;
	if (*sleft >= '0' && *sleft <= '9') {
	  sright = index(sleft, ']');
	  if (sright && sright-sleft < 6) {
	    inreplyto = atoi(sleft);
	    break;
	  }
	}
      }
    }
  }

  /* Open temporary file */
  strcpy(tmpfile, "/tmp/lucymXXXXXX");
  mktemp(tmpfile);
  fp = fopen(tmpfile, "w+");

  /* Read/write body of mail message */
  while(fgets(buf, LUCYM_LINE, stdin)) {
    if (!returned_mail &&
	strncmp(buf, LUCY_POST, strlen(LUCY_POST)) == 0) {
      for(s = buf+strlen(LUCY_POST); isspace(*s); s++);
      if (*s == 'Y' || *s == 'y') flags |= LUCY_WANT_POST;
    }
    else if (!returned_mail &&
	     strncmp(buf, LUCY_REPLY, strlen(LUCY_REPLY)) == 0) {
      for(s = buf+strlen(LUCY_REPLY); isspace(*s); s++);
      strcpy(replyto, s);
      /* remove trailing whitespace */
      for (s = &(replyto[strlen(replyto)-1]);
	   isspace(*s) && s > replyto; s--) *s = '\0';
      if (strcmp(replyto, "nobody") && strcmp(replyto, "no"))
	flags |= LUCY_WANT_REPLY;
    }
    else if (returned_mail &&
	     strncasecmp(buf, LUCY_HINTS, strlen(LUCY_HINTS)) == 0) {
      for(s = buf+strlen(LUCY_HINTS); isspace(*s); s++);
      flagstoclr = luc_flgset(s);
      while(1) {
	while(!isspace(*(++s))); /* skip ahead to spaces */
	if (*s == '\n') break;
	while(isspace(*(++s)));	/* skip past spaces */
	if (atoi(s))
	  if (!luc_unmark(&luv_qmtg, atoi(s), flagstoclr))
	    inreplyto=atoi(s);
      }
    }
    fputs(buf, fp);
  }
  if (fflush(fp) == EOF) {
    perror(argv[0]);
    unlink(tmpfile);
    exit(2);
  }

  rewind(fp);
  tf = unix_tfile(fp->_file);

  /* set answer subject to "answer from ..." */
  if (inreplyto && !returned_mail) {
    code = luc_trn_info(&luv_qmtg, inreplyto, &info);
    if (code) {
      inreplyto = 0;
    } else {
      strcpy(subject, "answer from ");
      strcat(subject, from);
    }
  }

  /* if this isn't an answer or returned mail, use PERSONAL REPLY TO: */
  if (!inreplyto) strcpy(from, replyto);

  dsc_add_trn2(&(luv_qmtg.nb), tf, subject, from, inreplyto, &j, &code);
  unlink(tmpfile);
  if (code) {
    com_err(argv[0], code, "while adding transaction");
    exit(4);
  }

  /* mark question answered */
  if (inreplyto && !returned_mail) {
    code = luc_mark(&(luv_qmtg), inreplyto, LUCY_ANSWERED);
    if (code) com_err(argv[0], code, "marking [%04d]", inreplyto);
    flags |= LUCY_ANSWERED;
  }
  code = luc_mark(&(luv_qmtg), j, flags);
  if (code) com_err(argv[0], code, "marking [%04d]", j);
#ifdef LOG_USAGE
  if (!inreplyto && !returned_mail)
    log_view(luc_flgstr(flags));
#endif
  exit(0);
}

rm_whitespace(s)
     char *s;
{
  int i;

  /* remove trailing blanks */
  for(i=strlen(s)-1; i>0; i--) {
    if (isspace(s[i]))
      s[i] = '\0';
    else break;
  }

  /* remove leading blanks */
  for(i=0; i<strlen(s); i++)
    if (! isspace(s[i]))
      break;
  (void) strcpy(s, s+i);
}
