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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s MKSECUREPASS 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 = 1000;
#if 0
  dl->func = m_mksecurepass;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_mksecurepass");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_mksecurepass(char *a)
{
  int i;
  char password1[PASSLEN+1];
  char *str;
  char str2;
  unsigned char *qcryptresults;
  int sizeofresult;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s mksecurepass [%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 MKSECUREPASS password", BOT_NAME);
    return;
  }
  qcrypt(password1,qcryptresults,&sizeofresult);
  sprintf(str,"%.2x",qcryptresults[0]);
  for (i = 1; i < sizeofresult-1; i++) {
   sprintf(str2,"%.2x",qcryptresults[i]);
   strcat(str,str2);
  }
   
  say_sender_("SecureKey for [%s] is %s", password1, str);
  return;
}
