/**********************************************************************
 * serve_repl.c -- reply to a transaction
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/serve_repl.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/serve_repl.c,v 1.2 92/04/03 11:04:36 brlewis Exp $
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

#ifndef lint
static char rcsid_serve_repl_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/serve_repl.c,v 1.2 92/04/03 11:04:36 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <syslog.h>
#include <strings.h>
#include <sys/errno.h>
#include <lucy/lucy.h>

/**********************************************************************
 * serve_repl(int n1, int n2, int n3, char *recpient)
 *	caller sets n1 to first trn in chain
 *	caller sets n2 to transaction with text
 *	caller sets n3 to transaction requesting reply
 *	caller sets recipient to email address
 *
 * - checks to see that all transactions are marked with r
 * - sends reply via sendmail
 * - returns error code
 **********************************************************************/

long
serve_repl(n1, n2, n3, recipient)
     int n1, n2, n3;
     char *recipient;
{
  long code;
  char buf[BUFSIZ];
  char *filename;
  int nbytes;
  FILE *pipe, *fp;
  ltrn *trninfo;

  /* make sure it hasn't already been parsed */
  if (code = luc_trn_info(&luv_qmtg, n3, &trninfo)) return(code);
  if (trninfo->flags & LUCY_REPLIED) {
    syslog(LOG_DEBUG, "Already parsed %4d %s %s\n", n3,
	   luc_flgstr(trninfo->flags), trninfo->subject);
    return(0L);
  }

  /* make sure everything's marked for reply */
  if (!(trninfo->flags & LUCY_WANT_REPLY)) {
    sprintf(luv_context, "%d", n3);
    return(LUC_ERR_REPL);
  }
  if (code = luc_trn_info(&luv_qmtg, n2, &trninfo)) return(code);
  if (!(trninfo->flags & LUCY_WANT_REPLY)) {
    sprintf(luv_context, "%d", n2);
    return(LUC_ERR_REPL);
  }
  if (code = luc_trn_info(&luv_qmtg, n1, &trninfo)) return(code);
  if (!(trninfo->flags & LUCY_WANT_REPLY)) {
    sprintf(luv_context, "%d", n1);
    return(LUC_ERR_REPL);
  }

  /* Get filename */
  if (code = luc_trn_file(&luv_qmtg, n2, &filename)) return(code);

  /* Start sendmail */
  pipe = popen("/usr/lib/sendmail -t", "w");
  if (!pipe) {
    strcpy(luv_context, "starting sendmail");
    return((long) errno);
  }

  /* Write mail header */
  fprintf(pipe, "From: %s\nTo: %s\nSubject: %s\n",
	  luv_email, recipient, trninfo->subject);
  fprintf(pipe, "lucymail-hints: R %d %d %d\n\n", n1, n2, n3);

  /* Open file of text for question */
  fp = fopen(filename, "r");
  if (!fp) {
    fprintf(pipe, "%s (%s)\n", error_message(errno), filename);
    pclose(pipe);
    strcpy(luv_context, filename);
    return((long) errno);
  }

  /* Copy the file into the sendmail pipe */
  do {
    if (nbytes = fread(buf, sizeof(char), BUFSIZ, fp)) {
      if (!fwrite(buf, sizeof(char), nbytes, pipe)) {
        code = (long) errno;
	fprintf(pipe, "\nAn error may have truncated this reply.\n");
	strcpy(luv_context, filename);
	break;
      }
    }
  } while (nbytes == BUFSIZ);

  if (!code) {			/* try to mark replied */
    luc_mark(&luv_qmtg, n1, LUCY_REPLIED);
    luc_mark(&luv_qmtg, n2, LUCY_REPLIED);
    luc_mark(&luv_qmtg, n3, LUCY_REPLIED);
  }

  /* clean up */
  (void) fclose(fp);
  pclose(pipe);			/* check exit status? */
  luc_free_file(&filename);
  return(code);
}
