| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: mhdysub.pl,v 2.12 2009/05/03 20:11:27 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## Definition of create_routines() that creates routines are | ||
| 8 | ## runtime. | ||
| 9 | ##---------------------------------------------------------------------------## | ||
| 10 | ## MHonArc -- Internet mail-to-HTML converter | ||
| 11 | ## Copyright (C) 1996-2001 Earl Hood, mhonarc@mhonarc.org | ||
| 12 | ## | ||
| 13 | ## This program is free software; you can redistribute it and/or modify | ||
| 14 | ## it under the terms of the GNU General Public License as published by | ||
| 15 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 16 | ## (at your option) any later version. | ||
| 17 | ## | ||
| 18 | ## This program is distributed in the hope that it will be useful, | ||
| 19 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 20 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 21 | ## GNU General Public License for more details. | ||
| 22 | ## | ||
| 23 | ## You should have received a copy of the GNU General Public License | ||
| 24 | ## along with this program; if not, write to the Free Software | ||
| 25 | ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 26 | ##---------------------------------------------------------------------------## | ||
| 27 | |||
| 28 | package mhonarc; | ||
| 29 | |||
| 30 | my $_sub_eval_cnt = 0; | ||
| 31 | |||
| 32 | ##--------------------------------------------------------------------------- | ||
| 33 | ## create_routines is used to dynamically create routines that | ||
| 34 | ## would benefit from being create at run-time. Routines | ||
| 35 | ## that have to check against several regular expressions | ||
| 36 | ## are candidates. | ||
| 37 | ## | ||
| 38 | ## NOTE: Subroutine references would be cleaner, but code | ||
| 39 | ## pre-dates Perl 5 where references were not supported. | ||
| 40 | ## | ||
| 41 | sub create_routines { | ||
| 42 | my ($sub) = ''; | ||
| 43 | |||
| 44 | ##----------------------------------------------------------------------- | ||
| 45 | ## exclude_field: Used to determine if field should be excluded from | ||
| 46 | ## message header | ||
| 47 | ## | ||
| 48 | $sub = <<'EndOfRoutine'; | ||
| 49 | sub exclude_field { | ||
| 50 | my($f) = shift; | ||
| 51 | my $ret = 0; | ||
| 52 | EXC_FIELD_SW: { | ||
| 53 | EndOfRoutine | ||
| 54 | |||
| 55 | # Create switch block for checking field against regular | ||
| 56 | # expressions (a large || statement could also work). | ||
| 57 | my $pat; | ||
| 58 | foreach $pat (keys %HFieldsExc) { | ||
| 59 | $sub .= join('', | ||
| 60 | 'if ($f =~ /^', $pat, '/i) { $ret = 1; last EXC_FIELD_SW; }', | ||
| 61 | "\n"); | ||
| 62 | } | ||
| 63 | |||
| 64 | $sub .= <<'EndOfRoutine'; | ||
| 65 | }; | ||
| 66 | $ret; | ||
| 67 | } | ||
| 68 | EndOfRoutine | ||
| 69 | |||
| 70 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 71 | ++$_sub_eval_cnt; | ||
| 72 | eval $sub; | ||
| 73 | die("ERROR: Unable to create exclude_field routine:\n$@\n") if $@; | ||
| 74 | |||
| 75 | ##----------------------------------------------------------------------- | ||
| 76 | ## subject_strip: Used to apply user-defined s/// operations on | ||
| 77 | ## message subjects as they are read; | ||
| 78 | ## | ||
| 79 | $sub = <<EndOfRoutine; | ||
| 80 | sub subject_strip { | ||
| 81 | local(\$_) = shift; | ||
| 82 | $SubStripCode; | ||
| 83 | \$_; | ||
| 84 | } | ||
| 85 | EndOfRoutine | ||
| 86 | |||
| 87 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 88 | ++$_sub_eval_cnt; | ||
| 89 | eval $sub; | ||
| 90 | die("ERROR: Unable to create subject_strip routine:\n$@\n") if $@; | ||
| 91 | |||
| 92 | ##----------------------------------------------------------------------- | ||
| 93 | ## Routine to determine last message number in use. | ||
| 94 | ## If $LastMsgNum set, we return it after a sanity check | ||
| 95 | ## is done. If check fails, scan directory to determine last number. | ||
| 96 | ## | ||
| 97 | $sub = <<'EndOfRoutine'; | ||
| 98 | sub get_last_msg_num { | ||
| 99 | opendir(DIR, $OUTDIR) || die("ERROR: Unable to open $OUTDIR\n"); | ||
| 100 | my($max) = -1; | ||
| 101 | if ($LastMsgNum >= 0) { | ||
| 102 | return $LastMsgNum if ( | ||
| 103 | -s $OUTDIR . $DIRSEP . msgnum_filename($LastMsgNum)) && | ||
| 104 | ((! -f $OUTDIR . $DIRSEP . msgnum_filename($LastMsgNum + 1)) | ||
| 105 | ); | ||
| 106 | } | ||
| 107 | |||
| 108 | my $msgrex = '^'. | ||
| 109 | "\Q$MsgPrefix". | ||
| 110 | '(\d+)\.'. | ||
| 111 | "\Q$HtmlExt". | ||
| 112 | '$'; | ||
| 113 | chop $msgrex if ($HtmlExt =~ /html$/i); | ||
| 114 | |||
| 115 | foreach (readdir(DIR)) { | ||
| 116 | if (/$msgrex/io) { $max = int($1) if $1 > $max; } | ||
| 117 | } | ||
| 118 | closedir(DIR); | ||
| 119 | $LastMsgNum = $max; | ||
| 120 | $max; | ||
| 121 | } | ||
| 122 | EndOfRoutine | ||
| 123 | |||
| 124 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 125 | ++$_sub_eval_cnt; | ||
| 126 | eval $sub; | ||
| 127 | die("ERROR: Unable to create get_last_msg_num routine:\n$@\n") if $@; | ||
| 128 | |||
| 129 | ##----------------------------------------------------------------------- | ||
| 130 | ## Routine to get base subject text from index | ||
| 131 | ## | ||
| 132 | $sub = <<'EndOfRoutine'; | ||
| 133 | sub get_base_subject { | ||
| 134 | my($ret) = ($Subject{$_[0]}); | ||
| 135 | 1 while $ret =~ s/$SubReplyRxp//io; | ||
| 136 | if ($ret eq "") { | ||
| 137 | return $NoSubjectTxt; | ||
| 138 | } | ||
| 139 | $ret; | ||
| 140 | } | ||
| 141 | EndOfRoutine | ||
| 142 | |||
| 143 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 144 | ++$_sub_eval_cnt; | ||
| 145 | eval $sub; | ||
| 146 | die("ERROR: Unable to create get_base_subject routine:\n$@\n") if $@; | ||
| 147 | |||
| 148 | ##----------------------------------------------------------------------- | ||
| 149 | ## Routine to rewrite mail addresses in message header | ||
| 150 | ## | ||
| 151 | $sub = <<EndOfRoutine; | ||
| 152 | sub rewrite_address { | ||
| 153 | package mhonarc::Pkg_rewrite_address; | ||
| 154 | local \$_ = mhonarc::dehtmlize(shift); | ||
| 155 | $AddressModify; | ||
| 156 | \$_; | ||
| 157 | } | ||
| 158 | EndOfRoutine | ||
| 159 | |||
| 160 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 161 | ++$_sub_eval_cnt; | ||
| 162 | eval $sub; | ||
| 163 | die("ERROR: Unable to create rewrite_address routine:\n$@\n") if $@; | ||
| 164 | |||
| 165 | ##----------------------------------------------------------------------- | ||
| 166 | ## Routine to rewrite raw mail addresses | ||
| 167 | ## | ||
| 168 | $sub = <<EndOfRoutine; | ||
| 169 | sub rewrite_raw_address { | ||
| 170 | package mhonarc::Pkg_rewrite_raw_address; | ||
| 171 | local \$_ = shift; | ||
| 172 | $AddressModify; | ||
| 173 | \$_; | ||
| 174 | } | ||
| 175 | EndOfRoutine | ||
| 176 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 177 | ++$_sub_eval_cnt; | ||
| 178 | eval $sub; | ||
| 179 | die("ERROR: Unable to create rewrite_raw_address routine:\n$@\n") | ||
| 180 | if $@; | ||
| 181 | |||
| 182 | ##----------------------------------------------------------------------- | ||
| 183 | ## message_exclude: User-defined code to check if a message should | ||
| 184 | ## be added or not. | ||
| 185 | ## | ||
| 186 | $sub = <<EndOfRoutine; | ||
| 187 | sub message_exclude { | ||
| 188 | package mhonarc::Pkg_message_exclude; | ||
| 189 | local(\$_) = shift; | ||
| 190 | $MsgExcFilter; | ||
| 191 | } | ||
| 192 | EndOfRoutine | ||
| 193 | |||
| 194 | $sub .= "# $_sub_eval_cnt\n"; | ||
| 195 | ++$_sub_eval_cnt; | ||
| 196 | eval $sub; | ||
| 197 | die("ERROR: Unable to create subject_strip routine:\n$@\n") if $@; | ||
| 198 | |||
| 199 | } | ||
| 200 | |||
| 201 | ##---------------------------------------------------------------------------## | ||
| 202 | 1; |