/**********************************************************************
 * do_forward.c  -- SS command for forwarding a transaction
 * tty lucy client using SS library
 *
 * $Author: brlewis $
 * $Source: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_forward.c,v $
 * $Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_forward.c,v 1.1 91/09/24 14:44:20 brlewis Exp Locker: brlewis $
 *
 * 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_do_forward_c[] = "$Header: /afs/athena.mit.edu/astaff/project/lucydev/src/tty/do_forward.c,v 1.1 91/09/24 14:44:20 brlewis Exp Locker: brlewis $";
#endif /* lint */

#include <stdio.h>
#include <strings.h>
#include <netdb.h>
#include <ss/ss.h>
#include "lucy/lucy.h"
#include "lucy/memory.h"
#include "config.h"

void
do_forward(argc, argv)
     int argc;
     char *argv[];
{
  long code;
  char **clist;
  char **csp;
  char prompt[256];
  char *hname;
  extern int h_errno;

  /* Check usage */
  if (argc != 3 || !atoi(argv[1])) {
    printf("Usage: %s number correspondent.\n", argv[0]);
    return;
  }

  /* check address */
  if (hname = index(argv[2], '@'))
    if (!gethostbyname(++hname)) {
      if (h_errno == HOST_NOT_FOUND) {
	printf("Unknown host: `%s'\n", hname);
	return;
      }
    }

  /* Set prompt */
  sprintf(prompt, "Forward %s to %s?", argv[1], luc_fullname(argv[2]));

  /* Show what is about to be forwarded */
  do_show(2, argv);
  if (!say_yes(prompt, 0)) return;

  /* get correspondent list */
  code = luc_csp(&clist);
  if (code) {
    com_err(argv[0], code, "(%s)", luv_context);
    return;
  }

  /* check clist for recipient */
  for(csp = clist; *csp != NULL && strcasecmp(*csp, argv[2]); csp++);
  if (!*csp) {
    printf("Could not find %s in your list of correspondents.\n",
	   luc_fullname(argv[2]));
    if (say_yes("Add to list?", 0)) {
      code = luc_add_csp(argv[2]);
      if (code) com_err("Warning", code, "(couldn't add %s)", argv[2]);
    } else goto DO_FORWARD_DONE;
  }

  /* call luc_forward */
  code = luc_forward(luv_curmtgp, atoi(argv[1]), argv[2]);
  if (code) com_err(argv[0], code, "(%s)", luv_context);
  else printf("Forwarded %s to %s.\n", argv[1], argv[2]);

 DO_FORWARD_DONE:
  luc_free_csp(&clist);
  return;
}
