/*****************************************************************
 *       plist.txt                                               *
 *       Original code by Thanos 9/25/2003                       *
 *                                                               *
 * This file is free to use, distribute and modify as you like   *
 * but it would be nice if you mention me somewhere or even      *
 * write me an e-mail at jessechoward@netscape.net               *
 *                                                               *
 * plist was my first attempt to get a listing of ALL the        *
 * players on my MUD. With a little modification it can also be  *
 * used to quickly find players of certain guilds/clans etc.     *
 * without having to scan through every pfile or keep a large    *
 * list of names in memory. How many times did you just want to  *
 * know who is in what guild  and how many active players are in *
 * it?                                                           *
 *****************************************************************/
 
/*
 * Drop these in with the rest of the includes of
 * act_wiz.c or the appropriate file.
 */
#include <stddef.h>
#include <sys/stat.h>
#include <dirent.h>


/*
 * paste these 2 functions at the bottom of act_wiz.c
 * or the appropriate file.
 */
 
int days_since_last_file_mod( char *filename )
{
    int days;
    struct stat buf;
    extern time_t current_time;

    if (!stat(filename, &buf))
    {
	days = (current_time - buf.st_mtime)/86400;
    }
    else
	days = 0;

    return days;
}

void do_plist( CHAR_DATA *ch, char *argument )
{
    DIR *dp;
    struct dirent *ep;
    char buf[80];
    char buffer[MAX_STRING_LENGTH*4];
    int days;
    bool fAll = TRUE, fImmortal = FALSE;

    buffer[0] = '\0';

    if ( argument[0] == '\0' || !str_cmp( argument, "all" ) )
    {
	fAll = TRUE;
    }
    else
    if ( !str_prefix( argument, "immortal" ) )
    {
	fImmortal = TRUE;
    }
    else
    {
	send_to_char( "Syntax: PLIST [ALL/IMMORTAL]\n\r", ch );
	return;
    }

    if ( fImmortal )
    {
	/*
	 * change the string literals to GODS_DIR or where
	 * you keep your gods folder
	 */
	dp = opendir ("../gods");
    }
    else
    {
	/*
	 * change the string literals to PLAYER_DIR or where
	 * you keep your gods folder
	 */
	dp = opendir ("../player");
    }

    if (dp != NULL)
    {
 	while ( (ep = readdir (dp)) )
      	{
	    if ( ep->d_name[0] == '.' )
	    	continue;

	/*
	 * change the string literals to fit your needs like above
	 */
	    sprintf( buf, "%s%s", fImmortal ? "../gods/" : "../player/", ep->d_name );

	    days = days_since_last_file_mod( buf );

	/*
	 * color coding using Lopes comaptable color to highlight
	 * inactivity. green = active, red = innactive
	 * Just remove the colors if they cause problems.
	 */
	    sprintf( buf, "%-15s %s%-3d{x days\n\r",
		ep->d_name, days > 30 ? "{r" : days > 20 ? "{Y" : days > 10 ? "{g" : "{G", days );
	    strcat( buffer, buf );
      	}
      	closedir (dp);
    }
    else
    	perror ("Couldn't open the directory");

    page_to_char( buffer, ch );

  return;
}

/*****************************************************************
 * Add the appropriate lines to interp.c/h and you               *
 * should be good to go.                                         *
 *                                                               *
 * If you found this useful or just want to yell at me           *
 * for wasting your time, e-mail me at                           *
 * jessechoward@netscape.net                                     *
 *****************************************************************/