]> git.donarmstrong.com Git - bin.git/blob - parse_banlist
add mutt alias which executes neomutt if that exists
[bin.git] / parse_banlist
1 #! /usr/bin/perl
2
3 use warnings;
4 use strict;
5
6
7 use IO::File;
8 use Getopt::Long;
9 use Pod::Usage;
10
11 =head1 NAME
12
13 parse_banlist
14
15 =head1 SYNOPSIS
16
17 parse_banlist [options]
18
19  Options:
20   --bans, -b list of bans (bans.txt)
21   --remove, -r file to save bans to remove (bans_to_remove.txt)
22   --keep, -k file to save bans to keep (bans_to_keep.txt)
23   --debug, -d debugging level (Default 0)
24   --help, -h display this help
25   --man, -m display manual
26
27 =head1 OPTIONS
28
29 =over
30
31 =item B<--debug, -d>
32
33 Debug verbosity. (Default 0)
34
35 =item B<--help, -h>
36
37 Display brief useage information.
38
39 =item B<--man, -m>
40
41 Display this manual.
42
43 =back
44
45 =head1 EXAMPLES
46
47
48 =cut
49
50
51 use vars qw($DEBUG);
52
53 # XXX parse config file
54
55 my %options = (debug       => 0,
56                help        => 0,
57                man         => 0,
58                age         => 60*60*24*7,
59                unbans      => 4,
60                bans        => 'bans.txt',
61                remove      => 'bans_to_remove.txt',
62                keep        => 'bans_to_keep.txt',
63                pattern     => 1,
64                user        => 1,
65                wide        => 1,
66               );
67
68 GetOptions(\%options,'age|a=i','unbans|u=i','bans|b=s','remove|r=s',
69            'pattern|p!','keep|k=s','debug|d+','help|h|?','man|m',
70            'user|specific-user|s!','wide|wide-bans|w!',
71           );
72
73 pod2usage() if $options{help};
74 pod2usage({verbose=>2}) if $options{man};
75
76 $DEBUG = $options{debug};
77
78 my $bans_fh = new IO::File $options{bans}, 'r' or die "Unable to open file $options{bans} for reading: $!";;
79
80 my $bans_r_fh = new IO::File $options{remove}, 'w' or
81      die "Unable to open file $options{remove} for writing: $!";
82
83 my $bans_k_fh = new IO::File $options{keep}, 'w' or
84      die "Unable to open file $options{keep} for writing: $!";
85
86 my @bans_to_remove;
87
88 while (<$bans_fh>) {
89      # pull the ban and the times.
90      print {$bans_k_fh} "nomatch: $_" and next unless my ($channel, $banmask, $time) = 
91           $_ =~ /(\#\w+)\:\s+ban\s+([^\s]+)\s+\[by\s*.*?\,\s+(\d+)\s+secs/;
92      print {$bans_k_fh} "time: $_" and next unless $time > $options{age};
93
94      # Ignore bans against specific users.
95      if ($options{user}) {
96           print {$bans_k_fh} "user: $_" and next if $banmask =~ /^\%?\w/
97      }
98      # Ignore wide bans
99      if ($options{wide}) {
100           print {$bans_k_fh} "wide: $_" and next if $banmask =~ /\*\!.*?\@.*\*.*/;
101      }
102
103      push @bans_to_remove, $banmask;
104 }
105
106
107 while (my @unbans = splice(@bans_to_remove,0,$options{unbans})) {
108      print {$bans_r_fh} q(/mode -), q(b) x ($#unbans+1), q( ), join(' ',@unbans);
109      print {$bans_r_fh} qq(\n);
110 }