[804] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r805 - in trunk/owl/perl/modules/IRC/lib/BarnOwl: Message Module Module/IRC

daemon@ATHENA.MIT.EDU (geofft@MIT.EDU)
Thu Oct 29 18:09:54 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
To: dirty-owl-hackers@mit.edu
From: geofft@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Tue,  8 Jan 2008 23:48:57 -0500 (EST)

Author: geofft
Date: 2008-01-08 23:24:09 -0500 (Tue, 08 Jan 2008)
New Revision: 805

Modified:
   trunk/owl/perl/modules/IRC/lib/BarnOwl/Message/IRC.pm
   trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
   trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
Log:
Added support for channels in IRC. After you :irc-connect to a server, you can
:irc-join #channel, and then :irc-msg either a user or a #channel. Replies,
isprivate, and context should work vaguely as expected.



Modified: trunk/owl/perl/modules/IRC/lib/BarnOwl/Message/IRC.pm
===================================================================
--- trunk/owl/perl/modules/IRC/lib/BarnOwl/Message/IRC.pm	2008-01-08 05:31:31 UTC (rev 804)
+++ trunk/owl/perl/modules/IRC/lib/BarnOwl/Message/IRC.pm	2008-01-09 04:24:09 UTC (rev 805)
@@ -44,7 +44,8 @@
 sub network {shift->{network}}
 
 # display
-sub context {shift->{channel};}
+sub context {shift->{network};}
+sub subcontext {shift->{recipient};}
 
 sub long_sender {shift->{from} || ""};
 

Modified: trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
===================================================================
--- trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm	2008-01-08 05:31:31 UTC (rev 804)
+++ trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm	2008-01-09 04:24:09 UTC (rev 805)
@@ -14,8 +14,9 @@
 
 =cut
 
-use base qw(Net::IRC::Connection Class::Accessor);
+use base qw(Net::IRC::Connection Class::Accessor Exporter);
 __PACKAGE__->mk_accessors(qw(alias channels));
+our @EXPORT_OK = qw(&is_private);
 
 use BarnOwl;
 
@@ -29,9 +30,10 @@
     $self->channels([]);
     bless($self, $class);
 
-    $self->add_global_handler(endofmotd => sub { goto &on_connect });
-    $self->add_global_handler(msg => sub { goto &on_msg });
-    $self->add_global_handler(notice => sub { goto &on_msg });
+    $self->add_global_handler(376 => sub { goto &on_connect });
+    $self->add_global_handler(['msg', 'notice', 'public', 'caction'],
+            sub { goto &on_msg });
+    $self->add_global_handler(cping => sub { goto &on_ping });
     $self->add_default_handler(sub { goto &on_event; });
 
     return $self;
@@ -48,25 +50,43 @@
 
 sub on_msg {
     my ($self, $evt) = @_;
-    my $replycmd = "irc-msg " . $evt->nick;
+    my ($recipient) = $evt->to;
+    my $body = strip_irc_formatting([$evt->args]->[0]);
+    $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction';
     my $msg = BarnOwl::Message->new(
         type        => 'IRC',
         direction   => 'in',
         server      => $self->server,
         network     => $self->alias,
-        recipient   => $self->nick,
-        body        => strip_irc_formatting([$evt->args]->[0]),
+        recipient   => $recipient,
+        body        => $body,
         sender      => $evt->nick,
         hostname    => $evt->host,
         from        => $evt->from,
-        notice      => $evt->type eq 'notice' ? 'true' : '',
-        isprivate   => 'true',
-        replycmd    => $replycmd,
-        replysendercmd => $replycmd
+        $evt->type eq 'notice' ?
+          (notice     => 'true') : (),
+        is_private($recipient) ?
+          (isprivate  => 'true') : (),
+        replycmd    => 'irc-msg ' .
+            (is_private($recipient) ? $evt->nick : $recipient),
+        replysendercmd => 'irc-msg ' . $evt->nick,
        );
     BarnOwl::queue_message($msg);
 }
 
+sub on_ping {
+    my ($self, $evt) = @_;
+    $self->ctcp_reply($evt->nick, join (' ', ($evt->args)));
+}
+
+sub on_event {
+    my ($self, $evt) = @_;
+    BarnOwl::admin_message("IRC",
+            "Unhandled IRC event of type " . $evt->type . ":\n"
+            . strip_irc_formatting(join("\n", $evt->args)))
+        if BarnOwl::getvar('irc:spew') eq 'on';
+}
+
 ################################################################################
 ########################### Utilities/Helpers ##################################
 ################################################################################
@@ -82,5 +102,10 @@
     return $out;
 }
 
