| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: mhthread.pl,v 2.13 2011/01/02 10:14:13 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## Thread routines for MHonArc | ||
| 8 | ##---------------------------------------------------------------------------## | ||
| 9 | ## MHonArc -- Internet mail-to-HTML converter | ||
| 10 | ## Copyright (C) 1995-2001 Earl Hood, mhonarc@mhonarc.org | ||
| 11 | ## | ||
| 12 | ## This program is free software; you can redistribute it and/or modify | ||
| 13 | ## it under the terms of the GNU General Public License as published by | ||
| 14 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 15 | ## (at your option) any later version. | ||
| 16 | ## | ||
| 17 | ## This program is distributed in the hope that it will be useful, | ||
| 18 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 19 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 20 | ## GNU General Public License for more details. | ||
| 21 | ## | ||
| 22 | ## You should have received a copy of the GNU General Public License | ||
| 23 | ## along with this program; if not, write to the Free Software | ||
| 24 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 25 | ## 02111-1307, USA | ||
| 26 | ##---------------------------------------------------------------------------## | ||
| 27 | |||
| 28 | package mhonarc; | ||
| 29 | |||
| 30 | ##--------------------------------------------------------------------------- | ||
| 31 | ## write_thread_index outputs the thread index | ||
| 32 | ## | ||
| 33 | sub write_thread_index { | ||
| 34 | local ($onlypg) = shift; | ||
| 35 | local ($tmpl, $handle); | ||
| 36 | local ($index) = (""); | ||
| 37 | local (*a); | ||
| 38 | local ($PageNum, $PageSize, $totalpgs, %Printed); | ||
| 39 | local ($lastlevel, $tlevel, $iscont, $i, $offstart, $offend); | ||
| 40 | my ($tmpfile); | ||
| 41 | |||
| 42 | local ($level) = 0; ## !!!Used in print_thread!!! | ||
| 43 | local ($last0index) = ''; | ||
| 44 | |||
| 45 | ## Make sure list orders are set | ||
| 46 | if (!scalar(@TListOrder)) { | ||
| 47 | &compute_threads(); | ||
| 48 | } | ||
| 49 | if (!scalar(@MListOrder)) { # need for resource variable expansions | ||
| 50 | @MListOrder = &sort_messages(); | ||
| 51 | %Index2MLoc = (); | ||
| 52 | @Index2MLoc{@MListOrder} = (0 .. $#MListOrder); | ||
| 53 | } | ||
| 54 | |||
| 55 | &compute_page_total(); | ||
| 56 | @ThreadList = @TListOrder; | ||
| 57 | $PageNum = $onlypg || 1; | ||
| 58 | $totalpgs = $onlypg || $NumOfPrintedPages; | ||
| 59 | |||
| 60 | for (; $PageNum <= $totalpgs; ++$PageNum) { | ||
| 61 | next if $PageNum < $TIdxMinPg; | ||
| 62 | |||
| 63 | if ($MULTIIDX) { | ||
| 64 | $offstart = ($PageNum - 1) * $IDXSIZE; | ||
| 65 | $offend = $offstart + $IDXSIZE - 1; | ||
| 66 | $offend = $#TListOrder if $#TListOrder < $offend; | ||
| 67 | @a = @TListOrder[$offstart .. $offend]; | ||
| 68 | |||
| 69 | if ($PageNum > 1) { | ||
| 70 | $TIDXPATHNAME = join("", | ||
| 71 | $OUTDIR, $DIRSEP, $TIDXPREFIX, $PageNum, ".", $HtmlExt); | ||
| 72 | } else { | ||
| 73 | $TIDXPATHNAME = join($DIRSEP, $OUTDIR, $TIDXNAME); | ||
| 74 | } | ||
| 75 | |||
| 76 | } else { | ||
| 77 | $TIDXPATHNAME = join($DIRSEP, $OUTDIR, $TIDXNAME); | ||
| 78 | if ($IDXSIZE && (($i = ($#ThreadList + 1) - $IDXSIZE) > 0)) { | ||
| 79 | if ($TREVERSE) { | ||
| 80 | @NotIdxThreadList = splice(@ThreadList, $IDXSIZE); | ||
| 81 | } else { | ||
| 82 | @NotIdxThreadList = splice(@ThreadList, 0, $i); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | *a = *ThreadList; | ||
| 86 | } | ||
| 87 | $PageSize = scalar(@a); | ||
| 88 | |||
| 89 | if ($IDXONLY) { | ||
| 90 | $handle = \*STDOUT; | ||
| 91 | } else { | ||
| 92 | ($handle, $tmpfile) = file_temp('tidxXXXXXXXXXX', $OUTDIR); | ||
| 93 | } | ||
| 94 | print STDOUT "Writing $TIDXPATHNAME ...\n" unless $QUIET; | ||
| 95 | |||
| 96 | $tmpl = ($TIDXPGSSMARKUP ne '') ? $TIDXPGSSMARKUP : $SSMARKUP; | ||
| 97 | if ($tmpl ne '') { | ||
| 98 | $tmpl =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 99 | print $handle $tmpl; | ||
| 100 | } | ||
| 101 | |||
| 102 | print $handle "<!-- ", &commentize("MHonArc v$VERSION"), " -->\n"; | ||
| 103 | |||
| 104 | ($tmpl = $TIDXPGBEG) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 105 | print $handle $tmpl; | ||
| 106 | |||
| 107 | ($tmpl = $THEAD) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 108 | print $handle $tmpl; | ||
| 109 | |||
| 110 | ## Flag visible messages for use in printing thread index page | ||
| 111 | foreach $index (@a) { $TVisible{$index} = 1; } | ||
| 112 | |||
| 113 | ## Print index. Print unless message has been printed, or | ||
| 114 | ## unless it has reference that is visible. | ||
| 115 | $level = 0; # !!!Used in print_thread!!! | ||
| 116 | $lastlevel = $ThreadLevel{$a[0]}; | ||
| 117 | |||
| 118 | # check if continuing a thread | ||
| 119 | if ($lastlevel > 0) { | ||
| 120 | ($tmpl = $TCONTBEG) =~ s/$VarExp/&replace_li_var($1,$a[0])/geo; | ||
| 121 | print $handle $tmpl; | ||
| 122 | } | ||
| 123 | # perform any indenting | ||
| 124 | for ($i = 0; $i < $lastlevel; ++$i) { | ||
| 125 | ++$level; | ||
| 126 | if ($level <= $TLEVELS) { | ||
| 127 | ($tmpl = $TINDENTBEG) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 128 | print $handle $tmpl; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | # print index listing | ||
| 132 | foreach $index (@a) { | ||
| 133 | $tlevel = $ThreadLevel{$index}; | ||
| 134 | if (($lastlevel > 0) && ($tlevel < $lastlevel)) { | ||
| 135 | for ($i = $tlevel; $i < $lastlevel; ++$i) { | ||
| 136 | if ($level <= $TLEVELS) { | ||
| 137 | ($tmpl = $TINDENTEND) =~ | ||
| 138 | s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 139 | print $handle $tmpl; | ||
| 140 | } | ||
| 141 | --$level; | ||
| 142 | } | ||
| 143 | $lastlevel = $tlevel; | ||
| 144 | if ($lastlevel < 1) { # Check if continuation done | ||
| 145 | ($tmpl = $TCONTEND) =~ | ||
| 146 | s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 147 | print $handle $tmpl; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | unless ($Printed{$index} | ||
| 151 | || ($HasRef{$index} && $TVisible{$HasRef{$index}})) { | ||
| 152 | &print_thread($handle, $index, ($lastlevel > 0) ? 0 : 1); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | # unindent if required | ||
| 156 | for ($i = 0; $i < $lastlevel; ++$i) { | ||
| 157 | if ($level <= $TLEVELS) { | ||
| 158 | ($tmpl = $TINDENTEND) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 159 | print $handle $tmpl; | ||
| 160 | } | ||
| 161 | --$level; | ||
| 162 | } | ||
| 163 | # close continuation if required | ||
| 164 | if ($lastlevel > 0) { | ||
| 165 | ($tmpl = $TCONTEND) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 166 | print $handle $tmpl; | ||
| 167 | } | ||
| 168 | |||
| 169 | ## Reset visibility flags | ||
| 170 | foreach $index (@a) { $TVisible{$index} = 0; } | ||
| 171 | |||
| 172 | ($tmpl = $TFOOT) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 173 | print $handle $tmpl; | ||
| 174 | |||
| 175 | &output_doclink($handle); | ||
| 176 | |||
| 177 | ($tmpl = $TIDXPGEND) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 178 | print $handle $tmpl; | ||
| 179 | |||
| 180 | print $handle "<!-- ", &commentize("MHonArc v$VERSION"), " -->\n"; | ||
| 181 | |||
| 182 | if (!$IDXONLY) { | ||
| 183 | close($handle); | ||
| 184 | file_gzip($tmpfile) if $GzipFiles; | ||
| 185 | file_chmod(file_rename($tmpfile, $TIDXPATHNAME)); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | ##--------------------------------------------------------------------------- | ||
| 191 | ## Routine to compute the order messages are listed by thread. | ||
| 192 | ## Main use is to provide the ability to correctly define | ||
| 193 | ## values for resource variables related to next/prev thread | ||
| 194 | ## message. | ||
| 195 | ## | ||
| 196 | ## NOTE: Thread order is determined by all the messages in an | ||
| 197 | ## archive, and not by what is visible in the thread index page. | ||
| 198 | ## Hence, if the thread index page size is less than number of | ||
| 199 | ## messages, the next/prev messages of thread (accessible via | ||
| 200 | ## resource variables) will not necessarily correspond to the | ||
| 201 | ## actual physical next/prev message listed in the thread index. | ||
| 202 | ## | ||
| 203 | ## The call to do_thread() defines the TListOrder array for use | ||
| 204 | ## in expanding thread related resource variables. | ||
| 205 | ## | ||
| 206 | sub compute_threads { | ||
| 207 | local (%FirstSub2Index) = (); | ||
| 208 | local (%Counted) = (); | ||
| 209 | local (%stripsub) = (); | ||
| 210 | local (@refs); | ||
| 211 | local ($index, $msgid, $refindex, $depth, $tmp); | ||
| 212 | |||
| 213 | ## Reset key data structures | ||
| 214 | @TListOrder = (); | ||
| 215 | %Index2TLoc = (); | ||
| 216 | %ThreadLevel = (); | ||
| 217 | %HasRef = (); | ||
| 218 | %HasRefDepth = (); | ||
| 219 | %Replies = (); | ||
| 220 | %SReplies = (); | ||
| 221 | |||
| 222 | ## Sort by date first for subject based threads | ||
| 223 | @ThreadList = sort_messages(0, 0, 0, 0); | ||
| 224 | |||
| 225 | ## Find first occurrances of subjects | ||
| 226 | if (!$NoSubjectThreads) { | ||
| 227 | foreach $index (@ThreadList) { | ||
| 228 | $tmp = lc $Subject{$index}; | ||
| 229 | 1 while (($tmp =~ s/^$SubReplyRxp//io) | ||
| 230 | || ($tmp =~ s/\s*-\s*re(ply|sponse)\s*$//io)); | ||
| 231 | |||
| 232 | $stripsub{$index} = $tmp; | ||
| 233 | next unless $tmp =~ /\S/; | ||
| 234 | $FirstSub2Index{$tmp} = $index | ||
| 235 | unless defined($FirstSub2Index{$tmp}) | ||
| 236 | || (defined($Refs{$index}) | ||
| 237 | && grep($MsgId{$_}, @{$Refs{$index}})); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | ## Compute thread data | ||
| 242 | TCOMP: foreach $index (@ThreadList) { | ||
| 243 | next unless defined($Refs{$index}); | ||
| 244 | |||
| 245 | # Check for explicit threading | ||
| 246 | if (@refs = @{$Refs{$index}}) { | ||
| 247 | $depth = 0; | ||
| 248 | while ($msgid = pop(@refs)) { | ||
| 249 | if (($refindex = $MsgId{$msgid})) { | ||
| 250 | |||
| 251 | $HasRef{$index} = $refindex; | ||
| 252 | $HasRefDepth{$index} = $depth; | ||
| 253 | if ($Replies{$refindex}) { | ||
| 254 | push(@{$Replies{$refindex}}, $index); | ||
| 255 | } else { | ||
| 256 | $Replies{$refindex} = [$index]; | ||
| 257 | } | ||
| 258 | next TCOMP; | ||
| 259 | } | ||
| 260 | ++$depth; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | } continue { | ||
| 265 | # Check for subject-based threading | ||
| 266 | if (!$NoSubjectThreads && !$HasRef{$index}) { | ||
| 267 | $refindex = $FirstSub2Index{$stripsub{$index}}; | ||
| 268 | if ($refindex && ($refindex ne $index)) { | ||
| 269 | |||
| 270 | $HasRef{$index} = $refindex; | ||
| 271 | $HasRefDepth{$index} = 0; | ||
| 272 | if ($SReplies{$refindex}) { | ||
| 273 | push(@{$SReplies{$refindex}}, $index); | ||
| 274 | } else { | ||
| 275 | $SReplies{$refindex} = [$index]; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | ## Calculate thread listing order | ||
| 282 | @ThreadList = sort_messages($TNOSORT, $TSUBSORT, 0, $TREVERSE); | ||
| 283 | foreach $index (@ThreadList) { | ||
| 284 | unless ($Counted{$index} || $HasRef{$index}) { | ||
| 285 | &do_thread($index, 0); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | ##--------------------------------------------------------------------------- | ||
| 291 | ## do_thread() computes the order messages are listed by thread. | ||
| 292 | ## Uses %Counted defined locally in compute_thread_from_list(). | ||
| 293 | ## do_thread() main purpose is to set the TListOrder array and | ||
| 294 | ## Index2TLoc assoc array. | ||
| 295 | ## | ||
| 296 | sub do_thread { | ||
| 297 | local ($idx, $level) = ($_[0], $_[1]); | ||
| 298 | local (@repls, @srepls) = (); | ||
| 299 | |||
| 300 | ## Get replies | ||
| 301 | @repls = sort increase_index @{$Replies{$idx}} | ||
| 302 | if defined($Replies{$idx}); | ||
| 303 | @srepls = sort increase_index @{$SReplies{$idx}} | ||
| 304 | if defined($SReplies{$idx}); | ||
| 305 | |||
| 306 | ## Add index to printed order list (IMPORTANT SIDE-EFFECT) | ||
| 307 | push(@TListOrder, $idx); | ||
| 308 | $Index2TLoc{$idx} = $#TListOrder; | ||
| 309 | |||
| 310 | ## Mark message | ||
| 311 | $Counted{$idx} = 1; | ||
| 312 | $ThreadLevel{$idx} = $level; | ||
| 313 | |||
| 314 | if (@repls) { | ||
| 315 | foreach (@repls) { | ||
| 316 | &do_thread($_, $level + 1 + $HasRefDepth{$_}); | ||
| 317 | } | ||
| 318 | } | ||
| 319 | if (@srepls) { | ||
| 320 | foreach (@srepls) { | ||
| 321 | &do_thread($_, $level + 1 + $HasRefDepth{$_}); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | ##--------------------------------------------------------------------------- | ||
| 327 | ## Routine to print thread. | ||
| 328 | ## Uses %Printed defined by caller. | ||
| 329 | ## | ||
| 330 | sub print_thread { | ||
| 331 | local ($handle, $idx, $top) = ($_[0], $_[1], $_[2]); | ||
| 332 | my (@repls, @srepls) = (); | ||
| 333 | my ($attop, $haverepls, $hvnirepls, $single, $depth, $i); | ||
| 334 | my $didtliend = 0; | ||
| 335 | |||
| 336 | ## Get replies | ||
| 337 | @repls = sort increase_index @{$Replies{$idx}} | ||
| 338 | if defined($Replies{$idx}); | ||
| 339 | @srepls = sort increase_index @{$SReplies{$idx}} | ||
| 340 | if defined($SReplies{$idx}); | ||
| 341 | $depth = $HasRefDepth{$idx}; | ||
| 342 | $hvnirepls = (@repls || @srepls); | ||
| 343 | |||
| 344 | @repls = grep($TVisible{$_}, @repls); | ||
| 345 | @srepls = grep($TVisible{$_}, @srepls); | ||
| 346 | $haverepls = (@repls || @srepls); | ||
| 347 | |||
| 348 | ## $hvnirepls is a flag if the message has replies, but they are | ||
| 349 | ## not visible. $haverepls is a flag if the message has visible | ||
| 350 | ## replies. $hvnirepls is used to determine the $attop and | ||
| 351 | ## $single flags. $haverepls is used for determine recursive | ||
| 352 | ## calls and level. | ||
| 353 | |||
| 354 | ## Print entry | ||
| 355 | #$attop = ($top && $haverepls); | ||
| 356 | #$single = ($top && !$haverepls); | ||
| 357 | $attop = ($top && $hvnirepls); | ||
| 358 | $single = ($top && !$hvnirepls); | ||
| 359 | |||
| 360 | if ($attop) { | ||
| 361 | &print_thread_var($handle, $idx, \$TTOPBEG); | ||
| 362 | } elsif ($single) { | ||
| 363 | &print_thread_var($handle, $idx, \$TSINGLETXT); | ||
| 364 | } else { | ||
| 365 | ## Check for missing messages | ||
| 366 | if ($DoMissingMsgs) { | ||
| 367 | for ($i = $depth; $i > 0; --$i) { | ||
| 368 | ++$level; | ||
| 369 | &print_thread_var($handle, $idx, \$TLINONE); | ||
| 370 | &print_thread_var($handle, $idx, \$TSUBLISTBEG) | ||
| 371 | if $level <= $TLEVELS; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | &print_thread_var($handle, $idx, \$TLITXT); | ||
| 375 | } | ||
| 376 | |||
| 377 | ## Increment level count if their are replies | ||
| 378 | ++$level if ($haverepls); | ||
| 379 | |||
| 380 | ## Print list item close if hit max depth | ||
| 381 | if (!$attop && !$single && ($level > $TLEVELS)) { | ||
| 382 | &print_thread_var($handle, $idx, \$TLIEND); | ||
| 383 | $didtliend = 1; | ||
| 384 | } | ||
| 385 | |||
| 386 | ## Mark message printed | ||
| 387 | $Printed{$idx} = 1; | ||
| 388 | |||
| 389 | ## Print sub-threads | ||
| 390 | if (scalar(@repls) || scalar(@srepls)) { | ||
| 391 | &print_thread_var($handle, $idx, \$TSUBLISTBEG) if $level <= $TLEVELS; | ||
| 392 | foreach (@repls) { | ||
| 393 | &print_thread($handle, $_); | ||
| 394 | } | ||
| 395 | if (@srepls) { | ||
| 396 | &print_thread_var($handle, $idx, \$TSUBJECTBEG); | ||
| 397 | foreach (@srepls) { | ||
| 398 | &print_thread($handle, $_); | ||
| 399 | } | ||
| 400 | &print_thread_var($handle, $idx, \$TSUBJECTEND); | ||
| 401 | } | ||
| 402 | &print_thread_var($handle, $idx, \$TSUBLISTEND) if $level <= $TLEVELS; | ||
| 403 | } | ||
| 404 | |||
| 405 | ## Decrement level count if their were replies | ||
| 406 | --$level if ($haverepls); | ||
| 407 | |||
| 408 | ## Check for missing messages | ||
| 409 | if ($DoMissingMsgs && !($attop || $single)) { | ||
| 410 | for ($i = $depth; $i > 0; --$i) { | ||
| 411 | &print_thread_var($handle, $idx, \$TSUBLISTEND) | ||
| 412 | if $level <= $TLEVELS; | ||
| 413 | &print_thread_var($handle, $idx, \$TLINONEEND); | ||
| 414 | --$level; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 418 | ## Close entry text | ||
| 419 | if ($attop) { | ||
| 420 | &print_thread_var($handle, $idx, \$TTOPEND); | ||
| 421 | } elsif (!$single && !$didtliend) { | ||
| 422 | &print_thread_var($handle, $idx, \$TLIEND); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | ##--------------------------------------------------------------------------- | ||
| 427 | ## Print out text based upon resource variable referenced by $tvar. | ||
| 428 | ## | ||
| 429 | sub print_thread_var { | ||
| 430 | my ($handle, $index, $tvar) = @_; | ||
| 431 | my ($tmpl); | ||
| 432 | ($tmpl = $$tvar) =~ s/$VarExp/&replace_li_var($1,$index)/geo; | ||
| 433 | print $handle $tmpl; | ||
| 434 | } | ||
| 435 | |||
| 436 | ##--------------------------------------------------------------------------- | ||
| 437 | ## make_thread_slice generates a slice of the thread listing. | ||
| 438 | ## Arguments are: | ||
| 439 | ## | ||
| 440 | ## $refindex : Reference message index that slice is based | ||
| 441 | ## $bcnt : Number of messages before $refindex to list | ||
| 442 | ## $acnt : Number of messages after $refindex to list | ||
| 443 | ## | ||
| 444 | ## Returns string containing thread slice text. | ||
| 445 | ## | ||
| 446 | sub make_thread_slice { | ||
| 447 | my ($refindex, $bcnt, $acnt, $inclusive) = @_; | ||
| 448 | my ($slicetxt) = ""; | ||
| 449 | |||
| 450 | my ($pos) = $Index2TLoc{$refindex}; | ||
| 451 | my ($start) = $pos - $bcnt; | ||
| 452 | my ($end) = $pos + $acnt; | ||
| 453 | $start = 0 if $start < 0; | ||
| 454 | $end = $#TListOrder if $end > $#TListOrder; | ||
| 455 | if ($inclusive) { | ||
| 456 | # adjust before count | ||
| 457 | if ($bcnt == 0 || $ThreadLevel{$TListOrder[$pos]} <= 0) { | ||
| 458 | $start = $pos; | ||
| 459 | } else { | ||
| 460 | for ($i = $pos - 1; ($i > $start) && ($i > 0); --$i) { | ||
| 461 | last if ($ThreadLevel{$TListOrder[$i]} <= 0); | ||
| 462 | } | ||
| 463 | $start = $i; | ||
| 464 | } | ||
| 465 | # adjust after count | ||
| 466 | if ($acnt != 0) { | ||
| 467 | for ($i = $pos + 1; ($i <= $end) && ($i <= $#TListOrder); ++$i) { | ||
| 468 | last if ($ThreadLevel{$TListOrder[$i]} <= 0); | ||
| 469 | } | ||
| 470 | $end = $i - 1; | ||
| 471 | } | ||
| 472 | |||
| 473 | } | ||
| 474 | my (@a) = @TListOrder[$start .. $end]; | ||
| 475 | my ($lastlevel) = $ThreadLevel{$a[0]}; | ||
| 476 | my ($tmpl, $index, $tlevel, $iscont, $i); | ||
| 477 | |||
| 478 | local ($level) = 0; ## XXX: Used in make_thread!!! | ||
| 479 | local (%Printed) = (); ## XXX: Used in make_thread!!! | ||
| 480 | |||
| 481 | ($tmpl = $TSLICEBEG) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 482 | $slicetxt .= $tmpl; | ||
| 483 | |||
| 484 | ## Flag visible messages for use in printing thread | ||
| 485 | foreach $index (@a) { $TVisible{$index} = 1; } | ||
| 486 | |||
| 487 | # check if continuing a thread | ||
| 488 | if ($lastlevel > 0) { | ||
| 489 | ($tmpl = $TSLICECONTBEG) =~ s/$VarExp/&replace_li_var($1,$a[0])/geo; | ||
| 490 | $slicetxt .= $tmpl; | ||
| 491 | } | ||
| 492 | # perform any indenting | ||
| 493 | for ($i = 0; $i < $lastlevel; ++$i) { | ||
| 494 | ++$level; | ||
| 495 | if ($level <= $TSLICELEVELS) { | ||
| 496 | ($tmpl = $TSLICEINDENTBEG) =~ | ||
| 497 | s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 498 | $slicetxt .= $tmpl; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | # print index listing | ||
| 502 | foreach $index (@a) { | ||
| 503 | $tlevel = $ThreadLevel{$index}; | ||
| 504 | if (($lastlevel > 0) && ($tlevel < $lastlevel)) { | ||
| 505 | for ($i = $tlevel; $i < $lastlevel; ++$i) { | ||
| 506 | if ($level <= $TSLICELEVELS) { | ||
| 507 | ($tmpl = $TSLICEINDENTEND) =~ | ||
| 508 | s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 509 | $slicetxt .= $tmpl; | ||
| 510 | } | ||
| 511 | --$level; | ||
| 512 | } | ||
| 513 | $lastlevel = $tlevel; | ||
| 514 | if ($lastlevel < 1) { # Check if continuation done | ||
| 515 | ($tmpl = $TSLICECONTEND) =~ | ||
| 516 | s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 517 | $slicetxt .= $tmpl; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | unless ($Printed{$index} | ||
| 521 | || ($HasRef{$index} && $TVisible{$HasRef{$index}})) { | ||
| 522 | $slicetxt .= | ||
| 523 | &make_thread($index, (($lastlevel > 0) ? 0 : 1), $refindex); | ||
| 524 | } | ||
| 525 | } | ||
| 526 | # unindent if required | ||
| 527 | for ($i = 0; $i < $lastlevel; ++$i) { | ||
| 528 | if ($level <= $TSLICELEVELS) { | ||
| 529 | ($tmpl = $TSLICEINDENTEND) =~ | ||
| 530 | s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 531 | $slicetxt .= $tmpl; | ||
| 532 | } | ||
| 533 | --$level; | ||
| 534 | } | ||
| 535 | # close continuation if required | ||
| 536 | if ($lastlevel > 0) { | ||
| 537 | ($tmpl = $TSLICECONTEND) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 538 | $slicetxt .= $tmpl; | ||
| 539 | } | ||
| 540 | |||
| 541 | ## Reset visibility flags | ||
| 542 | foreach $index (@a) { $TVisible{$index} = 0; } | ||
| 543 | |||
| 544 | ($tmpl = $TSLICEEND) =~ s/$VarExp/&replace_li_var($1,'')/geo; | ||
| 545 | $slicetxt .= $tmpl; | ||
| 546 | |||
| 547 | $slicetxt; | ||
| 548 | } | ||
| 549 | |||
| 550 | ##--------------------------------------------------------------------------- | ||
| 551 | ## Routine to generate text representing a thread. | ||
| 552 | ## Used by make_thread_slice(). | ||
| 553 | ## Uses %Printed and $level defined by caller. | ||
| 554 | ## | ||
| 555 | sub make_thread { | ||
| 556 | my ($idx, $top, $refidx) = @_; | ||
| 557 | my ($attop, $haverepls, $hvnirepls, $single, $depth, $i); | ||
| 558 | my (@repls, @srepls) = (); | ||
| 559 | my ($ret) = ""; | ||
| 560 | |||
| 561 | ## Get replies | ||
| 562 | @repls = sort increase_index @{$Replies{$idx}} | ||
| 563 | if defined($Replies{$idx}); | ||
| 564 | @srepls = sort increase_index @{$SReplies{$idx}} | ||
| 565 | if defined($SReplies{$idx}); | ||
| 566 | $depth = $HasRefDepth{$idx}; | ||
| 567 | $hvnirepls = (@repls || @srepls); | ||
| 568 | |||
| 569 | @repls = grep($TVisible{$_}, @repls); | ||
| 570 | @srepls = grep($TVisible{$_}, @srepls); | ||
| 571 | $haverepls = (@repls || @srepls); | ||
| 572 | |||
| 573 | ## $hvnirepls is a flag if the message has replies, but they are | ||
| 574 | ## not visible. $haverepls is a flag if the message has visible | ||
| 575 | ## replies. $hvnirepls is used to determine the $attop and | ||
| 576 | ## $single flags. $haverepls is used for determine recursive | ||
| 577 | ## calls and level. | ||
| 578 | |||
| 579 | ## Print entry | ||
| 580 | $attop = ($top && $hvnirepls); | ||
| 581 | $single = ($top && !$hvnirepls); | ||
| 582 | |||
| 583 | if ($attop) { | ||
| 584 | $ret .= &expand_thread_var($idx, | ||
| 585 | ($idx eq $refidx) ? \$TSLICETOPBEGCUR : \$TSLICETOPBEG); | ||
| 586 | } elsif ($single) { | ||
| 587 | $ret .= &expand_thread_var($idx, | ||
| 588 | ($idx eq $refidx) ? \$TSLICESINGLETXTCUR : \$TSLICESINGLETXT); | ||
| 589 | } else { | ||
| 590 | ## Check for missing messages | ||
| 591 | if ($DoMissingMsgs) { | ||
| 592 | for ($i = $depth; $i > 0; $i--) { | ||
| 593 | $level++; | ||
| 594 | $ret .= &expand_thread_var($idx, \$TSLICELINONE); | ||
| 595 | $ret .= &expand_thread_var($idx, \$TSLICESUBLISTBEG) | ||
| 596 | if $level <= $TSLICELEVELS; | ||
| 597 | } | ||
| 598 | } | ||
| 599 | $ret .= &expand_thread_var($idx, | ||
| 600 | ($idx eq $refidx) ? \$TSLICELITXTCUR : \$TSLICELITXT); | ||
| 601 | } | ||
| 602 | |||
| 603 | ## Increment level count if their are replies | ||
| 604 | if ($haverepls) { | ||
| 605 | $level++; | ||
| 606 | } | ||
| 607 | |||
| 608 | ## Mark message printed | ||
| 609 | $Printed{$idx} = 1; | ||
| 610 | |||
| 611 | ## Print sub-threads | ||
| 612 | if (@repls) { | ||
| 613 | $ret .= &expand_thread_var($idx, \$TSLICESUBLISTBEG) | ||
| 614 | if $level <= $TSLICELEVELS; | ||
| 615 | foreach (@repls) { | ||
| 616 | $ret .= &make_thread($_, 0, $refidx); | ||
| 617 | } | ||
| 618 | $ret .= &expand_thread_var($idx, \$TSLICESUBLISTEND) | ||
| 619 | if $level <= $TSLICELEVELS; | ||
| 620 | } | ||
| 621 | if (@srepls) { | ||
| 622 | $ret .= &expand_thread_var($idx, \$TSLICESUBLISTBEG) | ||
| 623 | if $level <= $TSLICELEVELS; | ||
| 624 | $ret .= &expand_thread_var($idx, \$TSLICESUBJECTBEG); | ||
| 625 | foreach (@srepls) { | ||
| 626 | $ret .= &make_thread($_, 0, $refidx); | ||
| 627 | } | ||
| 628 | $ret .= &expand_thread_var($idx, \$TSLICESUBJECTEND); | ||
| 629 | $ret .= &expand_thread_var($idx, \$TSLICESUBLISTEND) | ||
| 630 | if $level <= $TSLICELEVELS; | ||
| 631 | } | ||
| 632 | |||
| 633 | ## Decrement level count if their were replies | ||
| 634 | if ($haverepls) { | ||
| 635 | $level--; | ||
| 636 | } | ||
| 637 | ## Check for missing messages | ||
| 638 | if ($DoMissingMsgs && !($attop || $single)) { | ||
| 639 | for ($i = $depth; $i > 0; $i--) { | ||
| 640 | $ret .= &expand_thread_var($idx, \$TSLICESUBLISTEND) | ||
| 641 | if $level <= $TSLICELEVELS; | ||
| 642 | $ret .= &expand_thread_var($idx, \$TSLICELINONEEND); | ||
| 643 | $level--; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | ## Close entry text | ||
| 648 | if ($attop) { | ||
| 649 | $ret .= &expand_thread_var($idx, | ||
| 650 | ($idx eq $refidx) ? \$TSLICETOPENDCUR : \$TSLICETOPEND); | ||
| 651 | } elsif (!$single) { | ||
| 652 | $ret .= &expand_thread_var($idx, | ||
| 653 | ($idx eq $refidx) ? \$TSLICELIENDCUR : \$TSLICELIEND); | ||
| 654 | } | ||
| 655 | |||
| 656 | $ret; | ||
| 657 | } | ||
| 658 | |||
| 659 | ##--------------------------------------------------------------------------- | ||
| 660 | ## Expand text based upon resource variable referenced by $tvar. | ||
| 661 | ## | ||
| 662 | sub expand_thread_var { | ||
| 663 | my ($index, $tvar) = @_; | ||
| 664 | my ($expstr); | ||
| 665 | ($expstr = $$tvar) =~ s/$VarExp/&replace_li_var($1,$index)/geo; | ||
| 666 | $expstr; | ||
| 667 | } | ||
| 668 | |||
| 669 | ##--------------------------------------------------------------------------- | ||
| 670 | 1; |