| 1 | package Helm::Log::Channel::irc; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Moose; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use DateTime; | ||
| 7 | use AnyEvent; | ||
| 8 | use IO::Pipe; | ||
| 9 | |||
| 10 | BEGIN { | ||
| 11 | eval { require AnyEvent::IRC::Client }; | ||
| 12 | die "Could not load AnyEvent::IRC::Client. It must be installed to use Helm's irc logging" | ||
| 13 | if $@; | ||
| 14 | } | ||
| 15 | |||
| 16 | extends 'Helm::Log::Channel'; | ||
| 17 | has irc_pipe => (is => 'ro', writer => '_irc_pipe'); | ||
| 18 | has pipes => (is => 'ro', writer => '_pipes', isa => 'HashRef'); | ||
| 19 | has is_parallel => (is => 'rw', isa => 'Bool', default => 0); | ||
| 20 | has irc_pause => (is => 'ro', writer => '_irc_pause', isa => 'Int', default => 0); | ||
| 21 | has prefix => (is => 'rw', isa => 'Str', default => ''); | ||
| 22 | |||
| 23 | my $DISCONNECT = 'Disconnecting'; | ||
| 24 | |||
| 25 | # first parse the IRC URI into some parts that we can use to create an IRC connection. | ||
| 26 | # Then fork off an IRC worker process to go into an event loop that will read input | ||
| 27 | # from the main process via a pipe and then output that to the IRC server. We need | ||
| 28 | # to do it in an event loop because it needs to also respond asynchronously to the | ||
| 29 | # IRC server for pings and such. | ||
| 30 | sub initialize { | ||
| 31 | my ($self, $helm) = @_; | ||
| 32 | my $options = $helm->extra_options; | ||
| 33 | my $pause = $options->{'irc-pause'} || $options->{'irc_pause'}; | ||
| 34 | $self->_irc_pause($pause) if $pause; | ||
| 35 | |||
| 36 | my %irc_info; | ||
| 37 | |||
| 38 | # file the file and open it for appending | ||
| 39 | my $uri = $self->uri; | ||
| 40 | if ($uri->authority =~ /@/) { | ||
| 41 | my ($nick, $host) = split(/@/, $uri->authority); | ||
| 42 | $irc_info{nick} = $nick; | ||
| 43 | $irc_info{server} = $host; | ||
| 44 | } else { | ||
| 45 | $irc_info{nick} = 'helm'; | ||
| 46 | $irc_info{server} = $uri->authority; | ||
| 47 | } | ||
| 48 | $helm->die("No IRC server given in URI $uri") unless $irc_info{server}; | ||
| 49 | |||
| 50 | # get the channel | ||
| 51 | my $channel = $uri->path; | ||
| 52 | $helm->die("No IRC channel given in URI $uri") unless $channel; | ||
| 53 | $channel =~ s/^\///; # remove leading slash | ||
| 54 | $channel = "#$channel" unless $channel =~ /^#/; | ||
| 55 | $irc_info{channel} = $channel; | ||
| 56 | |||
| 57 | # do we need a password | ||
| 58 | my $query = $uri->query; | ||
| 59 | if ($query && $query =~ /(?:^|&|;)(pass|pw|password|passw|passwd)=(.*)(?:$|&|;)/) { | ||
| 60 | $irc_info{password} = $1; | ||
| 61 | } | ||
| 62 | |||
| 63 | # do we have a port? | ||
| 64 | if ($irc_info{server} =~ /:(\d+)$/) { | ||
| 65 | $irc_info{port} = $1; | ||
| 66 | $irc_info{server} =~ s/:(\d+)$//; | ||
| 67 | } else { | ||
| 68 | $irc_info{port} = 6667; | ||
| 69 | } | ||
| 70 | |||
| 71 | # setup a pipe for communicating | ||
| 72 | my $irc_pipe = IO::Pipe->new(); | ||
| 73 | |||
| 74 | # fork off a child process | ||
| 75 | my $pid = fork(); | ||
| 76 | $helm->die("Couldn't fork IRC bot process") if !defined $pid; | ||
| 77 | if ($pid) { | ||
| 78 | # parent here | ||
| 79 | $irc_pipe->writer; | ||
| 80 | $irc_pipe->autoflush(1); | ||
| 81 | $self->_irc_pipe($irc_pipe); | ||
| 82 | Helm->debug("Parent IRC pipe set up"); | ||
| 83 | } else { | ||
| 84 | Helm->debug("Child IRC worker process"); | ||
| 85 | # child here | ||
| 86 | $irc_pipe->reader; | ||
| 87 | $irc_pipe->autoflush(1); | ||
| 88 | Helm->debug("Child IRC pipe set up"); | ||
| 89 | $self->_irc_events($irc_pipe, %irc_info); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub finalize { | ||
| 94 | my ($self, $helm) = @_; | ||
| 95 | |||
| 96 | Helm->debug("IRC channel finalized: Nothing to do"); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub start_server { | ||
| 100 | my ($self, $server) = @_; | ||
| 101 | $self->SUPER::start_server($server); | ||
| 102 | $self->_say("BEGIN Helm task \"" . $self->task . "\" on $server"); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub end_server { | ||
| 106 | my ($self, $server) = @_; | ||
| 107 | $self->SUPER::end_server($server); | ||
| 108 | $self->_say("END Helm task \"" . $self->task . "\" on $server"); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub debug { | ||
| 112 | my ($self, $msg) = @_; | ||
| 113 | $self->_say("[debug] $msg"); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub info { | ||
| 117 | my ($self, $msg) = @_; | ||
| 118 | $self->_say("$msg"); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub warn { | ||
| 122 | my ($self, $msg) = @_; | ||
| 123 | $self->_say("[warn] $msg"); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub error { | ||
| 127 | my ($self, $msg) = @_; | ||
| 128 | $self->_say("[error] $msg"); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _say { | ||
| 132 | my ($self, $msg) = @_; | ||
| 133 | my $prefix = $self->prefix; | ||
| 134 | Helm->debug("Sending message to IO worker: $prefix$msg"); | ||
| 135 | $self->irc_pipe->print("MSG: $prefix$msg\n") or CORE::die("Could not print message to IO Worker: $!"); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _irc_events { | ||
| 139 | my ($self, $irc_pipe, %args) = @_; | ||
| 140 | my $irc = AnyEvent::IRC::Client->new(); | ||
| 141 | my $done = AnyEvent->condvar; | ||
| 142 | my $io_watcher; | ||
| 143 | |||
| 144 | $irc->reg_cb( | ||
| 145 | join => sub { | ||
| 146 | my ($irc, $nick, $channel, $is_myself) = @_; | ||
| 147 | Helm->debug("IRC worker joined channel $channel"); | ||
| 148 | # send the initial message | ||
| 149 | if ($is_myself && $channel eq $args{channel}) { | ||
| 150 | $io_watcher = AnyEvent->io( | ||
| 151 | fh => $irc_pipe, | ||
| 152 | poll => 'r', | ||
| 153 | cb => sub { | ||
| 154 | my $msg = <$irc_pipe>; | ||
| 155 | if(!$msg) { | ||
| 156 | Helm->debug("IRC worker ran out of pipe"); | ||
| 157 | $irc->send_msg(PRIVMSG => $channel, $DISCONNECT); | ||
| 158 | undef $io_watcher; | ||
| 159 | } else { | ||
| 160 | chomp($msg); | ||
| 161 | if ($msg =~ /^MSG: (.*)/) { | ||
| 162 | my $content = $1; | ||
| 163 | chomp($content); | ||
| 164 | if( my $secs = $self->irc_pause ) { | ||
| 165 | Helm->debug("IRC worker sleeping for $secs seconds"); | ||
| 166 | sleep($secs); | ||
| 167 | } | ||
| 168 | Helm->debug("IRC worker sending message to IRC channel: $content"); | ||
| 169 | $irc->send_msg(PRIVMSG => $channel, $content); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | ); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | ); | ||
| 177 | |||
| 178 | # we aren't done until the server acknowledges the send disconnect message | ||
| 179 | $irc->reg_cb( | ||
| 180 | sent => sub { | ||
| 181 | my ($irc, $junk, $type, $channel, $msg) = @_; | ||
| 182 | if( $type eq 'PRIVMSG' && $msg eq $DISCONNECT ) { | ||
| 183 | Helm->debug("IRC channel received DISCONNECT message"); | ||
| 184 | $done->send(); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | ); | ||
| 188 | |||
| 189 | Helm->debug("IRC worker connecting to server $args{server}"); | ||
| 190 | $irc->connect($args{server}, $args{port}, {nick => $args{nick}}); | ||
| 191 | Helm->debug("IRC worker trying to join channel $args{channel}"); | ||
| 192 | $irc->send_srv(JOIN => ($args{channel})); | ||
| 193 | Helm->debug("IRC worker waiting for work"); | ||
| 194 | $done->recv; | ||
| 195 | Helm->debug("IRC worker done with work, disconnecting"); | ||
| 196 | $irc->disconnect(); | ||
| 197 | exit(0); | ||
| 198 | } | ||
| 199 | |||
| 200 | # we already have an IRC bot forked off which has a pipe to our main process for | ||
| 201 | # communication. But if we then share that pipe in all our children we'll end up | ||
| 202 | # with garbled messages. So we need to fork off another worker process which has | ||
| 203 | # multiple pipes, one for each possible server that we'll be executing tasks on. | ||
| 204 | # This extra IO worker process will multi-plex the output coming from those pipes | ||
| 205 | # into something reasonable for the IRC bot to handle. | ||
| 206 | sub parallelize { | ||
| 207 | my ($self, $helm) = @_; | ||
| 208 | $self->is_parallel(1); | ||
| 209 | |||
| 210 | # if we're going to do parallel stuff, then create a pipe for each server now | ||
| 211 | # that we can use to communicate with the child processes later | ||
| 212 | my %pipes = map { $_->name => IO::Pipe->new } (@{$helm->servers}); | ||
| 213 | $self->_pipes(\%pipes); | ||
| 214 | |||
| 215 | # fork off an IO worker process | ||
| 216 | my $pid = fork(); | ||
| 217 | $helm->die("Couldn't fork IRC IO worker process") if !defined $pid; | ||
| 218 | if (!$pid) { | ||
| 219 | Helm->debug("IO worker forked"); | ||
| 220 | # child here | ||
| 221 | my %pipe_cleaners; | ||
| 222 | my $all_clean = AnyEvent->condvar; | ||
| 223 | foreach my $server (keys %pipes) { | ||
| 224 | my $pipe = $pipes{$server}; | ||
| 225 | $pipe->reader; | ||
| 226 | |||
| 227 | # create an IO watcher for this pipe | ||
| 228 | Helm->debug("IO worker setting up AnyEvent reads on pipe for $server"); | ||
| 229 | $pipe_cleaners{$server} = AnyEvent->io( | ||
| 230 | fh => $pipe, | ||
| 231 | poll => 'r', | ||
| 232 | cb => sub { | ||
| 233 | my $msg = <$pipe>; | ||
| 234 | if ($msg) { | ||
| 235 | Helm->debug("Printing message to IRC PIPE: $msg"); | ||
| 236 | $self->irc_pipe->print($msg) or CORE::die "Could not print message to IRC PIPE: $!"; | ||
| 237 | } else { | ||
| 238 | delete $pipe_cleaners{$server}; | ||
| 239 | Helm->debug("Removing IO pipe for $server"); | ||
| 240 | # tell the main program we're done if this is the last broom | ||
| 241 | $all_clean->send unless %pipe_cleaners; | ||
| 242 | } | ||
| 243 | }, | ||
| 244 | ); | ||
| 245 | } | ||
| 246 | |||
| 247 | Helm->debug("Waiting for IO to send to IRC worker process"); | ||
| 248 | $all_clean->recv; | ||
| 249 | Helm->debug("All done with IO to send to IRC worker process"); | ||
| 250 | exit(0); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | # we've been forked, and if it's a child we want to initialize the pipe | ||
| 255 | # for this worker child's server | ||
| 256 | sub forked { | ||
| 257 | my ($self, $type) = @_; | ||
| 258 | |||
| 259 | if ($type eq 'child') { | ||
| 260 | Helm->debug("Forked worker process for " . $self->current_server->name); | ||
| 261 | my $pipes = $self->pipes; | ||
| 262 | my $pipe = $pipes->{$self->current_server->name}; | ||
| 263 | $pipe->writer(); | ||
| 264 | $pipe->autoflush(1); | ||
| 265 | $self->_irc_pipe($pipe); | ||
| 266 | $self->prefix('[' . $self->current_server->name . '] '); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | __PACKAGE__->meta->make_immutable; | ||
| 271 | |||
| 272 | 1; |