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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s CHANTYPE <#channel> [type]\nChange/view the channel-type. It's used to set different category on channels.\n"\
	   "/msg %s CHANTYPE #chan         View the type on the channel.\n"\
	   "/msg %s CHANTYPE #chan #5      Change the type on the channel.\n"\
	   "/msg %s CHANTYPE #chan clan    Change the type on the channel.\n"
	   , BOT_NAME, BOT_NAME, BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Change/view the channel-type.");
  dl->level = 900;
#if 0
  dl->func = m_chantype;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_chantype");
#endif
  return 1;
}

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

#if 0
char *get_chantype_name(int id)
{
  ListElem *el;
  struct chantype *ct;

  el = GetHead(chantype_list);
  while(el) {
    ct = (struct chantype *)el->data;
    if(ct->id == id) {
      break;
    }
    el = GetSucc(el);
  }
  if(!el) return NULL;
  return ct->name;
}
#endif

MODULE_PUBLIC void m_chantype(char *a)
{
  int i, nr;
  char channel[CHANNELLEN+1];
  char chantypestr[CHANTYPELEN+1];
  struct chanopts *c;
  struct chantype *ct;
  ListElem *el;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s chantype [%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(channel,getword(a,&i),CHANNELLEN);
  channel[CHANNELLEN] = '\0';
  if(!strlen(channel)) {
    say_sender("You forgot the channel parameter!");
    return;
  }
  c=findchannel(channel);
  if(!c) {
    say_sender("Can't find that channel!");
    return;
  }

  strncpy(chantypestr,getword(a,&i),CHANTYPELEN);
  chantypestr[CHANTYPELEN] = '\0';
  if(!strlen(chantypestr)) {
    if(c->type) {
      ct = NULL;
      el = GetHead(chantype_list);
      while(el) {
	ct = (struct chantype *)el->data;
	if(ct->id == c->type) {
	  break;
	}
	el = GetSucc(el);
      }
      if(el) {
	say_sender_("Current type is: %d [%s]", c->type, ct->name);
      } else {
	say_sender_("Current type is: %d (Unknown)", c->type);
      }
    } else {
      say_sender("Current type not set");
    }
    return;
  }

  nr = -1;
  if(chantypestr[0] == '#') {
    if(!isdigit(chantypestr[1])) {
      say_sender("Couldn't find a digit after #");
      return;
    }
    nr = atoi(&chantypestr[1]);
    if(nr < 0) {
      say_sender("Invalid chantype number");
      return;
    }
  }

  if(nr == 0) {
    c->type = nr;
    say_sender_("Channeltype cleared for channel [%s]", c->channel);
    return;
  }

  el = GetHead(chantype_list);
  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("Channeltype not found");
    return;
  }
  c->type = ct->id;
  say_sender_("Channeltype set to %d [%s] for [%s]", c->type, ct->name, c->channel);
  return;
}
