forked from hoytech/strfry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumbFilter.pl
123 lines (101 loc) · 2.6 KB
/
dumbFilter.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env perl
use strict;
use JSON::XS;
binmode(STDOUT, ":utf8");
my $filterJson = shift || die "need filter";
my $filter = decode_json($filterJson);
while(<STDIN>) {
my $ev = decode_json($_);
if (doesMatch($ev, $filter)) {
print $_;
}
}
sub doesMatch {
my $ev = shift;
my $filter = shift;
$filter = [$filter] if ref $filter eq 'HASH';
foreach my $singleFilter (@$filter) {
return 1 if doesMatchSingle($ev, $singleFilter);
}
return 0;
}
sub doesMatchSingle {
my $ev = shift;
my $filter = shift;
if (defined $filter->{since}) {
return 0 if $ev->{created_at} < $filter->{since};
}
if (defined $filter->{until}) {
return 0 if $ev->{created_at} > $filter->{until};
}
if ($filter->{ids}) {
my $found;
foreach my $id (@{ $filter->{ids} }) {
if (startsWith($ev->{id}, $id)) {
$found = 1;
last;
}
}
return 0 if !$found;
}
if ($filter->{authors}) {
my $found;
foreach my $author (@{ $filter->{authors} }) {
if (startsWith($ev->{pubkey}, $author)) {
$found = 1;
last;
}
}
return 0 if !$found;
}
if ($filter->{kinds}) {
my $found;
foreach my $kind (@{ $filter->{kinds} }) {
if ($ev->{kind} == $kind) {
$found = 1;
last;
}
}
return 0 if !$found;
}
if ($filter->{'#e'}) {
my $found;
foreach my $search (@{ $filter->{'#e'} }) {
foreach my $tag (@{ $ev->{tags} }) {
if ($tag->[0] eq 'e' && $tag->[1] eq $search) {
$found = 1;
last;
}
}
}
return 0 if !$found;
}
if ($filter->{'#p'}) {
my $found;
foreach my $search (@{ $filter->{'#p'} }) {
foreach my $tag (@{ $ev->{tags} }) {
if ($tag->[0] eq 'p' && $tag->[1] eq $search) {
$found = 1;
last;
}
}
}
return 0 if !$found;
}
if ($filter->{'#t'}) {
my $found;
foreach my $search (@{ $filter->{'#t'} }) {
foreach my $tag (@{ $ev->{tags} }) {
if ($tag->[0] eq 't' && $tag->[1] eq $search) {
$found = 1;
last;
}
}
}
return 0 if !$found;
}
return 1;
}
sub startsWith {
return rindex($_[0], $_[1], 0) == 0;
}