/*
 * chantypeadd.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_chantypeadd(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s CHANTYPEADD <type> [id]\nAdd a new channel-type. It's possible to add channels to this type later.\n"\
	   "/msg %s CHANTYPEADD clan         New type called \"clan\".\n"\
	   "/msg %s CHANTYPEADD private 3    New type called \"private\" with id 3.\n"
	   , BOT_NAME, BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Add a new channel-type.");
  dl->level = 999;
#if 0
  dl->func = m_chantypeadd;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_chantypeadd");
#endif
  return 1;
}

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

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

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s chantypeadd [%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(chantypestr,getword(a,&i),CHANTYPELEN);
  chantypestr[CHANTYPELEN] = '\0';
  len = strlen(chantypestr);
  if(!len) {
    say_sender("You forgot the channeltype parameter.");
    return;
  }

  nr = -1;
  strncpy(chantypeid,getword(a,&i),CHANTYPELEN);
  chantypeid[CHANTYPELEN] = '\0';
  if(strlen(chantypeid)) {
    if(chantypeid[0] != '#') {
      say_sender("Argument doen't begin with #");
      return;
    }
    if(!isdigit(&chantypeid[1])) {
      say_sender("Argument is not a valid number");
      return;
    }
    nr = atoi(&chantypeid[1]);
    if(nr < 0) {
      say_sender("Invalid channeltype id");
      return;
    }
  }

  el = GetHead(chantype_list);
  maxval = 0;
  while(el) {
    ct = (struct chantype *)el->data;
    if(ct->id > maxval) maxval = ct->id;
    if(!strcasecmp(ct->name, chantypestr)) {
      say_sender("Channeltype name already exists");
      return;
    }
    if(nr >= 0) {
      if(ct->id == nr) {
	say_sender("Channeltype id already exists.");
	return;
      }
    }
    el = GetSucc(el);
  }

  ct = Malloc(sizeof(struct chantype));
  if(!ct) return;
  memset(ct, 0, sizeof(struct chantype));
  ct->name = Malloc(len+1);
  strncpy(ct->name, chantypestr, len);
  ct->name[len] = '\0';
  for(i=0; i<len; i++) ct->name[i] = tolower(ct->name[i]);

  if(nr >= 0) {
    ct->id = nr;
  } else {
    ct->id = maxval + 1;
  }
  ct->join_burst = 1;
  ct->priority = 0;

  el = NewElem(ct);
  AddTail(chantype_list, el);

  say_sender_("Channeltype added id=%d [%s]", ct->id, ct->name);
  return;
}
