File: Part.pm

package info (click to toggle)
ciderwebmail 1.05%2B20240702-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,392 kB
  • sloc: perl: 3,201; xml: 782; javascript: 675; sh: 42; makefile: 29
file content (431 lines) | stat: -rw-r--r-- 10,492 bytes parent folder | download | duplicates (6)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package CiderWebmail::Part;

use Moose;
use Petal;
use MIME::Base64;
use MIME::QuotedPrint;
use CiderWebmail::Util qw/ decode_mime_words /;
use CiderWebmail::MIMEIcons;
use Module::Pluggable require => 1, search_path => [__PACKAGE__];

use Carp qw/ carp cluck /;

has c              => (is => 'ro', isa => 'Object');

has root_message   => (is => 'ro', required => 1, isa => 'Object'); #ref to the CiderWebmail::Message object this part is part of
has parent_message => (is => 'ro', required => 1, isa => 'Object'); #ref to the CiderWebmail::Part::(Root|RFC822) object this part is part of

has bodystruct     => (is => 'ro', isa => 'Object'); #Mail::IMAPClient::BodyStructure Object of this part

has children       => (is => 'rw', isa => 'ArrayRef', default => sub { [] });

has renderable          => (is => 'rw', isa => 'Bool', default => 0 ); #override me!

#stubs are used to render things that get fetched later (like images that get fetched by clicking on a [+] symbol or iframes for html rendering
has render_as_stub      => (is => 'rw', isa => 'Bool', default => 1 ); #override me! 

has message             => (is => 'rw', isa => 'Bool', default => 0 ); #override me!

my %renderers = map{ $_->supported_type => $_ } __PACKAGE__->plugins();

sub BUILD {
    my $self = shift;

    $self->load_children();

    return;
}

sub load_children {
    my ($self) = @_;

    return unless defined $self->bodystruct->{bodystructure};

    foreach(@{ $self->bodystruct->{bodystructure} }) {
        my $part = $self->handler({ bodystruct => $_ });

        push(@{ $self->{children} }, $part) if $part;
        $self->root_message->part_id_to_part->{$part->part_id} = $part;
        if (defined $part->body_id) { $self->root_message->body_id_to_part->{$part->body_id} = $part; }
    }

    return;
}

=head2 main_body_part()

Returns the main body part for using when forwarding/replying the message.

=cut

sub main_body_part {
    my ($self) = @_;

    my @to_check = @{ $self->children };

    foreach (@to_check) {
        my $part = $_;
        if ( ($part->content_type or '') eq 'text/plain') {
            return $part;
        }

        if ($part->children) {
            push(@to_check, @{ $part->children });
        }
    }

    return CiderWebmail::Part::Dummy->new({ root_message => $self->root_message, parent_message => $self->get_parent_message });
}

=head2 body()

returns the body of the part
unless body({ raw => 1}) is specified converting the body to utf-8 will be attempted

=cut

sub body {
    my ($self, $o) = @_;

    my $body = $self->c->model('IMAPClient')->bodypart_as_string({ mailbox => $self->mailbox, uid => $self->uid, part => $self->part_id });

    if (defined($self->bodystruct->{bodyenc}) and (lc($self->bodystruct->{bodyenc}) eq 'base64')) {
        $body = decode_base64($body);
    }

    if (defined($self->bodystruct->{bodyenc}) and (lc($self->bodystruct->{bodyenc}) eq 'quoted-printable')) {
        $body = decode_qp($body);
    }


    return (defined($o->{raw}) ? $body : $self->_decode_body({ body => $body }));
}

sub header {
    my ($self) = @_;

    croak("attempted to call header() on a not-message CiderWebmail::Part object. this is a ".$self->content_type." part") unless $self->message;

    my $body = $self->body({ raw => 1 });
    my $email = Email::Simple->new($body);
    return $email->header_obj->as_string;
}


sub mailbox {
    my ($self) = @_;

    return $self->root_message->mailbox;
}

sub uid {
    my ($self) = @_;

    return $self->root_message->uid;
}

=head2 _decode_body()

attempt a best-effort $charset to utf-8 conversion

=cut

sub _decode_body {
    my ($self, $o) = @_;

    my $part_string;
    unless ($self->charset and $self->charset !~ /utf-8/ixm
        and eval {
            my $converter = Text::Iconv->new($self->charset, "utf-8");
            $part_string = $converter->convert($o->{body});
        }) {

        carp "unable to convert ".$self->charset." to utf-8 using Text::Iconv: $!" if $@;
        $part_string = $o->{body};
    }

    utf8::decode($part_string);

    return $part_string;
}

=head2 part_id()

returns the part_id of the part

=cut

sub part_id {
    my ($self) = @_;

    return $self->bodystruct->id;
}

=head2 body_id()

returns the body_id of the part or undef

=cut

sub body_id {
    my ($self) = @_;

    return unless defined $self->bodystruct->{bodyid};
    return unless ($self->bodystruct->{bodyid} ne 'NIL');

    my $body_id = $self->bodystruct->{bodyid};
    $body_id =~ s/^\<//xm;
    $body_id =~ s/\>$//xm;

    return $body_id;
}


sub charset {
    my ($self) = @_;

    return unless ((defined $self->bodystruct->bodyparms) and ($self->bodystruct->bodyparms ne 'NIL'));
    return $self->bodystruct->bodyparms->{charset};
}

=head2 guess_recipient()

Tries to guess the recipient address used to deliver this message to this mailbox.
Used for suggesting a From address on reply/forward.

=cut

