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 44c8d0d..5d3bb14 100644 (file)
@@ -27,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 {
@@ -37,7 +41,6 @@ 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;
 }
@@ -49,9 +52,19 @@ sub handle_message {
 
   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/\/.*//;
 
@@ -92,6 +105,8 @@ 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;
 }