IRC/Plugin.pm

filter: Statementstmt 100% 19 / 19 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 100% 24 / 24 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« IRC/Common.pm ↑ Summary
1 package POE::Component::Server::IRC::Plugin;
2 BEGIN {
3 20 ▶ $POE::Component::Server::IRC::Plugin::AUTHORITY = 'cpan:HINRIK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 20 $POE::Component::Server::IRC::Plugin::VERSION = '1.54';
7 }
8  
9 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 require Exporter;
13 20 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 our @EXPORT_OK = qw(PCSI_EAT_NONE PCSI_EAT_CLIENT PCSI_EAT_PLUGIN PCSI_EAT_ALL);
15 20 our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] );
16  
17 use constant {
18 20 PCSI_EAT_NONE => 1,
19 PCSI_EAT_CLIENT => 2,
20 PCSI_EAT_PLUGIN => 3,
21 PCSI_EAT_ALL => 4,
22 20 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 20 1;
25  
26 =encoding utf8
27  
28 =head1 NAME
29  
30 POE::Component::Server::IRC::Plugin - Provides plugin documentation for
31 POE::Component::Server::IRC.
32  
33 =head1 DESCRIPTION
34  
35 This is the document coders/users should refer to when using/developing
36 plugins for POE::Component::Server::IRC.
37  
38 The plugin system works by letting coders hook into aspects of
39 POE::Component::Server::IRC::Backend. More details are found in the docs
40 for L<Object::Pluggable|Object::Pluggable>.
41  
42 The general architecture of using the plugins should be:
43  
44 # Import the stuff...
45 use POE;
46 use POE::Component::Server::IRC::Backend;
47 use POE::Component::Server::IRC::Plugin::ExamplePlugin;
48  
49 # Create our session here
50 POE::Session->create( ... );
51  
52 # Create the IRC session here
53 my $irc = POE::Component::Server::IRC::Backend->spawn() or die 'Nooo!';
54  
55 # Create the plugin
56 # Of course it could be something like $plugin = MyPlugin->new();
57 my $plugin = POE::Component::Server::IRC::Plugin::ExamplePlugin->new( ... );
58  
59 # Hook it up!
60 $irc->plugin_add( 'ExamplePlugin', $plugin );
61  
62 # OOPS, we lost the plugin object!
63 my $pluginobj = $irc->plugin_get( 'ExamplePlugin' );
64  
65 # We want a list of plugins and objects
66 my $hashref = $irc->plugin_list();
67  
68 # Oh! We want a list of plugin aliases.
69 my @aliases = keys %{ $irc->plugin_list() };
70  
71 # Ah, we want to remove the plugin
72 $plugin = $irc->plugin_del( 'ExamplePlugin' );
73  
74 The plugins themselves will conform to the standard API described here. What
75 they can do is limited only by imagination and the IRC RFC's ;)
76  
77 package POE::Component::Server::IRC::ExamplePlugin;
78  
79 # Import the constants
80 use POE::Component::Server::IRC::Plugin qw( :ALL );
81  
82 # Our constructor
83 sub new {
84 # ...
85 }
86  
87 # Required entry point for POE::Component::Server::IRC::Backend
88 sub PCSI_register {
89 my ($self, $irc) = @_;
90 # Register events we are interested in
91 $irc->plugin_register( $self, 'SERVER', qw(connection) );
92  
93 # Return success
94 return 1;
95 }
96  
97 # Required exit point for PoCo-Server-IRC
98 sub PCSI_unregister {
99 my ($self, $irc) = @_;
100  
101 # PCSIB will automatically unregister events for the plugin
102  
103 # Do some cleanup...
104  
105 # Return success
106 return 1;
107 }
108  
109 # Registered events will be sent to methods starting with IRC_
110 # If the plugin registered for SERVER - irc_355
111 sub IRCD_connection {
112 my ($self, $irc, $line) = @_;
113  
114 # Remember, we receive pointers to scalars, so we can modify them
115 $$line = 'frobnicate!';
116  
117 # Return an exit code
118 return PCSI_EAT_NONE;
119 }
120  
121 # Default handler for events that do not have a corresponding
122 # plugin method defined.
123 sub _default {
124 my ($self, $irc, $event) = splice @_, 0, 3;
125  
126 print "Default called for $event\n";
127  
128 # Return an exit code
129 return PCSI_EAT_NONE;
130 }
131  
132 =head2 Pipeline
133  
134 The plugins are given priority on a first come, first serve basis.
135 Therefore, plugins that were added before others have the first shot at
136 processing events. See
137 L<Object::Pluggable::Pipeline|Object::Pluggable::Pipeline> for details.
138  
139 my $pipeline = $ircd->pipeline();
140  
141 =head1 EVENTS
142  
143 =head2 SERVER hooks
144  
145 Hooks that are targeted toward data received from the server will get the
146 exact same arguments as if it was a normal event, look at the
147 POE::Component::Server::IRC::Backend docs for more information.
148  
149 B<Note:> Server methods are identified in the plugin namespace by the
150 subroutine prefix of IRCD_*. I.e. an ircd_cmd_kick event handler would be:
151  
152 sub IRCD_cmd_kick {}
153  
154 The only difference is instead of getting scalars, the hook will get a
155 reference to the scalar, to allow it to mangle the data. This allows the
156 plugin to modify data *before* they are sent out to registered sessions.
157  
158 They are required to return one of the exit codes so
159 POE::Component::Server::IRC::Backend will know what to do.
160  
161 Names of potential hooks:
162  
163 socketerr
164 connected
165 plugin_del
166 ...
167  
168 Keep in mind that they are always lowercased, check out the
169 POE::Component::Server::IRC documentation.
170  
171 =head2 C<_default>
172  
173 If a plugin doesn't have a specific hook method defined for an event, the
174 component will attempt to call a plugin's C<_default> method. The first
175 parameter after the plugin and irc objects will be the handler name.
176  
177 sub _default {
178 my ($self, $irc, $event) = splice @_, 0, 3;
179 return PCSI_EAT_NONE;
180 }
181  
182 The C<_default> handler is expected to return one of the exit codes so
183 POE::Component::Server::IRC::Backend will know what to do.
184  
185 =head1 EXPORTS
186  
187 The following constants are exported on demand.
188  
189 =head2 C<PCSI_EAT_NONE>
190  
191 This means the event will continue to be processed by remaining plugins and
192 finally, sent to interested sessions that registered for it.
193  
194 =head2 C<PCSI_EAT_CLIENT>
195  
196 This means the event will continue to be processed by remaining plugins but
197 it will not be sent to any sessions that registered for it.
198  
199 =head2 C<PCSI_EAT_PLUGIN>
200  
201 This means the event will not be processed by remaining plugins, it will go
202 straight to interested sessions.
203  
204 =head2 C<PCSI_EAT_ALL>
205  
206 This means the event will be completely discarded, no plugin or session
207 will see it.
208  
209 =head1 SEE ALSO
210  
211 L<Object::Pluggable|Object::Pluggable>
212  
213 =cut
« IRC/Common.pm ↑ Summary