clan/
class/
data/
doc/ideas/
doc/mobprogs/
gods/
log/
player/
time/
/*
 *  The unique portions of SunderMud code as well as the integration efforts
 *  for code from other sources is based on the efforts of:
 *
 *  Lotherius (elfren@aros.net)
 *
 *  This code can only be used under the terms of the DikuMud, Merc,
 *  and ROM licenses. The same requirements apply to the changes that
 *  have been made.
 *
 * All other copyrights remain in place and in force.
*/


/***************************************************************************
 *  File: olc_help.c                                                       *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 *                                                                         *
 *  This work is a derivative of Talen's post to the Merc Mailing List.    *
 *  It has been modified by Jason Dinkel to work with the new OLC.         *
 *                                                                         *
 ***************************************************************************/

#include "everything.h"
#include "olc.h"


#define HEDIT( fun )		bool fun( CHAR_DATA *ch, char *argument )
#define EDIT_HELP(Ch, Help)	( Help = (HELP_DATA *)Ch->desc->pEdit )

/*
 * Help Editor Prototypes
 */
DECLARE_OLC_FUN( hedit_create 		);
DECLARE_OLC_FUN( hedit_delete 		);
DECLARE_OLC_FUN( hedit_desc 		);
DECLARE_OLC_FUN( hedit_level 		);
DECLARE_OLC_FUN( hedit_keywords 	);
DECLARE_OLC_FUN( hedit_show 		);
DECLARE_OLC_FUN( hedit_save		);


const struct olc_cmd_type hedit_table[] =
{
/*  {   command		function	}, */

    {   "commands",	show_commands	},
    {   "create",	hedit_create	},
    {   "delete",	hedit_delete	},
    {   "desc",		hedit_desc	},
    {   "level",	hedit_level	},
    {	"keywords",	hedit_keywords	},
    {	"show",		hedit_show	},
    {   "save", 	hedit_save	},
    {   "?",		show_help	},

    {	"",		0,		}
};


/*
 * Stupid leading space muncher fix             -Thoric
 */
/*
char *help_fix( char *text )
{
    char *fixed;
    if ( !text )
      return "";
    fixed = strip_cr(text);
    if ( fixed[0] == ' ' )
      fixed[0] = '.';
    return fixed;
}
*/


void hedit( CHAR_DATA *ch, char *argument )
{
    char arg [MAX_INPUT_LENGTH];
    char command[MAX_INPUT_LENGTH];
    int cmd;

    smash_tilde( argument );
    strcpy( arg, argument );
    argument = one_argument( argument, command );

    if ( ch->pcdata->security == 0 )
	send_to_char( "HEdit: Insufficient security to modify area.\n\r", ch );

    if( command[0] == '\0' )
    {
	hedit_show( ch, argument );
	return;
    }

    if ( !str_cmp(command, "done") )
    {
	edit_done( ch );
	return;
    }

    if ( ch->pcdata->security == 0 )
    {
	interpret( ch, arg );
	return;
    }

    /* Search Table and Dispatch Command. */
    for ( cmd = 0; hedit_table[cmd].name[0] != '\0'; cmd++ )
    {
	if ( !str_cmp( command, hedit_table[cmd].name ) )
	{
	    (*hedit_table[cmd].olc_fun) ( ch, argument );
	    return;
	}
    }

    /* Default to Standard Interpreter. */
    interpret( ch, arg );
    return;
}



