aboutsummaryrefslogtreecommitdiff
path: root/lib/App/Acmeman/Domain.pm
blob: 46c3f1d3385156839519f3beeda3b8a3106f0e80 (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
package App::Acmeman::Domain;

use strict;
use warnings;
use Carp;
use Clone;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(CERT_FILE KEY_FILE CA_FILE);
our %EXPORT_TAGS = ( files => [ qw(CERT_FILE KEY_FILE CA_FILE) ] );
our $VERSION = '1.00';

use constant {
    CERT_FILE => 0,
    KEY_FILE => 1,
    CA_FILE => 2
};

sub uniq {
    my %h;
    map {
	if (exists($h{$_})) {
	    ()
	} else {
	    $h{$_} = 1;
	    $_
        }
    } @_; 
}

sub new {
    my ($class, %args) = @_;
   
    my $self = bless { }, $class;
    my $v;

    if ($v = delete $args{cn}) {
	$self->{_cn} = lc $v;
    }
    if ($v = delete $args{alt}) {
	if (ref($v) eq 'ARRAY') {
	    $self->{_alt} =  [ uniq(map { lc } @$v) ];
	} else {
	    $self->{_alt} = [ lc $v ];
	}
    }
    if ($v = delete $args{type}) {
	$self->{_cert_type} = $v;
    }
    if (($v = delete $args{certificate_file})
	|| ($v = delete $args{'certificate-file'})) {
	$self->{_file}[CERT_FILE] = $v;
    }
    if (($v = delete $args{ca_file})
	|| ($v = delete $args{'ca-file'})) {
	$self->{_file}[CA_FILE] = $v;
    }
    if (($v = delete $args{key_file})
	|| ($v = delete $args{'key-file'})) {
	$self->{_file}[KEY_FILE] = $v;
    }

    $v = delete($args{argument}) || '$domain';
    $v =~ s{\$}{\\\$};
    $self->{_argument} = qr($v);

    $self->{_postrenew} = delete $args{'postrenew'};
    
    croak "unrecognized arguments" if keys %args;
    return $self;
}

sub names {
    my $self = shift;
    if (wantarray) {
	return ($self->cn, $self->alt);
    } else {
	return $self->alt() + 1;
    }
}

sub contains {
    my $self = shift;
    my $val = lc shift;
    return grep { $_ eq $val } $self->names;
}   

sub _domain_cmp {
    my ($a,$b) = @_;

    carp "righthand-side argument should be a App::Acmeman::Domain"
	unless $b->isa('App::Acmeman::Domain');
    return $a->{_cn} cmp $b->{_cn};
}

sub _domain_plus {
    my ($a, $b) = @_;
    
    carp "righthand-side argument should be a App::Acmeman::Domain"
	unless $b->isa('App::Acmeman::Domain');

    $a = Clone::clone($a);
    push @{$a->{_alt}}, $b->cn
	unless $a->contains($b->cn);
    @{$a->{_alt}} = uniq($a->alt, $b->alt);
    return $a;
}
    
use overload
    cmp => \&_domain_cmp,
    '+' => \&_domain_plus,
    '""' => sub { $_[0]->cn };

sub cn {
    my $self = shift;
    return $self->{_cn};
}

sub alt {
    my $self = shift;
    if (wantarray) {
	return exists($self->{_alt}) ? (@{$self->{_alt}}) : ();
    } else {
	return exists($self->{_alt}) ? (0+@{$self->{_alt}}) : 0;
    }
}

sub file {
    my ($self, $type) = @_;
    return undef unless exists($self->{_file}[$type]);
    my $res = $self->{_file}[$type];
    $res =~ s{$self->{_argument}}{$self->cn}ge;
    return $res;
}

sub certificate_file {
    my $self = shift;
    return $self->file(CERT_FILE);
}

sub postrenew {
    my $self = shift;
    return $self->{_postrenew}
}

1;

Return to:

Send suggestions and report system problems to the System administrator.