legend/
legend/area/
legend/player/
/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Merc Diku Mud, you must comply with   *
 *  both the original Diku license in 'license.doc' as well the Merc       *
 *  license in 'license.txt'.  In particular, you may not remove either of *
 *  these copyright notices.                                               *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/
/***************************************************************************
 *  God Wars Mud copyright (C) 1994, 1995, 1996 by Richard Woolcock        *
 *                                                                         *
 *  Legend of Chrystancia copyright (C) 1999, 2000, 2001 by Matthew Little *
 *  This mud is NOT to be copied in whole or in part, or to be run without *
 *  the permission of Matthew Little. Nobody else has permission to        *
 *  authorise the use of this code.                                        *
 ***************************************************************************/


#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "merc.h"
#include "build.h"

void initialise_email_list()
{
    FILE *fp;

    email_list = NULL;
    
    if ((fp = fopen(EMAIL_DB,"r"))==NULL)
        return;
    
    for ( ; ; )
    {
        EMAIL_DATA *s;
        char letter;
    
        if ( (letter = fread_letter( fp ) ) == '~' )
            break;   
        
        s = alloc_perm( sizeof(*s) );
        
        s->address = fread_string( fp );
        s->next = email_list;
        email_list = s;
    }
        
    fclose( fp );
}
        
void add_email_to_list(char *address)
{
    EMAIL_DATA *s;   
    FILE *fp;

    if (address == NULL
        || !str_cmp(address, "END"))
        return;
        
    s = alloc_perm( sizeof(*s) );
        
    s->address   = str_dup(address);
    s->next   = email_list;
    email_list = s;
    
    if ((fp = fopen(EMAIL_DB,"w"))==NULL)
    {
        bug("Error saving email.list!",0);
        return;
    }
            
    for (s = email_list; s != NULL; s = s->next)
        fprintf(fp, "-%s~\n", s->address);
    fprintf(fp, "~");
        
    fclose( fp );
}
bool check_email(char *address)
{
    EMAIL_DATA *em;
        
    for (em = email_list; em != NULL; em = em->next)
        if (!str_cmp(em->address, address)) return TRUE;
    
    return FALSE;
}
void do_email_list( CHAR_DATA *ch, char *argument )
{
    emailing_to = email_list;
}
        
void email_next()
{
    char buf[MAX_STRING_LENGTH];
            
    if (emailing_to == NULL || dice(1, 10) != 4) return;
 
    sprintf(buf, "AutoEmailing %s", emailing_to->address);
    log_string(buf, NULL);
    sprintf(buf, "mail -s 'The Oracle - Automailer' %s < email.mesg&", emailing_to->address);
    system(buf);
    emailing_to = emailing_to->next;
}

void email_character( CHAR_DATA *ch )
{
    FILE *fp;
    char buf[MAX_STRING_LENGTH];
    
    if (ch == NULL || ch->email_set != 1 || IS_NPC(ch)) return;
    
    if ((fp = fopen("email.tmp","w"))==NULL)
        return;
        
    fprintf(fp, "This is an automatic email from the MUD The Oracle\n\n");
    fprintf(fp, "Character Registration Information:\n\n");
    fprintf(fp, "Name: %s    Registration Key: %d\n\n", ch->name, ch->registration_key);
    fprintf(fp,  "This email was generated by : %s\n\n", ch->desc->host);
    fprintf(fp, "\nIf you have received this email in error please ignore.\n");    
    fclose( fp );
        
    sprintf(buf, "mail -s 'The Oracle - Automailer' %s < email.tmp&",ch->pcdata->email);
    system(buf);
}

