/*
 * trustdel.c
 * (C) Christian Magnusson 2000, Email: d1mag@dtek.chalmers.se
 * This program is copyright, and covered by the Gnu Public License.
 * The Q bot.
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "settings.h"
#include "bottypes.h"
#include "globals.h"
#include "dl.h"

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

void m_trustdel(char *a);

MODULE_PUBLIC int _init_trustdel(void *a, void *b)
{
  dl_command_entry_t *dl = a;
  if(!a) return 0;

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s TRUSTDEL <host>\n"\
	   "/msg %s TRUSTDEL #2\nDelete trusted host. Either give the exact hostname or the number from TRUSTLIST.\n", BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Delete trusted host.");
  dl->level = 950;
#if 0
  dl->func = m_trustdel;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_trustdel");
#endif
  return 1;
}

MODULE_PUBLIC int _remove_trustdel(char *a, char *b)
{
  remove_command("trustdel");
  return 1;
}

MODULE_PUBLIC void m_trustdel(char *a)
{
  char tmp[BIGBUF];
  int i, nr;
  ListElem *el;
  char host[HOSTLEN+1];
  struct trusted *tr;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s trustdel [%s]\n", db->nick, a);
  } else
    return;
  if(!is_oper()) {
    say_sender("You are not an Oper.");
    log("To: %s Is not an Oper\n", db->nick);
    return;
  }

  i=0;
  strncpy(host, getword(a,&i), HOSTLEN);
  host[HOSTLEN] = '\0';

  if(strlen(host) < 1) {
    say_sender( "You forgot to specify host");
    say_sender_( "/msg %s TRUSTDEL <host>\n/msg %s TRUSTDEL #2\n",
		 BOT_NAME, BOT_NAME);
    return;
  }

  if(host[0] == '#') {
    nr = atoi(&host[1]);
    if(nr <= 0) {
      say_sender("Number must be greater than 0");
      return;
    }
    i = 0;
    el = GetHead(trusted_list);
    while(el) {
      tr = (struct trusted *)el->data;
      i++;
      if(i == nr) {
	trusted_del(tr->host);
	break;
      }
      el = GetSucc(el);
    }
    if(!el) {
      say_sender_( "There are only %d trusted hosts", i);
      return;
    }
  } else {
    if(!trusted_del(host)) {
      say_sender_( "Couldn't find that host", i);
      return;
    }
  }

  sprintf(tmp, "TRUSTDEL %s by %s\n", host, sender_au->dbuser->nick);
  wallop(tmp);
  return;
}
