Initial commit master
authorRalf Ertzinger <sun@fred.camperquake.de>
Sun, 22 Nov 2009 13:38:07 +0000 (14:38 +0100)
committerRalf Ertzinger <sun@fred.camperquake.de>
Sun, 22 Nov 2009 13:38:07 +0000 (14:38 +0100)
BettIrssi.pm [new file with mode: 0644]
BettIrssiEvent.pm [new file with mode: 0644]

diff --git a/BettIrssi.pm b/BettIrssi.pm
new file mode 100644 (file)
index 0000000..989e6d2
--- /dev/null
@@ -0,0 +1,100 @@
+package BettIrssi;
+
+@ISA = qw(Exporter);
+@EXPORT_OK = qw(_bcb _bcs);
+
+use strict;
+
+delete($INC{'BettIrssiEvent.pm'});
+use BettIrssiEvent;
+use Irssi;
+require Exporter;
+
+sub _cmd_callback {
+    my $callback = shift;
+    my ($cmdline, $server, $witem) = @_;
+    my $data;
+
+    $data = BettIrssiEvent->new(
+        SIGNAL => undef,
+        SERVER => $server,
+        EVWITEM => $witem,
+        MESSAGE => $cmdline,
+        SENDER => undef,
+    );
+
+    $callback->($data);
+}
+
+sub _sig_callback {
+    my $callback = shift;
+    my $signal = shift;
+    my ($server, $msg, $nick, $userhost, $channel) = @_;
+    my $data;
+    my $evwitem;
+
+    # Change all attributes into Irssi classes
+    $evwitem = $server->window_item_find($channel);
+
+    if (defined($nick)) {
+        foreach ($evwitem->nicks()) {
+            if ($_->{'nick'} eq $nick) {
+                $nick = $_;
+                last;
+            }
+        }
+    } else {
+        $nick = $channel->{'ownnick'};
+    }
+
+    $data = BettIrssiEvent->new(
+        SIGNAL => $signal,
+        SERVER => $server,
+        EVWITEM => $evwitem,
+        MESSAGE => $msg,
+        SENDER => $nick,
+    );
+
+    $callback->($data);
+}
+
+sub _bcb {
+    my $cmd = shift;
+    my $callback = shift;
+
+    return ($cmd, sub { _cmd_callback($callback, @_) });
+}
+
+sub _bcs {
+    my $signal = lc(shift);
+    my $callback = shift;
+
+    if ($signal eq 'message public') {
+        # messages to a channel by someone not us.
+        return ($signal, sub { _sig_callback($callback, $signal, @_) });
+
+    } elsif ($signal eq 'message own_public') {
+        # messages to a channel by us.
+        return ($signal, sub { _sig_callback($callback, $signal, $_[0], $_[1], undef, undef, $_[2]) });
+
+    } elsif ($signal eq 'message private') {
+        # messages in a query by someone not us
+        return ($signal, sub { _sig_callback($callback, $signal, $_[0], $_[1], $_[2], $_[3], $_[2]) });
+
+    } elsif ($signal eq 'message own_private') {
+        # messages in a query by us
+        return ($signal, sub { _sig_callback($callback, $signal, $_[0], $_[1], undef, undef, $_[2]) });
+
+    } elsif ($signal eq 'message irc action') {
+        return ($signal, sub { _sig_callback($callback, $signal, @_) });
+
+    } elsif ($signal eq 'message irc own_action') {
+        return ($signal, sub { _sig_callback($callback, $signal, $_[0], $_[1], undef, undef, $_[2]) });
+
+    } else {
+        Irssi::print("%mBettIrssi:%n %RUnknown signal%n %y$signal%n%R. Please bug the BettIrssi author about this.");
+        return ($signal, sub { Irssi::print("%mBettIrssi:%n %RSignal not handled") });
+    }
+}
+
+1;
diff --git a/BettIrssiEvent.pm b/BettIrssiEvent.pm
new file mode 100644 (file)
index 0000000..e060791
--- /dev/null
@@ -0,0 +1,95 @@
+package BettIrssiEvent;
+
+use strict;
+use Irssi;
+use Data::Dumper;
+
+sub new {
+    my $class = shift;
+    my $self = {@_};
+
+    return bless($self, $class);
+}
+
+sub message {
+    my $self = shift;
+
+    return $self->{'MESSAGE'};
+}
+
+sub channel {
+    my $self = shift;
+
+    return $self->{'EVWITEM'};
+}
+
+sub sender {
+    my $self = shift;
+
+    return $self->{'SENDER'};
+}
+
+
+sub ewpf {
+    my $self = shift;
+
+    return sub {$self->ewprint(@_)};
+}
+
+sub ewprint {
+    my $self = shift;
+    my $data = shift;
+
+    if (defined($self->{'EVWITEM'})) {
+        $self->{'EVWITEM'}->print($data);
+    } else {
+        # No witem. This probably means that the event fired
+        # in the status window.
+        Irssi::print($data);
+    }
+}
+
+sub awpf {
+    my $self = shift;
+
+    return sub {$self->awprint(@_)};
+}
+
+sub awprint {
+    my $self = shift;
+    my $data = shift;
+    my $w;
+
+    $w = Irssi::active_win();
+
+    if (defined($w)) {
+        foreach ($w->items()) {
+            if ($_->is_active()) {
+                $_->print($data);
+                return;
+            }
+        }
+        #
+        # No active window item found. Write to the window directly.
+        $w->print($data);
+    } else {
+        # No active win. This should not happen, but nonetheless.
+        # Write to the status window.
+        Irssi::print($data);
+    }
+}
+
+sub swpf {
+    my $self = shift;
+
+    return sub { $self->swprint(@_) };
+}
+
+sub swprint {
+    my $self = shift;
+
+    Irssi::print(shift);
+}
+
+
+1;