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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s BANLIST #channel\nList all permanent bans on a channel.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "List all permanent bans on a channel.");
  dl->level = 1;
#if 0
  dl->func = m_banlist;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_banlist");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_banlist(char *a)
{
  int i;
  char channel[CHANNELLEN+1];
  unsigned int slevel;
  struct chanopts *c;
  struct ban *b;
  int oper;
  char tmp[2*BANLEN+50];
  char owner[NICKLEN+1];
  struct dbaseuser *d;
  int pos, len, j;
    
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s banlist [%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 BANLIST #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 BANLIST!");
      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;
    }
  }

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

  i = 1;
  b = c->bans;
  sprintf(tmp, "Banlist for %s", channel);
  say_sender( tmp);
  sprintf(tmp, "Nr. %-15s %s", "Owner", "Hostmask");
  say_sender( tmp);
  while(b) {
    d = NULL;
    if(b->placedby) d = finduserfromid(b->placedby);
    if(d) {
      strcpy(owner, d->nick);
    } else {
      strcpy(owner, "Not permanent");
    }
    sprintf(tmp, "#%-2d %-15s %s", i++, owner, b->ban);
    say_sender( tmp);

    if(0) {
      if(b->ban == NULL) {
	len = 0;
	say_sender("b->ban=NULL");
      } else {
	len = strlen(b->ban);
	say_sender_("len=%d b->ban=%X [%s]", len, b->ban, b->ban);
      }
      tmp[0] = '\0';
      pos = 0;
      for(j=0; j<len; j++) {
	pos += sprintf(&tmp[pos], "%X ", b->ban[j]);
	if(pos > 200) break;
      }
      say_sender( tmp);
    }
    b = b->next;
  }
  say_sender( "End of list.");
  return;
}
