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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s HELLO\nCreates a new user account in the bot. Note that there are *NO HOSTMASKS* on this bot. Other bots allow you to ident if you just know the password, so this one just uses the password and scraps the hostmask as pointless.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Creates a new user account in the bot.");
  dl->level = 0;
#if 0
  dl->func = m_hello;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_hello");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_hello(char *a)
{
  ListElem *el;
  struct dbaseuser *p;
  char message[512];
  char password[PASSLEN+1];
  int i, len;

  if(!sender_au) {
    return;
  }

  p = finduserfromname(sender_au->nick); 
  /* Not from nick, or else it would give completely the wrong answer! */
  if (p) {
    say_sender("Someone's already registered with that nick!");
    say_sender_("If it's YOUR nick type: /msg %s@%s AUTH nick password",BOT_NAME,BOT_HOSTNAME);
    return;
  }

  if(sender_au->userid != 0) {
    say_sender("You are already AUTH'd! You can't create a new account now.");
    return;
  }

  log("From: %s hello\n", sender_au->nick);

  p = (struct dbaseuser *)Malloc(sizeof(struct dbaseuser));
  memset(p, 0, sizeof(struct dbaseuser));
  len = MIN(strlen(sender_au->nick), NICKLEN);
  p->nick = Malloc(len + 1);
  strncpy(p->nick, sender_au->nick, len);
  p->nick[len] = '\0';
  /*
  strncpy(p->nick, sender_au->nick, NICKLEN);
  p->nick[NICKLEN] = '\0';
  */
  strncpy(p->password, sender_au->nick, PASSLEN);
  i = (int)(9999.0*rand()/(RAND_MAX+1.0));
  if(strlen(p->password) < (PASSLEN-4)) {
    sprintf(&p->password[strlen(p->password)], "%04d", i);
  } else {
    sprintf(&p->password[PASSLEN-4], "%04d", i);
  }
  p->password[PASSLEN] = '\0';
  p->userid = ++maxuid;
  p->privs = NULL;
  p->globalauth = 1;
  p->last_auth = time(NULL);

  strncpy(p->last_yxx, sender_au->yxx, YXXLEN);
  p->last_yxx[YXXLEN] = '\0';
  strncpy(p->last_username, sender_au->username, USERLEN);
  p->last_username[USERLEN] = '\0';

  len = MIN(strlen(sender_au->hostname), HOSTLEN);
  p->last_hostname = Malloc(len + 1);
  strncpy(p->last_hostname, sender_au->hostname, len);
  p->last_hostname[len] = '\0';
  /*
  strncpy(p->last_hostname, sender_au->hostname, HOSTLEN);
  p->last_hostname[HOSTLEN] = '\0';
  */
  /* user have now authed with this priviligies */
  sender_au->userid = p->userid;
  sender_au->auth = p->globalauth;
  sender_au->dbuser = p;   /* pointer to the database entry */

  /* add new user to hashlist, Database & authed userlist */
  el = NewElem(p);
  p->h_elem_nick = el;
  AddElem(dbusers_nick, el);

  el = NewElem(p);
  p->h_elem_lastyxx = el;
  AddElem(dbusers_lastyxx, el);

  el = NewElem(p);
  p->h_elem_userid = el;
  AddElem(dbusers_userid, el);

  el = NewElem(p);
  p->l_elem = el;
  AddTail(dbuser_list, el);

  i = (int)(9999.0*rand()/(RAND_MAX+1.0));
  sprintf(password, "abc%04d", i);

  add_userid_history(sender_au, sender_au->userid);

  sqlqlog(QLOG_HELLO,sender_au->nick,sender_au->username,sender_au->hostname,"%s %s",p->nick,p->password);
  sprintf(message,"Hello, %s. A new user account has been created for you, with nick %s.",p->nick,p->nick);
  say_sender(message);
  say_sender( "Next time you login and want to identify yourself type:");
  say_sender_("/MSG %s AUTH %s %s", ourmsghost, p->nick, p->password);
  say_sender_("Your password is initially %s. You'll need to use the NEWPASS command to change it. To change to the password '%s' then type:",
	      p->password, password);
  say_sender_("/MSG %s NEWPASS %s %s", BOT_NAME, password, password);
  say_sender("After you change your password, you have to AUTH yourself with the new password:");
  say_sender_("/MSG %s@%s AUTH %s %s", BOT_NAME, BOT_HOSTNAME, p->nick, password);

  say_sender("MOTD: We have added a new feature to Q now. To prevent problems in case you forget your login/password, two new commands are added. (EMAIL, REQUESTPASSWORD) IRCOperators will NEVER send passwords to users anymore. If you have set your E-mail you can get the password by yourself, otherwise you have to re-create a new account.");
  say_sender_("/MSG %s HELP EMAIL    /MSG %s HELP REQUESTPASSWORD", BOT_NAME, BOT_NAME);
  return;
}
