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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s BANCLEAR #channel\nDelete all bans from a channel. The difference from UNBANALL, is that this command removes all permanent bans too.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Delete all bans from a channel.");
  dl->level = 1;
#if 0
  dl->func = m_banclear;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_banclear");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_banclear(char *a)
{
  int i;
  char channel[CHANNELLEN+1];
  unsigned int slevel;
  struct chanopts *c;
  struct ban *b, *bn;
  int oper;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s banclear [%s]\n", db->nick, a);
  } else
    return;

  i = 0;
  strncpy(channel,getword(a,&i),CHANNELLEN);
  channel[CHANNELLEN] = '\0';
  if(!strlen(channel)) {
    say_sender("You forgot the channel parameter");
    say_sender_( "/msg %s BANCLEAR #channel", BOT_NAME);
    return;
  }

  oper = is_oper();
  slevel = userlevelonchan_d(sender_au->dbuser, channel);

  if(slevel==0) {
    if(!oper || (sender_au->auth<950)) {
      say_sender( "You are not known on this channel and can't use BANCLEAR!");
      return;
    }
  }

  if(!oper && (sender_au->auth<950)) {
    if((!(slevel & CHAN_MASTER)) &&
       (!(slevel & CHAN_OWNER))) {
      say_sender("You should have the +m flag on the channel (and be AUTHED) to use this command.");
      return;
    }
  }

  c = findchannel(channel);
  if(!c) {
    say_sender( "Couldn't find channel.");
    return;
  }
  if(c->suspended) { say_sender("Channel is suspended"); return; }

  b = c->bans;
  while(b) {
    bn = b->next;
    putserver("%s M %s -b %s\n\r", yxx, channel, b->ban);
    /* remove the perm ban here */
    delban_ch(c, b->ban, 1);
    b = bn;
  }
  say_sender( "Done.");
  return;
}
