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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s UNBANALL #channel\nUnban all temporary hostmasks on a channel. Permanent bans set by BAN must be removed with BANCLEAR.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Unban all temporary bans on a channel.");
  dl->level = 1;
#if 0
  dl->func = m_unbanall;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_unbanall");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_unbanall(char *a)
{
  int i, nr;
  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 unbanall [%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 UNBANALL #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 UNBANALL!");
      return;
    }
  }

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

  nr = 0;
  c = findchannel(channel);
  if(c) {
    if(c->suspended) { say_sender("Channel is suspended"); return; }
    b = c->bans;

    if(c->bot_joined) {
      while(b) {
	bn = b->next;
	/*
	  this will remove the ban, but Q will still remember the perm bans
	  even if they aren't set.
	*/
	putserver("%s M %s -b %s\n\r", yxx, channel, b->ban);
	/* don't remove permanent bans */
	delban_ch(c, b->ban, 0);
	nr++;
	b = bn;
      }
    } else {
      /* in case Q isn't in the channel make a server hack. */
      if(oper && (sender_au->auth>=950)) {
	while(b) {
	  bn = b->next;
	  putserver("%c%c M %s -b %s\n\r", yxx[0], yxx[1], channel, b->ban);
	  /* don't remove permanent bans */
	  delban_ch(c, b->ban, 0);
	  nr++;
	  b = bn;
	}
      } else {
	say_sender("Sorry, you are not allowed to remove the bans");
      }
    }
    say_sender_("%d bans removed from channel.", nr);
  } else {
    say_sender("Couldn't find that channel");
  }
  return;
}
