Index: perl/modules/jabber.pl
===================================================================
--- perl/modules/jabber.pl	(revision ea67541aa97219aec31886e456cba74267cd3cb1)
+++ perl/modules/jabber.pl	(revision 8f2ce3a2d784853e0969b8ebddb70dda860bdae3)
@@ -39,14 +39,12 @@
     my $jidStr = shift;
 
-    my %args = ();
-    if(owl::getvar('debug') eq 'on') {
-        $args{debuglevel} = 1;
-        $args{debugfile} = 'jabber.log';
-    }
-    my $client = Net::Jabber::Client->new(%args);
-
-    $self->{Client}->{$jidStr} = $client;
-    $self->{Roster}->{$jidStr} = $client->Roster();
-    return $client;
+    $self->{Client}->{$jidStr} = 
+      Net::Jabber::Client->new(
+          debuglevel => owl::getvar('debug') eq 'on' ? 1 : 0,
+          debugfile => 'jabber.log'
+      );
+    my $refConn = \$self->{Client}->{$jidStr};
+    $self->{Roster}->{$jidStr} = $$refConn->Roster();
+    return $refConn;
 }
 
@@ -54,11 +52,14 @@
     my $self = shift;
     my $jidStr = shift;
-    return 0 unless exists $self->{Client}->{$jidStr};
-    
-    $self->{Client}->{$jidStr}->Disconnect();
-    delete $self->{Roster}->{$jidStr};
-    delete $self->{Client}->{$jidStr};
-    
-    return 1;
+    my $ret = 0;
+    foreach my $j ( keys %{ $self->{Client} } ) {
+        if ($j eq $jidStr) {
+            $self->{Client}->{$j}->Disconnect();
+            delete $self->{Roster}->{$j};
+            delete $self->{Client}->{$j};
+            $ret = 1;
+        }
+    }
+    return $ret;
 }
 
@@ -76,5 +77,8 @@
     my $self = shift;
     my $jidStr = shift;
-    return exists $self->{Client}->{$jidStr};
+    foreach my $j ( keys %{ $self->{Client} } ) {
+        return 1 if ($j eq $jidStr);
+    }
+    return 0;
 }
 
@@ -88,33 +92,45 @@
 }
 
