#!/usr/bin/perl -wT
# -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Referrer Tracking: Registration

use strict;
use CGI::Carp;
use HTML::Entities;
use lib 'lib';

use vars '@ISA';
@ISA = qw(TheCommon);
require TheCommon;

sub main {
    my $self = shift;

    # /register?email=<e>&name=<name>
    #    if either email or name is blank, show form, exit
    #    check email doesn't already exist
    #    if email exists, say "this e-mail is already registered, e-mail has been sent", send e-mail, and exit
    #    make new password, new id
    #    store email, id, password
    #    if user has cookie, mark cookie as "has user"
    #    send e-mail to email with link to script with email and password
    #    show page telling user to read e-mail and exit

    my $email = $self->{query}->param('email');
    my $password; # generated later
    my $id; # generated later
    my $name = $self->{query}->param('name');

    # if either email or name is blank, show form, exit
    unless (defined $email and defined $name) {
        foreach ($email, $name) {
            if (defined $_) {
                $_ = encode_entities($_);
            } else {
                $_ = '';
            }
        }
        $self->{http}->page('Registration');
        $self->{http}->print(<<eof);
   <p>To register you need to provide us with your e-mail address and
   your name.</p>
   <p>Your e-mail address is only used to send you e-mails when you
   win a prize, or when you forget your password. We won't give it to
   anyone else, and that's a promise. Unless you really annoy us, in
   which case we might give your e-mail address to a few pornography
   spammers. It's also what you use to log in. Your name is used when
   we display your amazing number of referrals in the high score
   table. You can use whatever you want as your name, but if it is
   insulting, or annoys us in some way, well, see above.</p>
   <form action="" method="post">
    <p><label>Your e-mail address: <input type="text" name="email" value="$email"></label></p>
    <p><label>Your name: <input type="text" name="name" value="$name"></label></p>
    <p><input type="submit" value="Register"></p>
   </form>
eof
        return;                                                  # unstick fontlock: '
    }
    
    # check email doesn't already exist
    # if email exists, say "this e-mail is already registered, e-mail has been sent", send e-mail, and exit
    my($Epassword, $Eid, $Ename) = $self->{database}->getUser($email);
    if (defined($Eid)) {

        # send e-mail
        my $smtp = TheSMTP->new($self->{config}->{smtp}, $self->{config}->{from}, $email);
        $smtp->print("Subject: Your $self->{config}->{name} password\n");
        $smtp->print("From: $self->{config}->{name} <$self->{config}->{from}>\n");
        $smtp->print("To: $Ename <$email>\n");
        $smtp->print("\n");
        $smtp->print("Your $self->{config}->{name} account details are:\n");
        $smtp->print("\n");
        $smtp->print("   Username: $email\n");
        $smtp->print("   Password: $Epassword\n");
        $smtp->print("\n");
        $smtp->print("Hope that helps,\n");
        $smtp->print("-- \n");
        $smtp->print("Secretary for the $self->{config}->{name}");

        # tell user
        $email = encode_entities($email);
        $name = encode_entities($name);
        $self->{http}->page('Registration');
        $self->{http}->print(<<eof);
   <p>The e-mail address you gave (<code>$email</code>) is already
   registered for $Ename. I've sent the account's password to that
   address.</p>
eof
        # unstick fontlock: ;

        # exit
        return;
    }

    # user doesn't already exist
    # make new password, new id
    # store email, id, password
    $password = $self->newPassword();
    $id = $self->{database}->newUser($email, $password, $name);

    # if user has cookie, mark cookie as "has user"
    my $cookie = $self->getCookie();
    if (defined $cookie) {
        $self->{database}->updateReferralSetUser($cookie, $id);
    }

    # send e-mail to email with link to script with email and password
    my $smtp = TheSMTP->new($self->{config}->{smtp}, $self->{config}->{from}, $email);
    $smtp->print("Subject: Welcome to $self->{config}->{name}\n");
    $smtp->print("From: $self->{config}->{name} <$self->{config}->{from}>\n");
    $smtp->print("To: $name <$email>\n");
    $smtp->print("\n");
    $smtp->print("Your $self->{config}->{name} account details are:\n");
    $smtp->print("\n");
    $smtp->print("   Username: $email\n");
    $smtp->print("   Password: $password\n");
    $smtp->print("\n");
    $smtp->print("You can log in to this site by going to the following page:\n");
    $smtp->print("\n");
    $smtp->print("   $self->{config}->{home}\n");
    $smtp->print("\n");
    $smtp->print("Hope that helps,\n");
    $smtp->print("-- \n");
    $smtp->print("Secretary for the $self->{config}->{name}");

    # show page telling user to read e-mail and exit
    $email = encode_entities($email);
    $self->{http}->page('Registration Complete');
    $self->{http}->print(<<eof);
   <p>An e-mail containing your account details and brief instructions
   on what to do now has been sent to the e-mail address you provided
   (<code>$email</code>).</p>
eof

}
