/*
 * $Id: module.pmod.in,v 1.8 1997/06/10 20:31:04 grubba Exp $
 *
 * YP (NIS) Functionality for Pike
 *
 * Per Hedbor & Henrik Grubbström 1996-1997
 */

inherit @module@;

// Usual nicknames
constant nicknames = ([
  "passwd":"passwd.byname",
  "group":"group.byname",
  "networks":"networks.byaddr",
  "hosts":"hosts.byname",
  "protocols":"protocols.bynumber",
  "services":"services.byname",
  "aliases":"mail.aliases",
  "ethers":"ethers.byname"
]);

class Map {
  object(Domain) domain;
  string ypmap;

  void create(string map,string|void d)
  {
    if (d) {
      domain = Domain(d);
    } else {
      domain = Domain();
    }
    ypmap = map;
    array err;
    do {
      // Force an error if the map does not exist.
      err = catch{
	domain->match(ypmap, "foo");
	return;
      };
    } while (ypmap = nicknames[ypmap]);
    throw(err);
  }
  string match(string key)
  {
    return(domain->match(ypmap, key));
  }
  string `[](string key)
  {
    return(domain->match(ypmap, key));
  }
  mapping all()
  {
    return(domain->all(ypmap));
  }
  mapping cast_to_mapping()
  {
    return(domain->all(ypmap));
  }
  void map(function(string,string:void)|array(function(string,string:void)) fn)
  {
    domain->map(ypmap, fn);
  }
  string server()
  {
    return(domain->server(ypmap));
  }
  int order()
  {
    return(domain->order(ypmap));
  }
  int _sizeof()
  {
    return(sizeof(indices(domain->all(ypmap))));
  }
  array(string) _indices()
  {
    return(indices(domain->all(ypmap)));
  }
  array(string) _values()
  {
    return(values(domain->all(ypmap)));
  }
  mixed cast(string type)
  {
    switch(type) {
    case "mapping":
      return(cast_to_mapping());
    default:
      throw(({ "Yp.YpDomain: Cast to " + type + " not supported.\n",
		 backtrace() }));
    }
  }
};
