summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Model/ReadMails/Role.pm
blob: 466e3b095fc96041377685ca85d92c1c72882873 (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
package JWebmail::Model::ReadMails::Role;

use Params::Check 'check';

use Mojo::Base -role; # load after imports


package JWebmail::Model::ReadMails::Role::Shadow {

    use overload '""' => sub { '***' };

    sub new { shift; bless \(shift.'') }

    sub show_password { ${shift()} }

}

sub Auth {
    shift;
    state $AuthCheck = {
        user =>      {defined => 1, required => 1},
        password =>  {defined => 1, required => 1},
        challenge => {},
    };
    my $self = @_ == 1 ? $_[0] : {@_};

    my $res = check($AuthCheck, $self, 0) || die Params::Check::last_error;
    $res->{password} = JWebmail::Model::ReadMails::Role::Shadow->new($res->{password});
    return $res;
}

my @methods = (
    # name:type  parmeter of type
    # *key       key => value
    # key=value  default argument of value
    # ^          throws exception
    # ^type      throws exception of type
    # Read operations
    'verify_user',      # auth:Auth -> :truthy
    'read_headers_for', # auth:Auth, *folder='', *start=0, *end=24, *sort='date' -> ^ :hashref
    'count',            # auth:Auth, folder -> ^ size:int count:int new:int
    'show',             # auth:Auth, mid -> ^ :hashref
    'search',           # auth:Auth, pattern, folder -> ^ :hashref
    'folders',          # auth:Auth -> ^ :arrayref
    # Write operations
    'move',             # auth:Auth, mid, folder -> ^ 1
);

requires(@methods);

around @methods => sub {
    my $orig = shift;
    my $safe = $_[1]->{password};
    $_[1]->{password} = $safe->show_password;
    my @res;
    my $succ = eval { @res = $orig->(@_); 1 };
    $_[1]->{password} = $safe;
    die unless $succ;
    return wantarray ? @res : $res[$#res];
};

around read_headers_for => sub {
    my $orig = shift;
    my $self = shift;
    my $auth = shift;
    my $args = {@_};

    state $ArgsCheck = {
        start =>  {default => 0},
        end =>    {default => 24},
        sort =>   {default => 'date'},
        folder => {default => ''},
    };

    $orig->($self, $auth, %{ check($ArgsCheck, $args, 1) })
};


1

__END__

=encoding utf-8

=head1 NAME

ReadMails::Role - Interface to a repository of mails

=head1 SYNOPSIS

    my $m = Some::Implementation->with_role('JWebmail::Model::ReadMails::Role');
    $m->search($auth, qr/Hot singles in your area/, '');

=head1 DESCRIPTION

The communication is assumed to be stateless.

=head1 INTERFACE

=head2 verify_user

Checks user name and password.

=head2 read_headers_for

Provides bundeled information on a subset of mails of a mailbox.
Can be sorted and of varying size.

Arguments:
    start..end   inclusive 0 based range

=head2 count

Returns size of the mail box folder in bytes the number of mails.

=head2 show

Returns a sepecific mail as a perl hash.

=head2 search

Searches for a message with the given pattern.

=head2 folders

List all mailbox sub folders.

=head2 move

Move mails between folders.

=head2 Auth

A sub that returns a hashref of bundled authentication data.

=head3 Attributes

=head4 user

The user name.

=head4 password

The users password in plaintext or as hmac if cram is used.

=head4 challenge

Optinal challange for when you use cram authentication.

=head1 SEE ALSO

L<JWebmail::Model::ReadMails::QMailAuthuser>, L<JWebmail::Model::ReadMails::Mock>, L<JWebmail>

=cut