/*
 * users.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"

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

void m_users(char *a);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s USERS #channel    show users on a channel\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Show users on a channel.");
  dl->level = 950;
#if 0
  dl->func = m_users;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_users");
#endif
  return 1;
}

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

#if 0
static void inc_channel(struct chan_stat *list, char *channel)
{
  struct chan_stat *c, *lc, *newc;
  lc = list;
  c = list->next;
  while(c) {
    if(strcasecmp(c->channel, channel)==0) {
      c->count++;
      return;
    }
    lc = c;
    c = c->next;
  }
  newc = (struct chan_stat *)Malloc(sizeof(struct chan_stat));
  if(newc) {
    strncpy(newc->channel, channel, CHANNELLEN);
    newc->channel[CHANNELLEN] = '\0';
    newc->count = 1;
    newc->next = NULL;
    lc->next = newc;
  }
  return;
}
#endif

#define MAX_LEN 450

MODULE_PUBLIC void m_users(char *a)
{
  struct chan_stat first_chanstat;
  struct chanmod *c;
  struct activeuser *u;
  char channel[CHANNELLEN+1];
  ListElem *el;
  int i;
  char user_op[MAX_LEN+NICKLEN+20];
  char user_noop[MAX_LEN+NICKLEN+20];
  char user_oper[MAX_LEN+NICKLEN+20];
  char user_nooper[MAX_LEN+NICKLEN+20];
  char user_authed[MAX_LEN+NICKLEN+20];
  char user_noauthed[MAX_LEN+NICKLEN+20];
  int user_op_len, user_noop_len, user_oper_len, user_nooper_len, user_authed_len, user_noauthed_len;
  int matches;


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

  i=0;
  /*
    This function MUST be fixed and clean up everything from
    all channels, and in all lists.
  */
  strncpy(channel,getword(a,&i),CHANNELLEN);
  channel[CHANNELLEN] = '\0';
  if(!strlen(channel)) {
    say_sender( "You forgot to type the #channel");
    say_sender_( "/msg %s USERS #channel    show users on a channel", BOT_NAME);
    return;
  }

  say_sender_("Users on channel: %s", channel);

  /* first post is not used actually */
  first_chanstat.next = NULL;
  first_chanstat.count = 0;
  first_chanstat.channel[0] = '\0';

  matches = 0;

  user_op[0] = '\0';
  user_op_len = 0;
  user_noop[0] = '\0';
  user_noop_len = 0;
  user_oper[0] = '\0';
  user_oper_len = 0;
  user_nooper[0] = '\0';
  user_nooper_len = 0;
  user_authed[0] = '\0';
  user_authed_len = 0;
  user_noauthed[0] = '\0';
  user_noauthed_len = 0;

  el = GetHead(auser_list);
  while(el) {
    u = (struct activeuser *)el->data;
    c = u->chanmodes;
    while(c) {
      if(
#if 0
	 !match(channel, c->channel)
#else
	 (strcasecmp(channel, c->channel) == 0)
#endif
	 ) {
	/*
	  inc_channel(&first_chanstat, c->channel);
	*/
	if(matches++ < 100) {
	  /*
	    this will only list people who are authed
	  */
	  if((c->mode & (1<<('o'-'a')))) {
	    if(user_op_len<MAX_LEN)
	      user_op_len += sprintf(&user_op[user_op_len], "%s ", u->nick);
	  } else {
	    if(user_noop_len<MAX_LEN)
	      user_noop_len += sprintf(&user_noop[user_noop_len], "%s ", u->nick);
	  }

	  if(u->oper) {
	    if(user_oper_len<MAX_LEN)
	      user_oper_len += sprintf(&user_oper[user_oper_len], "%s ", u->nick);
	  } else {
	    if(user_nooper_len<MAX_LEN)
	      user_nooper_len += sprintf(&user_nooper[user_nooper_len], "%s ", u->nick);
	  }
	  if(u->userid) {
	    if(user_authed_len<MAX_LEN)
	      user_authed_len += sprintf(&user_authed[user_authed_len], "%s ", u->nick);
	  } else {
	    if(user_noauthed_len<MAX_LEN)
	      user_noauthed_len += sprintf(&user_noauthed[user_noauthed_len], "%s ", u->nick);
	  }

	  if(matches == 100) {
	    say_sender("Warning: Stop due to 100 matches.");
	  }
	}
      }
      c = c->next;
    }
    el = GetSucc(el);
  }
  say_sender_("%s%s", "Have @: ", user_op);
  say_sender_("%s%s", "Have NO @: ", user_noop);
  say_sender_("%s%s", "Is Oper: ", user_oper);
  say_sender_("%s%s", "Is NO Oper: ", user_nooper);
  say_sender_("%s%s", "Is Authed: ", user_authed);
  say_sender_("%s%s", "Is NOT Authed: ", user_noauthed);
  return;
}
