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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s BADCHANNELADD *pattern* [reason]\nAdd an illegal channel. Make sure you DON'T add some strange pattern, since it will kill lots of people!\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Add an illegal channel");
  dl->level = 990;
#if 0
  dl->func = m_badchanneladd;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_badchanneladd");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_badchanneladd(char *a)
{
  ListElem *el;
  struct bad_chan *bad_channel;
  struct bad_chan *new_bad_chan;
  int i;
  char channel[CHANNELLEN+1];
  char *str, *ptr;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s badchanneladd [%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';
  i++;
  if(!strlen(channel)) {
    say_sender("You forgot to type a channel.");
    return;
  }


  el = GetHead(bad_channel_list);
  while(el) {
    bad_channel = ((struct bad_chan *)(el->data));
    if(bad_channel && bad_channel->channel) {
      if(!strcasecmp(bad_channel->channel, channel)) {
	say_sender( "That channel is already in the list");
	return;
      }
    }
    el = GetSucc(el);
  }

  new_bad_chan = Malloc(sizeof(struct bad_chan));
  strncpy(new_bad_chan->channel, channel, CHANNELLEN);
  new_bad_chan->channel[CHANNELLEN] = '\0';
  str = new_bad_chan->channel;
  ptr = strstr(str, "\n"); if(ptr) *ptr = '\0';
  ptr = strstr(str, "\r"); if(ptr) *ptr = '\0';

  strncpy(new_bad_chan->reason, "Bad channel", REASONLEN);
  new_bad_chan->reason[REASONLEN] = '\0';
  
  new_bad_chan->owner = db->userid;

  el = NewElem(new_bad_chan);
  AddTail(bad_channel_list, el);

  say_sender("Done.");
  return;
}
