/stop command
[scpubgit/Clifton.git] / lib / App / Clifton / Chain.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,
   });
 }