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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s TESTDELCHAN <channel>\nDeletes a channel from the lists of the bot. COMPLETELY. Do not use this command unless you really mean it.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Deletes a channel from the lists");
  dl->level = 999;
#if 0
  dl->func = m_testdelchan;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_testdelchan");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_testdelchan(char *a)
{
  int from_nr, to_nr, nr, left, nr_deleted;
  char from_nr_str[BIGBUF];
  char to_nr_str[BIGBUF];
  int i;
  char channel[2*CHANNELLEN];
  char channel_tmp[CHANNELLEN+1];
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s testdelchan [%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_tmp,getword(a,&i),CHANNELLEN);
  channel_tmp[CHANNELLEN] = '\0';
  if(strlen(channel_tmp) < 2) {
    say_sender("You forgot to type a channel name.");
    say_sender_("/msg %s TESTDELCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    say_sender_("/msg %s TESTDELCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    return;
  }
  if (channel_tmp[0]!='#') {
    say_sender("Channel MUST start with a '#'");
    return;
  }

  strncpy(from_nr_str,getword(a,&i),64);
  from_nr_str[64] = '\0';
  if(strlen(from_nr_str) < 2) {
    say_sender("You forgot to type any #from_nr.");
    say_sender_("/msg %s TESTDELCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    say_sender_("/msg %s TESTDELCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    return;
  }
  from_nr = atoi(&from_nr_str[1]);
  if((from_nr_str[0] != '#') || !isdigit(from_nr_str[1])) {
    say_sender("Paramenter should be written #10");
    return;
  }


  strncpy(to_nr_str,getword(a,&i),64);
  to_nr_str[64] = '\0';
  if(strlen(to_nr_str) < 2) {
    say_sender("You forgot to type any #to_nr.");
    say_sender_("/msg %s TESTDELCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    say_sender_("/msg %s TESTDELCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    return;
  }
  to_nr = atoi(&to_nr_str[1]);
  if((to_nr_str[0] != '#') || !isdigit(to_nr_str[1])) {
    say_sender("Paramenter should be written #10");
    return;
  }

  if (from_nr > to_nr) {
    say_sender("Make sure from_nr < to_nr");
    return;
  }
  left = to_nr - from_nr;
  nr = from_nr;
  nr_deleted = 0;

  while(left >= 0) {
    sprintf(channel, "%s%d", channel_tmp, nr);
    if(strlen(channel) >= CHANNELLEN) {
      say_sender_("Channel name too long [%s]", channel);
      break;
    }
    if(delchannel(channel)) {
      //      say_sender_( "Channel deleted [%s]", channel);
      nr_deleted++;
    } else {
      say_sender_( "Couldn't delete [%s]", channel);
    }
    left--;
    nr++;
  }

  say_sender_("%d channels deleted", nr_deleted);
  say_sender("Done.");
  return;
}
