From: Matt S Trout Date: Wed, 16 Feb 2011 15:02:00 +0000 (+0000) Subject: /stop command X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3b5393442399a78f1fddb61c492eaa523c67e8fc;p=scpubgit%2FClifton.git /stop command --- diff --git a/lib/App/Clifton/Chain.pm b/lib/App/Clifton/Chain.pm index 3641613..81ccfd1 100644 --- a/lib/App/Clifton/Chain.pm +++ b/lib/App/Clifton/Chain.pm @@ -12,17 +12,37 @@ has irc_tower => (is => 'ro', required => 1, weak_ref => 1); sub handle_xmpp_message { my ($self, $msg) = @_; - $self->irc_tower->send_irc_message({ - to => $self->irc_channel, - text => $msg->body - }); + if (my ($cmd, $args) = $msg->body =~ m{^/(\w+)\s*(.*)$}) { + if (my $meth = $self->can("_handle_command_${cmd}")) { + $self->$meth($msg, $args); + } else { + $self->_send_jabber("Unknown command ${cmd}"); + } + } else { + $self->irc_tower->send_irc_message({ + to => $self->irc_channel, + text => $msg->body + }); + } +} + +sub _handle_command_stop { + my ($self) = @_; + $self->irc_tower->stop_chain($self); + $self->jabber_tower->chain_set->remove($self); + $self->_send_jabber('Stopped.'); } sub handle_irc_message { my ($self, $message, $hints) = @_; + $self->_send_jabber(join(': ', $hints->{prefix_name}, $hints->{text})); +} + +sub _send_jabber { + my ($self, $text) = @_; $self->jabber_tower->send_xmpp_message({ to => $self->jabber_user, - body => join(': ', $hints->{prefix_name}, $hints->{text}) + body => $text, }); } diff --git a/lib/App/Clifton/Set.pm b/lib/App/Clifton/Set.pm index 2bbf860..a6e613c 100644 --- a/lib/App/Clifton/Set.pm +++ b/lib/App/Clifton/Set.pm @@ -18,6 +18,11 @@ sub get { $self->_set->{$self->_spec_to_key($spec)}; } +sub remove { + my ($self, $spec) = @_; + delete $self->_set->{$self->_spec_to_key($spec)}; +} + sub flatten { my ($self) = @_; my $set = $self->_set; diff --git a/lib/App/Clifton/Tower/IRC.pm b/lib/App/Clifton/Tower/IRC.pm index 5dffc0e..982d94f 100644 --- a/lib/App/Clifton/Tower/IRC.pm +++ b/lib/App/Clifton/Tower/IRC.pm @@ -47,6 +47,13 @@ sub start_chain { }); } +sub stop_chain { + my ($self, $chain) = @_; + $self->irc_client->send_message('PART', undef, $chain->irc_channel); + delete $self->chains->{$chain->irc_channel}; + $self->remove_child($chain); +} + sub send_irc_message { my ($self, $args) = @_; $self->irc_client->send_message('PRIVMSG', undef, @{$args}{qw(to text)}); diff --git a/lib/App/Clifton/Tower/Jabber.pm b/lib/App/Clifton/Tower/Jabber.pm index 1798f46..e57e962 100644 --- a/lib/App/Clifton/Tower/Jabber.pm +++ b/lib/App/Clifton/Tower/Jabber.pm @@ -93,6 +93,7 @@ sub handle_message { sub send_xmpp_message { my ($self, $args) = @_; + s/&/&/g, s/"/"/g, s//>/g for $args->{body}; $self->xmpp_client->compose(%$args)->send; }