| 1 | package Lingua::JA::Mail::Header; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | our $VERSION = '0.02'; # 2003-04-03 (since 2003-03-05) | |||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 9 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 9 | my $class = shift; | |||||||||
| 15 | 9 | my $self = {}; | |||||||||
| 16 | 9 | bless $self, $class; | |||||||||
| 17 | 9 | return $self; | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub set { | ||||||||
|
Subroutine
set: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 21 | 0 | my ($self, $entity, $value) = @_; | |||||||||
| 22 | 0 | $$self{$entity} = $value; | |||||||||
| 23 | 0 | return $self; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 4 | ▶ | sub build { | ||||||||
|
Subroutine
build: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 27 | 4 | my $self = shift; | |||||||||
| 28 | 4 | my @key = $self->_header_order; | |||||||||
| 29 | 4 | my @header; | |||||||||
| 30 | 4 | foreach my $key (@key) { | |||||||||
| 31 | 20 | push(@header, "$key: $$self{$key}"); | |||||||||
| 32 | } | ||||||||||
| 33 | 4 | return join("\n", @header); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 9 | ▶ | sub _header_order { | ||||||||
|
Subroutine
_header_order: called CC=8 SCAR=20.8
| |||||||||||
| 37 | 9 | my $self = shift; | |||||||||
| 38 | 9 | my @key = keys(%$self); | |||||||||
| 39 | 9 | my @order = qw( | |||||||||
| 40 | Date From Sender Reply-To To Cc Bcc | ||||||||||
| 41 | Message-ID In-Reply-To References | ||||||||||
| 42 | Subject Comments Keywords | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | 9 | my @newkey; | |||||||||
| 46 | 9 | foreach my $order (@order) { | |||||||||
| 47 | 117 | foreach my $key (@key) { | |||||||||
| 48 | 689 | ▶ | if ($key eq $order) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 48 | push(@newkey, $key); | |||||||||
| 50 | } | ||||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 9 | my @oldkey; | |||||||||
| 55 | 9 | foreach my $key (@key) { | |||||||||
| 56 | 53 | my $exist = 0; | |||||||||
| 57 | 53 | foreach my $newkey (@newkey) { | |||||||||
| 58 | 196 | ▶ | if ($key eq $newkey) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 48 | $exist = 1; | |||||||||
| 60 | 48 | last; | |||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | 53 | ▶ | if ($exist != 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 5 | push(@oldkey, $key); | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 9 | return @newkey, @oldkey; | |||||||||
| 69 | } | ||||||||||
| 70 | ######################################################################## | ||||||||||
| 71 | # specify the origination date. | ||||||||||
| 72 | 9 | ▶ | sub date { | ||||||||
|
Subroutine
date: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 9 | my($self, $date_time) = @_; | |||||||||
| 74 | 9 | $$self{'Date'} = $date_time; | |||||||||
| 75 | 9 | return $self; | |||||||||
| 76 | } | ||||||||||
| 77 | ######################################################################## | ||||||||||
| 78 | # add a originator address or a destination address. | ||||||||||
| 79 | 15 | ▶ | sub add_from { | ||||||||
|
Subroutine
add_from: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | 15 | my($self, $addr_spec, $name) = @_; | |||||||||
| 81 | 15 | $self->_add_mailbox('From', $addr_spec, $name); | |||||||||
| 82 | 15 | return $self | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 3 | ▶ | sub sender { | ||||||||
|
Subroutine
sender: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 86 | 3 | my($self, $addr_spec, $name) = @_; | |||||||||
| 87 | 3 | $self->_add_mailbox('Sender', $addr_spec, $name); | |||||||||
| 88 | 3 | return $self | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 9 | ▶ | sub add_reply { | ||||||||
|
Subroutine
add_reply: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 92 | 9 | my($self, $addr_spec, $name) = @_; | |||||||||
| 93 | 9 | $self->_add_mailbox('Reply-To', $addr_spec, $name); | |||||||||
| 94 | 9 | return $self | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 27 | ▶ | sub add_to { | ||||||||
|
Subroutine
add_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 98 | 27 | my($self, $addr_spec, $name) = @_; | |||||||||
| 99 | 27 | $self->_add_mailbox('To', $addr_spec, $name); | |||||||||
| 100 | 27 | return $self | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 9 | ▶ | sub add_cc { | ||||||||
|
Subroutine
add_cc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 104 | 9 | my($self, $addr_spec, $name) = @_; | |||||||||
| 105 | 9 | $self->_add_mailbox('Cc', $addr_spec, $name); | |||||||||
| 106 | 9 | return $self | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 9 | ▶ | sub add_bcc { | ||||||||
|
Subroutine
add_bcc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 110 | 9 | my($self, $addr_spec, $name) = @_; | |||||||||
| 111 | 9 | $self->_add_mailbox('Bcc', $addr_spec, $name); | |||||||||
| 112 | 9 | return $self | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 72 | ▶ | sub _add_mailbox { | ||||||||
|
Subroutine
_add_mailbox: called CC=9 SCAR=22.0
| |||||||||||
| 116 | 72 | my($self, $field, $addr_spec, $name) = @_; | |||||||||
| 117 | |||||||||||
| 118 | 72 | my $address; | |||||||||
| 119 | 72 | ▶ | if ($name) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 62 | ▶ | if ( _check_if_contain_japanese($name) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 24 | my $name = encoded_header($name); | |||||||||
| 122 | 24 | $address = "$name\n <$addr_spec>"; | |||||||||
| 123 | } | ||||||||||
| 124 | else { | ||||||||||
| 125 | 38 | ▶ | if ( length($name) <= 73) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 16 | $address = "\"$name\"\n <$addr_spec>"; | |||||||||
| 127 | } | ||||||||||
| 128 | else { | ||||||||||
| 129 | 22 | my @name = split(/ /, $name); | |||||||||
| 130 | 22 | my $too_long_word = 0; | |||||||||
| 131 | 22 | foreach my $piece (@name) { | |||||||||
| 132 | 33 | ▶ | if ( length($piece) > 75 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 11 | $too_long_word = 1; | |||||||||
| 134 | 11 | last; | |||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | 22 | ▶ | if ($too_long_word) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 11 | $name = encoded_header_ascii($name); | |||||||||
| 139 | 11 | $address = "$name\n <$addr_spec>"; | |||||||||
| 140 | } | ||||||||||
| 141 | else { | ||||||||||
| 142 | 11 | $name = join("\n ", @name); | |||||||||
| 143 | 11 | $address = "$name\n <$addr_spec>"; | |||||||||
| 144 | } | ||||||||||
| 145 | } | ||||||||||
| 146 | } | ||||||||||
| 147 | } | ||||||||||
| 148 | else { | ||||||||||
| 149 | 10 | $address = $addr_spec; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 72 | ▶ | if ($$self{$field}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 42 | ▶ | if ($field eq 'Sender') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | 0 | croak "a violation of the RFC2822 - you can specify the 'Sender:' field with only one 'mailbox'"; | |||||||||
| 155 | } | ||||||||||
| 156 | else { | ||||||||||
| 157 | 42 | $$self{$field} = "$$self{$field},\n $address"; | |||||||||
| 158 | } | ||||||||||
| 159 | } | ||||||||||
| 160 | else { | ||||||||||
| 161 | 30 | $$self{$field} = "\n $address"; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 72 | return $self; | |||||||||
| 165 | } | ||||||||||
| 166 | ######################################################################## | ||||||||||
| 167 | 62 | ▶ | sub _check_if_contain_japanese { | ||||||||
|
Subroutine
_check_if_contain_japanese: called CC=1 SCAR=0.0
| |||||||||||
| 168 | 62 | my $string = shift; | |||||||||
| 169 | |||||||||||
| 170 | # $string = decode('utf8', $string); | ||||||||||
| 171 | 62 | $string =~ tr/\n//d; # ignore line-break | |||||||||
| 172 | 62 | return $string =~ | |||||||||
| 173 | tr/\x01-\x08\x0B\x0C\x0E-\x1F\x7F\x21\x23-\x5B\x5D-\x7E\x20//c; | ||||||||||
| 174 | # this tr/// checks if there is other than qtext characters or SPACE. | ||||||||||
| 175 | # from RFC2822: | ||||||||||
| 176 | # qtext = NO-WS-CTL / %d33 / %d35-91 / %d93-126 | ||||||||||
| 177 | # qcontent = qtext / quoted-pair | ||||||||||
| 178 | # quoted-string = [CFWS] DQUOTE *([FWS] qcontent) [FWS] DQUOTE [CFWS] | ||||||||||
| 179 | } | ||||||||||
| 180 | ######################################################################## | ||||||||||
| 181 | 9 | ▶ | sub subject { | ||||||||
|
Subroutine
subject: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 182 | 9 | my($self, $string) = @_; | |||||||||
| 183 | 9 | $$self{'Subject'} = encoded_header($string); | |||||||||
| 184 | 9 | $$self{'Subject'} = "\n $$self{'Subject'}"; | |||||||||
| 185 | 9 | return $self; | |||||||||
| 186 | } | ||||||||||
| 187 | ######################################################################## | ||||||||||
| 188 | |||||||||||
| 189 | # RFC2822 describes about the length of a line | ||||||||||
| 190 | # Max: 998 = 1000 - (CR + LF) | ||||||||||
| 191 | # Rec: 76 = 78 - (CR + LF) | ||||||||||
| 192 | # RFC2047 describes about the length of an encoded-word | ||||||||||
| 193 | # Max: 75 = 76 - SPACE | ||||||||||
| 194 | |||||||||||
| 195 | 33 | ▶ | sub encoded_header { | ||||||||
|
Subroutine
encoded_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 196 | 33 | my ($string) = @_; | |||||||||
| 197 | |||||||||||
| 198 | 33 | my @lines = _encoded_word($string); | |||||||||
| 199 | |||||||||||
| 200 | 33 | my $line = join("\n ", @lines); | |||||||||
| 201 | 33 | return $line; | |||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | # an encoded-word is composed of | ||||||||||
| 205 | # 'charset', 'encoding', 'encoded-text' and delimiters. | ||||||||||
| 206 | # Hence the max length of an encoded-text is: | ||||||||||
| 207 | # 75 - ('charset', 'encoding' and delimiters) | ||||||||||
| 208 | # | ||||||||||
| 209 | # charset 'ISO-2022-JP' is 11. | ||||||||||
| 210 | # encoding 'B' is 1. | ||||||||||
| 211 | # delimiters '=?', '?', '?' and '?=' is total 6. | ||||||||||
| 212 | # 75 - (11 + 1 + 6) = 57 | ||||||||||
| 213 | # It is said that the max length of an encoded-text is 57 | ||||||||||
| 214 | # when we use ISO-2022-JP B encoding. | ||||||||||
| 215 | |||||||||||
| 216 | 33 | ▶ | sub _encoded_word { | ||||||||
|
Subroutine
_encoded_word: called CC=2 SCAR=6.9
| |||||||||||
| 217 | 33 | my ($string) = @_; | |||||||||
| 218 | |||||||||||
| 219 | 33 | my @words = _encoded_text($string); | |||||||||
| 220 | |||||||||||
| 221 | 33 | foreach my $word (@words) { | |||||||||
| 222 | 69 | $word = "=?ISO-2022-JP?B?$word?="; | |||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | 33 | return @words; | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | # Through Base64 encoding, a group of 4 ASCII-6bit characters | ||||||||||
| 229 | # is generated by 3 ASCII-8bit pre-encode characters. | ||||||||||
| 230 | # We can get 14 group of encoded 4 ASCII-6bit characters under | ||||||||||
| 231 | # the encoded-text's 57 characters limit. | ||||||||||
| 232 | # Hence, we may handle max 42 ASCII-8bit characters as | ||||||||||
| 233 | # a pre-encode text. | ||||||||||
| 234 | # So we should split a ISO-2022-JP text that | ||||||||||
| 235 | # each splitted piece's length is within 42 | ||||||||||
| 236 | # if it is counted as ASCII-8bit characters. | ||||||||||
| 237 | |||||||||||
| 238 | 33 | ▶ | sub _encoded_text { | ||||||||
|
Subroutine
_encoded_text: called CC=2 SCAR=6.9
| |||||||||||
| 239 | 33 | my ($string) = @_; | |||||||||
| 240 | |||||||||||
| 241 | 33 | my @text = _split($string); | |||||||||
| 242 | |||||||||||
| 243 | 33 | foreach my $text (@text) { | |||||||||
| 244 | 69 | $text = encode_base64($text); | |||||||||
| 245 | 69 | $text =~ tr/\n//d; | |||||||||
| 246 | } | ||||||||||
| 247 | |||||||||||
| 248 | 33 | return @text; | |||||||||
| 249 | } | ||||||||||
| 250 | |||||||||||
| 251 | 33 | ▶ | sub _split { | ||||||||
|
Subroutine
_split: called CC=2 SCAR=6.9
| |||||||||||
| 252 | 33 | my ($string) = @_; | |||||||||
| 253 | |||||||||||
| 254 | 33 | my @strings; | |||||||||
| 255 | 33 | ▶ | while ($string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 256 | 69 | (my $piece, $string) = _cut_once($string); | |||||||||
| 257 | 69 | push(@strings, $piece); | |||||||||
| 258 | } | ||||||||||
| 259 | |||||||||||
| 260 | 33 | return @strings; | |||||||||
| 261 | } | ||||||||||
| 262 | |||||||||||
| 263 | 69 | ▶ | sub _cut_once { | ||||||||
|
Subroutine
_cut_once: called CC=4 SCAR=14.0
| |||||||||||
| 264 | 69 | my ($string) = @_; | |||||||||
| 265 | |||||||||||
| 266 | 69 | my $whole = encode('iso-2022-jp', $string); | |||||||||
| 267 | 69 | ▶ | if ( length($whole) <= 42 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 268 | 33 | return $whole; | |||||||||
| 269 | 0 | last; | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | 36 | my $letters = length($string); | |||||||||
| 273 | 36 | ▶ | for (my $i = 1; $i <= $letters; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 274 | 684 | my $temp = substr($string, 0, $i); | |||||||||
| 275 | 684 | $temp = encode('iso-2022-jp', $temp); | |||||||||
| 276 | 684 | ▶ | if (length($temp) > 42) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 277 | 36 | my $piece = substr($string, 0, $i - 1); | |||||||||
| 278 | 36 | $piece = encode('iso-2022-jp', $piece); | |||||||||
| 279 | 36 | my $rest = substr($string, $i - 1); | |||||||||
| 280 | 36 | return ($piece, $rest); | |||||||||
| 281 | 0 | last; | |||||||||
| 282 | } | ||||||||||
| 283 | } | ||||||||||
| 284 | } | ||||||||||
| 285 | ######################################################################## | ||||||||||
| 286 | 11 | ▶ | sub encoded_header_ascii { | ||||||||
|
Subroutine
encoded_header_ascii: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 287 | 11 | my ($string) = @_; | |||||||||
| 288 | |||||||||||
| 289 | 11 | my @lines = _encoded_word_q($string); | |||||||||
| 290 | |||||||||||
| 291 | 11 | my $line = join("\n ", @lines); | |||||||||
| 292 | 11 | return $line; | |||||||||
| 293 | } | ||||||||||
| 294 | |||||||||||
| 295 | 11 | ▶ | sub _encoded_word_q { | ||||||||
|
Subroutine
_encoded_word_q: called CC=2 SCAR=6.9
| |||||||||||
| 296 | 11 | my ($string) = @_; | |||||||||
| 297 | |||||||||||
| 298 | 11 | my @words = _encoded_text_q($string); | |||||||||
| 299 | |||||||||||
| 300 | 11 | foreach my $word (@words) { | |||||||||
| 301 | 22 | $word = "=?US-ASCII?Q?$word?="; | |||||||||
| 302 | } | ||||||||||
| 303 | |||||||||||
| 304 | 11 | return @words; | |||||||||
| 305 | } | ||||||||||
| 306 | |||||||||||
| 307 | 11 | ▶ | sub _encoded_text_q { | ||||||||
|
Subroutine
_encoded_text_q: called CC=2 SCAR=6.9
| |||||||||||
| 308 | 11 | my ($string) = @_; | |||||||||
| 309 | |||||||||||
| 310 | 11 | my @text = _split_q($string); | |||||||||
| 311 | |||||||||||
| 312 | 11 | foreach my $text (@text) { | |||||||||
| 313 | 22 | $text = encode_q($text); | |||||||||
| 314 | } | ||||||||||
| 315 | |||||||||||
| 316 | 11 | return @text; | |||||||||
| 317 | } | ||||||||||
| 318 | |||||||||||
| 319 | 11 | ▶ | sub _split_q { | ||||||||
|
Subroutine
_split_q: called CC=2 SCAR=6.9
| |||||||||||
| 320 | 11 | my ($string) = @_; | |||||||||
| 321 | |||||||||||
| 322 | 11 | my @strings; | |||||||||
| 323 | 11 | ▶ | while ($string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 324 | 22 | (my $piece, $string) = _cut_once_q($string); | |||||||||
| 325 | 22 | push(@strings, $piece); | |||||||||
| 326 | } | ||||||||||
| 327 | |||||||||||
| 328 | 11 | return @strings; | |||||||||
| 329 | } | ||||||||||
| 330 | |||||||||||
| 331 | 22 | ▶ | sub _cut_once_q { | ||||||||
|
Subroutine
_cut_once_q: called CC=4 SCAR=14.0
| |||||||||||
| 332 | 22 | my ($string) = @_; | |||||||||
| 333 | |||||||||||
| 334 | 22 | my $whole = encode_q($string); | |||||||||
| 335 | 22 | ▶ | if ( length($whole) <= 60 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 336 | 11 | return $string; | |||||||||
| 337 | 0 | last; | |||||||||
| 338 | } | ||||||||||
| 339 | |||||||||||
| 340 | 11 | my $letters = length($string); | |||||||||
| 341 | 11 | ▶ | for (my $i = 1; $i <= $letters; $i++) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 342 | 671 | my $temp = substr($string, 0, $i); | |||||||||
| 343 | 671 | $temp = encode_q($temp); | |||||||||
| 344 | 671 | ▶ | if (length($temp) > 60) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 345 | 11 | my $piece = substr($string, 0, $i - 1); | |||||||||
| 346 | 11 | my $rest = substr($string, $i - 1); | |||||||||
| 347 | 11 | return ($piece, $rest); | |||||||||
| 348 | 0 | last; | |||||||||
| 349 | } | ||||||||||
| 350 | } | ||||||||||
| 351 | } | ||||||||||
| 352 | |||||||||||
| 353 | 715 | ▶ | sub encode_q { | ||||||||
|
Subroutine
encode_q: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||
| 354 | 715 | my ($string) = @_; | |||||||||
| 355 | |||||||||||
| 356 | 715 | $string =~ | |||||||||
| 357 | 0 | s/([^\x21\x23-\x3C\x3E\x40-\x5B\x5D\x5E\x60-\x7E])/uc sprintf("=%02x", ord($1))/eg; | |||||||||
| 358 | |||||||||||
| 359 | 715 | return $string; | |||||||||
| 360 | } | ||||||||||
| 361 | |||||||||||
| 362 | |||||||||||
| 363 | 2 | 1; | |||||||||
| 364 | __END__ | ||||||||||