void do_register(CHAR_DATA *ch, char *argument)
{
    char arg[MAX_INPUT_LENGTH];

    if (IS_NPC(ch)) return;

    if (ch->registerred == 1 && !IS_IMMORTAL(ch)) 
    {
	stc("You have already registerred.\n\r",ch);
	return;
    }


    if (ch->registration_key < 100000 && !IS_IMMORTAL(ch)) ch->registerred = 0;

    if (ch->pcdata->email == NULL)
    {
	stc("You must use the email command to enter your email address first.\n\r",ch);
	return;
    }

    if (argument[0] == '\0')
    {
	stc("Syntax: register email <address>\n\r",ch);
	stc("        register key <key code>\n\r",ch);
	if (IS_IMMORTAL(ch))
	    stc("        register player <name>\n\r",ch);
	return;
    }

    argument = one_argument(argument, arg);

    if (arg[0] == '\0' ||
	(str_prefix(arg, "email") && str_prefix(arg, "key")
	&& (!IS_IMMORTAL(ch) || str_prefix(arg, "player"))))
    {
	stc("Syntax: register email <address>\n\r",ch);
	stc("        register key <key code>\n\r",ch);
	if (IS_IMMORTAL(ch))
	    stc("        register player <name>\n\r",ch);
	return;
    }

    if (!str_prefix(arg, "player"))
    {
	CHAR_DATA *wch;

	if (argument[0] == '\0')
	{
	    stc("Overide Registration on which character?\n\r",ch);
	    return;
	}

	if ((wch = get_char_world(ch, argument)) != NULL)
	{
	    wch->registerred = 1;
	    wch->email_set = 2;
	    save_char_obj( wch );
	    send_to_char("Character Registerred.\n\r",wch);
	    send_to_char("You can now save.\n\r",wch);
	    stc("They have now been registerred.\n\r",ch);
	    return;
	}
	else
	{
	    stc("They are not playing at the moment.\n\r",ch);
	    return;
	}
    }

    if (!str_prefix(arg, "email"))
    {
	if (ch->email_set != 0)
	{
	    stc("You have already registerred your email address.\n\r",ch);
	    return;
	}

	if (!str_cmp(argument, ch->pcdata->email))
	{
//	    if (check_email(argument))
//	    {
//		stc("That email address has already been taken.\n\r",ch);
//		return;
//	    }
	    while (ch->registration_key < 100000)
	    {
	   	ch->registration_key = dice(1,899999) + 100000;
	    }
	    ch->email_set = 1;
	    email_character( ch );
	    ch->registerred = 3;
	    save_char_obj( ch );
	    stc("Email address registerred.  Registration Email sent.\n\r",ch);
	    stc("Your character has been saved.\n\r",ch);
	    return;
	}
	else
	{
	    stc("Your two entered email addresses differ.\n\r",ch);
	    stc("The one entered using the email command is:\n      ",ch);
	    stc(ch->pcdata->email,ch);
	    stc("\n\rEither correct using the email command, or enter this address correctly\n\rwith the register command.\n\r",ch);
	    return;
	}
    }

    if (!str_prefix(arg, "key"))
    {
	char buf[MAX_INPUT_LENGTH];

	if (ch->registration_key < 100000)
	{
	    stc("Invalid Registration Key.\n\r",ch);
	    stc("Please re-register.\n\r",ch);
	    ch->email_set = 0;
	    return;
	}

	sprintf(buf, "%d", ch->registration_key);

	if (!str_cmp(argument, buf))
	{
	    ch->registerred = 1;
	    ch->email_set = 2;
	    save_char_obj( ch );
	    add_email_to_list( ch->pcdata->email );
	    send_to_char("Character Registered.\n\r",ch);
	    send_to_char("You can  now save.\n\r",ch);
	}
	else
	{
	    stc("The registration key you have entered is not correct.\n\r",ch);
	}
    }
}


void do_reregister(CHAR_DATA *ch, char *argument)
{
    CHAR_DATA *vch;

    if (argument[0] == '\0')
    {
	stc("Syntax:  reregister <character>\n\rUsed if they used an invalid email account for registerring.\n\r", ch);
	return;
    }

    if ((vch = get_char_world(ch, argument)) == NULL)
    {
	stc("They are not logged on.\n\r",ch);
	return;
    }

    vch->registerred = 0;
    vch->email_set = 0;
    stc("They can now register.\n\r",ch);
    stc("You can now attempt to register again.\n\r",vch);
}