##---------------------------------------------------------------------------##
## $Id: mhrcvars.pl,v 2.21 2002/07/27 05:13:13 ehood Exp $
## Earl Hood mhonarc@mhonarc.org
## Defines routine for expanding resource variables.
##---------------------------------------------------------------------------##
## MHonArc -- Internet mail-to-HTML converter
## Copyright (C) 1996-2001 Earl Hood, mhonarc@mhonarc.org
## 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 version 2 of the License, 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
##---------------------------------------------------------------------------##
## Mapping of old resource variables to current versions.
'FIRSTPG' => [ 'PG', 'FIRST' ],
'LASTPG' => [ 'PG', 'LAST' ],
'NEXTBUTTON' => [ 'BUTTON', 'NEXT' ],
'NEXTFROM' => [ 'FROM', 'NEXT' ],
'NEXTFROMADDR' => [ 'FROMADDR', 'NEXT' ],
'NEXTFROMNAME' => [ 'FROMNAME', 'NEXT' ],
'NEXTLINK' => [ 'LINK', 'NEXT' ],
'NEXTMSG' => [ 'MSG', 'NEXT' ],
'NEXTMSGNUM' => [ 'MSGNUM', 'NEXT' ],
'NEXTPG' => [ 'PG', 'NEXT' ],
'NEXTPGLINK' => [ 'PGLINK', 'NEXT' ],
'NEXTSUBJECT' => [ 'SUBJECT', 'NEXT' ],
'PREVBUTTON' => [ 'BUTTON', 'PREV' ],
'PREVFROM' => [ 'FROM', 'PREV' ],
'PREVFROMADDR' => [ 'FROMADDR', 'PREV' ],
'PREVFROMNAME' => [ 'FROMNAME', 'PREV' ],
'PREVLINK' => [ 'LINK', 'PREV' ],
'PREVMSG' => [ 'MSG', 'PREV' ],
'PREVMSGNUM' => [ 'MSGNUM', 'PREV' ],
'PREVPGLINK' => [ 'PGLINK', 'PREV' ],
'PREVPG' => [ 'PG', 'PREV' ],
'PREVSUBJECT' => [ 'SUBJECT', 'PREV' ],
'TFIRSTPG' => [ 'PG', 'TFIRST' ],
'TLASTPG' => [ 'PG', 'TLAST' ],
'TNEXTBUTTON' => [ 'BUTTON', 'TNEXT' ],
'TNEXTFROM' => [ 'FROM', 'TNEXT' ],
'TNEXTFROMADDR' => [ 'FROMADDR', 'TNEXT' ],
'TNEXTFROMNAME' => [ 'FROMNAME', 'TNEXT' ],
'TNEXTLINK' => [ 'LINK', 'TNEXT' ],
'TNEXTMSG' => [ 'MSG', 'TNEXT' ],
'TNEXTMSGNUM' => [ 'MSGNUM', 'TNEXT' ],
'TNEXTPGLINK' => [ 'PGLINK', 'TNEXT' ],
'TNEXTPG' => [ 'PG', 'TNEXT' ],
'TNEXTSUBJECT' => [ 'SUBJECT', 'TNEXT' ],
'TPREVBUTTON' => [ 'BUTTON', 'TPREV' ],
'TPREVFROM' => [ 'FROM', 'TPREV' ],
'TPREVFROMADDR' => [ 'FROMADDR', 'TPREV' ],
'TPREVFROMNAME' => [ 'FROMNAME', 'TPREV' ],
'TPREVLINK' => [ 'LINK', 'TPREV' ],
'TPREVMSG' => [ 'MSG', 'TPREV' ],
'TPREVMSGNUM' => [ 'MSGNUM', 'TPREV' ],
'TPREVPGLINK' => [ 'PGLINK', 'TPREV' ],
'TPREVPG' => [ 'PG', 'TPREV' ],
'TPREVSUBJECT' => [ 'SUBJECT', 'TPREV' ],
##---------------------------------------------------------------------------
## replace_li_var() is used to substitute vars to current
## values. This routine relies on some variables being set by the
## calling routine or as globals.
my($val, $index) = ($_[0], $_[1]);
my($var,$len,$canclip,$raw,$isurl,$tmp,$ret) = ('',0,0,0,0,'','');
my($arg, $opt) = ("", "");
## Get variable argument string
if ($val =~ s/\(([^()]*)\)//) {
## Get length specifier (if defined)
($var, $len) = split(/:/, $val, 2);
$len = -1 unless defined $len;
## Check for old resource variables and map to new
($var, $arg) = @
{$old2new{$var}} if defined($old2new{$var});
## Check if variable in a URL string
$isurl = 1 if ($len =~ s/u//ig);
## Check if variable in a JavaScript string
$jstr = 1 if ($len =~ s/j//ig);
## Do variable replacement
## Invoke callback if defined
if (defined($CBRcVarExpand) && defined(&$CBRcVarExpand)) {
($tmp, $expand, $canclip) = &$CBRcVarExpand($index, $var, $arg);
last REPLACESW
if defined($tmp);
## -------------------------------------- ##
## Message information resource variables ##
## -------------------------------------- ##
if ($var eq 'DATE') { ## Message "Date:"
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
$Date{$key} : "";
if ($var eq 'DDMMYY' || $var eq 'DDMMYYYY' ||
$var eq 'MMDDYY' || $var eq 'MMDDYYYY' ||
$var eq 'YYMMDD' || $var eq 'YYYYMMDD') {
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
&time2mmddyy
((split(/$X/o, $key))[0], lc $var) :
my($cnd1, $cnd2, $cnd3) = (0,0,0);
if (($cnd1 = ($var eq 'FROM')) || ## Message "From:"
($cnd2 = ($var eq 'FROMADDR')) || ## Message from mail address
($cnd3 = ($var eq 'FROMNAME'))) { ## Message from name
my $esub = $cnd1 ?
sub { $_[0]; } :
$cnd2 ? \
&extract_email_address
:
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
&$esub($From{$key}) : "(nil)";
if ( ($cnd1 = ($var eq 'FROMADDRNAME')) ||
($cnd2 = ($var eq 'FROMADDRDOMAIN')) ) {
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
my @a = split(/@/, extract_email_address
($From{$key}), 2);
$tmp = defined($a[1]) ?
$a[1] : "";
if ($var eq 'ICON') { ## Message icon
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
my($iconurl, $iw, $ih) = mhonarc
::get_icon_url
($ContentType{$key});
my $alttext = $iconurl ?
$ContentType{$key} : 'unknown';
$tmp = qq|<img src
="$iconurl" border
="0" alt
="[$alttext]"|;
$tmp .= ' width="' . $iw . '"' if $iw;
$tmp .= ' height="' . $ih . '"' if $ih;
if ($var eq 'ICONURL') { ## URL to message icon
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
my($iconurl, $iw, $ih) = mhonarc
::get_icon_url
($ContentType{$key});
$tmp = $iconurl if defined($iconurl);
if ($var eq 'MSG') { ## Filename of message page
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
&msgnum_filename
($IndexNum{$key}) : "";
if ($var eq 'MSGGMTDATE') { ## Message GMT date
($lref, $key, $pos, $opt) = compute_msg_pos
($index, $var, $arg);
$tmp = &time2str
($opt || $MsgGMTDateFmt,
&get_time_from_index
($key), 0);
if ($var eq 'MSGID') { ## Message-ID
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
$Index2MsgId{$key} : "";
if ($var eq 'MSGLOCALDATE') { ## Message local date
($lref, $key, $pos, $opt) = compute_msg_pos
($index, $var, $arg);
$tmp = &time2str
($opt || $MsgLocalDateFmt,
&get_time_from_index
($key), 1);
if ($var eq 'MSGNUM') { ## Message number
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
&fmt_msgnum
($IndexNum{$key}) : "";
if ($var eq 'MSGTORDNUM') { ## Message ordinal num in cur thread
# Some form of optimization should be done here since
# computation can degrade to n^2 (where n is size of thread)
# if variable is referenced for each message on thread index
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg, 1);
my $level = $ThreadLevel{$key};
for (--$pos ; ($level > 0) && ($pos >= 0); --$pos, ++$tmp ) {
$level = $ThreadLevel{$TListOrder[$pos]};
if ($var eq 'NOTE') { ## Annotation template markup
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = note_exists
($key) ?
$NOTE : $NOTEIA;
if ($var eq 'NOTEICON') { ## Annotation ICON (HTML markup)
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = note_exists
($key) ?
$NOTEICON : $NOTEICONIA;
if ($var eq 'NOTETEXT') { ## Annotation text
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
if ($var eq 'NUMFOLUP') { ## Number of explicit follow-ups
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
$FolCnt{$key} : "";
if ($var eq 'ORDNUM') { ## Sort order number of message
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
$pos+1 : -1;
if ($var eq 'SUBJECT') { ## Message subject
$canclip = 1; $raw = 1; $isurl = 0;
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = $NoSubjectTxt if $tmp eq "";
if ($var eq 'SUBJECTNA') { ## Message subject (not linked)
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = $NoSubjectTxt if $tmp eq "";
if ($var eq 'TLEVEL') { ## Thread level
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = $ThreadLevel{$key};
## ------------------------------------- ##
## Message navigation resource variables ##
## ------------------------------------- ##
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
$NEXTBUTTON : $NEXTBUTTONIA;
$tmp = defined($key) ?
$PREVBUTTON : $PREVBUTTONIA;
$tmp = defined($key) ?
$TNEXTBUTTON : $TNEXTBUTTONIA;
$tmp = defined($key) ?
$TPREVBUTTON : $TPREVBUTTONIA;
$tmp = defined($key) ?
$TNEXTINBUTTON : $TNEXTINBUTTONIA;
$tmp = defined($key) ?
$TPREVINBUTTON : $TPREVINBUTTONIA;
if ($arg eq 'TNEXTTOP') {
$tmp = defined($key) ?
$TNEXTTOPBUTTON : $TNEXTTOPBUTTONIA;
if ($arg eq 'TPREVTOP') {
$tmp = defined($key) ?
$TPREVTOPBUTTON : $TPREVTOPBUTTONIA;
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
$tmp = defined($key) ?
$NEXTLINK : $NEXTLINKIA;
$tmp = defined($key) ?
$PREVLINK : $PREVLINKIA;
$tmp = defined($key) ?
$TNEXTLINK : $TNEXTLINKIA;
$tmp = defined($key) ?
$TPREVLINK : $TPREVLINKIA;
$tmp = defined($key) ?
$TNEXTINLINK : $TNEXTINLINKIA;
$tmp = defined($key) ?
$TPREVINLINK : $TPREVINLINKIA;
if ($arg eq 'TNEXTTOP') {
$tmp = defined($key) ?
$TNEXTTOPLINK : $TNEXTTOPLINKIA;
if ($arg eq 'TPREVTOP') {
$tmp = defined($key) ?
$TPREVTOPLINK : $TPREVTOPLINKIA;
my($bcnt, $acnt, $inclusive);
($bcnt, $acnt, $inclusive) = split(/[;:]/, $arg);
$bcnt = $TSliceNBefore if (!defined($bcnt) || $bcnt !~ /^\d+$/);
$acnt = $TSliceNAfter if (!defined($acnt) || $acnt !~ /^\d+$/);
$inclusive = $TSliceInclusive if (!defined($inclusive));
$inclusive = $TSliceInclusive;
$tmp = &make_thread_slice
($index, $bcnt, $acnt, $inclusive)
if ($bcnt != 0 || $acnt != 0);
## -------------------------------- ##
## Index related resource variables ##
## -------------------------------- ##
if ($var eq 'A_ATTR') { ## Anchor attrs to link to message
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
if (!defined($key)) { $tmp = ""; last REPLACESW
; }
$tmp = qq/name="/ . &fmt_msgnum
($IndexNum{$key}) .
&msgnum_filename
($IndexNum{$key}) .
if ($var eq 'A_NAME') { ## Anchor name for message position
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
if (!defined($key)) { $tmp = ""; last REPLACESW
; }
$tmp = qq/name="/ . &fmt_msgnum
($IndexNum{$key}) . qq/"/;
if ($var eq 'A_HREF') { ## Anchor href to link to message
($lref, $key, $pos) = compute_msg_pos
($index, $var, $arg);
if (!defined($key)) { $tmp = ""; last REPLACESW
; }
$tmp = qq/href="/ . &msgnum_filename
($IndexNum{$key}) . qq/"/;
if ($var eq 'IDXFNAME') { ## Filename of index page
if ($MULTIIDX && ($n = int($Index2MLoc{$index}/$IDXSIZE)+1) > 1) {
$tmp = sprintf("%s%d.$HtmlExt",
$IDXPREFIX, $index ne '' ?
$n : 1);
$tmp .= ".gz" if $GzipLinks;
if ($var eq 'IDXLABEL') { ## Label for main index
if ($var eq 'IDXSIZE') { ## Index page size
if ($var eq 'IDXTITLE') { ## Main index title
$canclip = 1; $expand = 1;
if ($var eq 'NUMOFIDXMSG') { ## Number of items on the index page
if ($var eq 'NUMOFMSG') { ## Total number of messages
if ($var eq 'SORTTYPE') { ## Sort type of index
if ($NOSORT) { $tmp = 'Number'; last SORTTYPE
; }
if ($AUTHSORT) { $tmp = 'Author'; last SORTTYPE
; }
if ($SUBSORT) { $tmp = 'Subject'; last SORTTYPE
; }
if ($var eq 'TIDXFNAME') {
if ($MULTIIDX && ($n = int($Index2TLoc{$index}/$IDXSIZE)+1) > 1) {
$tmp = sprintf("%s%d.$HtmlExt",
$TIDXPREFIX, $index ne '' ?
$n : 1);
$tmp .= ".gz" if $GzipLinks;
if ($var eq 'TIDXLABEL') {
if ($var eq 'TIDXTITLE') {
$canclip = 1; $expand = 1;
if ($var eq 'TSORTTYPE') {
if ($TNOSORT) { $tmp = 'Number'; last TSORTTYPE
; }
if ($TSUBSORT) { $tmp = 'Subject'; last TSORTTYPE
; }
$tmp = $PageNum < $NumOfPages ?
$NEXTPGLINK : $NEXTPGLINKIA;
$tmp = $PageNum > 1 ?
$PREVPGLINK : $PREVPGLINKIA;
$tmp = $PageNum < $NumOfPages ?
$TNEXTPGLINK : $TNEXTPGLINKIA;
$tmp = $PageNum > 1 ?
$TPREVPGLINK : $TPREVPGLINKIA;
if ($var eq 'PGLINKLIST') {
my($before, $after) = split(/;/, $arg);
my $prefix = $t ?
$TIDXPREFIX : $IDXPREFIX;
$suffix .= '.gz' if $GzipLinks;
$before = $num - abs($before);
$before = 1 unless $before > 1;
$after = $num + abs($after);
$after = $NumOfPages unless $after < $NumOfPages;
for ($i=$before; $i < $num; ++$i) {
$tmp .= sprintf('<a href="%s%s">%d</a> | ',
($t ?
$TIDXNAME : $IDXNAME),
($GzipLinks ?
'.gz' : ""), $i);
$tmp .= sprintf('<a href="%s%d.%s">%d</a> | ',
$prefix, $i, $suffix, $i);
for ($i=$num+1; $i <= $after; ++$i) {
$tmp .= sprintf(' | <a href="%s%d.%s">%d</a>',
$prefix, $i, $suffix, $i);
if ($var eq 'NUMOFPAGES') {
my $t = ($arg =~ s/^T//);
my $prefix = $t ?
$TIDXPREFIX : $IDXPREFIX;
if ($arg eq 'NEXT') { $num = $PageNum+1; last SW
; }
if ($arg eq 'PREV') { $num = $PageNum-1; last SW
; }
if ($arg eq 'FIRST') { $num = 0; last SW
; }
if ($arg eq 'LAST') { $num = $NumOfPages; last SW
; }
if ($arg =~ /^-?\d+$/) { $num = $PageNum+$arg; last SW
; }
$tmp = $t ?
$TIDXNAME : $IDXNAME;
$num = $NumOfPages if $num > $NumOfPages;
$tmp = sprintf("%s%d.$HtmlExt", $prefix, $num);
$tmp .= ".gz" if $GzipLinks;
## -------------------------------- ##
## Miscellaneous resource variables ##
## -------------------------------- ##
$tmp = htmlize
($ENV{$arg});
$tmp = &time2str
($arg || $GMTDateFmt, time, 0);
if ($var eq 'IDXPREFIX') {
if ($var eq 'LOCALDATE') {
$tmp = &time2str
($arg || $LocalDateFmt, time, 1);
if ($var eq 'MSGPREFIX') {
if ($var eq 'TIDXPREFIX') {
## --------------------------- ##
## User defined variable check ##
## --------------------------- ##
if (defined($CustomRcVars{$var})) {
$tmp = $CustomRcVars{$var};
warn qq/Warning: Unrecognized variable: "$val"\n/;
## Check if string needs to be expanded again
$tmp =~ s/$VarExp/&replace_li_var($1,$index)/geo;
## Check if URL text specifier is set
$ret = &$MHeadCnvFunc($tmp);
if ($len > 0 && $canclip && (length($ret) > 0)) {
$ret = &$TextClipFunc($ret, $len, 1);
# Check if JavaScript string
$ret =~ s/\\/\\\\/g; # escape backslashes
$ret =~ s/(["'])/\\$1/g; # escape quotes
$ret =~ s/\n/\\n/g; # escape newlines
$ret =~ s/\r/\\r/g; # escape returns
## Check for subject link
&fmt_msgnum($IndexNum{$index}) .
&msgnum_filename($IndexNum{$index}) .
if $var eq 'SUBJECT' && $arg eq "";
##---------------------------------------------------------------------------##
## compute_msg_pos(): Get message location data.
## ($aref, : Reference to message listing array.
## $key, : Message index key
## $pos, : Integer offset location in $aref
## $opt) : Left-over option string
## $key will be undefined and $post will be set to -1 if message
## position cannot be computed or is out-of-bounds.
my($idx, $var, $arg, $usethread) = @_;
my($ofs, $pos, $aref, $href, $key);
## Determine what list type
if (($arg =~ s/^T//) || $usethread) {
## Extract out optional data
($arg, $opt) = split(/;/, $arg, 2);
if ($usethread && $TREVERSE) {
# when threads are listed in reverse, we preserve the
# sematics of "next/prev thread"
} elsif ($arg eq 'PREVTOP') {
if (!defined($arg)) || ($arg eq '') || ($arg eq 'CUR');
$ofs = ($flip ?
-$arg : $arg), last SW
if ($arg eq 'NEXT') { # next message
if (!$usethread || !$TREVERSE) {
# get here, it is thread and reverse
if (($pos < $#$aref) && ($ThreadLevel{$aref->[$pos+1]} > 0)) {
# get here, must goto physical previous top
# note no `last SW' statement
if ($arg eq 'PREV') { # prev message
if (!$usethread || !$TREVERSE) {
# get here, it is thread and reverse
if ($ThreadLevel{$idx} > 0) {
if (($pos > 0) && ($ThreadLevel{$aref->[$pos-1]} >= 0)) {
# get here, must goto physical next top
# note no `last SW' statement
$pos = $flip ?
$#$aref : 0;
$pos = $flip ?
0 : $#$aref;
# if not thread variable, no more checking
warn qq/Warning: $var: Invalid variable argument: "$orgarg"\n/;
if ($arg eq 'NEXTIN') { # next message within a thread
$pos = $href->{$idx} + 1;
if ($pos > $#$aref || $ThreadLevel{$aref->[$pos]} <= 0) {
if ($arg eq 'PREVIN') { # previous message within a thread
if ($ThreadLevel{$aref->[$pos]} <= 0) {
if ($arg eq 'PARENT') { # parent message in thread
my $level = $ThreadLevel{$idx};
$pos = $Index2TLoc{$idx};
last SW
if ($level <= 0);
for (--$pos; $pos >= 0; --$pos) {
last if $ThreadLevel{$aref->[$pos]} < $level;
$pos = $Index2TLoc{$idx};
for (; $pos >= 0; --$pos) {
last if $ThreadLevel{$aref->[$pos]} <= 0;
if (($arg eq 'NEXTTOP') ){ # start of next thread
$pos = $Index2TLoc{$idx};
for (++$pos; $pos <= $#$aref; ++$pos) {
last if $ThreadLevel{$aref->[$pos]} <= 0;
if (($arg eq 'PREVTOP') ){ # start of previous thread
# Find current top first, then find previous top
for ($pos = $Index2TLoc{$idx}; $pos >= 0; --$pos) {
last if $ThreadLevel{$aref->[$pos]} <= 0;
for (--$pos; $pos >= 0; --$pos) {
last if $ThreadLevel{$aref->[$pos]} <= 0;
if ($arg eq 'END') { # last message of thread
$pos = $Index2TLoc{$idx};
for (; $pos < $#$aref; ++$pos) {
last if $ThreadLevel{$aref->[$pos+1]} <= 0;
warn qq/Warning: $var: Unrecognized variable argument: "$orgarg"\n/;
$pos = $href->{$idx} + $ofs if defined($ofs);
if (($pos > $#$aref) || ($pos < 0)) {
($aref, $key, $pos, $opt);
##---------------------------------------------------------------------------##