Index: perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
===================================================================
--- perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	(revision 9620c8d92ecfed30b4b6c4d5d6861179e2ab418d)
+++ perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	(revision 5c6d661cb25bf113e3f1b18aaed92753a61a9f96)
@@ -20,5 +20,5 @@
 use BarnOwl::Module::IRC::Completion;
 
-use Net::IRC;
+use AnyEvent::IRC;
 use Getopt::Long;
 use Encode;
@@ -30,6 +30,4 @@
 # Hash alias -> BarnOwl::Module::IRC::Connection object
 our %ircnets;
-our %channels;
-our %reconnect;
 
 sub startup {
@@ -70,5 +68,4 @@
 
     register_commands();
-    register_handlers();
     BarnOwl::filter(qw{irc type ^IRC$ or ( type ^admin$ and adminheader ^IRC$ )});
 }
@@ -76,5 +73,5 @@
 sub shutdown {
     for my $conn (values %ircnets) {
-        $conn->conn->disconnect();
+        $conn->conn->disconnect('Quitting');
     }
 }
@@ -98,6 +95,5 @@
         $list .= "\n";
 
-        for my $chan (keys %channels) {
-            next unless grep $_ eq $conn, @{$channels{$chan}};
+        for my $chan (keys %{$conn->conn->{channel_list}}) {
             $list .= "  $chan\n";
         }
@@ -105,28 +101,4 @@
 
     return $list;
-}
-
-#sub mainloop_hook {
-#    return unless defined $irc;
-#    eval {
-#        $irc->do_one_loop();
-#    };
-#    return;
-#}
-
-sub OwlProcess {
-    return unless defined $irc;
-    eval {
-        $irc->do_one_loop();
-    };
-    return;
-}
-
-
-sub register_handlers {
-    if(!$irc) {
-        $irc = Net::IRC->new;
-        $irc->timeout(0);
-    }
 }
 
@@ -406,21 +378,13 @@
     }
 
-    my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias,
-        Nick      => $nick,
-        Server    => $host,
-        Port      => $port,
-        Username  => $username,
-        Ircname   => $ircname,
-        Port      => $port,
-        Password  => $password,
-        SSL       => $ssl
-       );
-
-    if ($conn->conn->connected) {
-        $conn->connected("Connected to $alias as $nick");
-    } else {
-        die("IRC::Connection->connect failed: $!");
-    }
-
+    my $conn = BarnOwl::Module::IRC::Connection->new($alias, $host, $port, {
+        nick      => $nick,
+        user      => $username,
+        real      => $ircname,
+        password  => $password,
+        SSL       => $ssl,
+        timeout   => sub {0}
+       });
+    $ircnets{$alias} = $conn;
     return;
 }
@@ -429,10 +393,12 @@
     my $cmd = shift;
     my $conn = shift;
-    if ($conn->conn->connected) {
-        $conn->conn->disconnect;
-    } elsif ($reconnect{$conn->alias}) {
+    if ($conn->conn->{socket}) {
+        $conn->did_quit(1);
+        $conn->conn->disconnect("Goodbye!");
+    } elsif ($conn->{reconnect_timer}) {
         BarnOwl::admin_message('IRC',
                                "[" . $conn->alias . "] Reconnect cancelled");
         $conn->cancel_reconnect;
+        delete $ircnets{$conn->alias};
     }
 }
@@ -463,8 +429,8 @@
     for my $body (@msgs) {
 	if ($body =~ /^\/me (.*)/) {
-	    $conn->conn->me($to, Encode::encode('utf-8', $1));
+	    $conn->me($to, Encode::encode('utf-8', $1));
 	    $body = '* '.$conn->nick.' '.$1;
 	} else {
-	    $conn->conn->privmsg($to, Encode::encode('utf-8', $body));
+	    $conn->conn->send_msg('privmsg', $to, Encode::encode('utf-8', $body));
 	}
 	my $msg = BarnOwl::Message->new(
@@ -491,5 +457,5 @@
     my $target = shift;
     $target ||= shift;
-    $conn->conn->mode($target, @_);
+    $conn->conn->send_msg(mode => $target, @_);
     return;
 }
@@ -499,7 +465,5 @@
     my $conn = shift;
     my $chan = shift or die("Usage: $cmd channel\n");
-    $channels{$chan} ||= [];
-    push @{$channels{$chan}}, $conn;
-    $conn->conn->join($chan, @_);
+    $conn->conn->send_msg(join => $chan, @_);
     return;
 }
