From: paveza@ix.netcom.com

Due to the number of responses I received for this code, I am merely
posting it to the list instead of replying to individual requests.  I have
used some of the code or at least the ideas for some code from this mailing
list, so I feel it is only fair to give back :).  This code is provided as
is, no warranty is made.  Please send an email to
staffa@apache.wildstar.net or paveza@ix.netcom.com if you use this code in
your mud.  Other than that, please make some kind of notation (via comments
is fine) in your source code indicating that the snippet was created by
Joseph Keslin (aka Tyric).  He is the co-IMP of Last Aerie and has granted
permission to distribute this code.

Note that some of the fields the whowas is looking for are ones that we
have added on (such as hostname).  I leave it as an exercise of the reader
to perform the task of adding them to the pfiles.  If help is needed, just
email me and I will see what I can do.

Enjoy.

/***************************************************************************
*       Last Aerie is copyrighted by Gary Paveza and Joseph Keslin         *
*       Last Aerie is an advanced part of ROM 2.4b                         *
*                                                                          *
*       No claim is made to the original ROM 2.4b code, but only to those  *
*       improvements (additions) performed on this code by the staff of    *
*       the Last Aerie                                                     *
*                                                                          *
*       Last Aerie has been created by the following                       *
*            Gary Paveza (staffa@wildstar.net)                             *
*            Joseph Keslin (kesljos@charlie.cns.iit.edu)                   *
*            Alex Kayland (sccmud@charlie.cns.iit.edu)                     *
*                                                                          *
*       Use of this code is restricted to the use as defined by the file   *
*       Rom24/doc/LastAerie.doc.  If you do not have a copy of this file   *
*       then send email to staffa@wildstar.net                             *
***************************************************************************/


#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "merc.h"
#include "magic.h"
#include "lookup.h"			/* Tyric 6/6/97 */
#include "utils.h"
#include "recycle.h"
#include "tables.h"

/* Local Functions */
void whowas(CHAR_DATA *ch, FILE *fp);

void whowas(CHAR_DATA *ch, FILE *fp)
{
    char buf[MAX_STRING_LENGTH];
    char *word;
    bool fMatch;
    char *name,
         *race,
         *date,
         *host;
    int classnum, level, sex;

/* Initialize variables to Error checking states. */

    name = NULL;
    race = NULL;
    date = NULL;
    host = NULL;
    classnum = -1;
    level    = -1;
    sex      = -1;
          
    for ( ; ; )
    {
	word   = feof( fp ) ? "End" : fread_word( fp );
	fMatch = FALSE;
	switch ( UPPER(word[0]) )
	{
	case '*':
	    fMatch = TRUE;
	    fread_to_eol( fp );
	    break;

	case 'C':
/* Tyric for whowas */
            if ( !str_cmp( word, "Cla" ) )
            {
		classnum = fread_number(fp);
                fMatch = TRUE;
                break;
            }
            break;

	case 'E':
	    if ( !str_cmp( word, "End" ) )
	    {
                if ( (name    == NULL)
                   ||(race    == NULL)
                   ||(host    == NULL)
                   ||(date    == NULL)
                   ||(sex     == -1 )
                   ||(classnum== -1 )
                   ||(level   == -1 ) )
                {
                   send_to_char("Information not available.\n\r",ch);
                   return;
                }
	/*
	 * Tyric 6/6/97 Now we have our name, race, level, class, 
	 * and hopefully the host of the char. 
	 */
	        sprintf(buf, "{YName:{m %s{x\n\r", name );
                send_to_char(buf, ch);
	
                if (level >= 92)
                {
                   char *god     = " GOD";
                   char *godit   = " GODIT";
                   char *goddess = " GODDESS";
                   if (sex == 0)
	              sprintf(buf, "{YIs a%s.{x\n\r", godit);
       	           else
	           if (sex == 1)
	              sprintf(buf, "{YIs a%s.{x\n\r", god);
                   else
	           if (sex == 2)
	              sprintf(buf, "{YIs a%s.{x\n\r", goddess);
	        }
	        else
	           sprintf(buf, "{YLevel:{m %d{x\n\r", level);
	        send_to_char(buf, ch);

	        sprintf(buf, "{YRace:{m %s{Y Class:{m %s{x\n\r",
	               race, class_table[classnum].name);
	        send_to_char(buf, ch);
/* Tyric 11/16/97 Bug Fix */
       	        if ( IS_IMMORTAL(ch) || ( !IS_IMMORTAL(ch) && level < 92)) 
                {
		  if ( date == NULL )
	           date = str_dup("(unknown)");
	          sprintf(buf,"{YLast seen on:{m %s{x\n\r", date);
	          send_to_char(buf, ch);
                }
 
                if (IS_IMP(ch))
	        {
	       	   if (host == NULL)
	       	      host = str_dup("(unknown)");
	       	   sprintf(buf,"{YHost:{m %s{x\n\r", host);
	              send_to_char(buf, ch);
		}
                free_string(name);
                free_string(race);
                free_string(date);
                free_string(host);
		return;
	    }
	    break;

	case 'H':

            /* Tyric 6/6/97 for whowas command */
            if ( !str_cmp( word, "Hst" ) )
            {
		host = str_dup(fread_string( fp ));
		fMatch = TRUE;
		break;
            }
            break;

	case 'L':
/* Tyric 6/6/97 For whowas command */
            if ( !str_cmp(word,"Levl")
               ||!str_cmp(word,"Lev")
               ||!str_cmp(word,"Level") )
            {
		level = fread_number( fp );
   		fMatch = TRUE;
	        break;
            } 
            if ( !str_cmp(word, "LLog") )
            {
		date = str_dup(fread_string( fp ));
		fMatch = TRUE;
		break;
           }
/* end of add */

	    break;
            

	case 'N':
/* Tyric 6/6/97 for whowas command */
            if (!str_cmp(word,"Name") )
 	    { 
		name = str_dup(fread_string( fp ));
		fMatch = TRUE;
		break;
            }
/* end of add */
	    break;

	case 'R':

/* Tyric 6/6/97 for whowas command */
 	    if ( !str_cmp( word, "Race") )
            {
		race = str_dup(fread_string( fp ));
		fMatch = TRUE;
		break;
 	    }
/* end of add by Tyric */
	    break;

	case 'S':
/* Tyric 6/6/97 for whowas */
	    if ( !str_cmp( word, "Sex") )
 	    {
	 	sex = fread_number( fp );
		fMatch = TRUE;
		break;
	    }
/* end of add */
	    break;
        }

	if ( !fMatch )
	{
	    fread_to_eol( fp );
	}
    }
}

