MudOSa4DGD/
MudOSa4DGD/bin/
MudOSa4DGD/data/
MudOSa4DGD/doc/
MudOSa4DGD/doc/driver/
MudOSa4DGD/doc/efun/bitstrings/
MudOSa4DGD/doc/efun/command/
MudOSa4DGD/doc/efun/communication/
MudOSa4DGD/doc/efun/heart_beat/
MudOSa4DGD/doc/efun/interactive/
MudOSa4DGD/doc/efun/inventory/
MudOSa4DGD/doc/efun/living/
MudOSa4DGD/doc/efun/mappings/
MudOSa4DGD/doc/efun/strings/
MudOSa4DGD/doc/efun/uid/
MudOSa4DGD/doc/funs/
MudOSa4DGD/doc/language/
MudOSa4DGD/mudlib/dgd/doc/
MudOSa4DGD/mudlib/dgd/lib/include/dgd/
MudOSa4DGD/mudlib/dgd/lib/std/
MudOSa4DGD/mudlib/dgd/lib/sys/
MudOSa4DGD/mudlib/dgd/log/
MudOSa4DGD/mudlib/log/
MudOSa4DGD/mudlib/std/include/
MudOSa4DGD/mudlib/std/obj/
/*
 * editor.c
 *
 * An editor to be abused by users
 *
 * (C) Frank Schmidt, Jesus@NorseMUD
 *
 */

#define __EDITOR
#include <driver.h>


/* user for this editor */
private static object user;


/* responsible user */
object query_user() {
  if (!DRIVER_PRIV()) {
    illegal();
    return 0;
  }
  return user;
}


/* handle an editor command */
varargs void edit(string cmd) {
  if (!DRIVER_PRIV()) {
    illegal();
    return;
  }

  if (previous_object() == user)
    cmd = editor(cmd);
  else if (!user && previous_object() == this_user()) {
    user = this_user();
    cmd = editor(cmd);
  }
  if (cmd) {
    user->send_message(cmd);
  }

  /* clean up? */
  if (!query_editor(this_object()) || !user)
    __DESTROY_DEF();
}


/* attempt to rescue the currently edited file */
static void rescue_file() {
  string file, *path;

  if (user) {
    if (query_editor(this_object()) == "input")
      editor(".");

    if (sscanf(editor("f"), "\"%s\" [Modified] %*s", file) == 2) {
      path = explode(file, "/");
      RLIMITS_ON();
      catch(editor("w! " + call_other(master(), __GET_RESCUE_FILE_FUNC,
				      path[sizeof(path)-1], user->query_player())));
      RLIMITS_OFF();
    }
  }
}


static void __CREATE_DEF() {
  if (!IS_DRIVER_SOURCE(object_name(previous_object()))) {
    /* can only be cloned by driver objects */
    illegal();
    __DESTROY_DEF();
    return;
  }
  ::__CREATE_DEF();
#ifdef MUDOS_USER_ID
  seteuid(getuid());
#endif
}


/* unmovable */
varargs int __MOVE_DEF(object dest) {
  illegal();
}


/* we're going down! Ieeaarrrrghh.. */
void __DESTROY_DEF() {
  if (!DRIVER_PRIV()) {
    illegal();
    return;
  }
  rescue_file();
  ::__DESTROY_DEF();
}