/**********************************************************************
 * parse_trn.c -- translate user-readable string into cleared flags
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/parse_trn.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/parse_trn.c,v 1.2 92/04/03 11:04:39 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_parse_trn_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/server/RCS/parse_trn.c,v 1.2 92/04/03 11:04:39 brlewis Exp $";
#endif /* lint */

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

/**********************************************************************
 * parse_trn(char *indata)
 *	- indata points at an int, trn no. to parse
 *
 * - does reply, posting or forwarding
 * - returns error code for RPC to send back
 **********************************************************************/

char *
parse_trn(indata)
     char *indata;
{
  static long code;
  ltrn *trn_to_parse;		/* w. subject like "repl 48 brlewis" */
  ltrn *trn_with_text;		/* to reply, post or forward */
  int n1, n2, n3;		/* numbers of orig, text, parse trns */
  char *s;

  /* make sure cached trn info is up-to-date */
  luv_qmtg.info.date_modified--;
  if (code=luc_reset_mtg(&luv_qmtg)) goto PARSE_TRN_CLEANUP;
 
  /* get information on transaction to parse */
  if (code=luc_trn_info(&luv_qmtg, n3= *((int *)indata), &trn_to_parse))
    goto PARSE_TRN_CLEANUP;

  /* find first (should be only) number in subject */
  for(s=trn_to_parse->subject; *s != '\0' && !isdigit(*s); s++);
  if (!isdigit(*s)) {
    code = LUC_ERR_INVAL;
    sprintf(luv_context, "%d %s %s", trn_to_parse->current,
	    luc_flgstr(trn_to_parse->flags), trn_to_parse->subject);
    goto PARSE_TRN_CLEANUP;
  }

  /* get information on transaction with text */
  if (code=luc_trn_info(&luv_qmtg, n2=atoi(s), &trn_with_text))
    goto PARSE_TRN_CLEANUP;

  /* make sure transactions are in the same chain */
  if ((n1=trn_with_text->fref) != trn_to_parse->fref) {
    code = LUC_ERR_INVAL;
    sprintf(luv_context, "%d %s %s", trn_to_parse->current,
	    luc_flgstr(trn_to_parse->flags), trn_to_parse->subject);
    goto PARSE_TRN_CLEANUP;
  }

  /* skip rest of number */
  while (!isspace(*s) && *s != '\0') s++;
  /* skip white space */
  while (isspace(*s) && *s != '\0') s++;
  if (*s == '\0') {
    code = LUC_ERR_INVAL;
    sprintf(luv_context, "%d %s %s", trn_to_parse->current,
	    luc_flgstr(trn_to_parse->flags), trn_to_parse->subject);
    goto PARSE_TRN_CLEANUP;
  }

  /* is it asking for a personal reply? */
  if (trn_to_parse->flags & LUCY_WANT_REPLY) {
    code = serve_repl(n1, n2, n3, s);
    goto PARSE_TRN_CLEANUP;
  }

  /* is it asking for posting? */
  if (trn_to_parse->flags & LUCY_WANT_POST) {
    code = serve_post(n1, n2, n3, s);
    goto PARSE_TRN_CLEANUP;
  }

  /* is it asking for forwarding? */
  if (trn_to_parse->flags & LUCY_WANT_FORWARD) {
    code = serve_forw(n1, n2, n3, s);
    goto PARSE_TRN_CLEANUP;
  }

 PARSE_TRN_CLEANUP:
  if (code) syslog(LOG_ERR, "%s (%s)", error_message(code), luv_context);
  return((char *) &code);
}