sub guess_recipient {
    my ($self) = @_;

    return [] unless defined $self->to;
    return [ CiderWebmail::Util::filter_unusable_addresses(@{ $self->to }) ]
}


=head2 handler()

returns a CiderWebmail::Part::FooBar object for the specified part

=cut

sub handler {
    my ($self, $o) = @_;

    confess unless $o->{bodystruct};

    my $type = lc($o->{bodystruct}->bodytype.'/'.$o->{bodystruct}->bodysubtype);

    if (defined($renderers{$type})) {
        return $renderers{$type}->new({ c => $self->c, root_message => $self->root_message, bodystruct => $o->{bodystruct}, parent_message => $self->get_parent_message });
    } else {
        return $renderers{'x-ciderwebmail/attachment'}->new({ c => $self->c, root_message => $self->root_message, bodystruct => $o->{bodystruct}, parent_message => $self->get_parent_message });
    }
}

sub get_parent_message {
    my ($self) = @_;

    #if this part is a message (true for Part::RFC822 and Part::Root) use $self for parent_message
    #otherwise pass the last message part (RFC822 or Root) along
    return ( $self->message ? $self : $self->parent_message );
}

=head2 icon() {

returns the name of a icon representing the content type fo the part

=cut

sub icon {
    my ($self) = @_;

    return CiderWebmail::MIMEIcons::get_mime_icon($self->content_type);
}

=head2 render()

render a CiderWebmail::Part. just a stub - override in CiderWebmail::Part::FooBar

=cut

sub render {
    my ($self) = @_;

    confess "[FATAL] CiderWebmail::Part->render() called but was not overridden by anything!";
}

=head2 cid()

returns the Content-ID of the part

=cut

sub cid {
    my ($self) = @_;

    cluck("cid() not implemented");

    my $cid = ($self->entity->head->get('Content-ID') or '');
    chomp($cid);
    $cid =~ s/[<>]//gxm;

    return $cid;
}


=head2 content_type()

returns the content type of the CiderWebmail::Part

=cut

sub content_type {
    my ($self) = @_;

    return lc($self->bodystruct->bodytype.'/'.$self->bodystruct->bodysubtype);
}


=head2 name()

returns the name of the part or "(attachment|part) content/type"

=cut

sub display_name {
    my ($self) = @_;

    #if we have a file name use this
    return $self->file_name if defined $self->file_name;

    #we don't have a file name but it's an attachment - indicate this and show the content type
    return "attachment (".$self->content_type.")" if $self->is_attachment;

    #we don't have a file name and it's not some kind of attachment
    return "part (".$self->content_type.")";
}

=head2 file_name()

returns a best-guess file_name if one was supplied or undef

=cut

sub file_name {
    my ($self) = @_;

    my $bodydisp = $self->bodydisp;
    my $bodyparms = $self->bodyparms;

    if ($self->is_attachment) {
        return decode_mime_words({ data => $bodydisp->{attachment}->{filename} }) if ((defined $bodydisp->{attachment}->{filename}) and ($bodydisp->{attachment}->{filename} ne 'NIL'));
    }

    if ((defined $bodyparms) and (defined $bodyparms->{name}) and ($bodyparms->{name} ne 'NIL')) {
        return decode_mime_words({ data => $bodyparms->{name} }) if ($bodyparms->{name} =~ m/.*\..*/xm);   #name does not have to be a filename. if we want
                                                                                                                   #to treat it as such it should at least resemble 
                                                                                                                   #something like name.extension
    }

    return;
}

=head2 is_attachment()

returns true if the body disposition indicates it is an attachment

=cut

sub is_attachment {
    my ($self) = @_;

    return unless defined $self->bodydisp;
    return 'yep' if ((defined $self->bodydisp->{attachment}) and ($self->bodydisp->{attachment} ne 'NIL'));
    return;
};


=head2 bodydisp()

returnes the body disposition hash (if it exists) or undef

=cut

sub bodydisp {
    my ($self) = @_;

    return unless ((defined $self->bodystruct) and ($self->bodystruct ne 'NIL'));
    return unless ((defined $self->bodystruct->bodydisp) and ($self->bodystruct->bodydisp ne 'NIL'));
    return $self->bodystruct->bodydisp;
}

=head2 attachment()

returns true if the bodydisp indicates it is a attachment

=cut

sub attachment {
    my ($self) = @_;

    return 0 unless defined $self->bodystruct;
    return 0 unless defined $self->bodystruct->{bodydisp};
    return 0 unless (ref($self->bodystruct->{bodydisp}) eq 'HASH');
    return 1 if defined $self->bodystruct->{bodydisp}->{attachment};
    return 0;
}

=head2 bodyparms()

returnes the bodyparms hash (if it exists) or undef

=cut

sub bodyparms {
    my ($self) = @_;

    return unless ((defined $self->bodystruct) and ($self->bodystruct ne 'NIL'));
    return unless ((defined $self->bodystruct->bodyparms) and ($self->bodystruct->bodyparms ne 'NIL'));
    return $self->bodystruct->bodyparms;
}


=head2 uri_download

returns an http url to access the part

=cut

sub uri_download {
    my ($self) = @_;

    return $self->c->stash->{uri_folder} . '/' . $self->root_message->uid . '/part/download/' . $self->part_id;
}

=head2 uri_render

returns an http url to render the part

=cut

sub uri_render {
    my ($self) = @_;

    return $self->c->stash->{uri_folder} . '/' . $self->root_message->uid . '/part/render/' . $self->part_id;
}

=head2 is_root_part

returns true if this part is the root part (RFC822 message)

=cut

sub is_root_part {
    my ($self) = @_;

    return ($self->root_message->root_part eq $self);
}


1;