static const char* version_id =
  "$Id: msg.cc,v 1.10 1999/10/19 20:08:14 v2kea318 Exp $";

// $Log: msg.cc,v $
// Revision 1.10  1999/10/19  20:08:14  v2kea318
// Use the read_all function from misc-io.h to read the message from
// stdin.
//
// Revision 1.9  1999/08/12  19:25:17  v2kea318
// Do not embed C++ (//) style comments inside C (/* */) style comments.
//
// Revision 1.8  1999/07/30  20:24:09  v2kea318
// Finished converting to G++ compliant code.
//
// Revision 1.7  1999/07/30  17:55:48  v2kea318
// Reformat the header file names for Standard C++ compliance.
//
// Revision 1.6  1999/05/12  19:15:07  v2kea318
// *** empty log message ***
//
// Revision 1.5  1999/05/12  18:16:30  v2kea318
// Fixed the read_message routine to properly clear the buffer after each
// read operation.
//
// Revision 1.4  1999/05/10  22:14:30  v2kea318
// Added checks that each flag is only provided once.
//
// Revision 1.3  1999/05/07  21:41:02  v2kea318
// *** empty log message ***
//
// Revision 1.2  1999/05/07  21:39:16  v2kea318
// Fixed the read_message routine to correctly read from stdin (but
// it uses cstdio rather than iostream's).
//
// Revision 1.1  1999/05/07  20:19:19  v2kea318
// Initial revision
//
/**************************************************************************

Send a message to another user via the msg system.

***************************************************************************/

#include "route-message.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include "SocketBase.h" // needed for exceptions.
#include "cfailure.h"
#include "action_transfer.h"

// subroutines.
void usage();       // print the usage string and terminate.
void read_message(string& msg); // read the message text from stdin.

int main(int argc, char* argv[])
{
  string* recip = NULL;
  string* msg_text = NULL;

  // process the command line using the getopt system.
  if (argc == 1) usage();
  extern char* optarg;
  int opt;
  while ((opt = getopt(argc, argv, "t:m:")) != EOF) {
    switch (opt) {
    case 't':            // recipient defined.
      if (recip != NULL) {
	// -t already provided.
	cerr << "The -t flag may only be provided once!" << endl;
	return EXIT_FAILURE;
      }
      recip = new string(optarg);
      break;
    case 'm':            // message text
      if (msg_text) {
	// -m already provided.
	cerr << "The -m flag may only be provided once!" << endl;
	return EXIT_FAILURE;
      }
      msg_text = new string(optarg);
      break;
    default:
      usage();
    }
  }

  // ensure we have had a recipient defined.
  if (recip == NULL) usage();

  // if we are to take the message text from stdin, do so.
  if (msg_text == NULL) {
    msg_text = new string("\0");
    read_message(*msg_text);
  }

  // route the message.
  try {
    msg(*recip, *msg_text);
  } catch (const SocketBase::ConnectFailure& cf) {
    cerr << cf << endl;
    return EXIT_FAILURE;
  } catch (const SocketBase::SocketException& se) {
    cerr << "socket exception --> " << se.what() << endl;
    return EXIT_FAILURE;
  } catch (const action_failed& af) {
    cerr << af << endl;
    return EXIT_FAILURE;
  } catch (const exception& e) {
    cerr << e.what() << endl;
    return EXIT_FAILURE;
  } catch (...) {
    cerr << "unhandled exception" << endl;
    return EXIT_FAILURE;
  }

  // that's it.
  return EXIT_SUCCESS;
}

//***************************************************************************

#include "misc-io.h"

void read_message(string& s)
{
  read_all(cin, s);
}

//***************************************************************************

void usage()
{
  cerr << "usage:  msg -t recipient_userid [-m message_text]" << endl;
  exit(EXIT_FAILURE);
}
