/*
 * chantypedel.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"

void m_chantypedel(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s CHANTYPEDEL <name / #nr>\nDelete a channel-type. It's possible to add channels to this type later.\n"\
	   "/msg %s CHANTYPEDEL #4           Delete 4'th type.\n"\
	   "/msg %s CHANTYPEDEL clan         Delete type called \"clan\".\n"
	   , BOT_NAME, BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Delete a channel type.");
  dl->level = 999;
#if 0
  dl->func = m_chantypedel;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_chantypedel");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_chantypedel(char *a)
{
  int i, nr, len, maxval;
  char chantypestr[CHANTYPELEN+1];
  struct chantype *ct;
  ListElem *el;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s chantypedel [%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;
  }

  nr = -1;
  i=0;

  strncpy(chantypestr,getword(a,&i),CHANTYPELEN);
  chantypestr[CHANTYPELEN] = '\0';
  len = strlen(chantypestr);
  if(!len) {
    say_sender("You forgot the channeltype parameter.");
    return;
  }

  if(chantypestr[0] == '#') {
    if(!isdigit(chantypestr[1])) {
      say_sender("No digit after #");
      return;
    }
    nr = atoi(&chantypestr[1]);
    if(nr < 0) {
      say_sender("Bad channeltype number.");
      return;
    }
  }

  el = GetHead(chantype_list);
  maxval = 0;
  while(el) {
    ct = (struct chantype *)el->data;
    if(nr>=0) {
      if(ct->id == nr) {
	break;
      }
    } else {
      if(!strcasecmp(ct->name, chantypestr)) {
	break;
      }
    }
    el = GetSucc(el);
  }

  if(!el) {
    say_sender("Couldn't find that channeltype.");
    return;
  }

  Free(ct->name);
  Remove(el);
  Free(el);
  say_sender("Channeltype removed.");
  return;
}
