only set full jid when don't have one or it looks like a full one - otherwise things...
[scpubgit/Clifton.git] / lib / App / Clifton / Tower / Jabber.pm
index 381695f..5d3bb14 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,
@@ -25,9 +27,13 @@ has chain_set => (
   default => sub { ChainSet->new }
 );
 
+has full_jid => (is => 'rw');
+
 sub _build_xmpp_client {
   my ($self) = @_;
-  $self->_login_xmpp_client($self->_new_child(XMPP_Client, { }));
+  $self->_login_xmpp_client($self->_new_child(XMPP_Client, {
+    on_message => $self->_replace_weakself('handle_message'),
+  }));
 }
 
 sub _login_xmpp_client {
@@ -39,14 +45,26 @@ sub _login_xmpp_client {
   $xmpp;
 }
 
+sub BUILD { $_[0]->xmpp_client }
+
 sub handle_message {
   my ($self, $msg) = @_;
 
   my $me = $self->jabber_config->user;
 
+  return log_info {
+    "Received error message"
+  } if $msg->type eq 'error';
+
   return log_debug {
     "Received message for ${\$msg->to} instead of $me - ignoring"
-  } unless $msg->to eq $me;
+  } unless $msg->to =~ /^\Q$me/; # may be foo@gmail.com or .../gsklgsh
+
+  if (!$self->full_jid
+      or ($self->full_jid !~ m{/} and $msg->to =~ m{/})
+    ) {
+    $self->full_jid($msg->to); # need foo@gmail.com/skldshgsdg here
+  }
 
   (my $from = $msg->from) =~ s/\/.*//;
 
@@ -68,8 +86,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}
           });
         })
       );
@@ -84,9 +105,24 @@ 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};
+  $args->{from} = $self->full_jid;
   $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