/
roa/
roa/lib/boards/
roa/lib/config/
roa/lib/edits/
roa/lib/help/
roa/lib/misc/
roa/lib/plrobjs/
roa/lib/quests/
roa/lib/socials/
roa/lib/www/
roa/lib/www/LEDSign/
roa/lib/www/LEDSign/fonts/
roa/lib/www/LEDSign/scripts/
roa/src/s_inc/
roa/src/sclient/
roa/src/sclient/binary/
roa/src/sclient/text/
roa/src/util/
/************************************************************************
	Realms of Aurealis 		James Rhone aka Vall of RoA

texedit.c				Online text file editting and
					usage code... OLTC

   With this bit of code, adapting the menu driven style of all of RoAOLC,
   it will be possible to edit the imotd/motd/etc files online.

		******** 100% Completely Original Code ********
		*** BE AWARE OF ALL RIGHTS AND RESERVATIONS ***
		******** 100% Completely Original Code ********
		        All rights reserved henceforth. 

    Please note that no guarantees are associated with any code from
Realms of Aurealis.  All code which has been released to the general
public has been done so with an 'as is' pretense.  RoA is based on both
Diku and CircleMUD and ALL licenses from both *MUST* be adhered to as well
as the RoA license.   *** Read, Learn, Understand, Improve ***
*************************************************************************/
#include "conf.h"
#include "sysdep.h"

#include "structures.h"
#include "utils.h"
#include "comm.h"
#include "interpreter.h"
#include "acmd.h"
#include "handler.h"
#include "db.h"
#include "roaolc.h"

// externals
extern char *motd;
extern char *imotd;
extern char *info;
extern char *news;

/* internal functions */
ROA_MENU(tedit_top_menu);
ROA_MENU(tedit_confirm_quit);
ROA_MENU(tedit_confirm_save);

/* here we go, do_tedit, to get into social editting menu */
ACMD(do_tedit)
{
    char buf[80];

    if (IS_NPC(ch))
	return;

    one_argument(argument, buf);

    if (!*buf || (str_cmp(buf, "motd") && str_cmp(buf, "imotd") &&
	str_cmp(buf, "info") && str_cmp(buf, "policies") && str_cmp(buf, "news") &&
	str_cmp(buf, "assembly")))
    {
      send_to_char("usage: TEXEDIT <motd | imotd | info | policies | news | assembly>\n\r", ch);
      return;
    }
    
    SET_BIT(PLR_FLAGS(ch), PLR_BUILDING);
    /* use the char pointers on character to store the strings */
    /* allocate memory for em */
    /* note, these gotta be attached to the character so we can ref them
	without sending them as args */
    /* use any char pointer that char_data points to etc etc */
    /* is freed in hedit_confirm_save */
    CREATE(PSTR1(ch), char, MAX_POOFIN_LENGTH);
    CREATE(PSTR2(ch), char, MAX_STRING_LENGTH);

    // for now, leave assembly as special case... 2/12/98 -jtrhone...
    if (!str_cmp(buf, "assembly"))
      strcpy(PSTR1(ch), "config/assem.cfg");
    else
      sprintf(PSTR1(ch), "text/%s",buf);
    file_to_string(PSTR1(ch), PSTR2(ch));

    /* point their function ptr to the top menu function and call */
    SET_BIT(PLR_FLAGS(ch), PLR_BUILDING);
    MENU_DEPTH(ch) = 0;
    ch->pc_specials->field_changed = FALSE;
    menu_jump(ch, tedit_top_menu);
}

/* writes the actual textfile */
/* loads the new one into memory */
// updated filename locations, 5/23/98 -jtrhone
void update_text_entry(char *name, char *txt)
{
  FILE *fp;

  if (!*name || !*txt) 
  {
    log("SYSERR: Missing name or txt to update_text_entry.");
    return;
  }

  /* write the text file to the text dir */
  /* first, get rid of those annoying ^M s  (which are \r)*/
  killr(txt);

  if (!(fp = fopen(name, "wt")))
  {
    log("SYSERR: Error opening text file for writing.");
    return;
  }
  fprintf(fp, "%s", txt);
  fclose(fp);

  if (!str_cmp(name, "text/motd"))
    file_to_string_alloc(MOTD_FILE, &motd);
  else
  if (!str_cmp(name, "text/imotd"))
    file_to_string_alloc(IMOTD_FILE, &imotd);
  else
  if (!str_cmp(name, "text/info"))
    file_to_string_alloc(INFO_FILE, &info);
  else
  if (!str_cmp(name, "text/news"))
    file_to_string_alloc(NEWS_FILE, &news);
}

ROA_MENU(tedit_top_menu)
{
    char buf[MAX_STRING_LENGTH];
    char *p;
    int field;
    
    if (!input_str)
    {
      menu_title_send("TexEdit Main Menu", ch);
      sprintf(buf, " 1.) %%6File%%0: %s\n\r", PSTR1(ch)); S2C();

      sprintf(buf, " 2.) %%6Text%%0: \n\r%s\n\r", PSTR2(ch)); 
      // use page here, file could be long... 1/12/98  -jtrhone
      page_string(ch->desc, buf, 1);

      MENU_PROMPT(ch) = "Enter field number to change or 0 to end: ";
      return;
    }

    strcpy(buf, input_str);
    p = strtok(buf, " 	\n\r");
    if (p)
	field = atoi(p);
    else
	field = 0;

    switch (field)
    {
      case 0:
	MENU_HANDLER(ch) = tedit_confirm_quit;
	(*MENU_HANDLER(ch))(ch, NULL);
	break;

      case 1:
	do_string_arg(ch, "Enter new filename:\n\r", &PSTR1(ch), "");	
	break;

      case 2:
	do_max_string_arg(ch, "Enter file text (@):\n\r", &PSTR2(ch));	
	break;

      default:
	send_to_char("That field cannot be changed.\n\r", ch);
	break;
    }

    ch->pc_specials->field_changed = TRUE;
}

ROA_MENU(tedit_confirm_quit)
{
    char buf[MAX_STRING_LENGTH];
    char *p;
    
    if (!input_str)
    {
	MENU_PROMPT(ch) = "Quit editting file (yes/NO)? ";
	return;
    }
    
    strcpy(buf, input_str);
    p = strtok(buf, " 	\n\r");
    if (p && strncasecmp("yes", p, strlen(p)) == 0)
    {
	MENU_HANDLER(ch) = tedit_confirm_save;
	(*MENU_HANDLER(ch))(ch, NULL);
    }
    else
    {
	MENU_HANDLER(ch) = tedit_top_menu;
	(*MENU_HANDLER(ch))(ch, NULL);
    }
}

ROA_MENU(tedit_confirm_save)
{
    char buf[MAX_STRING_LENGTH];
    char *p;
    
    if (!input_str)
    {
	MENU_PROMPT(ch) = "Save editted file (YES/no)? ";
	return;
    }
    
    strcpy(buf, input_str);
    p = strtok(buf, " 	\n\r");
    if (!p || strncasecmp("no", p, strlen(p)) != 0)
    {
      update_text_entry(PSTR1(ch), PSTR2(ch));      
      sprintf(buf, "IMMUPD: %s editted file %s", GET_NAME(ch), PSTR1(ch));
      mudlog(buf, BRF, GET_LEVEL(ch), TRUE);
    }

    FREENULL(PSTR1(ch));
    FREENULL(PSTR2(ch));
    MENU_PROMPT(ch) = NULL;
    MENU_HANDLER(ch) = NULL;
    MENU_DEPTH(ch) = 0;
    REMOVE_BIT(PLR_FLAGS(ch), PLR_BUILDING);
}