2 package POE::Component::IRC::Plugin::NoPaste::Httpd::Index;
6 use POE::Component::IRC::Plugin::NoPaste::Httpd;
7 use POE::Component::IRC::Plugin::NoPaste::StaticFile;
8 use POE::Component::IRC::Plugin::NoPaste::Pager;
10 use Locale::TextDomain qw(pci-nopaste);
13 use Time::Format qw(%time);
14 use Encode qw(from_to);
15 use constant NICK_LIMIT => 20;
16 use constant TITLE_LIMIT => 100;
17 use constant BODY_LIMIT => 500 * 1024;
20 my ($this, $req, $resp) = @_;
22 if ($req->uri->path =~ m!^/nopaste/(.+?)/?$! and
23 $this->{nopaste}{id_to_channel}{$1}) {
25 $this, $req, $resp, $this->{nopaste}{id_to_channel}{$1});
27 elsif ($req->uri->path =~ m!^/post/(.+?)/?$! and
28 $this->{nopaste}{id_to_channel}{$1}) {
30 $this, $req, $resp, $this->{nopaste}{id_to_channel}{$1});
33 return notfound_handler(@_);
38 my ($this, $req, $resp, $ch, $error) = @_;
40 local $ENV{LANGUAGE} = $ch->{locale};
42 my $t = POE::Component::IRC::Plugin::NoPaste::Template->new(
43 -data => static_file('tmpl/index.html'));
46 lang => $ch->{locale},
47 title => "NoPaste :: $ch->{name}",
50 my ($ptr) = gethostbyaddr(
51 inet_aton($req->{connection}->remote_ip), AF_INET);
52 my @writable = $this->{nopaste}->pickup_writable($ch->{id}, $ptr);
53 my @readable = $this->{nopaste}->pickup_readable($ch->{id}, $ptr);
55 if (@writable or $ch->{restriction}{write} eq 'any') {
59 $t->writable->error->add(error => $_);
63 channel_id => $ch->{id},
64 l18n_name => __"Name",
67 if ($ch->{restriction}{write} ne 'any') {
69 $t->writable->restricted->only_one->add(
74 foreach my $nick (@writable) {
75 $t->writable->restricted->multiple->entry->add(
79 $t->writable->restricted->multiple->add;
81 $t->writable->restricted->add;
84 $t->writable->not_restricted->add;
88 l18n_title => __"Title",
93 l18n_paste => __"Paste",
96 # set the default values
97 my $q = $req->uri->clone;
98 if (not $q->query_param('nick') and @writable == 1) {
99 $q->query_param(nick => $writable[0]);
101 $t->setform($q => 'post');
104 my $msg = $this->{nopaste}->gen_unwritable_error($ch);
105 $msg =~ s!\n!<br />!g;
107 $t->not_writable->add(
112 if (@readable or $ch->{restriction}{read} eq 'any') {
113 my $pager = POE::Component::IRC::Plugin::NoPaste::Pager->new(
119 ORDER BY posted_time DESC
121 placeholder => [$ch->{id}],
122 dbh => $this->{nopaste}{db}->dbh,
124 page_to_show => scalar $req->uri->query_param('page'),
125 pagenums_limit => 20,
126 items_per_page => 20,
130 if ($meta{page} == 1) {
131 $t->readable->prev->nolink->add;
134 $t->readable->prev->link->add(
135 page => $meta{page} - 1,
138 $t->readable->prev->add;
140 foreach my $i ($meta{pagelink_begin} .. $meta{pagelink_end}) {
141 if ($i == $meta{page}) {
142 $t->readable->pages->page->nolink->add(
147 $t->readable->pages->page->link->add(
151 $t->readable->pages->page->add;
153 $t->readable->pages->add;
155 if ($meta{page} == $meta{last_page}) {
156 $t->readable->next->nolink->add;
159 $t->readable->next->link->add(
160 page => $meta{page} + 1,
163 $t->readable->next->add;
165 show_content => sub {
168 $t->readable->list_data->add(
169 id => $row->{post_id},
170 title => $row->{title},
171 name => $row->{nick},
172 body => $row->{body},
173 timestamp => $time{'yyyy-mm-dd hh:mm:ss', $row->{posted_time}},
177 $t->readable->list_nodata->add(
178 l18n_nodata => __"There are no entries currently.",
185 l18n_title => __"Title",
186 l18n_name => __"Name",
187 l18n_timestamp => __"Time Stamp",
191 my $msg = $this->{nopaste}->gen_unreadable_error($ch);
192 $msg =~ s!\n!<br />!g;
194 $t->not_readable->add(
199 $resp->content($t->str);
204 my ($this, $req, $resp, $ch) = @_;
205 my $nick = $req->uri->query_param('nick');
206 my $title = $req->uri->query_param('title');
207 my $body = $req->uri->query_param('body');
209 local $ENV{LANGUAGE} = $ch->{locale};
212 if (not defined $nick or not length $nick) {
213 push @error, __"Please don't omit your name.";
215 elsif (length($nick) > NICK_LIMIT) {
216 push @error, __x("Please shorten your name up to {n} bytes.", n => NICK_LIMIT);
218 if (not defined $title or not length $title) {
219 push @error, __"Please don't omit the title.";
221 elsif (length($title) > TITLE_LIMIT) {
222 push @error, __x("Please shorten the title up to {n} bytes.", n => TITLE_LIMIT);
224 if (not defined $body or not length $body) {
225 push @error, __"Please don't omit the body.";
227 elsif (length($body) > BODY_LIMIT) {
228 push @error, __x("Please shorten the body up to {n} bytes.", n => BODY_LIMIT);
232 return index_handler($this, $req, $resp, $ch, join("\n", map {"* $_"} @error));
235 my ($ptr) = gethostbyaddr(
236 inet_aton($req->{connection}->remote_ip), AF_INET);
237 my @writable = $this->{nopaste}->pickup_writable($ch->{id}, $ptr);
238 if ($ch->{restriction}{write} eq 'any' or
239 grep {$_ eq $nick} @writable) {
241 my $db = $this->{nopaste}{db};
243 # check for duplication
244 if ($db->dbh->selectrow_hashref(q{
247 WHERE channel_id = ? AND nick = ? AND
248 title = ? AND body = ?
249 }, undef, $ch->{id}, $nick, $title, $body)) {
250 return index_handler(
251 $this, $req, $resp, $ch, __"A duplicated entry is being pasted.");
257 (channel_id, posted_time, nick, title, body)
258 VALUES (? , ? , ? , ? , ? )
259 }, undef, $ch->{id}, time, $nick, $title, $body);
262 my ($post_id) = $db->dbh->selectrow_array(q{
263 SELECT LAST_INSERT_ROWID()});
265 # announce it in the channel
266 my $name = $ch->{name};
267 from_to $name, 'UTF-8' => $ch->{charset};
270 __x("{nick} pasted an entry: {title}", nick => $nick, title => $title),
272 my $uri = $req->uri->clone;
273 $uri->path("/pasted/$post_id");
275 $uri->fragment(undef);
281 from_to $line, 'UTF-8' => $ch->{charset};
283 $this->{nopaste}{irc}->yield(
284 notice => $name => $line);
287 # redirect to the index
288 return redirect_handler(
289 $this, $req, $resp, "/nopaste/$ch->{id}/");
292 return index_handler($this, $req, $resp, $ch);