🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #50069

Re: docs on filter_art? Mime tools?

#50069
From: Andrew Gierth
Date: Tue, 23 Jan 2001 12:23
109 lines
4555 bytes
>>>>> "Russ" == Russ Allbery <rra@stanford.edu> writes:

 Russ> There's a rough wildmat to regex converter in the same
 Russ> authentication hook code, although it doesn't fully handle
 Russ> character classes.

this is the best I've come up with so far for wildmat->regex
conversion:

sub wildmat_to_regex
{
    my $w = quotemeta shift;
    $w =~ s{\\([*?]|\[(?:\\\^)?(?:\\\])?[^]]*\\\])}
           {($1 eq '*' ? '.*' :
	     $1 eq '?' ? '.'  :
	     do { my $p = $1; $p =~ s/\\([][^-])/$1/g; $p; })}eg;
    return $w;
}

notes:
  - currently '\' is not special in a wildmat, most converters I've seen
    get this wrong. Likewise, '*' and '?' are not special inside [].
  - this code makes no attempt to detect invalid wildmats, and may behave
    strangely in such cases (but then this is also true of the C code),
    _but_ it shouldn't leak any dangerous constructs through to Perl
    since (, ?, { etc are always escaped.
  - this function doesn't anchor the regexp, the caller must do that, e.g.
    by using qr/^$re$/o to compile the result.

The above only handles single wildmats, not lists of wildmats. Turning
a list of wildmats into a single regexp is a more interesting
challenge, the following code _seems_ to work, but no guarantees:


# wildmat_list_to_regexp
# (C) 2001 Andrew Gierth, andrew@erlenstar.demon.co.uk
# This code is provided "AS IS" and WITHOUT ANY WARRANTY. It may be
# freely distributed in source form only, with or without modification,
# provided this copyright notice and disclaimer are preserved.

# notes:
#  assumes a default acceptance match if the first entry is a negated one.
#  i.e. !foo.* matches everything except foo.*, same as *,!foo.*
#  caller is responsible for splitting the pattern list and providing it
#  to this routine in list form.
#  e.g. wildmat_list_to_regex('foo.*', '!foo.private.*')
#  or wildmat_list_to_regex(split(/,+/,$patlist))
#  The basic idea is to use lookahead negative assertions to exclude
#  those patterns that should not match. The complicated part is dealing
#  with accepting patterns that follow rejecting ones; this requires
#  negative lookahead assertions inside negative lookahead assertions
#  (ow, my brane hurts). The complexity of the resulting regexp thus
#  grows according to the number of sense-reversals in the pattern list.
#  We reverse the order of the list during processing in order to handle
#  this.
#  This function _does_ include the ^ $ anchors in the returned string,
#  because they are necessary for correctness (lots of $ anchors are
#  embedded in the result).
 

sub wildmat_list_to_regex
{
    my @list = @_;
    my @clist;
    for (@list)
    {
	my $neg = s/^!//;
	my $pat = wildmat_to_regex($_);
	if (!@clist || $neg != $clist[0][0])
	{
	    unshift @clist,[ $neg, $pat ];
	}
	else
	{
	    $clist[0][1] .= "|$pat";
	}
    }
    push @clist,[ 0, ".*" ] if !@clist or $clist[-1][0];
    my @accept;
    my @reject;
    for (@clist)
    {
	my $pat = $_->[1];
	if ($_->[0])
	{
	    if (@accept)
	    {
		$pat = "(?!(?:".join('|',@accept).")\$)(?:$pat)";
	    }
	    unshift @reject,$pat;
	}
	else
	{
	    unshift @accept,$pat;
	}
    }

    my $pat = "^";
    $pat .= "(?!(?:".join('|',@reject).")\$)" if @reject;
    $pat .= "(?:".join('|',@accept).")";
    $pat .= "\$";
    return $pat;
}

__END__

-- 
Andrew.
Technical Details
Message-ID: <87n1cio3t8.fsf@erlenstar.demon.co.uk>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!supernews.google.com!sn-xit-02!sn-xit-03!supernews.com!news-feed.riddles.org.uk!erlenstar.demon.co.uk!newsmonster
References: <G7FGpD.6Fz@world.std.com> <yllms7cesh.fsf@windlord.stanford.edu> <G7Ko9B.M88@world.std.com> <yl7l3n3zy7.fsf@windlord.stanford.edu>

Show Full Headers
Complete Article Headers
Message-ID: <87n1cio3t8.fsf@erlenstar.demon.co.uk>
Subject: Re: docs on filter_art? Mime tools?
Date: Tue, 23 Jan 2001 12:23:15 +0000
From: Andrew Gierth <andrew@erlenstar.demon.co.uk>
References: <G7FGpD.6Fz@world.std.com> <yllms7cesh.fsf@windlord.stanford.edu> <G7Ko9B.M88@world.std.com> <yl7l3n3zy7.fsf@windlord.stanford.edu>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!supernews.google.com!sn-xit-02!sn-xit-03!supernews.com!news-feed.riddles.org.uk!erlenstar.demon.co.uk!newsmonster
Newsgroups: news.software.nntp
Organization: disorganised
Lines: 108
NNTP-Posting-Host: erlenstar.demon.co.uk (2f37:0574)
X-Mayan-Date: Long count = 12.19.7.16.8; tzolkin = 7 Lamat; haab = 11 Muan
X-Pgp-0x0E9FFBE9: 87 25 7F 14 41 24 B3 51 E0 19 8B DE 49 74 0C 29
X-Attribution: AG
X-P-Persistent-Headers: are a bad idea.
X-Newsreader: Gnus v5.5/Emacs 20.2
X-Complaints-To: andrew@erlenstar.demon.co.uk
X-Post-Filter: postfilter v0.6 alpha