File: mhonarc.pl

package info (click to toggle)
namazu2 2.0.21-22
  • links: PTS
  • area: main
  • in suites: buster
  • size: 7,032 kB
  • sloc: ansic: 14,161; perl: 10,789; sh: 9,355; lisp: 1,147; makefile: 914
file content (200 lines) | stat: -rw-r--r-- 5,866 bytes parent folder | download | duplicates (6)
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
#
# -*- Perl -*-
# $Id: mhonarc.pl,v 1.23.8.8 2008-05-09 07:52:21 opengl2772 Exp $
# Copyright (C) 1997-2000 Satoru Takabayashi ,
#               1999 NOKUBI Takatsugu ,
#               2002 Earl Hood ,
#               2000-2008 Namazu Project All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either versions 2, or (at your option)
#  any later version.
# 
#  This program is distributed in the hope that it will be useful
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
#  02111-1307, USA
#
#  This file must be encoded in EUC-JP encoding
#

package mhonarc;
use strict;
require 'util.pl';
require 'gfilter.pl';
require 'html.pl';
require 'mailnews.pl';

#
# This pattern specifies MHonArc's file names.
# NOTE: MHonArc does allow one to customize the filename for message files.
#	So we make the regex a little flexible to handle common variations.
#
my $MHONARC_MESSAGE_FILE = '\bmsg\d+\.s?html(?:\.gz)?\Z';

sub mediatype() {
    return ('text/html; x-type=mhonarc');
}

sub status() {
    # The check of a dependence filter.
    return 'no' if (html::status() ne 'yes');
    return 'no' if (mailnews::status() ne 'yes');

    return 'yes';
}

sub recursive() {
    return 0;
}

sub pre_codeconv() {
    return 1;
}

sub post_codeconv () {
    return 0;
}

sub add_magic ($) {
    my ($magic) = @_;

    $magic->addMagicEntry('0	string		\<!--\ MHonArc		text/html; x-type=mhonarc');

    return;
}

sub filter ($$$$$) {
    my ($orig_cfile, $contref, $weighted_str, $headings, $fields)
      = @_;
    my $cfile = defined $orig_cfile ? $$orig_cfile : '';

    util::vprint("Processing MHonArc file ...\n");

    if (($cfile !~ /$MHONARC_MESSAGE_FILE/o) ||
	($$contref !~ /\A\s*<!-- MHonArc /))
    {
	return "is not a MHonArc message file! skipped."; # error
    }

    mhonarc_filter($contref, $weighted_str, $fields);
    html::html_filter($contref, $weighted_str, $fields, $headings);

    $$contref =~ s/^\s+//;
    mailnews::uuencode_filter($contref);
    mailnews::mailnews_filter($contref, $weighted_str, $fields);
    mailnews::mailnews_citation_filter($contref, $weighted_str);

    gfilter::line_adjust_filter($contref);
    gfilter::line_adjust_filter($weighted_str);
    gfilter::white_space_adjust_filter($contref);
    gfilter::show_filter_debug_info($contref, $weighted_str,
			   $fields, $headings);
    return undef;
}

# Assume a normal message files by MHonArc v2.x
sub mhonarc_filter ($$) {
    my ($contref, $weighted_str, $fields) = @_;
    my $mha_fields = { };

    my $pos = index($$contref, '<!--X-Head-End-->');
    if ($pos > 0) {
	load_mhonarc_fields($mha_fields, $weighted_str,
			    substr($$contref, 0, $pos));
    }

    # Strip off front-matter
    $pos = index($$contref, '<!--X-Head-of-Message-->');
    substr($$contref, 0, $pos + length('<!--X-Head-of-Message-->')) = "";

    # Strip off end-matter
    $pos = index($$contref, '<!--X-Body-of-Message-End-->');
    substr($$contref, $pos) = "";

    # Extract message header for separate processing, will be added back
    my $msg_header = "";
    $pos = index($$contref, '<!--X-Head-of-Message-End-->');
    if ($pos >= 0) {
	$msg_header = substr($$contref, 0, $pos);
	substr($$contref, 0, $pos) = "";
    }

    # Strip out stuff between header and body
    $pos = index($$contref, '<!--X-Body-of-Message-->');
    substr($$contref, 0, $pos + length('<!--X-Body-of-Message-->')) = "";

    # Reformat header to make it nice for mailnews filter
    if ($msg_header ne "") {
	$msg_header =~ s/\A\s+//;
	html::remove_html_elements(\$msg_header);
	$msg_header =~ s/^\s*([\w\-_]+:)/$1/gm;
	$msg_header =~ s/^([\w\-_]+:)(?:[^\n\S]*\n[^\n\S]*)+/$1 /gm;
    }

    # Format MHonArc X comment extracted headers as regular headers
    my $mha_header = "";
    my($fld_name, $fld_value);
    while (($fld_name, $fld_value) = each %$mha_fields) {
	$mha_header .= join('', $fld_name, ': ', $fld_value, "\n");
    }

    # Added header back to content string.
    $$contref = $mha_header . $msg_header . "\n" . $$contref;

    # Return extract MHonArc fields
    #$mha_fields;
}

sub load_mhonarc_fields {
    my $fields	     = shift;
    my $weighted_str = shift;
    my $mha_head     = shift;

    if ($mha_head =~ /<!--X-Subject: ([^-]+) -->/) {
	my $subject = uncommentize($1);
        # codeconv::toeuc(\$subject);
        codeconv::codeconv_document(\$subject);
	1  while ($subject =~ s/\A\s*(re|sv|fwd|fw)[\[\]\d]*[:>-]+\s*//i);
	$subject =~ s/\A\s*\[[^\]]+\]\s*//;
	$fields->{'subject'} = $subject;
    }
    if ($mha_head =~ /<!--X-From-R13: ([^-]+) -->/) {
        my $from = mrot13(uncommentize($1));
        # codeconv::toeuc(\$from);
        codeconv::codeconv_document(\$from);
        $fields->{'from'} = $from;
    } elsif ($mha_head =~ /<!--X-From: ([^-]+) -->/) {
        my $from = uncommentize($1);
        # codeconv::toeuc(\$from);
        codeconv::codeconv_document(\$from);
        $fields->{'from'} = $from;
    }
    if ($mha_head =~ /<!--X-Message-Id: ([^-]+) -->/) {
	$fields->{'message-id'} = '&lt;' . uncommentize($1). '&gt;';
    }
    if ($mha_head =~ /<!--X-Date: ([^-]+) -->/) {
	$fields->{'date'} = uncommentize($1);
    }
}

sub uncommentize {
    my($txt) = $_[0];
    $txt =~ s/&#(\d+);/pack("C",$1)/ge;
    $txt;
}

sub mrot13 {
    my $str     = shift;
    $str =~ tr/@A-Z[a-z/N-Z[@A-Mn-za-m/;
    $str;
}

1;