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

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

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s REQUESTPASSWORD <user@host.com>\nGet your password mailed to your E-mail address in case you have lost it.\n"
	   , BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Request your password via E-mail");
  dl->level = 0;
#if 0
  dl->func = m_requestpassword;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_requestpassword");
#endif
  return 1;
}

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

MODULE_PUBLIC void m_requestpassword(char *a)
{
  FILE *fp;
  char cmd[512];
  int i, nr = 0;
  long diff;
  time_t t1;
  char email1[EMAILLEN+1];
  ListElem *el;
  struct dbaseuser *u;

  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s requestpassword [%s]\n", db->nick, a);
  } else {
    if(!sender_au) {
      say_sender("Couldn't locate you... internal error");
      return;
    }
    log("From: %s requestpassword (unauthed) [%s] %s@%s\n",
	sender_au->nick, a, sender_au->username, sender_au->hostname);
  }

  /*
  if(sender_au->userid==0) {
    say_sender("You need to AUTH first.");
    return;
  }
  */

  i=0;
  strncpy(email1,getword(a,&i),EMAILLEN);
  email1[EMAILLEN] = '\0';
  if(!email1[0]) {
    say_sender("You forgot to type your E-mail address");
    say_sender_("/MSG %s REQUESTPASSWORD user@mymailhost.xx", BOT_NAME);
    return;
  }

  t1 = time(NULL);
  el = GetHead(dbuser_list);
  while(el) {
    u = (struct dbaseuser *)el->data;
    if(u->email) {
      if(!strcasecmp(u->email, email1)) {
#define MAX_RESEND_TIME 24*3600L
	//#define MAX_RESEND_TIME 30L
	diff = MAX_RESEND_TIME + u->last_email_request - t1;
	if(diff > MAX_RESEND_TIME) diff = MAX_RESEND_TIME;
	if(diff > 0L) {
	  if(diff > 3600L) {
	    say_sender_("Sorry. Your password have already been requested. You have to wait %.1f hours", ((float)diff) / ((float)3600));
	  } else {
	    say_sender_("Sorry. Your password have already been requested. You have to wait %d seconds.", diff);
	  }
	} else {
	  u->last_email_request = t1;
	
	  sprintf(cmd, "%s %s", SENDMAIL, email1);
	  if((fp = popen(cmd, "w")) == NULL) {
	    say_sender("Error sending E-Mail. Please contact an IRCOperator.");
	    log("Error popen(SENDMAIL)\n");
	    return;
	  }
	  fprintf(fp, "From: %s <QBot@quakenet.eu.org>\r\n", BOT_NAME);
	  fprintf(fp, "Subject: Request of user info\r\n\r\n");
	  fprintf(fp, "\r\nYour login/password is:\r\n\r\n"\
		  "Login:    %s\r\n"\
		  "Password: %s\r\n", u->nick, u->password);
	  fprintf(fp, "\r\nTo auth yourself to %s, type the following command\r\n", BOT_NAME);
	  fprintf(fp, "\r\n   /MSG %s@%s AUTH %s %s\r\n", BOT_NAME, BOT_HOSTNAME, u->nick, u->password);
	  fprintf(fp, "\r\n--\r\nThis message was generated by %s at the IRC network QuakeNet.eu.org\r\n\r\n", BOT_NAME);
	  pclose(fp);
	  say_sender_("OK, Login/Password sent to %s\n", email1);
	  nr++;
	}
      }
    }
    el = GetSucc(el);
  }

  if(!nr) {
    say_sender("The E-Mail address wasn't found! Perhaps you haven't set your E-mail address?");
  }
  return;
}
