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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s SUSPENDLIST\nList all suspended channels, reasons and who decided it.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "List all suspended channels.");
  dl->level = 990;
#if 0
  dl->func = m_suspendlist;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_suspendlist");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_suspendlist(char *a)
{
  int nr;
  ListElem *el;
  char tmp[512];
  struct chanopts *c;
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s suspendlist\n", db->nick);
  } else
    return;
  if(!is_oper()) {
    say_sender("You are not an Oper.");
    log("To: %s Is not an Oper\n", db->nick);
    return;
  }

  nr = 0;
  el = GetHead(channel_list);
  while(el) {
    c = (struct chanopts *)(el->data);
    if(c->suspended) {
      char date_str[32+1];
      struct tm *tm_;
      struct dbaseuser *d;
      d = finduserfromid(c->suspend_who);
      tm_ = localtime(&c->suspend_time);
      strftime(date_str, 32, "%c", tm_);
      sprintf(tmp, "[%s] [%s] [%s] [%s]", c->channel, (d ? d->nick : "???"),
	      date_str, (c->suspend_reason ? c->suspend_reason : "???"));
      say_sender( tmp);
      nr++;
    }
    el = GetSucc(el);
  }
  sprintf(tmp, "%d channels suspended", nr);
  say_sender(tmp);
  return;
}
