aboutsummaryrefslogtreecommitdiffstats
path: root/fripost-adduser
diff options
context:
space:
mode:
Diffstat (limited to 'fripost-adduser')
-rwxr-xr-xfripost-adduser141
1 files changed, 100 insertions, 41 deletions
diff --git a/fripost-adduser b/fripost-adduser
index 73ff70b..ba97cdf 100755
--- a/fripost-adduser
+++ b/fripost-adduser
@@ -20,34 +20,14 @@ use File::Slurp qw(slurp);
use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;
-use IO::Prompt;
use Getopt::Long;
+use IO::Prompt;
+use MIME::Base64;
use MIME::Lite;
+use MIME::QuotedPrint;
+use Template;
use YAML::Syck;
-# Prompt for user info
-sub read_user_info {
- my $username = prompt_username("New username: ");
- my $name = prompt "Full (real) name: ";
- my $domain = (split /\@/, $username)[1];
- my $maildir = "$domain/". (split /\@/, $username)[0] . "/Maildir/"; # trailing slash important
- my $active = 1;
- my $password = prompt_password();
-
- # Show the information that will be inserted
- my $user = {
- username => $username,
- name => $name,
- domain => $domain,
- maildir => $maildir,
- active => $active,
- password => $password,
- };
- print Dumper $user;
-
- return $user;
-}
-
## Get command line options
our $conf = LoadFile('default.yml');
@@ -55,23 +35,42 @@ GetOptions(
'dbi_dsn' => \$conf->{dbi_dsn},
'admuser=s' => \$conf->{admuser},
'admpass=s' => \$conf->{admpass},
+ 'debug' => \$conf->{debug},
'pretend' => \$conf->{pretend},
+ 'verbose' => \$conf->{verbose},
) or die "Unable to get command line options.";
+sub dsay { say @_ if $conf->{debug}; }
+sub vsay { say @_ if $conf->{verbose} || $conf->{debug}; }
+
# Connect to the database
my $schema = Fripost::Schema->connect(
$conf->{dbi_dsn}, $conf->{admuser}, $conf->{admpass}, {} #\%dbi_params
);
-say "Adding a new virtual user.";
+my $user;
+{
+ my $username = prompt_email("New username: ", 'is_user');
+ my $name = prompt "Full (real) name: ";
+ my $domain = (split /\@/, $username)[1];
+ my $maildir = "$domain/". (split /\@/, $username)[0] . "/Maildir/"; # trailing slash important
+ my $active = 1;
+ my $password = prompt_password();
-my $user = read_user_info();
+ $user = {
+ username => $username,
+ name => $name,
+ domain => $domain,
+ maildir => $maildir,
+ active => $active,
+ password => $password,
+ };
-ask_if_ok_or_abort();
+ say "User name: $user->{username}";
+ say "Real name: $user->{name}";
+ say "Password: (hidden)";
-if ($conf->{pretend}) {
- say "Nothing to do since we are pretending...";
- exit 0;
+ confirm_or_abort();
}
die "User already exists"
@@ -79,21 +78,81 @@ die "User already exists"
username => $user->{username} })->count);
## Insert user into database
-my $db_user = $schema->resultset('Mailbox')->new($user);
-$db_user->insert;
-say "New account $user->{username} added.";
+if ($conf->{pretend}) {
+ vsay "Did not create user since we are pretending."
+}
+else {
+ $schema->resultset('Mailbox')->new($user)->insert;
+ say "New account $user->{username} added.";
+}
+
+### Prepare sending emails
+
+my $tt = Template->new({
+ INCLUDE_PATH => "$Bin/templ",
+ INTERPOLATE => 1,
+}) || die "$Template::ERROR\n";
-## Send email
my $msg = MIME::Lite->new(
- From => 'admin@fripost.org',
- To => $user->{username},
- Subject => 'Välkommen till Fripost!',
- Data => scalar slurp('templ/new_user_mail.tt'), # TODO: actually use TT
+ From => 'Friposts administratörer <admin@fripost.org>',
+ Subject => "=?UTF-8?B?" . encode_base64('Välkommen till Fripost!' . "?=",
+ Encoding => 'quoted-printable',
);
-$msg->send();
-say "Sent welcome message: ";
-say $msg->as_string;
+$msg->attr('content-type.charset' => 'utf-8');
+
+### Send welcome email to new user
+{
+ my ($vars, $data);
+ $vars = {};
+ $tt->process('new_user_mail.tt', $vars, \$data)
+ || die $tt->error(), '\n';
+
+ $msg->replace(To => $user->{username});
+ $msg->data($data);
+
+
+ unless ($conf->{pretend}) {
+ $msg->send() unless $conf->{pretend};;
+ say "Sent welcome message.";
+ }
+ dsay "-----------------------------------";
+ dsay "| Welcome mail |";
+ dsay "-----------------------------------";
+ dsay decode_qp($msg->as_string);
+ dsay "-----------------------------------";
+}
+
+### Send login credentials to new user
+{
+ my ($vars, $data);
+ $vars = {
+ user => $user->{username},
+ pass => $user->{password},
+ real => $user->{name},
+ };
+ $tt->process('user_info.tt', $vars, \$data)
+ || die $tt->error(), '\n';
+
+ $msg->data($data);
+
+ dsay "-----------------------------------";
+ dsay "| Login credentials mail |";
+ dsay "-----------------------------------";
+ dsay decode_qp($msg->as_string);
+ dsay "-----------------------------------";
+
+ confirm_or_abort("Send email with login information? ");
+ my $to = prompt_email("Where should the email be sent? ");
+ $msg->replace(To => $to);
+ if (!$conf->{pretend}) {
+ $msg->send;
+ say "Credentials sent.";
+ }
+ else {
+ say "Pretending, will not send credentials.";
+ }
+}
=head1 AUTHOR