+# Determines if the given message recipient is a username, as opposed to
+# a channel that starts with # or &.
+sub is_private {
+    return shift !~ /^[\#\&]/;
+}
 
 1;

Modified: trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
===================================================================
--- trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	2008-01-08 05:31:31 UTC (rev 804)
+++ trunk/owl/perl/modules/IRC/lib/BarnOwl/Module/IRC.pm	2008-01-09 04:24:09 UTC (rev 805)
@@ -5,11 +5,11 @@
 
 =head1 NAME
 
-BarnOwl::Module::Jabber
+BarnOwl::Module::IRC
 
 =head1 DESCRIPTION
 
-This module implements Jabber support for barnowl.
+This module implements IRC support for barnowl.
 
 =cut
 
@@ -21,7 +21,7 @@
 use Net::IRC;
 use Getopt::Long;
 
-our $VERSION = 0.01;
+our $VERSION = 0.02;
 
 our $irc;
 
@@ -29,9 +29,10 @@
 our %ircnets;
 
 sub startup {
-    BarnOwl::new_variable_string(ircnick => {default => $ENV{USER}});
-    BarnOwl::new_variable_string(ircuser => {default => $ENV{USER}});
-    BarnOwl::new_variable_string(ircname => {default => ""});
+    BarnOwl::new_variable_string('irc:nick', {default => $ENV{USER}});
+    BarnOwl::new_variable_string('irc:user', {default => $ENV{USER}});
+    BarnOwl::new_variable_string('irc:name', {default => ""});
+    BarnOwl::new_variable_bool('irc:spew', {default => 0});
     register_commands();
     register_handlers();
     BarnOwl::filter('irc type ^IRC$');
@@ -39,7 +40,7 @@
 
 sub shutdown {
     for my $conn (values %ircnets) {
-        $conn->disconnect;
+        $conn->disconnect();
     }
 }
 
@@ -62,6 +63,7 @@
     BarnOwl::new_command('irc-connect' => \&cmd_connect);
     BarnOwl::new_command('irc-disconnect' => \&cmd_disconnect);
     BarnOwl::new_command('irc-msg'     => \&cmd_msg);
+    BarnOwl::new_command('irc-join' => \&cmd_join);
 }
 
 $BarnOwl::Hooks::startup->add(\&startup);
@@ -75,9 +77,9 @@
 sub cmd_connect {
     my $cmd = shift;
 
-    my $nick = BarnOwl::getvar('ircnick');
-    my $username = BarnOwl::getvar('ircuser');
-    my $ircname = BarnOwl::getvar('ircname');
+    my $nick = BarnOwl::getvar('irc:nick');
+    my $username = BarnOwl::getvar('irc:user');
+    my $ircname = BarnOwl::getvar('irc:name');
     my $host;
     my $port;
     my $alias;
@@ -89,7 +91,9 @@
         GetOptions(
             "alias=s"    => \$alias,
             "ssl"        => \$ssl,
-            "password=s" => \$password);
+            "password=s" => \$password,
+            "port=i"     => \$port,
+        );
         $host = shift @ARGV or die("Usage: $cmd HOST\n");
         if(!$alias) {
             $alias = $1 if $host =~ /^(?:irc[.])?(\w+)[.]\w+$/;
@@ -125,6 +129,7 @@
     my $cmd = shift;
     my $conn = get_connection(\@_);
     my $to = shift or die("Usage: $cmd NICK\n");
+    # handle multiple recipients?
     if(@_) {
         process_msg($conn, $to, join(" ", @_));
     } else {
@@ -147,13 +152,20 @@
         recipient   => $to,
         body        => $body,
         sender      => $conn->nick,
-        isprivate   => 'true',
+        is_private($to) ?
+          (isprivate  => 'true') : (),
         replycmd    => "irc-msg $to",
-        replysendercmd => "irc-msg " . $conn->nick
+        replysendercmd => "irc-msg $to"
        );
     BarnOwl::queue_message($msg);
 }
 
+sub cmd_join {
+    my $cmd = shift;
+    my $conn = get_connection(\@_);
+    my $chan = shift or die("Usage: $cmd channel\n");
+    $conn->join($chan);
+}
 
 ################################################################################
 ########################### Utilities/Helpers ##################################
@@ -176,8 +188,15 @@
 }
 
 sub get_connection_by_alias {
-    die("No such ircnet: $alias\n") unless exists $ircnets{$key};
+    my $key = shift;
+    die("No such ircnet: $key\n") unless exists $ircnets{$key};
     return $ircnets{$key};
 }
 
+# Determines if the given message recipient is a username, as opposed to
+# a channel that starts with # or &.
+sub is_private {
+    return shift !~ /^[\#\&]/;
+}
+
 1;


home help back first fref pref prev next nref lref last post