-sub getConnectionFromSid {
+sub getConnRefFromSid {
     my $self = shift;
     my $sid = shift;
-    foreach my $c (values %{ $self->{Client} }) {
-        return $c if $c->{SESSION}->{id} eq $sid;
+    foreach my $j ( keys %{ $self->{Client} } ) {
+        if ($self->{Client}->{$j}->{SESSION}->{id} eq $sid) {
+            return \$self->{Client}->{$j};
+        }
     }
     return undef;
 }
 
-sub getConnectionFromJidStr {
+sub getConnRefFromJidStr {
     my $self = shift;
     my $jidStr = shift;
-    return $self->{Client}->{$jidStr};
-}
-
-sub getRosterFromSid {
+    foreach my $j ( keys %{ $self->{Client} } ) {
+        if ($jidStr eq $j) {
+            return \$self->{Client}->{$j};
+        }
+    }
+    return undef;
+}
+
+sub getRosterRefFromSid {
     my $self = shift;
     my $sid = shift;
-    foreach my $j ( $self->getJids ) {
-        return $self->{Roster}->{$j}
-          if $self->{Client}->{$j}->{SESSION}->{id} eq $sid;
+    foreach my $j ( keys %{ $self->{Client} } ) {
+        if ($self->{Client}->{$j}->{SESSION}->{id} eq $sid) {
+            return \$self->{Roster}->{$j};
+        }
     }
     return undef;
 }
 
-sub getRosterFromJidStr {
+sub getRosterRefFromJidStr {
     my $self = shift;
     my $jidStr = shift;
-    return $self->{Roster}->{$jidStr};
+    foreach my $j ( keys %{ $self->{Client} } ) {
+        if ($jidStr eq $j) {
+            return \$self->{Roster}->{$j};
+        }
+    }
     return undef;
 }
@@ -145,5 +161,5 @@
 
     foreach my $jid ( $conn->getJids() ) {
-        my $client = $conn->getConnectionFromJidStr($jid);
+        my $client = $conn->getConnRefFromJidStr($jid);
 
         my $status = $client->Process(0);
@@ -193,6 +209,6 @@
     return "" unless $jid;
     my $blist = "";
-    my $roster = $conn->getRosterFromJidStr($jid);
-    if ($roster) {
+    my $roster = $conn->getRosterRefFromJidStr($jid);
+    if ($$roster) {
         $blist .= "\n" . boldify("Jabber Roster for $jid\n");
 
@@ -335,5 +351,5 @@
             normal    => sub { owl_jabber::process_incoming_normal_message(@_) }
         );
-        $client->SetPresenceCallBacks(
+        $$client->SetPresenceCallBacks(
 #            available    => sub { owl_jabber::process_presence_available(@_) },
 #            unavailable  => sub { owl_jabber::process_presence_available(@_) },
@@ -363,6 +379,6 @@
         }
             else {
-                $conn->getRosterFromJidStr($jidStr)->fetch();
-                $client->PresenceSend( priority => 1 );
+                ${ $conn->getRosterRefFromJidStr($jidStr) }->fetch();
+                $$client->PresenceSend( priority => 1 );
                 queue_admin_msg("Connected to jabber as $jidStr");
             }
@@ -554,5 +570,5 @@
     }
 
-    $conn->getConnectionFromJidStr($jid)->Send($presence);
+    ${ $conn->getConnRefFromJidStr($jid) }->Send($presence);
 }
 
@@ -563,5 +579,6 @@
     die("Usage: jmuc part {muc} [-a account]") unless $muc;
 
-    $conn->getConnectionFromJidStr($jid)->PresenceSend( to => $muc, type => 'unavailable' );
+    ${ $conn->getConnRefFromJidStr($jid) }
+      ->PresenceSend( to => $muc, type => 'unavailable' );
     queue_admin_msg("$jid has left $muc.");
 }
@@ -581,5 +598,5 @@
     $x->AddInvite();
     $x->GetInvite()->SetTo($invite_jid);
-    $conn->getConnectionFromJidStr($jid)->Send($message);
+    ${ $conn->getConnRefFromJidStr($jid) }->Send($message);
     queue_admin_msg("$jid has invited $invite_jid to $muc.");
 }
@@ -596,5 +613,5 @@
     $x->SetType('submit');
 
-    $conn->getConnectionFromJidStr($jid)->Send($iq);
+    ${ $conn->getConnRefFromJidStr($jid) }->Send($iq);
     queue_admin_msg("Accepted default instant configuration for $muc");
 }
@@ -660,5 +677,5 @@
     my $baseJid = baseJID($jid);
 
-    my $roster = $conn->getRosterFromJidStr($jid);
+    my $roster = $conn->getRosterRefFromJidStr($jid);
 
     # Adding lots of users with the same name is a bad idea.
@@ -669,8 +686,8 @@
 
     foreach my $to (@ARGV) {
-        jroster_add($jid, $name, \@groups, $purgeGroups, ($to)) unless ($roster->exists($to));
+        jroster_add($jid, $name, \@groups, $purgeGroups, ($to)) unless ($$roster->exists($to));
 
         $p->SetTo($to);
-        $conn->getConnectionFromJidStr($jid)->Send($p);
+        ${ $conn->getConnRefFromJidStr($jid) }->Send($p);
         queue_admin_msg("You ($baseJid) have requested a subscription to ($to)'s presence.");
     }
@@ -688,5 +705,5 @@
     foreach my $to (@ARGV) {
         $p->SetTo($to);
-        $conn->getConnectionFromJidStr($jid)->Send($p);
+        ${ $conn->getConnRefFromJidStr($jid) }->Send($p);
         queue_admin_msg("You ($baseJid) have unsubscribed from ($to)'s presence.");
     }
@@ -700,5 +717,5 @@
     my $baseJid = baseJID($jid);
 
-    my $roster = $conn->getRosterFromJidStr($jid);
+    my $roster = $conn->getRosterRefFromJidStr($jid);
 
     # Adding lots of users with the same name is a bad idea.
@@ -706,5 +723,5 @@
 
     foreach my $to (@ARGV) {
-        my %jq  = $roster->query($to);
+        my %jq  = $$roster->query($to);
         my $iq = new Net::XMPP::IQ;
         $iq->SetType('set');
@@ -730,5 +747,5 @@
         $item->put_attrib(jid => $to);
         $item->put_attrib(name => $name) if $name;
-        $conn->getConnectionFromJidStr($jid)->Send($iq);
+        ${ $conn->getConnRefFromJidStr($jid) }->Send($iq);
         my $msg = "$baseJid: "
           . ($name ? "$name ($to)" : "($to)")
@@ -754,5 +771,5 @@
     foreach my $to (@ARGV) {
         $item->put_attrib(jid => $to);
-        $conn->getConnectionFromJidStr($jid)->Send($iq);
+        ${ $conn->getConnRefFromJidStr($jid) }->Send($iq);
         queue_admin_msg("You ($baseJid) have removed ($to) from your roster.");
     }
@@ -770,5 +787,5 @@
     foreach my $to (@ARGV) {
         $p->SetTo($to);
-        $conn->getConnectionFromJidStr($jid)->Send($p);
+        ${ $conn->getConnRefFromJidStr($jid) }->Send($p);
         queue_admin_msg("($to) has been subscribed to your ($baseJid) presence.");
     }
@@ -786,5 +803,5 @@
     foreach my $to (@ARGV) {
         $p->SetTo($to);
-        $conn->getConnectionFromJidStr($jid)->Send($p);
+        ${ $conn->getConnRefFromJidStr($jid) }->Send($p);
         queue_admin_msg("($to) has been unsubscribed from your ($baseJid) presence.");
     }
@@ -809,13 +826,15 @@
 
     my $m = j2o( $j, { direction => 'out' } );
-    if ( $vars{jwrite}{type} ne 'groupchat' && owl::getvar('displayoutgoing') eq 'on') {
+    if ( $vars{jwrite}{type} ne 'groupchat' ) {
+
+        #XXX TODO: Check for displayoutgoing.
         owl::queue_message($m);
     }
 
     if ($vars{jwrite}{sid} && $conn->sidExists( $vars{jwrite}{sid} )) {
-        $conn->getConnectionFromSid($vars{jwrite}{sid})->Send($j);
+        ${ $conn->getConnRefFromSid($vars{jwrite}{sid}) }->Send($j);
     }
     else {
-        $conn->getConnectionFromJidStr($vars{jwrite}{from})->Send($j);
+        ${ $conn->getConnRefFromJidStr($vars{jwrite}{from}) }->Send($j);
     }
 
@@ -949,5 +968,5 @@
             $to eq $cJid->GetJID('full')) {
 	    my $reply = $p->Reply(type=>"unsubscribed");
-	    $conn->getConnectionFromJidStr($jid)->Send($reply);
+	    ${ $conn->getConnRefFromJidStr($jid) }->Send($reply);
 	    return;
 	}