/* 
 * Tyric 6/6/97 This function requires that the char passed in to the 
 * function is not logged in.  If the char is, return a message that 
 * the char is on.  Otherwise, show date and time last logged on, and
 * also if the char using whowas is immortal, show the site logged in from.
 */
void do_whowas(CHAR_DATA *ch, char *argument)
{
    FILE *fp;
    char arg[MAX_INPUT_LENGTH];
    char strsave[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    char *name;
    DESCRIPTOR_DATA *d;
    bool found = FALSE;
    char letter;
    char *word;

    one_argument(argument,arg);

    if (arg[0] == '\0')
    {
        send_to_char("You must provide a name.\n\r",ch);
        return;
    }
    name = arg;
/* Tyric 7/22/97 Fixed Socket recycling bug */
    for (d = descriptor_list; d; d = d->next)
    {
        CHAR_DATA *wch;

        if (d->connected != CON_PLAYING || !can_see(ch,d->character))
            continue;

        wch = ( d->original != NULL ) ? d->original : d->character;
  	
	if ( !can_see(ch,wch) )
 	   continue;

        if (!str_prefix(arg,wch->name))
            found = TRUE;
    }

    if (found)
    {
        send_to_char("That char is playing right now.\n\r",ch);
        return;
    }
    else
    /* begin real whowas stuff */
    {
       /* open file */
       #if defined(unix)
       /* decompress if .gz file exists */
       sprintf( strsave, "%s%s%s", PLAYER_DIR, capitalize(name),".gz");
       if ( ( fp = fopen( strsave, "r" ) ) != NULL )
       {
          fclose(fp);
          sprintf(buf,"gzip -dfq %s",strsave);
          system(buf);
       }
       #endif
       sprintf( strsave, "%s%s", PLAYER_DIR, capitalize( name ) );
       if ( ( fp = fopen( strsave, "r" ) ) != NULL ) /* open file */
       {
          letter = fread_letter( fp );
          if ( letter == '*' )
             fread_to_eol( fp );
          if ( letter != '#' )
          {
             bug( "Do_whowas: # not found.", 0 );
             return;
          }
          word = fread_word( fp );
/* if not equal to PLAYER, barf cause we are not reading a player
 * file 
 */
          if ( str_cmp( word, "PLAYER" ) ) 
          {
	     bug("Bug in whowas: File opened is not Player file.",0);
             fclose( fp );
    	     return;
 	  }
/* Is a playerfile--- BINGO! */
/* Tyric 6/6/97 Get whowas info from file and output results */
           whowas(ch,fp);
           fclose(fp);     /* Tyric 8/1/97 hmmm did i fix the socket bug? */
       } /* end if open file */
       else /* file did not open */
       {
	  send_to_char("There is no player by that name.\n\r",ch);
	  return;
       }
    } /* end else begin real whowas stuff */    
}
