/*
 * mkpasswd.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 <unistd.h>

#include "settings.h"
#include "bottypes.h"
#include "globals.h"
#include "dl.h"

void m_mkpasswd(char *a);

char *crypt(const char *key, const char *salt);

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s MKPASSWD password\nCreate a crypt-string from a plain text password.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Create a crypt-string");
  dl->level = 100;
#if 0
  dl->func = m_mkpasswd;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_mkpasswd");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_mkpasswd(char *a)
{
  int i;
  char password1[PASSLEN+1];
  char salt[3];
  char *str;
  char sc[64] = {
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 
    'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 
    'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
    'y', 'z', 
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
    'Y', 'Z', 
    '0', '1', '2', '3', '4', '5', '6', '7', 
    '8', '9',
    '.', '/'
  };

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s mkpasswd [%s]\n", db->nick, a);
  } else {
    return;
  }

  i=0;
  strncpy(password1,getword(a,&i),PASSLEN);
  password1[PASSLEN] = '\0';
  if(!strlen(password1)) {
    say_sender( "You forgot to type a string.");
    say_sender_( "/msg %s MKPASSWD secret", BOT_NAME);
    return;
  }

  /* salt should be  [a-zA-Z0-9./]
  */
  salt[0] = sc[(int)(64.0*rand()/(RAND_MAX+1.0))];
  salt[1] = sc[(int)(64.0*rand()/(RAND_MAX+1.0))];
  salt[2] = '\0';

  str = crypt(password1, salt);
  say_sender_("Crypt for [%s] is %s", password1, str);
  return;
}
