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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
#!/usr/bin/perl -cw
#
# Copyright (c) 1994-2011 Carnegie Mellon University. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. The name "Carnegie Mellon University" must not be used to
# endorse or promote products derived from this software without
# prior written permission. For permission or any legal
# details, please contact
# Carnegie Mellon University
# Center for Technology Transfer and Enterprise Creation
# 4615 Forbes Avenue
# Suite 302
# Pittsburgh, PA 15213
# (412) 268-7393, fax: (412) 268-7395
# innovation@andrew.cmu.edu
#
# 4. Redistributions of any form whatsoever must retain the following
# acknowledgment:
# "This product includes software developed by Computing Services
# at Carnegie Mellon University (http://www.cmu.edu/computing/)."
#
# CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
# OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
use strict;
use warnings;
package Cyrus::Annotator::Message;
use MIME::Base64 qw(decode_base64);
use MIME::QuotedPrint qw(decode_qp);
use Encode qw(decode);
our $VERSION = '1.00';
=head1 NAME
Cyrus::Annotator::Message - representation of a message to annotate
=head1 SYNOPSIS
use warnings;
use strict;
package MyAnnotatorDaemon;
use base Cyrus::Annotator::Daemon;
sub annotate_message
{
my ($message) = @_;
$message->set_flag('\Flagged');
$message->set_shared_annotation('/comment', 'Hello!!');
}
MyAnnotatorDaemon->run();
=head1 DESCRIPTION
This module encapsulates a message which is being processed by the
annotator daemon.
=head1 METHODS
Cyrus::Annotator::Message has the following methods.
=over 4
=item I<new(%args)>
Takes the following args:
# required
* BODYSTRUCTURE => parsed bodystructure
# optional (but you need to provide SOMETHING if your code uses any
# of the accessors)
* GUID => 40 character sha1
* HEADER => Mail::Header object with headers pre-parsed
* FILENAME => path to rfc822 file
# totally optional (will be considered empty if not set)
* FLAGS => array of already set flags
* ANNOTATIONS => array of already set annotations
=cut
sub new {
my $class = shift;
my %args = @_;
my %flags;
my %annots;
my $fs = $args{FLAGS} || [];
my $as = $args{ANNOTATIONS} || [];
for my $name (@$fs) {
$flags{$name} = {
value => 1,
orig => 1,
};
}
while (my $entry = shift @$as) {
my $rest = shift @$as;
my ($type, $value) = @$rest;
$annots{$entry}{$type} = {
value => $value,
orig => $value,
};
}
my $self = bless {
filename => $args{FILENAME},
bodystructure => $args{BODYSTRUCTURE},
guid => $args{GUID},
header => $args{HEADER},
flag => \%flags,
annot => \%annots,
}, ref($class) || $class;
}
=item I<fh()>
returns a read-only filehandle to the raw (rfc822) representation
of the full message.
=cut
sub fh {
my $self = shift;
unless ($self->{fh}) {
die "Need a filename" unless $self->{filename};
require "IO/File.pm";
$self->{fh} = IO::File->new($self->{filename}, 'r');
}
# Move back to start of message
seek $self->{fh}, 0, 0;
return $self->{fh};
}
=item I<decode_part($Part, $Content)>
Given some content, decode it from the part's content
encoding and charset.
=cut
sub decode_part {
my $self = shift;
my ($Part, $Content) = @_;
if (lc $Part->{'Content-Transfer-Encoding'} eq 'base64') {
# remove trailing partial value
$Content =~ tr{[A-Za-z0-9+/=]}{}cd;
my $extra = length($Content) % 4;
if ($extra) {
# warn "stripping $extra chars " . length($Content);
$Content = substr($Content, 0, -$extra);
}
$Content = decode_base64($Content);
}
elsif (lc $Part->{'Content-Transfer-Encoding'} eq 'quoted-printable') {
# remove trailing partial value
$Content =~ s/=.?$//;
$Content = decode_qp($Content);
}
my $charset = lc($Part->{'Content-Type'}{charset} || 'iso-8859-1');
# If no charset is present, it defaults to ascii. But some systems
# send 8-bit data. For them, assume iso-8859-1, ascii is a subset anyway
$charset = 'iso-8859-1'
if $charset eq 'ascii' || $charset eq 'us-ascii';
# Fix up some bogus formatted iso charsets
$charset =~ s/^(iso)[\-_]?(\d+)[\-_](\d+)[\-_]?\w*/$1-$2-$3/i;
return eval { decode($charset, $Content) } || decode('iso-8859-1', $Content);
}
=item I<read_part_content($Part, $nbytes)>
returns the first n bytes of the bodypart passed. This is a section of the
bodystructure (hashref). If no part is passed, it's the raw message.
If no 'nbytes' is passed, read the entire part.
=cut
sub read_part_content {
my $self = shift;
my ($Part, $nbytes) = @_;
unless ($Part) {
$Part = $self->bodystructure();
}
my $fh = $self->fh();
die "No Offset for part"
unless defined $Part->{Offset};
die "No Size for part"
unless defined $Part->{Size};
if (!defined($nbytes) || $Part->{Size} < $nbytes) {
$nbytes = $Part->{Size};
}
seek $fh, $Part->{Offset}, 0
or die "Cannot seek: $!";
my $Content = '';
# Could be 0 length body, only die on undef (real error)
my $r = read($fh, $Content, $nbytes);
die "Cannot read: $!" if !defined $r;
return $self->decode_part($Part, $Content);
}
=item I<header()>
returns a Mail::Header object containing all the headers of the message.
=cut
sub header {
my $self = shift;
unless ($self->{header}) {
require "Mail/Header.pm";
$self->{header} = Mail::Header->new($self->fh());
}
return $self->{header};
}
=item I<bodystructure()>
returns a structure
is a structure closely based on the IMAP BODYSTRUCTURE, decoded into a
hash, including recursively all MIME sections. In general, the
following items are defined for all body structures:
=over 4
=item * MIME-Type
=item * MIME-Subtype
=item * Content-Type
=item * Content-Description
=item * Content-Disposition
=item * Content-Language
=back
Body structures which have a MIME-Type of 'multipart' contain the
following items:
=over 4
=item * MIME-Subparts
=back
For body structures B<except> those that have a MIME-Type of
'multipart', the following are defined:
=over 4
=item * Content-ID
=item * Content-Description
=item * Content-Transfer-Encoding
=item * Content-MD5
=item * Size
=item * Lines
=item * Offset
=item * HeaderSize
=back
=item I<guid()>
returns the hex encoded (40 character) sha1 of the rfc822 representation.
=item I<has_flag($name)>
=item I<set_flag($name)>
=item I<clear_flag($name)>
Check for the boolean value of a flag with $name, set the flag and remove
the flag respectively.
Note that changes are not immediate. They will be applied by the annotator
at the end.
For example:
$message->set_flag("\\Flagged");
=cut
sub bodystructure {
my $self = shift;
return $self->{bodystructure};
}
sub get_flag {
my $self = shift;
my ($name) = @_;
return $self->{flag}{$name}{value};
}
sub get_flags {
my $self = shift;
return grep { $self->{flag}{$_}{value} } keys %{$self->{flag}};
}
sub set_flag_value {
my $self = shift;
my ($name, $value) = @_;
$self->{flag}{$name}{orig} = 0
unless exists $self->{flag}{$name}{orig};
$self->{flag}{$name}{value} = $value;
}
sub set_flag {
my $self = shift;
my ($name) = @_;
$self->set_flag_value($name, 1);
}
sub clear_flag {
my $self = shift;
my ($name) = @_;
$self->set_flag_value($name, 0);
}
=item I<get_shared_annotation($entry)>
=item I<get_private_annotation($entry)>
=item I<set_shared_annotation($entry, $value)>
=item I<set_private_annotation($entry, $value)>
=item I<clear_shared_annotation($entry)>
=item I<clear_private_annotation($entry)>
Get, set and clear the value of an annotation, either shared or private. The
"get" accessors return a string with the value. Clear is the same as set
with $value of the empty string ('').
For example:
$message->set_shared_annotation('/comment', 'Hello World');
=cut
sub get_annotation {
my $self = shift;
my ($entry, $type) = @_;
return $self->{annot}{$entry}{$type}{value};
}
sub set_annotation {
my $self = shift;
my ($entry, $type, $value) = @_;
$value = '' unless defined $value;
$self->{annot}{$entry}{$type}{orig} = ''
unless exists $self->{annot}{$entry}{$type}{orig};
$self->{annot}{$entry}{$type}{value} = $value;
}
sub get_shared_annotation {
my $self = shift;
my ($entry) = @_;
return $self->get_annotation($entry, 'value.shared');
}
sub set_shared_annotation {
my $self = shift;
my ($entry, $value) = @_;
return $self->set_annotation($entry, 'value.shared', $value);
}
sub clear_shared_annotation {
my $self = shift;
my ($entry) = @_;
return $self->set_annotation($entry, 'value.shared', '');
}
sub get_private_annotation {
my $self = shift;
my ($entry) = @_;
return $self->get_annotation($entry, 'value.private');
}
sub set_private_annotation {
my $self = shift;
my ($entry, $value) = @_;
return $self->set_annotation($entry, 'value.private', $value);
}
sub clear_private_annotation {
my $self = shift;
my ($entry) = @_;
return $self->set_annotation($entry, 'value.private', '');
}
=item I<get_changed()>
returns two arrayrefs - [['flagname', 'bool']] and [['entry', 'type', 'value']], e.g.
[["\\Flagged", 1]], [['/comment', 'value.shared', 'Hello World']]
=cut
sub get_changed {
my $self = shift;
my @flags;
my @annots;
foreach my $name (sort keys %{$self->{flag}}) {
my $item = $self->{flag}{$name};
push @flags, [$name, $item->{value}]
unless $item->{value} == $item->{orig};
}
foreach my $entry (sort keys %{$self->{annot}}) {
foreach my $type (sort keys %{$self->{annot}{$entry}}) {
my $item = $self->{annot}{$entry}{$type};
push @annots, [$entry, $type, $item->{value}]
unless is_eq($item->{value}, $item->{orig});
}
}
return (\@flags, \@annots);
}
sub is_eq {
my ($l, $r) = @_;
if (defined $l && defined $r) {
return $l eq $r;
}
else {
return !defined $l && !defined $r;
}
}
=back
=head1 SEE ALSO
I<RFC3501>, I<RFC5257>.
=head1 AUTHOR
Greg Banks E<lt>gnb@fastmail.fmE<gt>.
Bron Gondwana E<lt>brong@fastmail.fmE<gt>.
=cut
1;
|