2 package POE::Component::IRC::Plugin::NoPaste;
6 Component::IRC::Plugin::NoPaste::Httpd
7 Component::IRC::Plugin::NoPaste::DB
9 use POE::Component::IRC::Plugin qw(:ALL);
10 use Encode qw(from_to);
11 use Locale::TextDomain qw(pci-nopaste);
13 use Net::Domain qw(hostfqdn);
14 use Hash::Util qw(lock_keys);
15 our $VERSION = '0.01';
17 #$SIG{__DIE__} = sub { print "$_[0]\n"; };
20 my ($class, $config) = @_;
21 my $this = bless {} => $class;
23 $this->{config} = $config; # comes from yaml
25 $this->{httpd_alias} = undef;
28 $this->{db} = POE::Component::IRC::Plugin::NoPaste::DB->new($config);
30 $this->{name_to_channel} = {}; # name => /channels/*
31 # Note: keys of hash are name of channels which are encoded in
32 # channel's specific charset, and are lowercased.
33 foreach my $ch (@{$config->{channels}}) {
34 my $name = $ch->{name};
35 from_to $name, 'UTF-8' => $ch->{charset};
36 $this->{name_to_channel}{lc $name} = $ch;
39 $this->{id_to_channel} = {}; # id => /channels/*
40 foreach my $ch (@{$config->{channels}}) {
41 $this->{id_to_channel}{$ch->{id}} = $ch;
50 my ($this, $irc) = @_;
54 $this->{httpd_alias} =
55 POE::Component::IRC::Plugin::NoPaste::Httpd->spawn($this);
57 $irc->plugin_register(
64 connect => $this->{config}{irc});
70 my ($this, $irc) = @_;
73 $this->{httpd_alias} => 'shutdown');
79 my ($this, $irc, $line) = @_;
82 "connected to {server}:{port}.",
83 server => $this->{config}{irc}{server},
84 port => $this->{config}{irc}{port}));
86 foreach my $ch (@{$this->{config}{channels}}) {
87 my $name = $ch->{name};
88 from_to $name, 'UTF-8' => $ch->{charset};
98 my ($this, $irc, $from, $to, $msg) = @_;
100 local $SIG{__DIE__} = sub {
101 print STDERR $_[0], "\n";
104 my $mynick = $irc->nick_name;
105 my $ch = $this->{name_to_channel}{lc $$to->[0]};
107 if ($$msg =~ m/$mynick/i and # Never use /o flag.
110 local $ENV{LANGUAGE} = $ch->{locale};
112 my $name = $ch->{name};
113 from_to $name, 'UTF-8' => $ch->{charset};
116 "{nick}: The NoPaste for {channel} is at {url}",
117 nick => (split /!/, $$from)[0],
118 channel => $ch->{name},
121 $uri->scheme('http');
122 $uri->host(hostfqdn);
123 $uri->port($this->{config}{httpd}{port});
124 $uri->path("/nopaste/$ch->{id}/");
125 $uri->canonical->as_string;
128 from_to $reply, 'UTF-8' => $ch->{charset};
131 privmsg => $name => $reply);
138 my ($this, $msg) = @_;
140 print STDERR "NoPaste: $msg\n";
143 sub pickup_writable {
144 my ($this, $channel_id, $host) = @_;
146 $this->pickup(write => $channel_id, $host);
149 sub pickup_readable {
150 my ($this, $channel_id, $host) = @_;
152 $this->pickup(read => $channel_id, $host);
156 # $what: 'read' or 'write'
157 my ($this, $what, $channel_id, $host) = @_;
159 my $ch = $this->{id_to_channel}{$channel_id};
162 my $name = $ch->{name};
163 from_to $name, 'UTF-8' => $ch->{charset};
165 my $restrict = $ch->{restriction}{$what};
167 #$this->log("restrict: $restrict, name: $name, nick: $_");
168 if (not $this->{irc}->nick_info($_)) {
171 elsif ($host ne $this->{irc}->nick_info($_)->{Host}) {
174 elsif ($restrict eq 'op') {
175 $this->{irc}->is_channel_operator($name, $_);
177 elsif ($restrict eq 'halfop') {
178 $this->{irc}->is_channel_halfop($name, $_);
180 elsif ($restrict eq 'voice') {
181 $this->{irc}->has_channel_voice($name, $_);
183 elsif ($restrict eq 'member') {
189 } $this->{irc}->channel_list($name);
192 sub gen_unwritable_error {
193 my ($this, $ch) = @_;
195 my $msg = __"Sorry, but you can't paste to this channel;\n";
197 $_ = $ch->{restriction}{write};
200 # "it" means "this channel"
201 "allowed only for those who have op in it.");
203 elsif ($_ eq 'halfop') {
204 $msg .= __"allowed only for those who have halfop in it.";
206 elsif ($_ eq 'voice') {
207 $msg .= __"allowed only for those who have voice in it.";
209 elsif ($_ eq 'member') {
210 $msg .= __"allowed only for those who are in it.";
216 sub gen_unreadable_error {
217 my ($this, $ch) = @_;
219 my $msg = __"Sorry, but you can't read posts pasted to this channel;\n";
221 $_ = $ch->{restriction}{read};
224 # "it" means "this channel"
225 "allowed only for those who have op in it.");
227 elsif ($_ eq 'halfop') {
228 $msg .= __"allowed only for those who have halfop in it.";
230 elsif ($_ eq 'voice') {
231 $msg .= __"allowed only for those who have voice in it.";
233 elsif ($_ eq 'member') {
234 $msg .= __"allowed only for those who are in it.";
243 # Below is stub documentation for your module. You'd better edit it!
247 POE::Component::IRC::Plugin::NoPaste - Perl extension for blah blah blah
251 use POE::Component::IRC::Plugin::NoPaste;
256 Stub documentation for POE::Component::IRC::Plugin::NoPaste, created by h2xs. It looks like the
257 author of the extension was negligent enough to leave the stub
270 Mention other useful documentation such as the documentation of
271 related modules or operating system documentation (such as man pages
272 in UNIX), or any relevant external documentation such as RFCs or
275 If you have a mailing list set up for your module, mention it here.
277 If you have a web site set up for your module, mention it here.
281 Administrador, E<lt>admin@apple.comE<gt>
283 =head1 COPYRIGHT AND LICENSE
285 Copyright (C) 2005 by Administrador
287 This library is free software; you can redistribute it and/or modify
288 it under the same terms as Perl itself, either Perl version 5.8.6 or,
289 at your option, any later version of Perl 5 you may have available.