@@ -509,6 +473,5 @@
     my $conn = shift;
     my $chan = shift;
-    $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}];
-    $conn->conn->part($chan);
+    $conn->conn->send_msg(part => $chan);
     return;
 }
@@ -518,5 +481,5 @@
     my $conn = shift;
     my $nick = shift or die("Usage: $cmd <new nick>\n");
-    $conn->conn->nick($nick);
+    $conn->conn->send_msg(nick => $nick);
     return;
 }
@@ -527,5 +490,5 @@
     my $chan = shift;
     $conn->names_tmp([]);
-    $conn->conn->names($chan);
+    $conn->conn->send_msg(names => $chan);
     return;
 }
@@ -535,5 +498,5 @@
     my $conn = shift;
     my $who = shift || die("Usage: $cmd <user>\n");
-    $conn->conn->whois($who);
+    $conn->conn->send_msg(whois => $who);
     return;
 }
@@ -542,5 +505,5 @@
     my $cmd = shift;
     my $conn = shift;
-    $conn->conn->motd;
+    $conn->conn->send_msg('motd');
     return;
 }
@@ -560,6 +523,5 @@
     my $conn = shift;
     my $who = shift || die("Usage: $cmd <user>\n");
-    BarnOwl::error("WHO $cmd $conn $who");
-    $conn->conn->who($who);
+    $conn->conn->send_msg(who => $who);
     return;
 }
@@ -569,5 +531,5 @@
     my $conn = shift;
     my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n");
-    $conn->conn->stats($type, @_);
+    $conn->conn->send_msg(stats => $type, @_);
     return;
 }
@@ -577,5 +539,5 @@
     my $conn = shift;
     my $chan = shift;
-    $conn->conn->topic($chan, @_ ? join(" ", @_) : undef);
+    $conn->conn->send_msg(topic => $chan, @_ ? join(" ", @_) : undef);
     return;
 }
@@ -584,5 +546,5 @@
     my $cmd = shift;
     my $conn = shift;
-    $conn->conn->sl(join(" ", @_));
+    $conn->conn->send_msg(@_);
     return;
 }
@@ -591,4 +553,15 @@
 ########################### Utilities/Helpers ##################################
 ################################################################################
+
+sub find_channel {
+    my $channel = shift;
+    my @found;
+    for my $conn (values %ircnets) {
+        if($conn->conn->{channel_list}{lc $channel}) {
+            push @found, $conn;
+        }
+    }
+    return $found[0] if(scalar @found == 1);
+}
 
 sub mk_irc_command {
@@ -614,7 +587,7 @@
             $channel = $ARGV[0];
             if(defined($channel) && $channel =~ /^#/) {
-                if($channels{$channel} && @{$channels{$channel}} == 1) {
+                if(my $c = find_channel($channel)) {
                     shift @ARGV;
-                    $conn = $channels{$channel}[0] unless $conn;
+                    $conn ||= $c;
                 }
             } elsif ($m && $m->type eq 'IRC' && !$m->is_private) {
@@ -654,7 +627,10 @@
     my $allow_disconnected = shift;
 
-    return $ircnets{$key} if exists $ircnets{$key};
-    return $reconnect{$key} if $allow_disconnected && exists $reconnect{$key};
-    die("No such ircnet: $key\n")
+    my $conn = $ircnets{$key};
+    die("No such ircnet: $key\n") unless $conn;
+    if ($conn->conn->{registered} || $allow_disconnected) {
+        return $conn;
+    }
+    die("[@{[$conn->alias]}] Not currently connected.");
 }
 
