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 "externs.h"
#include "plugins.h"

static PLUGIN *plugin_list = NULL;

void plugin_init(void)
{
}

void plugin_add(char *match, void (*main_func)(), PLUGIN_F *funcs)
{
  PLUGIN *p;

  p = (PLUGIN *)stack_alloc(sizeof(PLUGIN), 1, 0);
  p->command = (CS *)stack_alloc(sizeof(CS), 1, 0);
  SET(p->command->name, match);
  p->command->func = main_func;
  p->command->flags = ARG1|ARG2;
  p->funcs.startup = funcs->startup;
  p->funcs.shutdown = funcs->shutdown;
  p->funcs.timed = funcs->timed;
  p->funcs.login = funcs->login;
  p->funcs.logout = funcs->logout;
  p->funcs.create = funcs->create;
  p->funcs.destroy = funcs->destroy;

  p->next = plugin_list;
  plugin_list = p;
}

void plugin_funcs_init(PLUGIN_F *funcs)
{
  funcs->startup = NULL;
  funcs->shutdown = NULL;
  funcs->timed = NULL;
  funcs->login = NULL;
  funcs->logout = NULL;
  funcs->create = NULL;
  funcs->destroy = NULL;
}

void plugin_startup(void)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
    if(p->funcs.startup)
      p->funcs.startup();
}

void plugin_shutdown(void)
{
  PLUGIN *p, *pnext;

  for(p = plugin_list;p;p = pnext)
  {
    pnext = p->next;

    if(p->funcs.shutdown)
      p->funcs.shutdown();

    stack_free(p->command->name);
    stack_free(p->command);
    stack_free(p);
  }
}

void plugin_timed(void)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
    if(p->funcs.timed)
      p->funcs.timed();
}

void plugin_login(DDATA *des)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
    if(p->funcs.login)
      p->funcs.login(des);
}

void plugin_logout(DDATA *des)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
    if(p->funcs.logout)
      p->funcs.logout(des);
}

void plugin_create(OBJ *thing)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
    if(p->funcs.create)
      p->funcs.create(thing);
}

void plugin_destroy(OBJ *thing)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
    if(p->funcs.destroy)
      p->funcs.destroy(thing);
}

void plugin_command_match(char *str, CS ***clist, int *ctr)
{
  PLUGIN *p;

  for(p = plugin_list;p;p = p->next)
  {
    if(!string_prefix(p->command->name, str))
      continue;

    if(!*ctr)
      *clist = (CS **)stack_alloc(sizeof(CS *), 0, 0);
    else
      *clist = (CS **)stack_realloc_tmp(*clist, sizeof(CS *)*((*ctr)+1));
    (*clist)[(*ctr)++] = p->command;
  }
}

void do_plugins(OBJ *player)
{
  PLUGIN *p;
  char buf[4096];
  int ctr = 0;

  if(!plugin_list)
  {
    notify(player, "There are no plugins installed on this server.");
    return;
  }

  strcpy(buf, "|+B|Available plugins|+W|: ");

  for(p = plugin_list;p;p = p->next, ctr++)
    if(!ctr)
      strcpy(buf+strlen(buf), p->command->name);
    else
      sprintf(buf+strlen(buf), ", %s", p->command->name);

  notify(player, buf);
}