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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s SERVERLIST\nShow a list of servers connected to the network. Some stats are printed too.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Show a list of servers connected to the network");
  dl->level = 990;
#if 0
  dl->func = m_serverlist;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_serverlist");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_serverlist(char *a)
{
  char tmp[256];
  int i, nr;
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s serverlist\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;
  }
  say_sender( "Known servers are:");
  for(i=0, nr=0; i<=MAX_SERVERS; i++) {
    if(servers[i].connected && (strlen(servers[i].name) > 0)) {
      nr++;
      sprintf(tmp, "%2d: %4d %-30s Users: %5d (%2d op) %6.2f%%", nr, i,
	      servers[i].name, servers[i].users, servers[i].opers,
	      100.0*(float)servers[i].users/(float)servers[MAX_SERVERS].users);
      say_sender( tmp);
    }
  }
  say_sender("Done.");
  return;
}
