TinyMAZE/
TinyMAZE/config/
TinyMAZE/doc/
TinyMAZE/run/msgs/
TinyMAZE/src/
TinyMAZE/src/db/
TinyMAZE/src/ident/
TinyMAZE/src/io/
TinyMAZE/src/prog/
TinyMAZE/src/softcode/
TinyMAZE/src/util/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "db.h"
#include "externs.h"

static int search_it(OBJ *player, OBJ *thing, char *str)
{
  ALIST *atr;
  int found = 0;

  for(atr = thing->attrlist;atr;atr = atr->next)
  {
    if(strstr(atr->value, str))
      if(thing->flags & VISIBLE || controls(player, thing, POW_SEEATR))
      {
        if(!found)
          notify(player, tprintf("|+W|-- |n|%s",
            unparse_object(player, thing)));

        look_atr(player, thing, atr->attr);
        found++;
      }
  }

  return(found);
}

void do_grep(OBJ *player, char *arg1, char *arg2)
{
  OBJ *thing;
  int found = 0;

  if(!*arg2)
  {
    notify(player, "What string do you want to search for?");
    return;
  }

  if(!strcmp(arg1, "ALL"))
  {
    if(!power(player, POW_DB))
    {
      notify(player, perm_denied());
      return;
    }
  }
  else
  {
    thing = match_object(player, arg1, NOTYPE);
    if(!thing)
    {
      notify(player, no_match(arg1));
      return;
    }
    search_it(player, thing, arg2);
    return;
  }

  for(thing = player_list;thing;thing = thing->next)
    if(controls(player, thing, POW_EXAMINE) && !(thing->flags & VISIBLE))
      found += search_it(player, thing, arg2);
  for(thing = thing_list;thing;thing = thing->next)
    if(controls(player, thing, POW_EXAMINE) && !(thing->flags & VISIBLE))
      found += search_it(player, thing, arg2);
  for(thing = room_list;thing;thing = thing->next)
    if(controls(player, thing, POW_EXAMINE) && !(thing->flags & VISIBLE))
      found += search_it(player, thing, arg2);
  for(thing = exit_list;thing;thing = thing->next)
    if(controls(player, thing, POW_EXAMINE) && !(thing->flags & VISIBLE))
      found += search_it(player, thing, arg2);

  if(found)
    notify(player, "|+W|-- |+B|End Search |+W|--");
  else
    notify(player, "No matches were found.");
}

void do_getalias(OBJ *player, char *arg)
{
  OBJ *victim;

  if(!(victim = match_player(NULL, arg)))
  {
    notify(player, no_match(arg));
    return;
  }

  if(!*atr_get(victim, "ALIAS"))
    notify(player, tprintf("%s |+W|doesn't have an alias.",
      unparse_object(player, victim)));
  else
    notify(player, tprintf("%s |+B|alias is|+W|: |+Y|%s",
      poss(victim), atr_get(victim, "ALIAS")));
}

static void search_players(OBJ *player, OBJ *victim, char *name,
                           unsigned long flags, int class)
{
  OBJ *o;
  int ctr = 0;

  for(o = player_list;o;o = o->next)
    if(controls(player, o, POW_MODIFY))
      if(!victim || o->owner == victim)
        if(!name || strstr(o->name, name))
          if(!flags || (o->flags & flags))
            if(class == -1 || get_class(o) == class)
            {
              if(!ctr++)
                notify(player, "|M|PLAYERS:");
              notify(player, unparse_object(player, o));
            }

  if(ctr)
    notify(player, "");
}

static void search_things(OBJ *player, OBJ *victim, char *name,
                          unsigned long flags)
{
  OBJ *o;
  int ctr = 0;

  for(o = thing_list;o;o = o->next)
    if(controls(player, o, POW_MODIFY))
      if(!victim || o->owner == victim)
        if(!name || strstr(o->name, name))
          if(!flags || (o->flags & flags))
          {
            if(!ctr++)
              notify(player, "|M|THINGS:");
            notify(player, unparse_object(player, o));
          }

  if(ctr)
    notify(player, "");
}

static void search_rooms(OBJ *player, OBJ *victim, char *name,
                         unsigned long flags)
{
  OBJ *o;
  int ctr = 0;

  for(o = room_list;o;o = o->next)
    if(controls(player, o, POW_MODIFY))
      if(!victim || o->owner == victim)
        if(!name || strstr(o->name, name))
          if(!flags || (o->flags & flags))
          {
            if(!ctr++)
              notify(player, "|M|ROOMS:");
            notify(player, unparse_object(player, o));
          }

  if(ctr)
    notify(player, "");
}

static void search_exits(OBJ *player, OBJ *victim, char *name,
                         unsigned long flags)
{
  OBJ *o;
  int ctr = 0;

  for(o = exit_list;o;o = o->next)
    if(controls(player, o, POW_MODIFY))
      if(!victim || o->owner == victim)
        if(!name || strstr(o->name, name))
          if(!flags || (o->flags & flags))
          {
            if(!ctr++)
              notify(player, "|M|EXITS:");
            notify(player, unparse_object(player, o));
          }

  if(ctr)
    notify(player, "");
}

static void search_all(OBJ *player, OBJ *victim, int type, char *name,
                       unsigned long flags, int class)
{
  if(type & TYPE_PLAYER)
    search_players(player, victim, name, flags, class);
  if(type & TYPE_THING && class == -1)
    search_things(player, victim, name, flags);
  if(type & TYPE_ROOM && class == -1)
    search_rooms(player, victim, name, flags);
  if(type & TYPE_EXIT && class == -1)
    search_exits(player, victim, name, flags);
}

void do_search(OBJ *player, char *arg1, char *arg2)
{
  OBJ *victim;
  char *criteria;
  char *name;
  int type;
  int class;
  unsigned long flags;

  if(!(criteria = strchr(arg1, ' ')))
    criteria = arg1;
  else
    *criteria++ = '\0';

/* See if they specified a player */
  if(criteria != arg1 || (*arg1 && !*arg2))
  {
    if(!(victim = match_player(player, arg1)))
    {
      notify(player, no_match(arg1));
      return;
    }

    if(!controls(player, victim, POW_MODIFY))
    {
      notify(player, perm_denied());
      return;
    }
  }
  else
    victim = NULL;

  if(criteria == arg1 && !*arg2)
    criteria = "";

  type = NOTYPE;
  name = NULL;
  class = -1;
  flags = 0;

  if(!string_compare(criteria, "type"))
  {
    if(string_prefix("player", arg2))
      type = TYPE_PLAYER;
    else if(string_prefix("room", arg2))
      type = TYPE_ROOM;
    else if(string_prefix("thing", arg2))
      type = TYPE_THING;
    else if(string_prefix("exit", arg2))
      type = TYPE_EXIT;
    else
      notify(player, tprintf("Unknown type: %s", arg2));
  }
  else if(!string_compare(criteria, "name"))
  {
    name = arg2;
  }
  else if(!string_compare(criteria, "flags"))
  {
    flags = str_to_flags(player, arg2);
    if(!flags)
      return;
  }
  else if(!string_compare(criteria, "class"))
  {
    class = name_to_class(arg2);
    if(class == -1)
    {
      notify(player, tprintf("Unknown class: %s", arg2));
      return;
    }
  }
  else if(*criteria)
  {
    notify(player, tprintf("Unknown criteria: %s", criteria));
    return;
  }

  search_all(player, victim, type, name, flags, class);
  notify(player, "|+W|--- |+B|End of Search |+W|---");
}