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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s KILL <victim's nick> <reason>\nMakes the bot issue a /KILL for a given user. This allows opers to disconnect a user as a replacement to /KILL command.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Disconnects a user from the IRC network");
  dl->level = 950;
#if 0
  dl->func = m_kill;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_kill");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_kill(char *a)
{
  char victim[NICKLEN+1];
  char pyxx[YXXLEN+1];
  char reason[256+1];
  char reason_test[10+1];
  int i, start;
  struct activeuser *u;
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s kill [%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;
  strncpy(victim,getword(a,&i),NICKLEN);
  victim[NICKLEN] = '\0';
  if(!strlen(victim)) {
    say_sender("No nickname was given.");
    return;
  }

  start = i+1;
  strncpy(reason_test, getword(a,&i), 10);
  reason_test[10] = '\0';
  if(!strlen(reason_test)) {
    say_sender("No reason was given.");
    return;
  }
  strncpy(reason, &a[start], 256);
  reason[256] = '\0';

  if((u = nick_to_yxx(victim, pyxx))) { 
    putserver("%c%c D %s :%s\n\r", yxx[0], yxx[1], pyxx, reason);
    disconnect_auser(u);
  } else {
    say_sender( "Couldn't find that user.");
  }
  say_sender("Done.");
  return;
}
