diff --git a/lib/NewsStats.pm b/lib/NewsStats.pm index 01e1994..1664e48 100644 --- a/lib/NewsStats.pm +++ b/lib/NewsStats.pm @@ -2,7 +2,7 @@ # # Library functions for the NewsStats package. # -# Copyright (c) 2010-2013 Thomas Hochstein +# Copyright (c) 2010-2013, 2025 Thomas Hochstein # # This module can be redistributed and/or modified under the same terms under # which Perl itself is published. @@ -34,6 +34,7 @@ require Exporter; ListNewsgroups ParseHierarchies ReadGroupList + ParseHeaders OutputData FormatOutput SQLHierarchies @@ -254,6 +255,42 @@ sub ReadGroupList { return \%ValidGroups; }; +################################################################################ +sub ParseHeaders { +################################################################################ +### return a hash of all headers (ignoring duplicate headers) +### parsed from raw headers +### -> taken and modified from pgpverify +### -> Written April 1996, (David C Lawrence) +### -> Currently maintained by Russ Allbery +### IN : $RawHeaders : raw headers as found in posting +### OUT: %Headers : hash containing header contents, +### keyed by lower-case header name + my (%Header, $Label, $Value); + foreach (@_) { + s/\r?\n$//; + + last if /^$/; + + if (/^(\S+):[ \t](.+)/) { + ($Label, $Value) = ($1, $2); + # discard all duplicate headers + next if $Header{lc($Label)}; + $Header{lc($Label)} = $Value; + } elsif (/^\s/) { + # continuation lines + if ($Label) { + $Header{lc($Label)} .= "\n$_"; + } else { + warn (sprintf("Non-header line: %s\n",$_)); + } + } else { + warn (sprintf("Non-header line: %s\n",$_)); + } + } + return %Header; +}; + ################################################################################ #####----------------------------- TimePeriods ----------------------------#####