/**********************************************************************
 * serve_post.c -- post a transaction in the browser
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/serve_post.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/serve_post.c,v 1.2 92/04/03 11:04:28 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_post_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/serve_post.c,v 1.2 92/04/03 11:04:28 brlewis Exp $";
#endif /* lint */

#include <stdio.h>
#include <syslog.h>
#include <strings.h>
#include <ctype.h>
#include <lucy/lucy.h>

/**********************************************************************
 * serve_post(int n1, int n2, int n3, char *subject)
 *	caller sets n1 to first trn in chain
 *	caller sets n2 to transaction with text
 *	caller sets n3 to transaction requesting post
 *	caller sets subject
 *
 * - checks to see that all transactions are marked with p
 * - posts in the browser
 * - returns error code
 **********************************************************************/

long
serve_post(n1, n2, n3, subject)
     int n1, n2, n3;
     char *subject;
{
  long code;
  char *filename;
  ltrn *trninfo;
  int inreplyto=0, nposted, foo;
  char undo[128];

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

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

  /* is this a response to prev posting, e.g. "[0105] Re: Martians"? */
  if (*subject == '[' && (inreplyto=atoi(subject+1))
      && index(subject, ']')) {
    /* skip ahead to real subject, e.g. "Re: Martians" */
    while(!isspace(*subject)) subject++;
    while(isspace(*subject)) subject++;
  }

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

  /* Add transaction to browser */
  code = luc_add_trn(&luv_bmtg, filename, subject, NULL, inreplyto, &nposted);

  if (!code) {
    /* try to mark posted */
    luc_mark(&luv_qmtg, n1, LUCY_POSTED);
    luc_mark(&luv_qmtg, n2, LUCY_POSTED);
    luc_mark(&luv_qmtg, n3, LUCY_POSTED);

    /* Add comment telling user how to undo */
    sprintf(undo, "if it was a mistake to post %d, then type `unpost %d'",
	    n2, nposted);
    luc_add_trn(&luv_qmtg, "/dev/null", undo, NULL, n2, &foo);
  }

  /* clean up */
  luc_free_file(&filename);
  return(code);
}
