]> pd.if.org Git - newsd/blob - feed
Let database figure out close time for connection.
[newsd] / feed
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use DBI;
7
8 my $dsn = $ENV{'DBI_DSN'};
9 $dsn = 'dbi:Pg:dbname=news' unless defined $dsn;
10
11 my $db = DBI->connect($dsn,'','',{AutoCommit => 0,RaiseError=>1});
12
13 die unless $db;
14
15 $db->do("set CLIENT_ENCODING to 'SQL_ASCII'");
16
17
18 sub wildmat_to_re {
19         my ($wildmat) = @_;
20
21         $wildmat =~ s/\./\\\./g;
22         $wildmat =~ s/\?/\./g;
23         $wildmat =~ s/\*/\.\*/g;
24         return $wildmat;
25 }
26
27 sub wildmat {
28         my ($wildmat) = @_;
29
30         my @pats = split(/,/,$wildmat); # TODO look for escaped commas
31
32         my $sql = '';
33         # TODO special case '*' since it always matches
34
35         while ($pats[0] =~ /^!/) { shift @pats } # init neg can't match
36
37         my $negated;
38         foreach (@pats) {
39                 $negated = s/^!//;
40                 my $like = wildmat_to_re($_);
41                 if (!$negated) {
42                         $sql .= '|' . $like;
43                 } else {
44                         $sql =~ s/^\|//;
45                         $sql = "(^(?!$like)($sql)\$)";
46                 }
47         }
48         $sql =~ s/^\|//;
49         $sql = "^($sql)\$" unless $negated;
50         return $sql;
51 }
52
53 my ($peer, $feed, $port) = @ARGV;
54
55 $feed = '*,!control,!junk' unless $feed;
56 my $feedre = wildmat($feed);
57
58 my $add = $db->prepare('insert into feeds (name) values (?)');
59 my $setport = $db->prepare('update feeds set port = ? where name = ?');
60 my $setfeed = $db->prepare('update feeds set wildmat = ?, wildmatre = ? where name = ?');
61 my $exists = $db->prepare('select * from feeds where name = ?');
62
63 $exists->execute($peer);
64
65 if (!$exists->fetchrow_array) {
66         $add->execute($peer);
67 };
68
69 $setport->execute($port,$peer) if $port;
70 $setfeed->execute($feed,$feedre,$peer) if $feed;
71
72 $db->commit;
73 $db->disconnect;
74
75 __END__
76
77 create table feeds (
78         host    text primary key, -- TODO just make this a url? mailto:
79         port    integer default 119,
80         enabled boolean not null default true,
81         stream  boolean not null default false, -- use CHECK/TAKETHIS
82         groups  text default '*', -- wildmat
83         wildmat text default '*',
84         wildmatre       text default '.', -- parsed wildmat
85         noxposts        text,
86         noxpostsre      text,
87         distribution    text,
88         distributionre  text,
89         -- TODO feed moderated/unmoderated
90         -- see http://www.faqs.org/docs/linux_network/x18341.html
91         maxsize integer, -- maximum article size to feed
92         localonly       boolean default false,
93         path    text, -- regular expression default host?
94         frequency       interval default '1 hour'::interval, -- int secs?
95         feedtime        timestamp
96
97 );
98