/**********************************************************************
 * lucymail.c -- mail hook for lucy
 *
 * $Source$
 * $Author$
 * $Header$
 *
 * Copyright 1990 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$";
#endif /* lint */

#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include "lucy/lucy.h"
tfile unix_tfile();		/* XXX should be in discuss .h file */

main(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  char buf[256], subject[80], from[80];
  char *lbrace, *rbrace;
  int inreplyto = 0;
  char tmpfile[64];
  FILE *fp;
  tfile tf;
  lucy_qident i, j;
  name_blk nb;
  trn_info3 info;

  /* Initialize */
  init_dsc_err_tbl();
  from[0] = '\0';
  umask(077);

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

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

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

  /* If this is an answer to a question, from whom? */
  if (inreplyto) fprintf(fp, "%s\n", from);

  /* Write body of mail message */
  while(fgets(buf, 255, stdin)) {
    fputs(buf, fp);
  }
  if (fflush(fp) == EOF) {
    perror(argv[0]);
    unlink(tmpfile);
    exit(2);
  }

  rewind(fp);
  code = construct_nb(&nb);
  if (code) {
    com_err(argv[0], code, "");
    unlink(tmpfile);
    exit(3);
  }
  tf = unix_tfile(fp->_file);
  if (inreplyto) {
    dsc_get_trn_info3(&nb, inreplyto, &info, &code);
    if (code) {
      inreplyto = 0;
    } else {
      strcpy(subject, info.subject);
    }
  }
  if (!inreplyto) strcpy(from, "anonymous");
  dsc_add_trn2(&nb, tf, subject, from, inreplyto, &j, &code);
  unlink(tmpfile);
  if (code) {
    com_err(argv[0], code, "while adding transaction");
    exit(4);
  }
  if (inreplyto) {
    code = lucy_mark2(&nb, j, LUCY_ANSWERED);
    if (code) com_err(argv[0], code, "marking [%04d]", j);
    code = lucy_mark2(&nb, inreplyto, LUCY_ANSWERED);
    if (code) com_err(argv[0], code, "marking [%04d]", inreplyto);
  }
  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);
}
