aboutsummaryrefslogtreecommitdiffstats
path: root/fripost-adduser
blob: ba97cdf56812a27b0ff083858d44bb0df0c5404e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl

use 5.010_000;
use warnings;
use strict;

=head1 NAME

fripost-adduser - Add a new mailbox to the system

=cut

our $VERSION = '0.02';

use FindBin qw($Bin);
use lib "$Bin/lib";

use Data::Dumper;
use File::Slurp qw(slurp);
use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;
use Getopt::Long;
use IO::Prompt;
use MIME::Base64;
use MIME::Lite;
use MIME::QuotedPrint;
use Template;
use YAML::Syck;

## Get command line options
our $conf = LoadFile('default.yml');

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
);

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();

    $user = {
        username => $username,
        name     => $name,
        domain   => $domain,
        maildir  => $maildir,
        active   => $active,
        password => $password,
    };

    say "User name: $user->{username}";
    say "Real name: $user->{name}";
    say "Password:  (hidden)";

    confirm_or_abort();
}

die "User already exists"
    if ($schema->resultset('Mailbox')->search({
        username => $user->{username} })->count);

## Insert user into database
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";

my $msg = MIME::Lite->new(
    From    => 'Friposts administratörer <admin@fripost.org>',
    Subject => "=?UTF-8?B?" . encode_base64('Välkommen till Fripost!' . "?=",
    Encoding => 'quoted-printable',
);

$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

Stefan Kangas C<< <skangas at skangas.se> >>

=head1 COPYRIGHT

Copyright 2010,2011 Stefan Kangas.

=head1 LICENSE

This program is free software; you can redistribute it and/or modify it
under the same terms as perl itself.

=cut