/* Entry point for editing help_data. */
void do_hedit( CHAR_DATA *ch, char *argument )
{
    char arg[MAX_INPUT_LENGTH];
    HELP_DATA   *iHelp;

    if( IS_NPC( ch ) )
	return;

    argument = one_argument( argument, arg );

    if( arg[0] == '\0' )
    {
	send_to_char( "Syntax:  edit help <keywords>\n\r", ch );
	return;
    }
    else
    {
	for( iHelp = help_first; iHelp; iHelp = iHelp->next )
	{
	    /*
	     * This help better not exist already!
	     */
	    if( is_name( arg, iHelp->keyword ) )
	    {
		ch->desc->pEdit = (void *)iHelp;
		ch->desc->editor = ED_HELP;
		break;
	    }
	}

	if( !iHelp )
	{
	    iHelp		= new_help();
	    iHelp->keyword	= str_dup( arg );

	    if( !help_first )
		help_first	= iHelp;
	    if( help_last )
		help_last->next	= iHelp;

	    help_last	= iHelp;
	    iHelp->next	= NULL;
	    ch->desc->pEdit     = (void *)iHelp;
	    ch->desc->editor = ED_HELP;
	}
    }
    return;
}

HEDIT ( hedit_save )
{
    FILE *fp=NULL;
    HELP_DATA *pHelp;

    log_string( "Saving help.are..." );


    fp = fopen ( "help.are", "w");

        if (!fp)
        {
                bug ("Could not open help.are for writing.",0);
                return FALSE;
        }

    fprintf( fp, "#HELPS\n\n" );

    for( pHelp = help_first; pHelp; pHelp = pHelp->next )
    {
        fprintf( fp, "%d %s~\n%s~\n",
          pHelp->level,
          pHelp->keyword,
          pHelp->text);
    }

    fprintf( fp, "0 $~\n\n\n#$\n" );

    fclose( fp );

    send_to_char( "Saved.\n", ch );

    return TRUE;
}


HEDIT( hedit_show )
{
    HELP_DATA *pHelp;
    char buf[MAX_STRING_LENGTH];
    
    if ( !EDIT_HELP( ch, pHelp ) )
    {
	send_to_char( "Null help file.\n\r", ch );
	return FALSE;
    }

    sprintf( buf,
            "Seen at level: [%d]\n\r"
            "Keywords:      [%s]\n\r"
            "Text:\n\r%s\n\r",
            pHelp->level, pHelp->keyword, pHelp->text );
    send_to_char( buf, ch );

    return FALSE;
}



HEDIT( hedit_create )
{
    HELP_DATA *iHelp;
    HELP_DATA *NewHelp;
    char buf[MAX_STRING_LENGTH];

    if ( !EDIT_HELP( ch, iHelp ) )
    {
	send_to_char( "Null help file.\n\r", ch );
	return FALSE;
    }

    if( argument[0] == '\0' )
    {
	send_to_char( "Syntax: create <keywords>\n\r", ch );
	return FALSE;
    }

    /*
     * This help better not exist already!
     */
    for( iHelp = help_first; iHelp; iHelp = iHelp->next )
    {
	if( is_name( argument, iHelp->keyword ) )
	{
	    send_to_char( "That help file already exists.\n\r", ch );
	    return FALSE;
	}
    }

    NewHelp		= new_help();
    NewHelp->keyword	= str_dup( argument );

    if( !help_first )	/* If it is we have a leak */
	help_first	= NewHelp;
    if( help_last )
	help_last->next	= NewHelp;

    help_last	= NewHelp;
    NewHelp->next	= NULL;
    ch->desc->pEdit	= (void *)NewHelp;
    ch->desc->editor = ED_HELP;

    sprintf( buf, "Created help with the keyword(s): %s\n\r",
	NewHelp->keyword );
    send_to_char( buf, ch );

    return TRUE;
}



