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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s GETPASS <nickname>\nShow a user's password. You can only show normal user's passwords, not opers.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Show a user's password");
  dl->level = 900;
#if 0
  dl->func = m_getpass;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_getpass");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_getpass(char *a)
{
  int i;
  struct dbaseuser *p;
  char tmp[256];
  char victim[NICKLEN+2];

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s getpass [%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+1);
  victim[NICKLEN+1] = '\0';
  if (!strlen(victim)) {
    say_sender("You forgot to type the nickname.");
    say_sender_("/msg %s GETPASS <nickname>", BOT_NAME);
    return;
  }
  if (victim[0]!='#') {
    p=finduserfromnick(victim);
  } else {
    p=finduserfromname(victim+1);
  }
  if(!p) {
    say_sender( "Couldn't find that user in database.");
    return;
  }
  if(p->globalauth >= 100) {
    say_sender( "User has authlevel >= 100, and could not be showed.");
    return;
  }
  sprintf(tmp, "Password is currently: %s\n", p->password);
  say_sender( tmp);
  return;
}
