/*
 * cleanupstat.c
 * (C) Christian Magnusson 2001, 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"

#define UNKNOWN_OWNER "\02???\02"

void m_cleanupstat(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s CLEANUPSTAT #channel    only stat for #channel\n"\
	   "/msg %s CLEANUPSTAT 4           <=4 visits/week\n"\
	   "/msg %s CLEANUPSTAT 0 10        0-10 visists/week\n"
	   , BOT_NAME, BOT_NAME, BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Give stats and timestamps for channels.");
  dl->level = 999;
#if 0
  dl->func = m_cleanupstat;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_cleanupstat");
#endif
  return 1;
}

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

#define MAXAGE 100

MODULE_PUBLIC void m_cleanupstat(char *a)
{
  int i, d;
  ListElem *el;
  time_t t_now = time(NULL);
  int nr_chan[MAXAGE+1];
  struct chanopts *c;

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

  for(i=0; i<=MAXAGE; i++) nr_chan[i] = 0;
  el = GetHead(channel_list);
  while(el) {
    c = (struct chanopts *)el->data;
    if(c->last_joined)
      d = (t_now - c->last_joined) / (3600*24);
    else
      d = 0;
    if(d>MAXAGE) d = MAXAGE;
    else if(d<0) d = 0;
    nr_chan[d]++;
    el = GetSucc(el);
  }
  for(i=0; i<MAXAGE; i++) {
    say_sender_("%2d days old: %d", i, nr_chan[i]);
  }
  say_sender_("More than %2d days: %d", i, nr_chan[MAXAGE]);
  return;
}
