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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s CHANTYPESTAT [*pattern*]\nView some stats about different chantypes.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "View some stats about chantypes.");
  dl->level = 900;
#if 0
  dl->func = m_chantypestat;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_chantypestat");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_chantypestat(char *a)
{
  int nr;
  struct chanopts *c;
  struct chantype *ct;
  ListElem *el, *el1;

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

  say_sender("Id Prio Join Nr  Name");
  el = GetHead(chantype_list);
  while(el) {
    ct = (struct chantype *)el->data;

    nr = 0;
    el1 = GetHead(channel_list);
    while(el1) {
      c = (struct chanopts *)el1->data;
      if(c->type == ct->id) nr++;
      el1 = GetSucc(el1);
    }
    say_sender_("%-2d %-4d %-4d %-4d %s", ct->id, ct->priority, ct->join_burst, nr, ct->name);
    el = GetSucc(el);
  }

  nr = 0;
  el = GetHead(channel_list);
  while(el) {
    c = (struct chanopts *)el->data;
    if(c->type == 0) nr++;
    el = GetSucc(el);
  }
  say_sender_("%d undefined channels", nr, nr);

  return;
}