HEDIT( hedit_delete )
{
    HELP_DATA *pHelp;
    HELP_DATA *PrevHelp=NULL;

    if ( !EDIT_HELP( ch, pHelp ) )
    {
	send_to_char( "Null help file.\n\r", ch );
	return FALSE;
    }

    if( argument[0] == '\0' )
    {
	send_to_char( "Syntax: delete <keyword>\n\r", ch );
	return FALSE;
    }

    /*
     * This help better exist
     */
    for( pHelp = help_first; pHelp; PrevHelp = pHelp, pHelp = pHelp->next )
    {
	if( is_name( argument, pHelp->keyword ) )
	    break;
    }

    if( !pHelp )
    {
	send_to_char( "That help file does not exist.\n\r", ch );
	return FALSE;
    }

    if( pHelp == (HELP_DATA *)ch->desc->pEdit )
    {
	edit_done( ch );
    }

    if( !PrevHelp )          /* At first help file   */
    {
	help_first  = pHelp->next;
	free_help( pHelp );
    }
    else if( !pHelp->next )  /* At the last help file*/
    {
	help_last           = PrevHelp;
	PrevHelp->next      = NULL;
	free_help( pHelp );
    }
    else                            /* Somewhere else...    */
    {
	PrevHelp->next      = pHelp->next;
	free_help( pHelp );
    }

    send_to_char( "Help file deleted.\n\r", ch );
    return TRUE;
}



HEDIT( hedit_desc )
{
    HELP_DATA *pHelp;

    if ( !EDIT_HELP( ch, pHelp ) )
    {
	send_to_char( "Null help file.\n\r", ch );
	return FALSE;
    }

    if ( argument[0] != '\0' )
    {
	send_to_char( "Syntax:  desc\n\r", ch );
	return FALSE;
    }
    
    string_append( ch, &pHelp->text );
    return TRUE;
}



HEDIT( hedit_level )
{
    HELP_DATA *pHelp;
    int value;

    if ( !EDIT_HELP( ch, pHelp ) )
    {
	send_to_char( "Null help file.\n\r", ch );
	return FALSE;
    }

    value = atoi( argument );

    if ( argument[0] == '\0' || value < -1 )
    {
	send_to_char( "Syntax:  level [level >= -1]\n\r", ch );
	return FALSE;
    }

    pHelp->level = value;
    send_to_char( "Help level set.\n\r", ch );

    return TRUE;
}



HEDIT( hedit_keywords )
{
    HELP_DATA *pHelp;
    int i;
    int length;

    if ( !EDIT_HELP( ch, pHelp ) )
    {
	send_to_char( "Null help file.\n\r", ch );
	return FALSE;
    }

    if ( argument[0] == '\0' )
    {
	send_to_char( "Syntax:  keywords <keywords>\n\r", ch );
	return FALSE;
    }

    length = strlen(argument);
    for (i = 0; i < length; i++)
	argument[i] = toupper(argument[i]);

    pHelp->keyword = str_dup( argument );
    send_to_char( "Help keywords set.\n\r", ch );
    return TRUE;
}


void do_hlist( CHAR_DATA *ch, char *argument )
{
    int min, max, cnt;
    char arg[MAX_INPUT_LENGTH];
    char buf[MAX_STRING_LENGTH];
    char buf2[8*MAX_STRING_LENGTH]; 
    HELP_DATA *help;

    buf2[0] = '\0'; 
	buf[0]='\0';

    min = -1;
    max = get_trust(ch);

    argument = one_argument( argument, arg );
    if ( arg[0] != '\0' )
    min = atoi(arg);
    if ( argument[0] != '\0' )
    max = atoi(argument);

    if (max > get_trust(ch))
    {
	max = get_trust(ch);
    }
    if (min < -1 )
    {
	min = -1;
    }

    sprintf( buf, "{WHelp Topics in level range %d to %d:{x\n\r\n\r", min, max );
    strcat( buf2,buf); 
    for ( cnt = 0, help = help_first; help; help = help->next )
    	if ( help->level >= min && help->level <= max )
    		{
    	    sprintf( buf, "  {G%3d {Y%s{x\n\r", help->level, help->keyword );
			strcat(buf2,buf);
    	    ++cnt;
   			}
    if ( cnt )
    {
    sprintf( buf, "\n\r%d pages found.\n\r", cnt );
    strcat(buf2,buf);
    page_to_char (buf2, ch); 
    }
    else
    send_to_char( "None found.\n\r", ch );
}