basically operating chain code
[scpubgit/Clifton.git] / lib / App / Clifton / Tower / Jabber.pm
index 381695f..44c8d0d 100644 (file)
@@ -13,7 +13,9 @@ has xmpp_client => (is => 'lazy');
 sub xmpp_active { shift->xmpp_client->xmpp->is_loggedin }
 
 has $_ => (is => 'ro', required => 1)
-  for qw(bridge_name server jabber_config);
+  for qw(bridge_name jabber_config irc_config);
+
+has server => (is => 'ro', required => 1, weak_ref => 1);
 
 has userconfig_set => (
   is => 'ro', # Moobug init_arg => undef,
@@ -35,10 +37,13 @@ sub _login_xmpp_client {
   my $conf = $self->jabber_config;
   $xmpp->login(
     jid => $conf->user, host => $conf->server, password => $conf->pass,
+    on_message => $self->_replace_weakself('handle_message'),
   );
   $xmpp;
 }
 
+sub BUILD { $_[0]->xmpp_client }
+
 sub handle_message {
   my ($self, $msg) = @_;
 
@@ -68,8 +73,11 @@ sub handle_message {
       $self->start_chain_for($user)->on_finished(
         $self->_capture_weakself(sub {
           my ($self, $result) = @_;
+          if ($result->{chain}) {
+            $self->chain_set->add($result->{chain});
+          }
           $self->send_xmpp_message({
-            to => $from, body => 'Connection result: '.$result
+            to => $from, body => 'Connection result: '.$result->{message}
           });
         })
       );
@@ -87,6 +95,19 @@ sub send_xmpp_message {
   $self->xmpp_client->compose(%$args)->send;
 }
 
+sub start_chain_for {
+  my ($self, $user) = @_;
+  $self->server->start_chain({
+    jabber_tower => $self,
+    jabber_user => $user->name,
+    irc_tower => {
+      server => $self->irc_config->server,
+      irc_nick => $user->irc_nick
+    },
+    irc_channel => $self->irc_config->channel,
+  });
+}
+
 1;
 
 # and later