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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s DELUSER <user's nick>\nDeletes someone completely from all the bots databases. This is a permanent deletion, there is no undo, and all of the info on that user - ALL of it - is lost. Do not use without due cause.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Deletes a user from the database");
  dl->level = 950;
#if 0
  dl->func = m_deluser;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_deluser");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_deluser(char *a)
{
  int i, userid;
  struct activeuser *u;
  ListElem *el;
  struct chanpriv *p;
  struct dbaseuser *d;
  char victim[NICKLEN+2];
  char channel[CHANNELLEN+1];


  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s deluser [%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(victim,getword(a,&i),NICKLEN+1);
  victim[NICKLEN+1] = '\0';
  if(!strlen(victim)) {
    say_sender( "You forgot to type the nickname");
    say_sender_( "/msg %s DELUSER nickname", BOT_NAME);
    return;
  }
  if(victim[0]!='#') {
    d=finduserfromnick(victim);
  } else {
    d=finduserfromname(victim+1);
  }
  if(d) {
    userid = d->userid;  // save value before it's deallocated
    p = d->privs;
    while(p) {
      strncpy(channel, p->channel, CHANNELLEN);
      channel[CHANNELLEN] = '\0';
      d->privs = p->next;
      Free(p);
      p = d->privs;
      /* eventually remove the channel if last user on it */
      if(is_empty_channel(channel)) {
	delchannel(channel);
      }
    }

    if(d->h_elem_nick) {
      Remove(d->h_elem_nick);
      FreeElem(d->h_elem_nick);
      d->h_elem_nick = NULL;
    }

    if(d->h_elem_lastyxx) {
      Remove(d->h_elem_lastyxx);  /* this cause coredump ? */
      FreeElem(d->h_elem_lastyxx);
      d->h_elem_lastyxx = NULL;
    }

    if(d->h_elem_userid) {
      /* is perhaps not set to save memory */
      Remove(d->h_elem_userid);
      FreeElem(d->h_elem_userid);
      d->h_elem_userid = NULL;
    }
    if(d->l_elem) {
      /* this must be done last... will free lots of things */
      Remove(d->l_elem);
      FreeElem(d->l_elem);
      d->l_elem = NULL;
    }

    /* slow search throgh all ausers and remove their auth */
    if (userid) {
      el = GetHead(auser_list);
      while(el) {
	u = (struct activeuser *)el->data;
	if(u->userid == userid) {
	  u->auth = 0;
	  u->userid = 0;
	  u->dbuser = NULL;
	}
	el = GetSucc(el);
      }
    }
    say_sender("Done.");
  } else {
    say_sender("Can't find that person.");
  }
  return;
}
