summaryrefslogtreecommitdiff
path: root/fml/lib/Mail/Message/Outline.pm
blob: 43da915296a655c2ea709913be22628060683ba4 (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
231
232
233
234
235
236
237
238
239
240
241
242
#-*- perl -*-
#
# Copyright (C) 2005,2006 Ken'ichi Fukamachi
#
# $FML: Outline.pm,v 1.1 2005/09/11 13:12:46 fukachan Exp $
#

package Mail::Message::Outline;
use strict;
use Mail::Message::Language::Japanese::Outline;

=head1 NAME

Mail::Message::Outline - handle outline or outline.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=cut


# Descriptions: create outine / summary.
#    Arguments: OBJ($self) HASH_REF($params)
# Side Effects: none
# Return Value: STR
sub outline
{
    my ($self, $params) = @_;
    my $header = $self->whole_message_header();
    my $msg    = $self->find_first_plaintext_message();
    my $result = '';

    # options
    my $is_hdr = $params->{ with_header } || 'yes';
    my $is_msg = 1;

    # 1. prepend subject.
    if ($is_hdr eq 'yes' && defined $header) {
	my $subject = $header->get('subject') || '';
	use Mail::Message::Subject;
	my $subj = new Mail::Message::Subject $subject;
	$subj->mime_header_decode();
	$result .= $subj->as_external_form();
    }
    
    # XXX-TODO [BUG] we cannot handle the encoded phrase. is it correct?
    # XXX-TODO       1. decode each paragraph by checking each mime part header
    # XXX-TODO       2. run _is_useful_for_summary() for each paragraph
    # 2. summarize message to a few lines.
    if ($is_msg && defined $msg) {
	my $prgbuf = '';
	my $found  = 0;
	my $max    = $params->{ summary_max_lines } || 3;
	my $total  = $msg->paragraph_total();

      PARAGRAPH:
	for my $i (1 .. $total) {
	    $prgbuf = $msg->nth_paragraph($i);

	  LINE:
	    for my $buf (split(/\n/, $prgbuf)) {
		if ($buf && $self->_is_useful_for_summary($buf)) {
		    $result .= "   $buf\n";
		    $found++;
		}

		last PARAGRAPH if $found >= $max;
	    }
	}
    }

    return $result;
}


# Descriptions: check if $buf looks effective string e.g. not quote ?
#    Arguments: OBJ($self) STR($buf)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub _is_useful_for_summary
{
    my ($self, $buf) = @_;

    # ignore empty line.
    return 0 if $buf =~ /^\s*$/o;

    # ignore string similar to quote.
    return 0 if $self->_is_citation_or_signature($buf);

    # ignore mail header like patterns.
    return 0 if $buf =~ /^X-[-A-Za-z0-9]+:/io;
    return 0 if $buf =~ /^Return-[-A-Za-z0-9]+:/io;
    return 0 if $buf =~ /^Mime-[-A-Za-z0-9]+:/io;
    return 0 if $buf =~ /^Content-[-A-Za-z0-9]+:/io;
    return 0 if $buf =~ /^(To|From|Subject|Reply-To|Received):/io;
    return 0 if $buf =~ /^(Message-ID|Date):/io;

    # o.k.
    return 1;
}


# Descriptions: check if $buf looks not effective string e.g. quote ?
#    Arguments: OBJ($self) STR($buf)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub _is_citation_or_signature
{
    my ($self, $buf) = @_;

    use Mail::Message::String;
    my $string = new Mail::Message::String $buf;
    $string->charcode_convert_to_internal_code();
    return 1 if $string->is_citation();
    return 1 if $string->is_signature();

    my $str = $string->as_str();
    if ($str =~ /^[\>\#\|\*\:\;\=]/o) {
	return 1;
    }
    elsif ($str =~ /^in /o) { # citation ?
	return 1;
    }
    elsif ($str =~ /^\w+.*wrote:/io) { # citation.
	return 1;
    }
    elsif ($str =~ /\w+\@\w+/o) { # mail address ?
	return 1;
    }
    elsif ($str =~ /^\S+\>/o) { # citation ?
	return 1;
    }
    elsif ($str =~ /^hi|^hi,/io) { # self introduction.
	return 1;
    }

    return 0;
}


=head2 has_closing_phrase()

check if this message has closing phrase in it.

=head2 set_closing_phrase_rules($rules)

set rules.

=head2 get_closing_phrase_rules()

get rules.

=cut


# Descriptions: check if this message has closing phrase in it.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM
sub has_closing_phrase
{
    my ($self)  = @_;
    my $msg     = $self->find_first_plaintext_message();
    my $rules   = $self->get_closing_phrase_rules();
    my $regexp  = join("|", keys %$rules);

    if (defined($msg) && $regexp) {
	my ($buf, $string);

	my $num_prg = $msg->paragraph_total();
	for (my $i = 1; $i <= $num_prg; $i++) {
	    $buf = $msg->nth_paragraph($i);
	    $buf =~ s/^[\s\n]*//o;
	    $buf =~ s/[\s\n]*$//o;

	    if ($buf) {
		$string = new Mail::Message::String $buf;
		$string->charcode_convert_to_internal_code();
		$buf = $string->as_str();
		if ($buf =~ /$regexp/) { return 1;}
	    }
	}
    }

    return 0;
}


# Descriptions: set phrase trap rules.
#    Arguments: OBJ($self) HASH_REF($rules)
# Side Effects: update $self.
# Return Value: none
sub set_closing_phrase_rules
{
    my ($self, $rules) = @_;

    if (defined $rules) {
	$self->{ _closing_phrase_rules } = $rules || {};
    }
}


# Descriptions: return phrase trap rules.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: HASH_REF
sub get_closing_phrase_rules
{
    my ($self) = @_;
    my $rules  = $self->{ _closing_phrase_rules } || {};

    return $rules;
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2005,2006 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

Mail::Message::Outline first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;