/stop command
Matt S Trout [Wed, 16 Feb 2011 15:02:00 +0000 (15:02 +0000)]
lib/App/Clifton/Chain.pm
lib/App/Clifton/Set.pm
lib/App/Clifton/Tower/IRC.pm
lib/App/Clifton/Tower/Jabber.pm

index 3641613..81ccfd1 100644 (file)
@@ -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,
   });
 }
 
index 2bbf860..a6e613c 100644 (file)
@@ -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;
index 5dffc0e..982d94f 100644 (file)
@@ -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)});
index 1798f46..e57e962 100644 (file)
@@ -93,6 +93,7 @@ sub handle_message {
 
 sub send_xmpp_message {
   my ($self, $args) = @_;
+  s/&/&amp;/g, s/"/&quot;/g, s/</&lt;/g, s/>/&gt;/g for $args->{body};
   $self->xmpp_client->compose(%$args)->send;
 }