summaryrefslogtreecommitdiff
path: root/fml/lib/Mail/ThreadTrack/HeaderRewrite.pm
blob: 5b47445c3014d4db1b3285251a70ac0ae6ff6ee0 (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
#-*- perl -*-
#
#  Copyright (C) 2001,2002,2003 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.
#
# $FML: HeaderRewrite.pm,v 1.14 2003/08/23 04:35:49 fukachan Exp $
#

package Mail::ThreadTrack::HeaderRewrite;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;

=head1 NAME

Mail::ThreadTrack::HeaderRewrite - header manipulation

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 rewrite_header($msg)

C<$msg> is Mail::Message object.

=cut


# Descriptions: add thread track info into $msg where
#               $msg is Mail::Message object.
#    Arguments: OBJ($self) OBJ($msg)
# Side Effects: modify $msg header
# Return Value: none
sub rewrite_header
{
    my ($self, $msg) = @_;
    my $config  = $self->{ _config };
    my $loctype = $config->{ thread_subject_tag_location } || 'appended';
    my $header  = $msg->whole_message_header();
    my $tag     = $self->{ _thread_subject_tag } || '';

    # append the thread tag to the subject
    if (defined $header->get('subject')) {
	my $subject = $header->get('subject');

	if ($loctype eq 'appended' && $tag) {
	    $header->replace('subject', $subject ." ". $tag);
	}
	elsif ($loctype eq 'prepended' && $tag) {
	    $header->replace('subject', $tag ." ". $subject);
	}
	else {
	    $self->log("unknown thread_subject_tag_location type");
	}

	if (defined $self->{ _status_info }) {
	    $header->add('X-Thread-Status', $self->{ _status_info });
	}

	if (defined $self->{ _thread_id }) {
	    $header->add('X-Thread-ID', $self->{ _thread_id });
	}

	if (defined $self->{ _status_history }) {
	    $header->add('X-Thread-History', $self->{ _status_history });
	}
    }
}


# Descriptions: prepare history infomation for further header rewriting.
#    Arguments: OBJ($self) OBJ($msg)
# Side Effects: update $self->{ _status_history }
# Return Value: none
sub prepare_history_info
{
    my ($self, $msg) = @_;
    my $thread_id = $self->get_thread_id();

    # prepare hash table tied to db_dir/*db's
    my $rh = $self->{ _hash_table };

    if (defined $rh->{ _articles  }->{ $thread_id }) {
	my $buf    = '';
	my (@aid)  = split(/\s+/, $rh->{ _articles  }->{ $thread_id });
	my $sender = $rh->{ _sender }->{ $aid[0] };
	my $when   = $rh->{ _date }->{ $aid[0] };

	# clean up
	$sender =~ s/[\s\n]*$//;
	$when   =~ s/[\s\n]*$//;

	use Mail::Message::Date;
	$when = Mail::Message::Date->new($when)->mail_header_style();

	# XXX-TODO: validate $aid[0], $sender, $when, @aid.
	$buf .= "\t\n";
	$buf .= "\tthis thread is opended at article $aid[0]\n";
	$buf .= "\tby $sender\n";
	$buf .= "\ton $when\n";
	$buf .= "\tarticle references: @aid\n";
	$self->{ _status_history } = $buf;
    }
}


=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) 2001,2002,2003 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::ThreadTrack::HeaderRewrite first appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;