/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.io.*;

/**
 * This class represents the basic Jephyr message.  Jephyr messages are sent
 * as objects between the SNClient and the standalone applications.  A
 * simple representation of this object is encoded in the content of
 * the SIP messages.
 *
 * @author Bill Nagy
 *
 * @see SNClient
 * @see jlocate
 * @see jlogon
 * @see jlogoff
 * @see jwrite
 */
public class JMessage implements Serializable
{
  private String [] messagelines;
  private String sender;
  private String [] recipientlist;
  private boolean commandmsg;
  private int version;
  

  public JMessage()
  {
    version=1;
    return;
  }
  /**
   * This constructor will create a standard message which will be sent
   * to one or more recipients.
   *
   * @param messagelines Array of strings which compose the body of the message.
   * @param sender The sender of the message.
   * @param recipientlist An array which contains the list of recipients of the message.
   */
  public JMessage(String [] messagelines, String sender,
		  String [] recipientlist)
  {
    //How should we catch NULL recipientlist or NULL sender?
    this.messagelines = new String[messagelines.length];
    System.arraycopy(messagelines, 0, this.messagelines, 0,
		     messagelines.length); /*Copy the message*/
    this.sender = sender;
    this.recipientlist = new String[recipientlist.length];
    System.arraycopy(recipientlist, 0, this.recipientlist, 0,
		     recipientlist.length); /*Copy the names to the to list*/
    version = 1;
  }

  /**
   * This constructor will create a command message whose only recipient
   * will be the SNClient on the local machine.
   *
   * @param messagelines Array of strings which compose the body of the message.
   * @param sender The sender of the message.
   */
  public JMessage(String [] messagelines, String sender)
  {
    //How should we catch NULL sender?
    this.messagelines = new String[messagelines.length];
    System.arraycopy(messagelines, 0, this.messagelines, 0,
		     messagelines.length); /*Copy the message*/
    this.sender = sender;
    version = 1;
    this.commandmsg = true;
  }

  /**
   * Retrieve the body of the message.
   *
   * @return A string array consisting of the lines of the message.
   */
  public String [] getMessage()
  {
    return messagelines;
  }

  /**
   * Sets the lines of the message.
   *
   * @param messageslines The new contents of the message.
   */
  public void setMessage(String[] messagelines)
  {
    this.messagelines = new String[messagelines.length];
    System.arraycopy(messagelines, 0, this.messagelines, 0,
		     messagelines.length); /*Copy the message*/
    return;
  }
  
  /**
   * Retrieve the originator of the message.
   *
   * @return The originator of the message.
   */
  public String getSender()
  {
    return sender;
  }

  /**
   * Sets the name of the originator of the message.
   *
   * @param sender The name of the sender.
   */
  public void setSender(String sender)
  {
    this.sender = sender;
  }

  /**
   * Retrieve the list of recipients of the message.
   *
   * @return A string array containing all of the message recipients.
   */
  public String [] getRecipientList()
  {
    return recipientlist;
  }

  /**
   * Sets the message recipients.
   *
   * @param recipientlist The new array containing the desired recipients.
   */
  public void setRecipientList(String[] recipientlist)
  {
    this.recipientlist = new String[recipientlist.length];
    System.arraycopy(recipientlist, 0, this.recipientlist, 0,
		     recipientlist.length); /*Copy the recipients*/
    return;
  }

  /**
   * Returns true if this is a command message, and false otherwise.
   */
  public boolean isCommandMessage()
  {
    return commandmsg;
  }

  /**
   * Set whether or not this is a command message
   */
  public void setCommandMessage(boolean flag)
  {
    commandmsg = flag;
  }

  /**
   * Display the Jephyr message to stdout.
   */
  public void print()
  {
    int cnt;

    System.out.print("To: ");
    for (cnt = 0; cnt < recipientlist.length; cnt++)
      {
	System.out.print(recipientlist[cnt]);
	if (cnt != recipientlist.length-1)
	  System.out.print(", ");
      }
    System.out.print("\n");

   System.out.println("From: " + sender);
   for (cnt = 0; cnt < messagelines.length; cnt++)
     {
       System.out.println(messagelines[cnt]);
     }
  }
}
