/***************************************************************************
*                                                                          *
* Guest.m                                                                  *
* Copyright 1992 by Nik A Gervae                                           *
*                                                                          *
* Part of an example using the Objective-C classes (SktSocketManager,      *
* SktSocket, and SktSocketUser) which implement a convenient interface to  *
* Berkeley stream sockets under NeXTSTEP(r).  See the accompanying class   *
* specifications (files with a .rtf or .spec suffix) and the sources for   *
* further information.                                                     *
*                                                                          *
* NeXTSTEP is a registered trademark of NeXT Computer, Inc.                *
*                                                                          *
****************************************************************************
*                                                                          *
* LICENSE                                                                  *
*                                                                          *
* This program is free software; you can redistribute it and/or modify     *
* it under the terms of the GNU General Public License as published by     *
* the Free Software Foundation.                                            *
*                                                                          *
* The program and this makefile are distributed in the hope that it will   *
* be useful, but are provided "AS IS" AND WITHOUT ANY WARRANTY; without    *
* any express or implied warranty of MERCHANTABILITY or FITNESS FOR A      *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* Any use or distribution of the program and documentation must include    *
* appropriate copyrights to acknowledge Nik A. Gervae and the Free         *
* Software Foundation, Inc.                                                *
*                                                                          *
* You should have received a copy of the GNU General Public License        *
* along with this program; if not, write to the Free Software              *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
*                                                                          *
****************************************************************************
*                                                                          *
* VERSION HISTORY                                                          *
*                                                                          *
* Version numbers are simply dates in the form YYYYMMDD.  These represent  *
* the date that version was finished.  Only significantly changed versions *
* are reported here, or those versions requiring explanation of changes.   *
* There may be many interim stages between dated versions.                 *
*                                                                          *
* DateVersion Primary Author  Notes                                        *
* ----------- --------------- -------------------------------------------- *
* 19920327    Nik A Gervae    First released version                       *
*                                                                          *
***************************************************************************/
#import <stdio.h>
#import "chat.h"
#import "Guest.h"

#define STATE_NEW      0
#define STATE_CHATTING 1


/***************************************************************************
*                                                                          *
* These are the constant strings used.  Feel free to translate them into   *
* your favorite language.  Do be sure to keep all the % directives in      *
* place, or change the code that accesses these strings.                   *
*                                                                          *
***************************************************************************/
#define STR_HiThere             "Hi there!  What\'s your name? "
#define STR_NewChatter          "(%s) %s has joined the conversation.\n"
#define STR_NameAndQuote        "%s> %s\n"



@implementation Guest

/***************************************************************************
*                                                                          *
* initWithSocket:                                                          *
*                                                                          *
* Initializes, notifies the chat server, and dumps a friendly greeting.    *
*                                                                          *
***************************************************************************/

- initWithSocket:newSocket
{
  [super initWithSocket:newSocket];

  connectState = STATE_NEW;

  [self queueOutputString:STR_HiThere];
  [socket flushOutput];

  [Global_Chatserver guestDidInit:self];

  return self;

} /*initWithSocket:*/

/***************************************************************************
*                                                                          *
* update                                                                   *
*                                                                          *
* If there's no name yet, get it. Otherwise get next line, add your name,  *
* and send it off to everyone.                                             *
*                                                                          *
***************************************************************************/
- update
{
  char *inputLine;
  char *buf;

  buf = NULL;

 /*
  * If we don't have the name yet....
  */
  if (STATE_NEW == connectState) {

    /// Should do error checking here....
    inputLine = [self nextInputLine];

    if (inputLine && *inputLine) {

      name = inputLine;
      [Global_Chatserver log:STR_NewChatter, [[self class] name], name];
      connectState = STATE_CHATTING;
      return self;
    }
  }

 /*
  * They're happily chatting.
  */
  else {

    /// Should do error checking here....
    inputLine = [self nextInputLine];

    if (inputLine && *inputLine) {

      buf = (char *)malloc(10 + strlen(name) + strlen(inputLine));

      sprintf(buf, STR_NameAndQuote, name, inputLine);
      [Global_Chatserver announce:buf except:self];

      free(inputLine);
      free(buf);        // It's been copied elsewhere.
      }
  }
  return self;
}

/***************************************************************************
*                                                                          *
* -free                                                                    *
*                                                                          *
* Tell the chat server we're going away, and do the usual stuff.           *
*                                                                          *
***************************************************************************/
- free
{
  [Global_Chatserver guestWillFree:self];
  if (name) free(name);
  return [super free];
}

@end /*interface Guest*/

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