#! /usr/bin/env perl -ni # J$ try to restrict a (optionally continued) References: header properly # to $maxlen, keeping as many IDs as will fit, but: # - keep at least the first and the last three IDs # - keep all `non-deletables' # - replace deleted MsgIDs by a single space BEGIN { $keepend = 3; # Don't change! $keepstart= 1; # Ditto $maxlen = 998; # Lower this only if you _really_ have to } if($refs) { if (/^[ \t]/) { # continued refs header $refs .= $_; next; } else { # No more continuation: weedout damaged msgids, fit the rest nicely # One _might_ choose to be relatively permissive on bogus msgids # @refs = grep {/^<.+>$/} split /\s+/, $refs; # Be relatively strict about the correctness of msgids (RFC1036) # @refs = grep {/^<[^<>\@\s]+\@\S+\.\S+>$/} split /\s+/, $refs; # Even stricter: RFC1036bis @refs = grep {/^<[^<>!,;:\\\@\"\s]+\@\S+\.\S+>$/} split /\s+/, $refs; # Ok -- all refs in @refs valid, go ahead! undef $refs; $len = length(join(' ',"References:",@refs)); $numrefs = $#refs; $offset = $keepstart; while(($numrefs > ($keepstart+$keepend)) && ($len > $maxlen)) { if(!($refs[$offset] =~ /_-_\@/)) { # Don't remove non-deletable IDs if($refs[$offset-1] eq " ") { # Already marked, no need to mark $len -= length(splice(@refs, $offset, 1)) + 1; } else { # Mark deleted item (with ' ') $len -= length(splice(@refs, $offset++, 1, " ")) - 1; } } else { $offset++; # Skip non-deletable } $numrefs--; } # Print only if any msgids are left print join(' ', "References:", @refs), "\n" if @refs; } } if (/^References: /) { # first line of header $refs = $_; next; } print;