blob: dd226ac4159a027b0526a6fbf0da2995d6b1ac84 (
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
|
#-*- perl -*-
#
# Copyright (C) 2004,2005 Ken'ichi Fukamachi
#
# $FML: Subject.pm,v 1.7 2005/08/20 01:25:16 fukachan Exp $
#
package Mail::Message::Subject;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;
# base class is "Mail::Message::String".
use Mail::Message::String;
@ISA = qw(Mail::Message::String);
=head1 NAME
Mail::Message::Subject - utilities to manipulate subject string.
=head1 SYNOPSIS
my $subject = new Mail::Message::Subject $header->get('subject');
$subject->mime_header_decode();
if ($subject->has_reply_tag()) {
$subject->delete_dup_reply_tag();
}
$subject->mime_header_encode();
$header->set($subject->as_str());
=head1 DESCRIPTION
=head2 new($subject)
constructor.
=cut
# Descriptions: constructor.
# Arguments: OBJ($self) STR($subject)
# Side Effects: none
# Return Value: OBJ
sub new
{
my ($self, $subject) = @_;
$self->SUPER::new($subject);
}
=head1 Re: TAG HANLING
=cut
# Descriptions: cut off reply keywords like "Re:".
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub delete_dup_reply_tag
{
my ($self) = @_;
my $subject = $self->as_str();
# XXX-TODO: care for not Japanese string!
# XXX-TODO: call this module if $subject is Japanese or English.
# XXX-TODO: but what should we do when the code is not the two above ?
if (1) {
use Mail::Message::Language::Japanese::Subject;
my $sbj = new Mail::Message::Language::Japanese::Subject;
$subject = $sbj->cutoff_reply_tag($subject);
$self->set($subject);
}
}
# Descriptions: speculate $subject looks a reply message or not?
# Arguments: OBJ($self)
# Side Effects: none
# Return Value: 1 (looks reply message) or 0
sub has_reply_tag
{
my ($self) = @_;
my $subject = $self->as_str();
my $charset = $self->get_mime_charset();
return 1 if $subject =~ /^\s*Re:/i;
# XXX anyway, we use this method always :-)
# XXX-TODO: care for not Japanese string!
if (1 || $charset =~ /iso-2022-jp/io) {
use Mail::Message::Language::Japanese::Subject;
my $sbj = new Mail::Message::Language::Japanese::Subject;
if ($sbj->is_reply($subject)) {
return 1;
}
}
return 0;
}
=head1 ML TAG HANDLING
=cut
# Descriptions: remove tag-like string.
# Arguments: OBJ($self) STR($tag)
# Side Effects: none
# Return Value: STR(subject string)
sub delete_tag
{
my ($self, $tag) = @_;
my $subject = $self->as_str();
# XXX $subject SHOULD BE MIME DECODED ALREADY.
# for example, ml_name = elena
# if $tag has special regexp such as \U$ml_name\E or \L$ml_name\E
if (defined $tag) {
if ($tag =~ /\\E/o && $tag =~ /\\U|\\L/o) {
eval qq{ \$tag = "$tag";};
carp($@) if $@;
}
my $retag = $self->regexp_compile($tag);
$subject =~ s/$retag//g;
$subject =~ s/^\s*//;
$self->set($subject);
}
return $subject;
}
=head2 regexp_compile()
create the regexp for a subject tag,
for example "[%s %05d]" => "\[\S+ \d+\]".
=cut
# Descriptions: create regexp for a subject tag, for example
# "[%s %05d]" => "\[\S+ \d+\]"
# not OO style.
# Arguments: OBJ($self) STR($s)
# $s == a subject tag string
# Side Effects: none
# Return Value: STR(a regexp for the given tag)
sub regexp_compile
{
my ($self, $s) = @_;
if (defined $s) {
$s = quotemeta( $s );
$s =~ s@\\\%@\%@g;
$s =~ s@\%s@\\S+@g;
$s =~ s@\%d@\\d+@g;
$s =~ s@\%0\d+d@\\d+@g;
$s =~ s@\%\d+d@\\d+@g;
$s =~ s@\%\-\d+d@\\d+@g;
# quote for regexp substitute: [ something ] -> \[ something \]
# $s =~ s/^(.)/quotemeta($1)/e;
# $s =~ s/(.)$/quotemeta($1)/e;
return $s;
}
else {
return '';
}
}
=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) 2004,2005 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::Subject first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.
C<Subject_to_unixtime> is imported from fml 4.0-current libmti.pl.
=cut
1;
|