| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Bot::Net::Mixin::Server::IRC; | ||
| 5 | |||
| 6 | use Bot::Net::Mixin; | ||
| 7 | |||
| 8 | use POE qw/ Component::Server::IRC /; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Bot::Net::Mixin::Server::IRC - mixin class for building IRC servers | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | # Build a basic, no-frills IRC server | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | package MyBotNet::Server::Main; | ||
| 20 | |||
| 21 | use Bot::Net::Server; | ||
| 22 | use Bot::Net::Mixin::Server::IRC; | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This is the mixin-class for L<Bot::Net> IRC servers. By using this class you create an IRC daemon: | ||
| 29 | |||
| 30 | use Bot::Net::Server; # define common Bot::Net server features | ||
| 31 | use Bot::Net::Mixin::Server::IRC; # we're an IRC server | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | =head2 setup | ||
| 36 | |||
| 37 | Setup a new IRC server. | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub setup { | ||
| 42 | my $self = shift; | ||
| 43 | my $brain = shift; | ||
| 44 | |||
| 45 | $brain->remember( [ 'ircd' ] => POE::Component::Server::IRC->spawn( | ||
| 46 | antiflood => 0, | ||
| 47 | config => $brain->recall( [ config => 'ircd_config' ] ), | ||
| 48 | alias => 'ircd', | ||
| 49 | )); | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 default_configuration PACKAGE | ||
| 53 | |||
| 54 | Returns a base configuration for an IRC server daemon. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub default_configuration { | ||
| 59 | my $class = shift; | ||
| 60 | my $package = shift; | ||
| 61 | |||
| 62 | return { | ||
| 63 | ircd_config => { | ||
| 64 | servername => lc Bot::Net->short_name_for_server($package) . '.irc', | ||
| 65 | nicklen => 15, | ||
| 66 | network => Bot::Net->config->net('ApplicationName'), | ||
| 67 | }, | ||
| 68 | listeners => [ | ||
| 69 | { port => 6667 }, | ||
| 70 | ], | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head1 POE STATES | ||
| 75 | |||
| 76 | =head2 on _start | ||
| 77 | |||
| 78 | At startup, this hanlder loads the information stored in the configuration file and configures the IRC daemon. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | on _start => run { | ||
| 83 | # Start receiving server events | ||
| 84 | post ircd => 'register'; | ||
| 85 | |||
| 86 | yield 'install_auth_configuration'; | ||
| 87 | yield 'install_operator_configuration'; | ||
| 88 | yield 'install_listener_configuration'; | ||
| 89 | yield 'install_peer_configuration'; | ||
| 90 | }; | ||
| 91 | |||
| 92 | =head2 on install_auth_configuration | ||
| 93 | |||
| 94 | Called by L</on _start> to configure the authentication masks. This reads the "masks" section of the configuration and makes it so that only the users found in that configuration may successfully login. | ||
| 95 | |||
| 96 | See L<POE::Component::Server::IRC/add_auth>. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | on install_auth_configuration => run { | ||
| 101 | my $log = recall 'log'; | ||
| 102 | my $ircd = recall 'ircd'; | ||
| 103 | |||
| 104 | # Installing masks | ||
| 105 | $log->info("Installing the masks..."); | ||
| 106 | my $masks = recall [ config => 'masks' ]; | ||
| 107 | for my $mask (@$masks) { | ||
| 108 | $ircd->add_auth( %$mask ); | ||
| 109 | } | ||
| 110 | }; | ||
| 111 | |||
| 112 | =head2 on install_operator_configuration | ||
| 113 | |||
| 114 | This is called by L</on _start> and reads the "operators" setting from the configuration file. That configuration will be used to grant logging users server op status. | ||
| 115 | |||
| 116 | See L<POE::Component::Server::IRC/add_operator>. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | on install_operator_configuration => run { | ||
| 121 | my $log = recall 'log'; | ||
| 122 | my $ircd = recall 'ircd'; | ||
| 123 | |||
| 124 | # Installing operators | ||
| 125 | $log->info("Installing the operators..."); | ||
| 126 | my $operators = recall [ config => 'operators' ]; | ||
| 127 | for my $operator (@$operators) { | ||
| 128 | $ircd->add_operator( %$operator ); | ||
| 129 | } | ||
| 130 | }; | ||
| 131 | |||
| 132 | =head2 on install_listener_configuration | ||
| 133 | |||
| 134 | This event handler initializes a listening port for each entry in the "listeners" setting of the configuration file. This is called by L</on _start>. For each listener initialized, it also reports a READY status message to the logs. | ||
| 135 | |||
| 136 | See L<POE::Component::Server::IRC/add_listener>. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | on install_listener_configuration => run { | ||
| 141 | my $log = recall 'log'; | ||
| 142 | my $ircd = recall 'ircd'; | ||
| 143 | |||
| 144 | # Start a listener on the 'standard' IRC port. | ||
| 145 | $log->info("Installing the listeners..."); | ||
| 146 | my $listeners = recall [ config => 'listeners' ]; | ||
| 147 | for my $listener (@$listeners) { | ||
| 148 | $ircd->add_listener( %$listener ); | ||
| 149 | $log->info("SERVER READY : port $listener->{port}"); | ||
| 150 | } | ||
| 151 | }; | ||
| 152 | |||
| 153 | =head2 on install_peer_configuration | ||
| 154 | |||
| 155 | Run by L</on _start>, this handler intiates peer connections between IRC servers to create the IRC network. This will either notify the server that it should be anticipating an incoming connection from a peer or cause it to initiate a connection. | ||
| 156 | |||
| 157 | See L<POE::Component::Server::IRC/add_peer>. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | on install_peer_configuration => run { | ||
| 162 | my $log = recall 'log'; | ||
| 163 | my $ircd = recall 'ircd'; | ||
| 164 | |||
| 165 | # Tell the server to wait for a peer connection or initiate one | ||
| 166 | $log->info("Installing the peers..."); | ||
| 167 | my $peers = recall [ config => 'peers' ]; | ||
| 168 | for my $peer (@$peers) { | ||
| 169 | $ircd->add_peer( %$peer ); | ||
| 170 | $log->info( | ||
| 171 | ($peer->{type} eq 'r' ? 'Connecting to' : 'Listening for') | ||
| 172 | .' server peer named '.$peer->{name}); | ||
| 173 | } | ||
| 174 | }; | ||
| 175 | |||
| 176 | =head2 on server quit | ||
| 177 | |||
| 178 | This causes the IRC daemon to close all connections and stop listening. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | on server_quit => run { | ||
| 183 | recall('log')->warn("Shutting down the server."); | ||
| 184 | recall('ircd')->shutdown; | ||
| 185 | |||
| 186 | post ircd => unregister => 'all'; | ||
| 187 | forget 'ircd'; | ||
| 188 | }; | ||
| 189 | |||
| 190 | =head1 SEE ALSO | ||
| 191 | |||
| 192 | L<Bot::Net::Server> | ||
| 193 | |||
| 194 | =head1 AUTHORS | ||
| 195 | |||
| 196 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||
| 197 | |||
| 198 | =head1 COPYRIGHT AND LICENSE | ||
| 199 | |||
| 200 | Copyright 2007 Boomer Consulting, Inc. All Rights Reserved. | ||
| 201 | |||
| 202 | This program is free software and may be modified and distributed under the same terms as Perl itself. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | 1; |