| 1 | package POE::Component::IRC::Plugin::AutoJoin; | ||||||||||||||
| 2 | 9 | $POE::Component::IRC::Plugin::AutoJoin::VERSION = '6.95'; | |||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 9 | ▶ | use warnings FATAL => 'all'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 9 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 9 | ▶ | use IRC::Utils qw(parse_user lc_irc); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 9 | ▶ | use POE::Component::IRC::Plugin qw(:ALL); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 6 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 10 | 6 | my ($package) = shift; | |||||||||||||
| 11 | 6 | ▶ | croak "$package requires an even number of arguments" if @_ & 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 12 | 6 | my %self = @_; | |||||||||||||
| 13 | 6 | return bless \%self, $package; | |||||||||||||
| 14 | } | ||||||||||||||
| 15 | |||||||||||||||
| 16 | 6 | ▶ | sub PCI_register { | ||||||||||||
|
Subroutine
PCI_register: called CC=9 SCAR=22.6
Pod
undocumented
| |||||||||||||||
| 17 | 6 | my ($self, $irc) = @_; | |||||||||||||
| 18 | |||||||||||||||
| 19 | 6 | ▶ | if (!$self->{Channels}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 20 | 2 | ▶ | if ($irc->isa('POE::Component::IRC::State')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | 1 | for my $chan (keys %{ $irc->channels() }) { | |||||||||||||
| 22 | 0 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 23 | # note that this will not get the real key on ircu servers | ||||||||||||||
| 24 | # in channels where we don't have ops | ||||||||||||||
| 25 | 0 | ▶ | my $key = $irc->is_channel_mode_set($chan, 'k') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | ? $irc->channel_key($chan) | ||||||||||||||
| 27 | : '' | ||||||||||||||
| 28 | ; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 0 | $self->{Channels}->{$lchan} = $key; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | } | ||||||||||||||
| 33 | else { | ||||||||||||||
| 34 | 1 | $self->{Channels} = {}; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | elsif (ref $self->{Channels} eq 'ARRAY') { | ||||||||||||||
| 38 | 2 | my %channels; | |||||||||||||
| 39 | 2 | $channels{lc_irc($_, $irc->isupport('MAPPING'))} = undef for @{ $self->{Channels} }; | |||||||||||||
| 40 | 2 | $self->{Channels} = \%channels; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 6 | $self->{tried_keys} = { }; | |||||||||||||
| 44 | 6 | ▶ | $self->{Rejoin_delay} = 5 if !defined $self->{Rejoin_delay}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 6 | ▶ | $self->{NickServ_delay} = 5 if !defined $self->{NickServ_delay}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 6 | $irc->plugin_register($self, 'SERVER', qw(001 474 isupport chan_mode join kick part identified)); | |||||||||||||
| 47 | 6 | $irc->plugin_register($self, 'USER', qw(join)); | |||||||||||||
| 48 | 6 | return 1; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 6 | ▶ | sub PCI_unregister { | ||||||||||||
|
Subroutine
PCI_unregister: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 52 | 6 | return 1; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 5 | ▶ | sub S_001 { | ||||||||||||
|
Subroutine
S_001: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 56 | 5 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 57 | 5 | delete $self->{alarm_ids}; | |||||||||||||
| 58 | 5 | return PCI_EAT_NONE; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | # we join channels after S_isupport in case the server supports | ||||||||||||||
| 62 | # CAPAB IDENTIFY-MSG, so pocoirc can turn it on before we join channels | ||||||||||||||
| 63 | 5 | ▶ | sub S_isupport { | ||||||||||||
|
Subroutine
S_isupport: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||
| 64 | 5 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 5 | ▶ | if (!grep { $_->isa('POE::Component::IRC::Plugin::NickServID') } values %{ $irc->plugin_list() }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | # we don't have to wait for NickServ, so let's join | ||||||||||||||
| 68 | 5 | ▶ | while (my ($chan, $key) = each %{ $self->{Channels} }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 69 | 5 | ▶ | $irc->yield(join => $chan => (defined $key ? $key : ())); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | else { | ||||||||||||||
| 73 | 0 | ▶ | while (my ($chan, $key) = each %{ $self->{Channels} }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 0 | ▶ | push @{ $self->{alarm_ids} }, $irc->delay( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | [join => $chan => (defined $key ? $key : ())], | ||||||||||||||
| 76 | $self->{NickServ_delay}, | ||||||||||||||
| 77 | 0 | ); | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | 5 | return PCI_EAT_NONE; | |||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | ▶ | sub S_identified { | ||||||||||||
|
Subroutine
S_identified: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 84 | 0 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 0 | ▶ | if ($self->{alarm_ids}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | $irc->delay_remove($_) for @{ $self->{alarm_ids} }; | |||||||||||||
| 88 | 0 | delete $self->{alarm_ids}; | |||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | ▶ | while (my ($chan, $key) = each %{ $self->{Channels} }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 0 | ▶ | $irc->yield(join => $chan => (defined $key ? $key : ())); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | } | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | 0 | return PCI_EAT_NONE; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | # ERR_BANNEDFROMCHAN | ||||||||||||||
| 98 | 4 | ▶ | sub S_474 { | ||||||||||||
|
Subroutine
S_474: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||
| 99 | 4 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 100 | 4 | my $chan = ${ $_[2] }->[0]; | |||||||||||||
| 101 | 4 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 102 | 4 | ▶ | return PCI_EAT_NONE if !$self->{Retry_when_banned}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | |||||||||||||||
| 104 | 4 | my $key = $self->{Channels}{$lchan}; | |||||||||||||
| 105 | 4 | ▶ | $key = $self->{tried_keys}{$lchan} if defined $self->{tried_keys}{$lchan}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 4 | ▶ | $irc->delay([join => $chan => (defined $key ? $key : ())], $self->{Retry_when_banned}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | 4 | return PCI_EAT_NONE; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 3 | ▶ | sub S_chan_mode { | ||||||||||||
|
Subroutine
S_chan_mode: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 111 | 3 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 112 | 3 | pop @_; | |||||||||||||
| 113 | 3 | my $chan = ${ $_[1] }; | |||||||||||||
| 114 | 3 | my $mode = ${ $_[2] }; | |||||||||||||
| 115 | 3 | ▶ | my $arg = defined $_[3] ? ${ $_[3] } : ''; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 3 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 117 | |||||||||||||||
| 118 | 3 | ▶ | $self->{Channels}->{$lchan} = $arg if $mode eq '+k'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 3 | ▶ | $self->{Channels}->{$lchan} = '' if $mode eq '-k'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 3 | return PCI_EAT_NONE; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 11 | ▶ | sub S_join { | ||||||||||||
|
Subroutine
S_join: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||
| 124 | 11 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 125 | 11 | my $joiner = parse_user(${ $_[0] }); | |||||||||||||
| 126 | 11 | my $chan = ${ $_[1] }; | |||||||||||||
| 127 | 11 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 128 | |||||||||||||||
| 129 | 11 | ▶ | return PCI_EAT_NONE if $joiner ne $irc->nick_name(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 11 | delete $self->{alarm_ids}; | |||||||||||||
| 131 | |||||||||||||||
| 132 | 11 | ▶ | if (defined $self->{tried_keys}{$lchan}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 133 | 11 | $self->{Channels}->{$lchan} = $self->{tried_keys}{$lchan}; | |||||||||||||
| 134 | 11 | delete $self->{tried_keys}{$lchan}; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | else { | ||||||||||||||
| 137 | 0 | $self->{Channels}->{$lchan} = ''; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 11 | return PCI_EAT_NONE; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 4 | ▶ | sub S_kick { | ||||||||||||
|
Subroutine
S_kick: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||
| 144 | 4 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 145 | 4 | my $chan = ${ $_[1] }; | |||||||||||||
| 146 | 4 | my $victim = ${ $_[2] }; | |||||||||||||
| 147 | 4 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 148 | |||||||||||||||
| 149 | 4 | ▶ | if ($victim eq $irc->nick_name()) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 4 | ▶ | if ($self->{RejoinOnKick}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | $irc->delay([ | ||||||||||||||
| 152 | 'join', | ||||||||||||||
| 153 | $chan, | ||||||||||||||
| 154 | (defined $self->{Channels}->{$lchan} ? $self->{Channels}->{$lchan} : ()) | ||||||||||||||
| 155 | 4 | ▶ | ], $self->{Rejoin_delay}); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | } | ||||||||||||||
| 157 | 4 | delete $self->{Channels}->{$lchan}; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | 4 | return PCI_EAT_NONE; | |||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 0 | ▶ | sub S_part { | ||||||||||||
|
Subroutine
S_part: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 163 | 0 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 164 | 0 | my $parter = parse_user(${ $_[0] }); | |||||||||||||
| 165 | 0 | my $chan = ${ $_[1] }; | |||||||||||||
| 166 | 0 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 167 | |||||||||||||||
| 168 | 0 | ▶ | delete $self->{Channels}->{$lchan} if $parter eq $irc->nick_name(); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 0 | return PCI_EAT_NONE; | |||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 15 | ▶ | sub U_join { | ||||||||||||
|
Subroutine
U_join: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||
| 173 | 15 | my ($self, $irc) = splice @_, 0, 2; | |||||||||||||
| 174 | 15 | my (undef, $chan, $key) = split /\s/, ${ $_[0] }, 3; | |||||||||||||
| 175 | 15 | my $lchan = lc_irc($chan, $irc->isupport('MAPPING')); | |||||||||||||
| 176 | |||||||||||||||
| 177 | 15 | ▶ | $self->{tried_keys}->{$lchan} = $key if defined $key; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 178 | 15 | return PCI_EAT_NONE; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 9 | 1; | |||||||||||||
| 182 | |||||||||||||||
| 183 | =encoding utf8 | ||||||||||||||
| 184 | |||||||||||||||
| 185 | =head1 NAME | ||||||||||||||
| 186 | |||||||||||||||
| 187 | POE::Component::IRC::Plugin::AutoJoin - A PoCo-IRC plugin which | ||||||||||||||
| 188 | keeps you on your favorite channels | ||||||||||||||
| 189 | |||||||||||||||
| 190 | =head1 SYNOPSIS | ||||||||||||||
| 191 | |||||||||||||||
| 192 | use POE qw(Component::IRC::State Component::IRC::Plugin::AutoJoin); | ||||||||||||||
| 193 | |||||||||||||||
| 194 | my $nickname = 'Chatter'; | ||||||||||||||
| 195 | my $server = 'irc.blahblahblah.irc'; | ||||||||||||||
| 196 | |||||||||||||||
| 197 | my %channels = ( | ||||||||||||||
| 198 | '#Blah' => '', | ||||||||||||||
| 199 | '#Secret' => 'secret_password', | ||||||||||||||
| 200 | '#Foo' => '', | ||||||||||||||
| 201 | ); | ||||||||||||||
| 202 | |||||||||||||||
| 203 | POE::Session->create( | ||||||||||||||
| 204 | package_states => [ | ||||||||||||||
| 205 | main => [ qw(_start irc_join) ], | ||||||||||||||
| 206 | ], | ||||||||||||||
| 207 | ); | ||||||||||||||
| 208 | |||||||||||||||
| 209 | $poe_kernel->run(); | ||||||||||||||
| 210 | |||||||||||||||
| 211 | sub _start { | ||||||||||||||
| 212 | my $irc = POE::Component::IRC::State->spawn( | ||||||||||||||
| 213 | Nick => $nickname, | ||||||||||||||
| 214 | Server => $server, | ||||||||||||||
| 215 | ) or die "Oh noooo! $!"; | ||||||||||||||
| 216 | |||||||||||||||
| 217 | $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new( Channels => \%channels )); | ||||||||||||||
| 218 | $irc->yield(register => qw(join); | ||||||||||||||
| 219 | $irc->yield(connect => { } ); | ||||||||||||||
| 220 | } | ||||||||||||||
| 221 | |||||||||||||||
| 222 | sub irc_join { | ||||||||||||||
| 223 | my $chan = @_[ARG1]; | ||||||||||||||
| 224 | $irc->yield(privmsg => $chan => "hi $channel!"); | ||||||||||||||
| 225 | } | ||||||||||||||
| 226 | |||||||||||||||
| 227 | |||||||||||||||
| 228 | =head1 DESCRIPTION | ||||||||||||||
| 229 | |||||||||||||||
| 230 | POE::Component::IRC::Plugin::AutoJoin is a L<POE::Component::IRC|POE::Component::IRC> | ||||||||||||||
| 231 | plugin. If you get disconnected, the plugin will join all the channels you were | ||||||||||||||
| 232 | on the next time it gets connected to the IRC server. It can also rejoin a | ||||||||||||||
| 233 | channel if the IRC component gets kicked from it. It keeps track of channel | ||||||||||||||
| 234 | keys so it will be able to rejoin keyed channels in case of reconnects/kicks. | ||||||||||||||
| 235 | |||||||||||||||
| 236 | If a L<POE::Component::IRC::Plugin::NickServID|POE::Component::IRC::Plugin::NickServID> | ||||||||||||||
| 237 | plugin has been added to the IRC component, then AutoJoin will wait for a | ||||||||||||||
| 238 | reply from NickServ before joining channels on connect. | ||||||||||||||
| 239 | |||||||||||||||
| 240 | This plugin requires the IRC component to be | ||||||||||||||
| 241 | L<POE::Component::IRC::State|POE::Component::IRC::State> or a subclass thereof. | ||||||||||||||
| 242 | |||||||||||||||
| 243 | =head1 METHODS | ||||||||||||||
| 244 | |||||||||||||||
| 245 | =head2 C<new> | ||||||||||||||
| 246 | |||||||||||||||
| 247 | Takes the following optional arguments: | ||||||||||||||
| 248 | |||||||||||||||
| 249 | B<'Channels'>, either an array reference of channel names, or a hash reference | ||||||||||||||
| 250 | keyed on channel name, containing the password for each channel. By default it | ||||||||||||||
| 251 | uses the channels the component is already on if you are using | ||||||||||||||
| 252 | L<POE::Component::IRC::State|POE::Component::IRC::State>. | ||||||||||||||
| 253 | |||||||||||||||
| 254 | B<'RejoinOnKick'>, set this to 1 if you want the plugin to try to rejoin a | ||||||||||||||
| 255 | channel (once) if you get kicked from it. Default is 0. | ||||||||||||||
| 256 | |||||||||||||||
| 257 | B<'Rejoin_delay'>, the time, in seconds, to wait before rejoining a channel | ||||||||||||||
| 258 | after being kicked (if B<'RejoinOnKick'> is on). Default is 5. | ||||||||||||||
| 259 | |||||||||||||||
| 260 | B<'Retry_when_banned'>, if you can't join a channel due to a ban, set this | ||||||||||||||
| 261 | to the number of seconds to wait between retries. Default is 0 (disabled). | ||||||||||||||
| 262 | |||||||||||||||
| 263 | B<'NickServ_delay'>, how long (in seconds) to wait for a reply from NickServ | ||||||||||||||
| 264 | before joining channels. Default is 5. | ||||||||||||||
| 265 | |||||||||||||||
| 266 | Returns a plugin object suitable for feeding to | ||||||||||||||
| 267 | L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method. | ||||||||||||||
| 268 | |||||||||||||||
| 269 | =head1 AUTHOR | ||||||||||||||
| 270 | |||||||||||||||
| 271 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||||||||||||||
| 272 | |||||||||||||||
| 273 | =cut | ||||||||||||||