<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">##---------------------------------------------------------------------------##
##  File:
##	$Id: mhrcvars.pl,v 2.29 2009/05/03 20:11:27 ehood Exp $
##  Author:
##      Earl Hood       mhonarc@mhonarc.org
##  Description:
##      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
##    02111-1307, USA
##---------------------------------------------------------------------------##

package mhonarc;

## Mapping of old resource variables to current versions.
my %old2new = (
    'FIRSTPG'    	=&gt; [ 'PG', 'FIRST' ],
    'LASTPG'    	=&gt; [ 'PG', 'LAST' ],
    'NEXTBUTTON'    	=&gt; [ 'BUTTON', 'NEXT' ],
    'NEXTFROM'    	=&gt; [ 'FROM', 'NEXT' ],
    'NEXTFROMADDR'    	=&gt; [ 'FROMADDR', 'NEXT' ],
    'NEXTFROMNAME'    	=&gt; [ 'FROMNAME', 'NEXT' ],
    'NEXTLINK'    	=&gt; [ 'LINK', 'NEXT' ],
    'NEXTMSG'    	=&gt; [ 'MSG', 'NEXT' ],
    'NEXTMSGNUM'    	=&gt; [ 'MSGNUM', 'NEXT' ],
    'NEXTPG'    	=&gt; [ 'PG', 'NEXT' ],
    'NEXTPGLINK'    	=&gt; [ 'PGLINK', 'NEXT' ],
    'NEXTSUBJECT'	=&gt; [ 'SUBJECT', 'NEXT' ],
    'PREVBUTTON'    	=&gt; [ 'BUTTON', 'PREV' ],
    'PREVFROM'    	=&gt; [ 'FROM', 'PREV' ],
    'PREVFROMADDR'    	=&gt; [ 'FROMADDR', 'PREV' ],
    'PREVFROMNAME'    	=&gt; [ 'FROMNAME', 'PREV' ],
    'PREVLINK'    	=&gt; [ 'LINK', 'PREV' ],
    'PREVMSG'    	=&gt; [ 'MSG', 'PREV' ],
    'PREVMSGNUM'    	=&gt; [ 'MSGNUM', 'PREV' ],
    'PREVPGLINK'    	=&gt; [ 'PGLINK', 'PREV' ],
    'PREVPG'    	=&gt; [ 'PG', 'PREV' ],
    'PREVSUBJECT'	=&gt; [ 'SUBJECT', 'PREV' ],
    'TFIRSTPG'    	=&gt; [ 'PG', 'TFIRST' ],
    'TLASTPG'    	=&gt; [ 'PG', 'TLAST' ],
    'TNEXTBUTTON'    	=&gt; [ 'BUTTON', 'TNEXT' ],
    'TNEXTFROM'    	=&gt; [ 'FROM', 'TNEXT' ],
    'TNEXTFROMADDR'    	=&gt; [ 'FROMADDR', 'TNEXT' ],
    'TNEXTFROMNAME'    	=&gt; [ 'FROMNAME', 'TNEXT' ],
    'TNEXTLINK'    	=&gt; [ 'LINK', 'TNEXT' ],
    'TNEXTMSG'    	=&gt; [ 'MSG', 'TNEXT' ],
    'TNEXTMSGNUM'    	=&gt; [ 'MSGNUM', 'TNEXT' ],
    'TNEXTPGLINK'    	=&gt; [ 'PGLINK', 'TNEXT' ],
    'TNEXTPG'    	=&gt; [ 'PG', 'TNEXT' ],
    'TNEXTSUBJECT'	=&gt; [ 'SUBJECT', 'TNEXT' ],
    'TPREVBUTTON'    	=&gt; [ 'BUTTON', 'TPREV' ],
    'TPREVFROM'    	=&gt; [ 'FROM', 'TPREV' ],
    'TPREVFROMADDR'    	=&gt; [ 'FROMADDR', 'TPREV' ],
    'TPREVFROMNAME'    	=&gt; [ 'FROMNAME', 'TPREV' ],
    'TPREVLINK'    	=&gt; [ 'LINK', 'TPREV' ],
    'TPREVMSG'    	=&gt; [ 'MSG', 'TPREV' ],
    'TPREVMSGNUM'    	=&gt; [ 'MSGNUM', 'TPREV' ],
    'TPREVPGLINK'    	=&gt; [ 'PGLINK', 'TPREV' ],
    'TPREVPG'    	=&gt; [ 'PG', 'TPREV' ],
    'TPREVSUBJECT'	=&gt; [ '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.
##
sub replace_li_var {
    my($val, $index) = ($_[0], $_[1]);
    my($var,$len,$canclip,$raw,$isurl,$tmp,$ret) = ('',0,0,0,0,'','');
    my($jstr) = (0);
    my($expand) = (0);
    my($n) = (0);
    my($lref, $key, $pos);
    my($arg, $opt) = ("", "");
    my $isaddr = 0;
    my $mailto = 0;

    ##	Get variable argument string
    if ($val =~ s/\(([^()]*)\)//) {
	$arg = $1;
    }

    ##	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//g);	
    ##	Check if variable in a JavaScript string
    $jstr  = 1  if ($len =~ s/J//g);	
    ##	Check if variable in a JavaScript string
    $mailto = 1 if ($len =~ s/M//g);	

    ##	Do variable replacement
    REPLACESW: {
	## Invoke callback if defined
	if (defined($CBRcVarExpand) &amp;&amp; defined(&amp;$CBRcVarExpand)) {
	    ($tmp, $expand, $canclip) = &amp;$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} : "";
	    last REPLACESW;
	}
    	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);
	    $tmp = defined($key) ?
			&amp;time2mmddyy((split(/$X/o, $key))[0], lc $var) :
			"";
	    last REPLACESW;
	}
	if ($var eq 'FROM') {		## Message "From:"
	    $canclip = 1; $raw = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = defined($key) ? $From{$key} : '(nil)';
	    $isaddr = 1;
	    last REPLACESW;
	}
	if ($var eq 'FROMADDR') {	## Message from mail address
	    $canclip = 1; $raw = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) {
		$tmp = '(nil)';
		last REPLACESW;
	    }
	    my $caddr = $FromAddr{$key};
	    $tmp = defined($caddr)
		    ? $caddr
		    : extract_email_address($From{$key});
	    $isaddr = 1;
	    last REPLACESW;
	}
	if ($var eq 'FROMNAME') {	## Message from mail name
	    $canclip = 1; $raw = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) {
		$tmp = '(nil)';
		last REPLACESW;
	    }
	    my $cname = $FromName{$key};
	    $tmp = defined($cname)
		    ? $cname
		    : extract_email_name($From{$key});
	    if ($SpamMode) {
		$tmp =~ s/($AddrExp)/rewrite_raw_address($1)/geo;
	    } else {
		$isaddr = 1;
	    }
	    last REPLACESW;
	}

	my($cnd1, $cnd2) = (0,0);
    	if ( ($cnd1 = ($var eq 'FROMADDRNAME')) ||
	     ($cnd2 = ($var eq 'FROMADDRDOMAIN')) ) {
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) {
		$tmp = "";
		last REPLACESW;
	    }
	    my $caddr = $FromAddr{$key};
	    my @a;
	    if (defined($caddr)) {
	      @a = split(/@/, $caddr, 2);
	    } else {
	      @a = split(/@/, extract_email_address($From{$key}), 2);
	    }
	    if ($cnd1) {
		$tmp = $a[0];
		last REPLACESW;
	    }
	    $tmp = defined($a[1]) ? $a[1] : "";
	    last REPLACESW;
	}
    	if ($var eq 'ICON') {		## Message icon
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) {
		$tmp = "";
		last REPLACESW;
	    }
	    my($iconurl, $iw, $ih) = mhonarc::get_icon_url($ContentType{$key});
	    my $alttext = $iconurl ? $ContentType{$key} : 'unknown';
	    $tmp  = qq|&lt;img src="$iconurl" border="0" alt="[$alttext]"|;
	    $tmp .= ' width="' .  $iw . '"'  if $iw;
	    $tmp .= ' height="' . $ih . '"'  if $ih;
	    $tmp .= '&gt;';
	    last REPLACESW;
	}
    	if ($var eq 'ICONURL') {	## URL to message icon
	    $isurl = 0;
	    ($lref, $key, $pos)    = compute_msg_pos($index, $var, $arg);
	    my($iconurl, $iw, $ih) = mhonarc::get_icon_url($ContentType{$key});
	    $tmp = $iconurl  if defined($iconurl);
	    last REPLACESW;
	}
    	if ($var eq 'ICONURLPREFIX') {	## URL prefix to message icon
	    $isurl = 0;
	    $tmp = $IconURLPrefix;
	    last REPLACESW;
	}
    	if ($var eq 'MSG') {		## Filename of message page
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = defined($key) ? &amp;msgnum_filename($IndexNum{$key}) : "";
	    last REPLACESW;
	}
	if ($var eq 'MSGHFIELD') {	## Message header field
	    $canclip = 1; $raw = 1;
	    ($lref, $key, $pos, $opt) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) {
		$tmp = '';
		last REPLACESW;
	    }
	    $opt =~ s/\s+//g;  $opt = lc $opt;
	    HFIELD: {
		my $fields = $ExtraHFields{$key};
		if (defined($fields) &amp;&amp; defined($tmp = $fields-&gt;{$opt})) {
		    last HFIELD;
		}
		if ($opt eq 'subject') {
		    $tmp = $Subject{$key};
		    $tmp = $NoSubjectTxt  if $tmp eq '';
		    last HFIELD;
		}
		$tmp = '';
	    }
	    if (!$HFieldsAsIsList{$opt}) {
		$isaddr = 1;
	    }
	    last REPLACESW;
	}
    	if ($var eq 'MSGGMTDATE') {	## Message GMT date
	    ($lref, $key, $pos, $opt) = compute_msg_pos($index, $var, $arg);
	    $tmp = &amp;time2str($opt || $MsgGMTDateFmt,
			     $Time{$key}, 0);
	    last REPLACESW;
	}
    	if ($var eq 'MSGID') {		## Message-ID
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = defined($key) ? $Index2MsgId{$key} : "";
	    last REPLACESW;
	}
    	if ($var eq 'MSGLOCALDATE') {	## Message local date
	    ($lref, $key, $pos, $opt) = compute_msg_pos($index, $var, $arg);
	    $tmp = &amp;time2str($opt || $MsgLocalDateFmt,
			     $Time{$key}, 1);
	    last REPLACESW;
	}
    	if ($var eq 'MSGNUM') {		## Message number
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = defined($key) ? &amp;fmt_msgnum($IndexNum{$key}) : "";
	    last REPLACESW;
	}
    	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
	    # page.
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg, 1);
	    $tmp = 1;
	    my $level = $ThreadLevel{$key};
	    for (--$pos ; ($level &gt; 0) &amp;&amp; ($pos &gt;= 0); --$pos, ++$tmp ) {
		$level = $ThreadLevel{$TListOrder[$pos]};
	    }
	    last REPLACESW;
	}
    	if ($var eq 'NOTE') {		## Annotation template markup
	    $expand = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = note_exists($key) ? $NOTE : $NOTEIA;
	    last REPLACESW;
	}
    	if ($var eq 'NOTEICON') {	## Annotation ICON (HTML markup)
	    $expand = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = note_exists($key) ? $NOTEICON : $NOTEICONIA;
	    last REPLACESW;
	}
    	if ($var eq 'NOTETEXT') {	## Annotation text
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = get_note($key);
	    last REPLACESW;
	}
    	if ($var eq 'NUMFOLUP') {	## Number of explicit follow-ups
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = defined($key) ? $FolCnt{$key} : "";
	    last REPLACESW;
	}
    	if ($var eq 'ORDNUM') {		## Sort order number of message
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = defined($key) ? $pos+1 : -1;
	    last REPLACESW;
	}
    	if ($var eq 'SUBJECT') {	## Message subject
	    $canclip = 1; $raw = 1; $isurl = 0; $isaddr = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (defined($key)) {
		$tmp = $Subject{$key};
		$tmp = $NoSubjectTxt  if $tmp eq "";
	    } else {
		$tmp = "";
	    }
	    last REPLACESW;
	}
    	if ($var eq 'SUBJECTNA') {	## Message subject (not linked)
	    $canclip = 1; $raw = 1; $isaddr = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (defined($key)) {
		$tmp = $Subject{$key};
		$tmp = $NoSubjectTxt  if $tmp eq "";
	    } else {
		$tmp = "";
	    }
	    last REPLACESW;
	}
    	if ($var eq 'TLEVEL') {		## Thread level
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    $tmp = $ThreadLevel{$key};
	    last REPLACESW;
	}

	## ------------------------------------- ##
	## Message navigation resource variables ##
	## ------------------------------------- ##
	if ($var eq 'BUTTON') {
	    $expand = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    SW: {
		if ($arg eq 'NEXT') {
		    $tmp = defined($key) ? $NEXTBUTTON : $NEXTBUTTONIA;
		    last SW; }
		if ($arg eq 'PREV') {
		    $tmp = defined($key) ? $PREVBUTTON : $PREVBUTTONIA;
		    last SW; }
		if ($arg eq 'TNEXT') {
		    $tmp = defined($key) ? $TNEXTBUTTON : $TNEXTBUTTONIA;
		    last SW; }
		if ($arg eq 'TPREV') {
		    $tmp = defined($key) ? $TPREVBUTTON : $TPREVBUTTONIA;
		    last SW; }
		if ($arg eq 'TNEXTIN') {
		    $tmp = defined($key) ? $TNEXTINBUTTON : $TNEXTINBUTTONIA;
		    last SW; }
		if ($arg eq 'TPREVIN') {
		    $tmp = defined($key) ? $TPREVINBUTTON : $TPREVINBUTTONIA;
		    last SW; }
		if ($arg eq 'TNEXTTOP') {
		    $tmp = defined($key) ? $TNEXTTOPBUTTON : $TNEXTTOPBUTTONIA;
		    last SW; }
		if ($arg eq 'TPREVTOP') {
		    $tmp = defined($key) ? $TPREVTOPBUTTON : $TPREVTOPBUTTONIA;
		    last SW; }
		if ($arg eq 'TTOP') {
		    $tmp = ($key ne $index) ? $TTOPBUTTON : $TTOPBUTTONIA;
		    last SW; }
		if ($arg eq 'TEND') {
		    $tmp = ($key ne $index) ? $TENDBUTTON : $TENDBUTTONIA;
		    last SW; }
	    }
	    last REPLACESW;
	}
	if ($var eq 'LINK') {
	    $expand = 1;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    SW: {
		if ($arg eq 'NEXT') {
		    $tmp = defined($key) ? $NEXTLINK : $NEXTLINKIA;
		    last SW; }
		if ($arg eq 'PREV') {
		    $tmp = defined($key) ? $PREVLINK : $PREVLINKIA;
		    last SW; }
		if ($arg eq 'TNEXT') {
		    $tmp = defined($key) ? $TNEXTLINK : $TNEXTLINKIA;
		    last SW; }
		if ($arg eq 'TPREV') {
		    $tmp = defined($key) ? $TPREVLINK : $TPREVLINKIA;
		    last SW; }
		if ($arg eq 'TNEXTIN') {
		    $tmp = defined($key) ? $TNEXTINLINK : $TNEXTINLINKIA;
		    last SW; }
		if ($arg eq 'TPREVIN') {
		    $tmp = defined($key) ? $TPREVINLINK : $TPREVINLINKIA;
		    last SW; }
		if ($arg eq 'TNEXTTOP') {
		    $tmp = defined($key) ? $TNEXTTOPLINK : $TNEXTTOPLINKIA;
		    last SW; }
		if ($arg eq 'TPREVTOP') {
		    $tmp = defined($key) ? $TPREVTOPLINK : $TPREVTOPLINKIA;
		    last SW; }
		if ($arg eq 'TTOP') {
		    $tmp = ($key ne $index) ? $TTOPLINK : $TTOPLINKIA;
		    last SW; }
		if ($arg eq 'TEND') {
		    $tmp = ($key ne $index) ? $TENDLINK : $TENDLINKIA;
		    last SW; }
	    }
	    last REPLACESW;
	}

    	if ($var eq 'TSLICE') {
	    my($bcnt, $acnt, $inclusive);
	    if ($arg) {
	      ($bcnt, $acnt, $inclusive) = split(/[;:]/, $arg);
	      $bcnt = $TSliceNBefore  if (!defined($bcnt) || $bcnt !~ /^\d+$/);
	      $acnt = $TSliceNAfter   if (!defined($acnt) || $acnt !~ /^\d+$/);
	      $inclusive = $TSliceInclusive  if (!defined($inclusive));
	    } else {
	      $bcnt = $TSliceNBefore;
	      $acnt = $TSliceNAfter;
	      $inclusive = $TSliceInclusive;
	    }
	    $tmp = &amp;make_thread_slice($index, $bcnt, $acnt, $inclusive)
	    	if ($bcnt != 0 || $acnt != 0);
	    last REPLACESW;
	}

	## -------------------------------- ##
	## Index related resource variables ##
	## -------------------------------- ##
    	if ($var eq 'A_ATTR') {		## Anchor attrs to link to message
	    $isurl = 0;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) { $tmp = ""; last REPLACESW; }
	    $tmp = qq/name="/ . &amp;fmt_msgnum($IndexNum{$key}) .
		   qq/" href="/ .
		   &amp;msgnum_filename($IndexNum{$key}) .
		   qq/"/;
	    last REPLACESW;
	}
    	if ($var eq 'A_NAME') {		## Anchor name for message position
	    $isurl = 0;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) { $tmp = ""; last REPLACESW; }
	    $tmp = qq/name="/ . &amp;fmt_msgnum($IndexNum{$key}) . qq/"/;
	    last REPLACESW;
	}
    	if ($var eq 'A_HREF') {		## Anchor href to link to message
	    $isurl = 0;
	    ($lref, $key, $pos) = compute_msg_pos($index, $var, $arg);
	    if (!defined($key)) { $tmp = ""; last REPLACESW; }
	    $tmp = qq/href="/ . &amp;msgnum_filename($IndexNum{$key}) . qq/"/;
	    last REPLACESW;
	}
    	if ($var eq 'IDXFNAME') {	## Filename of index page
	    if ($MULTIIDX &amp;&amp;
		(($n = int($Index2MLoc{$index}/$IDXSIZE)+1) &gt; 1) &amp;&amp;
		(($MAXPGS &lt;= 0) || ($n &lt;= $MAXPGS)))
	    {
		$tmp = sprintf("%s%d.$HtmlExt",
			       $IDXPREFIX, $index ne '' ? $n : 1);
	    } else {
		$tmp = $IDXNAME;
	    }
	    $tmp .= ".gz"  if $GzipLinks;
	    last REPLACESW;
	}
    	if ($var eq 'IDXLABEL') {	## Label for main index
	    $tmp = $IDXLABEL;
	    last REPLACESW;
	}
    	if ($var eq 'IDXSIZE') {	## Index page size
	    $tmp = $IDXSIZE;
	    last REPLACESW;
	}
    	if ($var eq 'IDXTITLE') {	## Main index title
	    $canclip = 1; $expand = 1;
	    $tmp = $TITLE;
	    last REPLACESW;
	}
    	if ($var eq 'NUMOFIDXMSG') {	## Number of items on the index page
	    $tmp = $PageSize;
	    last REPLACESW;
	}
    	if ($var eq 'NUMOFMSG') {	## Total number of messages
	    $tmp = $NumOfMsgs;
	    last REPLACESW;
	}
    	if ($var eq 'SORTTYPE') {	## Sort type of index
	    SORTTYPE: {
		if ($NOSORT)   { $tmp = 'Number';  last SORTTYPE; }
		if ($AUTHSORT) { $tmp = 'Author';  last SORTTYPE; }
		if ($SUBSORT)  { $tmp = 'Subject'; last SORTTYPE; }
		$tmp = 'Date';
		last SORTTYPE;
	    }
	    last REPLACESW;
	}
    	if ($var eq 'TIDXFNAME') {
	    if ($MULTIIDX &amp;&amp;
		(($n = int($Index2TLoc{$index}/$IDXSIZE)+1) &gt; 1) &amp;&amp;
		(($MAXPGS &lt;= 0) || ($n &lt;= $MAXPGS)))
	    {
		$tmp = sprintf("%s%d.$HtmlExt",
			       $TIDXPREFIX, $index ne '' ? $n : 1);
	    } else {
		$tmp = $TIDXNAME;
	    }
	    $tmp .= ".gz"  if $GzipLinks;
	    last REPLACESW;
	}
    	if ($var eq 'TIDXLABEL') {
	    $tmp = $TIDXLABEL;
	    last REPLACESW;
	}
    	if ($var eq 'TIDXTITLE') {
	    $canclip = 1; $expand = 1;
	    $tmp = $TTITLE;
	    last REPLACESW;
	}
    	if ($var eq 'TSORTTYPE') {
	    TSORTTYPE: {
		if ($TNOSORT)   { $tmp = 'Number';  last TSORTTYPE; }
		if ($TSUBSORT)  { $tmp = 'Subject'; last TSORTTYPE; }
		$tmp = 'Date';
		last TSORTTYPE;
	    }
	    last REPLACESW;
	}

	if ($var eq 'PGLINK') {
	    $expand = 1;
	    SW: {
		if ($arg eq 'NEXT') {
		    $tmp = $PageNum &lt; $NumOfPrintedPages ?
		    			$NEXTPGLINK : $NEXTPGLINKIA;
		    last SW; }
		if ($arg eq 'PREV') {
		    $tmp = $PageNum &gt; 1 ? $PREVPGLINK : $PREVPGLINKIA;
		    last SW; }
		if ($arg eq 'TNEXT') {
		    $tmp = $PageNum &lt; $NumOfPrintedPages ?
		    			$TNEXTPGLINK : $TNEXTPGLINKIA;
		    last SW; }
		if ($arg eq 'TPREV') {
		    $tmp = $PageNum &gt; 1 ? $TPREVPGLINK : $TPREVPGLINKIA;
		    last SW; }
		if ($arg eq 'FIRST') {
		    $tmp = $FIRSTPGLINK;
		    last SW; }
		if ($arg eq 'LAST') {
		    $tmp = $LASTPGLINK;
		    last SW; }
		if ($arg eq 'TFIRST') {
		    $tmp = $TFIRSTPGLINK;
		    last SW; }
		if ($arg eq 'TLAST') {
		    $tmp = $TLASTPGLINK;
		    last SW; }
	    }
	    last REPLACESW;
	}
	if ($var eq 'PGLINKLIST') {
	    my $num = $PageNum;
	    my $t = $arg =~ s/T//gi;
	    my($before, $after) = split(/;/, $arg);
	    my $prefix  = $t ? $TIDXPREFIX : $IDXPREFIX;
	    my $suffix  = $HtmlExt;
	       $suffix .= '.gz'  if $GzipLinks;
	    if ($before ne "") {
		$before = $num - abs($before);
		$before = 1  unless $before &gt; 1;
	    } else {
		$before = 1;
	    }
	    if ($after ne "") {
		$after  = $num + abs($after);
		$after  = $NumOfPrintedPages
			  unless $after &lt; $NumOfPrintedPages;
	    } else {
		$after  = $NumOfPrintedPages;
	    }
	    $tmp = "";
	    for ($i=$before; $i &lt; $num; ++$i) {
		if ($i == 1) {
		    $tmp .= sprintf('&lt;a href="%s%s"&gt;%d&lt;/a&gt; | ',
				    ($t ? $TIDXNAME : $IDXNAME),
				    ($GzipLinks ? '.gz' : ""), $i);
		    next;
		}
		$tmp .= sprintf('&lt;a href="%s%d.%s"&gt;%d&lt;/a&gt; | ',
			        $prefix, $i, $suffix, $i);
	    }
	    $tmp .= $num;
	    for ($i=$num+1; $i &lt;= $after; ++$i) {
		$tmp .= sprintf(' | &lt;a href="%s%d.%s"&gt;%d&lt;/a&gt;',
			        $prefix, $i, $suffix, $i);
	    }
	    last REPLACESW;
	}

	if ($var eq 'PAGENUM') {
	    $tmp = $PageNum;
	    last REPLACESW;
	}
	if ($var eq 'NUMOFPAGES') {
	    #$tmp = $NumOfPages;
	    $tmp = $NumOfPrintedPages;
	    last REPLACESW;
	}

	if ($var eq 'PG') {
	    my $num = $PageNum;
	    my $t = ($arg =~ s/^T//);
	    my $prefix = $t ? $TIDXPREFIX : $IDXPREFIX;
	    SW: {
		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 = $NumOfPrintedPages; last SW; }
		if ($arg =~ /^-?\d+$/) { $num = $PageNum+$arg; last SW; }
	    }
	    if ($num &lt; 2) {
		$tmp = $t ? $TIDXNAME : $IDXNAME;
	    } else {
		$num = $NumOfPrintedPages  if $num &gt; $NumOfPrintedPages;
		$tmp = sprintf("%s%d.$HtmlExt", $prefix, $num);
	    }
	    $tmp .= ".gz"  if $GzipLinks;
	    last REPLACESW;
	}

	## -------------------------------- ##
	## Miscellaneous resource variables ##
	## -------------------------------- ##
    	if ($var eq 'ATTACHMENTURL') {
	    $isurl = 0;
	    $tmp = $AttachmentUrl;
	    last REPLACESW;
	}
    	if ($var eq 'DOCURL') {
	    $isurl = 0;
	    $tmp = $DOCURL;
	    last REPLACESW;
	}
	if ($var eq 'ENV') {
	    $tmp = htmlize($ENV{$arg});
	    last REPLACESW;
	}
    	if ($var eq 'GMTDATE') {
	    $tmp = &amp;time2str($arg || $GMTDateFmt, time, 0);
	    last REPLACESW;
	}
    	if ($var eq 'HTMLEXT') {
	    $tmp = $HtmlExt;
	    last REPLACESW;
	}
	if ($var eq 'IDXPREFIX') {
	    $tmp = $IDXPREFIX;
	    last REPLACESW;
	}
    	if ($var eq 'LOCALDATE') {
	    $tmp = &amp;time2str($arg || $LocalDateFmt, time, 1);
	    last REPLACESW;
	}
    	if ($var eq 'MSGPREFIX') {
	    $tmp = $MsgPrefix;
	    last REPLACESW;
	}
    	if ($var eq 'OUTDIR') {
	    $tmp = $OUTDIR;
	    last REPLACESW;
	}
    	if ($var eq 'PROG') {
	    $tmp = $PROG;
	    last REPLACESW;
	}
	if ($var eq 'TIDXPREFIX') {
	    $tmp = $TIDXPREFIX;
	    last REPLACESW;
	}
    	if ($var eq 'VERSION') {
	    $tmp = $VERSION;
	    last REPLACESW;
	}
    	if ($var eq '') {
	    $tmp = '$';
	    last REPLACESW;
	}

	## --------------------------- ##
	## User defined variable check ##
	## --------------------------- ##
	if (defined($CustomRcVars{$var})) {
	    $expand = 1;
	    $tmp = $CustomRcVars{$var};
	    last REPLACESW;
	}

	warn qq/Warning: Unrecognized variable: "$val"\n/;
	#return "\$$val\$";
	return "";
    }

    ##	Check if string needs to be expanded again
    if ($expand) {
	$tmp =~ s/$VarExp/&amp;replace_li_var($1,$index)/geo;
    }

    ##	Check if URL text specifier is set
    if ($isurl) {
	$ret = &amp;urlize($tmp);

    } else {
	if ($raw) {
	    $ret = &amp;$MHeadCnvFunc($tmp);
	    if ($isaddr) {
		if (!$mailto) {
		    $ret =~ s/($HAddrExp)/htmlize(rewrite_address($1))/geo;
		} else {
		    $ret =~ s/($HAddrExp)
			     /mailUrl($1, $Index2MsgId{$key},
					  $Subject{$key},
					  $From{$key})/gexo;
		    $canclip = 0;
		}
	    }
	} else {
	    $ret = $tmp;
	}

	# Check for clipping
	if ($len &gt; 0 &amp;&amp; $canclip &amp;&amp; (length($ret) &gt; 0)) {
	    $ret = &amp;$TextClipFunc($ret, $len, 1);
	}

	# Check if JavaScript string
	if ($jstr) {
	    $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
    $ret = qq|&lt;a name="| .
	   &amp;fmt_msgnum($IndexNum{$index}) .
	   qq|" href="| .
	   &amp;msgnum_filename($IndexNum{$index}) .
	   qq|"&gt;$ret&lt;/a&gt;|
	if $var eq 'SUBJECT' &amp;&amp; $arg eq "" &amp;&amp; !$mailto;

    $ret;
}

##---------------------------------------------------------------------------##
##	compute_msg_pos(): Get message location data.
##	Return:
##	    ($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.
##
sub compute_msg_pos {
    my($idx, $var, $arg, $usethread) = @_;
    my($ofs, $pos, $aref, $href, $key);
    my $opt  = undef;
    my $flip = 0;
    my $orgarg = $arg;

    ## Determine what list type
    if (($arg =~ s/^T//) || $usethread) {
	$aref = \@TListOrder;
	$href = \%Index2TLoc;
	$usethread = 1;
    } else {
	$aref = \@MListOrder;
	$href = \%Index2MLoc;
	$flip = $REVSORT;
    }

    ## Extract out optional data
    ($arg, $opt) = split(/;/, $arg, 2);

    SW: {
	if ($usethread &amp;&amp; $TREVERSE) {
	    # when threads are listed in reverse, we preserve the
	    # sematics of "next/prev thread"
	    if ($arg eq 'NEXTTOP') {
		$arg = 'PREVTOP';
	    } elsif ($arg eq 'PREVTOP') {
		$arg = 'NEXTTOP';
	    }
	}

	$ofs =  0, last SW
	    if (!defined($arg)) || ($arg eq '') || ($arg eq 'CUR');
	$ofs = ($flip ? -$arg : $arg), last SW
	    if $arg =~ /^-?\d+$/;

	if ($arg eq 'NEXT') {		# next message
	    if (!$usethread || !$TREVERSE) {
	      $ofs = ($flip ? -1 : 1);
	      last SW;
	    }
	    # get here, it is thread and reverse
	    undef $ofs;
	    $pos = $href-&gt;{$idx};
	    if (($pos &lt; $#$aref) &amp;&amp; ($ThreadLevel{$aref-&gt;[$pos+1]} &gt; 0)) {
		++$pos;
		last SW;
	    }
	    # get here, must goto physical previous top
	    # note no `last SW' statement
	    $arg = 'PREVTOP';
	}
	if ($arg eq 'PREV') {		# prev message
	    if (!$usethread || !$TREVERSE) {
	      $ofs = ($flip ? 1 : -1);
	      last SW;
	    }
	    # get here, it is thread and reverse
	    undef $ofs;
	    if ($ThreadLevel{$idx} &gt; 0) {
		$pos = $href-&gt;{$idx};
		if (($pos &gt; 0) &amp;&amp; ($ThreadLevel{$aref-&gt;[$pos-1]} &gt;= 0)) {
		    --$pos;
		    last SW;
		}
	    }
	    # get here, must goto physical next top
	    # note no `last SW' statement
	    $arg = 'NEXTTOP';
	}
	if ($arg eq 'FIRST') {
	    $pos = $flip ? $#$aref : 0;
	    undef $ofs;
	    last SW;
	}
	if ($arg eq 'LAST') {
	    $pos = $flip ? 0 : $#$aref;
	    undef $ofs;
	    last SW;
	}

	# if not thread variable, no more checking
	if (!$usethread) {
	    warn qq/Warning: $var: Invalid variable argument: "$orgarg"\n/;
	    $ofs = 0;
	    last SW;
	}

	if ($arg eq 'NEXTIN') {		# next message within a thread
	    $pos = $href-&gt;{$idx} + 1;
	    if ($pos &gt; $#$aref || $ThreadLevel{$aref-&gt;[$pos]} &lt;= 0) {
		$pos = -1;
	    }
	    undef $ofs;
	    last SW;
	}
	if ($arg eq 'PREVIN') {		# previous message within a thread
	    undef $ofs;
	    $pos = $href-&gt;{$idx};
	    if ($ThreadLevel{$aref-&gt;[$pos]} &lt;= 0) {
		$pos = -1;
		last SW;
	    }
	    --$pos;
	    $pos = -1  if ($pos &lt; 0);
	    last SW;
	}
	if ($arg eq 'PARENT') {		# parent message in thread
	    undef $ofs;
	    my $level = $ThreadLevel{$idx};
	    $pos = $Index2TLoc{$idx};
	    last SW  if ($level &lt;= 0);
	    for (--$pos; $pos &gt;= 0; --$pos) {
		last  if $ThreadLevel{$aref-&gt;[$pos]} &lt; $level;
	    }
	    last SW;
	}
	if ($arg eq 'TOP') {
	    undef $ofs;
	    $pos = $Index2TLoc{$idx};
	    for (; $pos &gt;= 0; --$pos) {
		last  if $ThreadLevel{$aref-&gt;[$pos]} &lt;= 0;
	    }
	    last SW;
	}
	if (($arg eq 'NEXTTOP') ){	# start of next thread
	    undef $ofs;
	    $pos = $Index2TLoc{$idx};
	    for (++$pos; $pos &lt;= $#$aref; ++$pos) {
		last  if $ThreadLevel{$aref-&gt;[$pos]} &lt;= 0;
	    }
	    last SW;
	}
	if (($arg eq 'PREVTOP') ){	# start of previous thread
	    undef $ofs;
	    # Find current top first, then find previous top
	    for ($pos = $Index2TLoc{$idx}; $pos &gt;= 0; --$pos) {
		last  if $ThreadLevel{$aref-&gt;[$pos]} &lt;= 0;
	    }
	    if ($pos &gt;= 0) {
		for (--$pos; $pos &gt;= 0; --$pos) {
		    last  if $ThreadLevel{$aref-&gt;[$pos]} &lt;= 0;
		}
	    }
	    last SW;
	}
	if ($arg eq 'END') {		# last message of thread
	    undef $ofs;
	    $pos = $Index2TLoc{$idx};
	    for (; $pos &lt; $#$aref; ++$pos) {
		last  if $ThreadLevel{$aref-&gt;[$pos+1]} &lt;= 0;
	    }
	    last SW;
	}

	warn qq/Warning: $var: Unrecognized variable argument: "$orgarg"\n/;
	$ofs = 0;
    }
    $pos = $href-&gt;{$idx} + $ofs  if defined($ofs);
    if (($pos &gt; $#$aref) || ($pos &lt; 0)) {
	$pos = -1;
	$key = undef;
    } else {
	$key = $aref-&gt;[$pos];
    }

    ($aref, $key, $pos, $opt);
}

##---------------------------------------------------------------------------##
1;
</pre></body></html>