# -*- cperl -*- package POE::Component::IRC::Plugin::NoPaste::Httpd::Pasted; use strict; use warnings; use POE; use POE::Component::IRC::Plugin::NoPaste::Httpd; use POE::Component::IRC::Plugin::NoPaste::StaticFile; use Locale::TextDomain qw(pci-nopaste); use HTTP::Status; use Socket; use Time::Format qw(%time); use Encode qw(from_to); sub handler { my ($this, $req, $resp) = @_; if ($req->uri->path =~ m!^/pasted/(\d+)$!) { return pasted_handler( $this, $req, $resp, $1); } else { return notfound_handler(@_); } } sub pasted_handler { my ($this, $req, $resp, $post_id) = @_; # check for the existence of line in db my $post = $this->{nopaste}{db}->dbh->selectrow_hashref(q{ SELECT * FROM post WHERE post_id = ? }, undef, $post_id); if (not $post) { return notfound_handler(@_); } # is the channel_id still defined? my $ch = $this->{nopaste}{id_to_channel}{$post->{channel_id}}; if (not $ch) { # no. probably there should be some way to change channel_id. return notfound_handler(@_); } my $escape = sub { $_ = shift; s!&!&!g; s!!>!g; $_; }; local $ENV{LANGUAGE} = $ch->{locale}; my $t = POE::Component::IRC::Plugin::NoPaste::Template->new( -data => static_file('tmpl/pasted.html')); $t->expand( lang => $ch->{locale}, title => $escape->("NoPaste :: $ch->{name}"), ); my ($ptr) = gethostbyaddr( inet_aton($req->{connection}->remote_ip), AF_INET); my @readable = $this->{nopaste}->pickup_readable($ch->{id}, $ptr); if (@readable or $ch->{restriction}{read} eq 'any') { # we want to know which are the next and the previous posts. my $prev = $this->{nopaste}{db}->dbh->selectrow_hashref(q{ SELECT * FROM post WHERE channel_id = ? AND posted_time > ? LIMIT 1 }, undef, $ch->{id}, $post->{posted_time}); my $next = $this->{nopaste}{db}->dbh->selectrow_hashref(q{ SELECT * FROM post WHERE channel_id = ? AND posted_time < ? LIMIT 1 }, undef, $ch->{id}, $post->{posted_time}); if ($prev) { $t->readable->prev->add( id => $prev->{post_id}, pasted_by => __x( "Pasted by {name}", name => $escape->($prev->{nick})), title => $escape->($prev->{title}), ); } $t->readable->expand( channel_id => $ch->{id}, l18n_index => __"Index", ); if ($next) { $t->readable->next->add( id => $next->{post_id}, pasted_by => __x( "Pasted by {name}", name => $escape->($next->{nick})), title => $escape->($next->{title}), ); } $t->readable->add( l18n_name => __"Name", name => $escape->($post->{nick}), l18n_title => __"Title", title => $escape->($post->{title}), l18n_body => __"Body", body => $escape->($post->{body}), ); } else { my $msg = $this->{nopaste}->gen_unreadable_error($ch); $msg =~ s!\n!
!g; $t->not_readable->add( error => $msg, ); } $resp->content($t->str); return RC_OK; } 1;