summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Model/ReadMails.pm
blob: e541de12ed4f706acaf6a2c8114e207437f6a13b (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package JWebmail::Model::ReadMails;

use Mojo::Base -base;

use Class::Struct AuthReadMails => {
    user => '$',
    password => '$',
    challenge => '$',
};


has 'driver';


sub verify_user {

    my $self = shift;

    my ($user, $password) = @_;

    return !scalar $self->driver->communicate(
        user => $user,
        password => $password,
        mode => 'auth',
    )
}


sub read_headers_for {

    my $self = shift;

    my %h = @_;
    my ($auth, $folder, $start, $end, $sort) = @h{qw(auth folder start end sort)};

    my ($resp, $rc) = $self->driver->communicate(
        user => $auth->user,
        password => $auth->password,
        challenge => $auth->challenge,
        mode => 'list',
        args => [$start // 0, $end // 0, $sort // 'date', $folder // ''],
    );
    die "connection error: $resp->{error}" if $rc;
    return $resp;
}


sub count {

    my $self = shift;

    my ($auth, $folder) = @_;

    my ($resp, $rc) = $self->driver->communicate(
        user      => $auth->user,
        password  => $auth->password,
        challenge => $auth->challenge,
        mode      => 'count',
        args      => [$folder],
    );
    die "connection error: $resp->{error}" if $rc;
    return ($resp->{size}, $resp->{count}, $resp->{new});
}


sub show {
    my $self = shift;

    my ($auth, $mid) = @_;

    my ($resp, $rc) = $self->driver->communicate(
        user => $auth->user,
        password => $auth->password,
        challenge => $auth->challenge,
        mode => 'read-mail',
        args => [$mid],
    );
    die "connection error: $resp->{error}, $resp->{mid}" if $rc;
    return $resp;
}


sub search {
    my $self = shift;

    my ($auth, $pattern, $folder) = @_;

    my ($resp, $rc) = $self->driver->communicate(
        user => $auth->user,
        password => $auth->password,
        challenge => $auth->challenge,
        mode => 'search',
        args => [$pattern, $folder],
    );
    die "connection error: $resp->{error}" if $rc;
    return $resp;
}


sub folders {
    my $self = shift;

    my ($auth) = @_;

    my ($resp, $rc) = $self->driver->communicate(
        user => $auth->user,
        password => $auth->password,
        challenge => $auth->challenge,
        mode => 'folders',
    );
    die "connection error: $resp->{error}" if $rc;
    return $resp;
}


sub move {
    my $self = shift;

    my ($auth, $mid, $folder) = @_;

    my ($resp, $rc) = $self->driver->communicate(
        user => $auth->user,
        password => $auth->password,
        challenge => $auth->challenge,
        mode => 'move',
        args => [$mid, $folder],
    );
    die "connection error: $resp->{error}" if $rc;
    return 1;
}


1

__END__

=encoding utf-8

=head1 NAME

ReadMails - Read received mails

=head1 SYNOPSIS

  my $m = JWebmail::Model::ReadMails->new(driver => ...);
  $m->search($auth, qr/Hot singles in your area/, '');

=head1 DESCRIPTION

This module is a facade for the actions of its driver.
All actions are delegated to it.

The first parameter is authentication info as AuthReadMails
whith the rest varying.

The communication is stateless.

=head1 ATTRIBUTES

=head2 driver

The driver does the actual work of reading the mailbox.

=head1 METHODS

=head2 new

Instantiate a new object. The 'driver' option is required.

=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.

=head1 CLASSES

=head2 AuthReadMails

A struct that bundles auth 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.

=head3 Methods

=head4 new

=head1 SEE ALSO

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

=cut