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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s TESTADDCHAN <#channel> <#from_nr> <#end_nr>\nAdd the bot to many new channels.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Add the bot to many new channels.");
  dl->level = 999;
#if 0
  dl->func = m_testaddchan;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_testaddchan");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_testaddchan(char *a)
{
  int i;
  char channel[2*CHANNELLEN];
  char channel_tmp[CHANNELLEN+1];
  struct dbaseuser *owner;
  unsigned int flags;
  struct chanopts *c;
  struct chanpriv *p;
  int from_nr, to_nr, nr, left, nr_added;
  char from_nr_str[BIGBUF];
  char to_nr_str[BIGBUF];

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

  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 TESTADDCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    say_sender_("/msg %s TESTADDCHAN <#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 TESTADDCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    say_sender_("/msg %s TESTADDCHAN <#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 TESTADDCHAN <#channel> <#from_nr> <#end_nr>", BOT_NAME);
    say_sender_("/msg %s TESTADDCHAN <#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;
  flags = CO_AUTO_JOIN;
  nr = from_nr;
  nr_added = 0;

  while(left >= 0) {
    sprintf(channel, "%s%d", channel_tmp, nr);
    if(strlen(channel) >= CHANNELLEN) {
      say_sender_("Channel name too long [%s]", channel);
      break;
    }
    c = findchannel(channel);
    if(c) {
      say_sender_("Channel [%s] already exists.", channel);
    } else {
      nr_added++;
      c = addchannel(channel,flags,0,owner->userid,channel);

      /* set which operator added the channel */
      c->created_by = sender_au->userid;
      c->type = 0;
      
      if(userlevelonchan_d(owner, channel) == 0) { 
	p = (struct chanpriv *)Malloc(sizeof(struct chanpriv));
	strncpy(p->channel,channel,CHANNELLEN);
	p->channel[CHANNELLEN] = '\0';
	p->chanlevel=CHAN_MASTER+CHAN_OWNER+CHAN_VOICE+CHAN_OPER+CHAN_AUTOOP;
	p->next=owner->privs;
	owner->privs=p;
      } else {
	p = owner->privs;
	while(p) {
	  if(!strcasecmp(channel,p->channel)) break;
	  p = p->next;
	}
	if(p) {
	  p->chanlevel=CHAN_OWNER+CHAN_MASTER+CHAN_OPER+CHAN_VOICE+CHAN_AUTOOP;
	}
      }
#if 1
      joinchannel(c);
#else
      c->bot_joined = 1;
#endif
    }
    nr++;
    left--;
  }

  say_sender_("%d channels added", nr_added);
  say_sender("Done.");
  return;
}
