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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "List all authed users.");
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "List all authed users.");
  dl->level = 900;
#if 0
  dl->func = m_lauthed;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_lauthed");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_lauthed(char *a)
{
  struct activeuser *p;
  struct dbaseuser *d;
  ListElem *el;
  int nr_authed = 0;
  char buffer[512];
  char name[NICKLEN+2];

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

  el = GetHead(auser_list);
  while(el) { 
    p = (struct activeuser *)el->data;
    if(p->auth) {
      /* d = finduserfromid(p->userid); */
      d = p->dbuser;
      if(d) {
	strncpy(name, d->nick, NICKLEN);
	name[NICKLEN] = '\0';
      } else {
	strcpy(name, "???");
      }
      sprintf(buffer,"%15s  %15s", p->nick, name);
      say_sender(buffer);
      nr_authed++;
    }
    el = GetSucc(el);
  }
  sprintf(buffer, "%d authed users found.", nr_authed);
  say_sender(buffer);
  return;
}
