]> gitweb @ CieloNegro.org - pci-nopaste.git/blob - lib/POE/Component/IRC/Plugin/NoPaste/Httpd/Pasted.pm
migrate from GNU arch to Git
[pci-nopaste.git] / lib / POE / Component / IRC / Plugin / NoPaste / Httpd / Pasted.pm
1 # -*- cperl -*-
2 package POE::Component::IRC::Plugin::NoPaste::Httpd::Pasted;
3 use strict;
4 use warnings;
5 use POE;
6 use POE::Component::IRC::Plugin::NoPaste::Httpd;
7 use POE::Component::IRC::Plugin::NoPaste::StaticFile;
8 use Locale::TextDomain qw(pci-nopaste);
9 use HTTP::Status;
10 use Socket;
11 use Time::Format qw(%time);
12 use Encode qw(from_to);
13
14 sub handler {
15     my ($this, $req, $resp) = @_;
16
17     if ($req->uri->path =~ m!^/pasted/(\d+)$!) {
18         return pasted_handler(
19             $this, $req, $resp, $1);
20     }
21     else {
22         return notfound_handler(@_);
23     }
24 }
25
26 sub pasted_handler {
27     my ($this, $req, $resp, $post_id) = @_;
28
29     # check for the existence of line in db
30     my $post = $this->{nopaste}{db}->dbh->selectrow_hashref(q{
31         SELECT *
32           FROM post
33          WHERE post_id = ?
34     }, undef, $post_id);
35
36     if (not $post) {
37         return notfound_handler(@_);
38     }
39     
40     # is the channel_id still defined?
41     my $ch = $this->{nopaste}{id_to_channel}{$post->{channel_id}};
42     if (not $ch) {
43         # no. probably there should be some way to change channel_id.
44         return notfound_handler(@_);
45     }
46
47     my $escape = sub {
48         $_ = shift;
49         s!&!&!g;
50         s!<!&lt;!g;
51         s!>!&gt;!g;
52         $_;
53     };
54
55     local $ENV{LANGUAGE} = $ch->{locale};
56
57     my $t = POE::Component::IRC::Plugin::NoPaste::Template->new(
58         -data => static_file('tmpl/pasted.html'));
59
60     $t->expand(
61         lang  => $ch->{locale},
62         title => $escape->("NoPaste :: $ch->{name}"),
63        );
64
65     my ($ptr) = gethostbyaddr(
66         inet_aton($req->{connection}->remote_ip), AF_INET);
67     my @readable = $this->{nopaste}->pickup_readable($ch->{id}, $ptr);
68
69     if (@readable or $ch->{restriction}{read} eq 'any') {
70         # we want to know which are the next and the previous posts.
71         my $prev = $this->{nopaste}{db}->dbh->selectrow_hashref(q{
72             SELECT *
73               FROM post
74              WHERE channel_id = ? AND posted_time > ?
75              LIMIT 1
76         }, undef, $ch->{id}, $post->{posted_time});
77         my $next = $this->{nopaste}{db}->dbh->selectrow_hashref(q{
78             SELECT *
79               FROM post
80              WHERE channel_id = ? AND posted_time < ?
81              LIMIT 1
82         }, undef, $ch->{id}, $post->{posted_time});
83
84         if ($prev) {
85             $t->readable->prev->add(
86                 id        => $prev->{post_id},
87                 pasted_by => __x(
88                     "Pasted by {name}", name => $escape->($prev->{nick})),
89                 title     => $escape->($prev->{title}),
90                );
91         }
92         $t->readable->expand(
93             channel_id => $ch->{id},
94             l18n_index => __"Index",
95            );
96         if ($next) {
97             $t->readable->next->add(
98                 id        => $next->{post_id},
99                 pasted_by => __x(
100                     "Pasted by {name}", name => $escape->($next->{nick})),
101                 title     => $escape->($next->{title}),
102                );
103         }
104
105         $t->readable->add(
106             l18n_name  => __"Name",
107             name       => $escape->($post->{nick}),
108             l18n_title => __"Title",
109             title      => $escape->($post->{title}),
110             l18n_body  => __"Body",
111             body       => $escape->($post->{body}),
112            );
113     }
114     else {
115         my $msg = $this->{nopaste}->gen_unreadable_error($ch);
116         $msg =~ s!\n!<br />!g;
117         
118         $t->not_readable->add(
119             error => $msg,
120            );
121     }
122
123     $resp->content($t->str);
124     return RC_OK;
125 }
126
127 1;