aboutsummaryrefslogtreecommitdiffstats
path: root/bnotify/bnotify.pl
blob: eaff6f87b0361a4469d5a9c4ebcf8e187a883572 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# todo: grap topic changes

use strict;
use vars qw($VERSION %IRSSI);

use Irssi;
$VERSION = '0.0.1';
%IRSSI = (
    authors     => 'richo',
    contact     => 'richo@psych0tik.net',
    name        => 'bnotify',
    description => 'Write notifications based on who\'s talking to you, also handle some window management and tmux alerts',
    url         => 'http://natalya.psych0tik.net/~richo/bnotify',
    license     => 'GNU General Public License',
    changed     => '$Date: 2011-06-21 21:51:30 +1000 (Tue, 21 Jun 2011) $'
);
# Originally based on fnotify.pl 0.0.3 by Thorsten Leemhuis
# fedora@leemhuis.info
# 'http://www.leemhuis.info/files/fnotify/',
#
#--------------------------------------------------------------------
# In parts based on knotify.pl 0.1.1 by Hugo Haas
# http://larve.net/people/hugo/2005/01/knotify.pl
# which is based on osd.pl 0.3.3 by Jeroen Coekaerts, Koenraad Heijlen
# http://www.irssi.org/scripts/scripts/osd.pl
#
# Other parts based on notify.pl from Luke Macken
# http://fedora.feedjack.org/user/918/
#
#--------------------------------------------------------------------

# TODO
# Add settings for which networks to beep on

my @alert_nets = ();
sub bnotify_init {
    Irssi::settings_add_str('bnotify', 'bnotify_alert_nets', '');
    @alert_nets = split(/ /, Irssi::settings_get_str('bnotify_alert_nets'));
}

#--------------------------------------------------------------------
# Private message parsing
#--------------------------------------------------------------------
# TODO
# Test to see if the privmsg went to a status window, and is from bitlbee in
# which case send it to it's own window

sub priv_msg {
    my ($server,$msg,$nick,$address,$target) = @_;
    # Does this expose issues if someone includes regexp chars in their server
    # tag?
    if (grep(/^$server->{tag}$/, @alert_nets)) {
        Irssi::command('beep');
    }
    filewrite($server->{tag}.":".$nick." private");
    #Irssi::settings_set_str('autocreate_query_level', 'DCCMSGS MSGS');
}

#--------------------------------------------------------------------
# Private msg windowing
#--------------------------------------------------------------------

sub priv_msg_winhook {
    my ($server,$msg,$nick,$address,$target) = @_;
    if (grep($server->{tag}, @alert_nets)) {
        Irssi::settings_set_str('autocreate_query_level', 'DCCMSGS MSGS');
    }
}

#--------------------------------------------------------------------
# Printing hilight's
#--------------------------------------------------------------------

sub hilight {
    my ($dest, $text, $stripped) = @_;
    if ($dest->{level} & MSGLEVEL_HILIGHT) {
        filewrite($dest->{server}->{tag}.":".$dest->{target}. " " .$stripped );
    }
}

#--------------------------------------------------------------------
# Handle Arguments
#--------------------------------------------------------------------

sub cmd_add {
    my $net = shift;
    if (not grep($net, @alert_nets)) {
        push @alert_nets, $net;
        Irssi::active_win->print("Added $net to alert networks.");
    } else {
        Irssi::active_win->print("$net already configured to alert.");
    }
}

sub cmd_del {
    my $net = shift;
    my @valid;
    my $idx = 0;
    while ($idx <= $#alert_nets) {
        if (lc($alert_nets[$idx]) eq lc($net)) {
            push @valid, $alert_nets[$idx];
        }
        $idx++;
    }
    if ($#alert_nets != $#valid) {
        Irssi::active_win->print("Removed $net from alert networks.");
    } else {
        Irssi::active_win->print("$net didn't exist in alert networks.");
    }
    @alert_nets = @valid;
}

#--------------------------------------------------------------------
# The actual printing
#--------------------------------------------------------------------

sub filewrite {
    my ($text) = @_;
    open(FILE, '>' .Irssi::get_irssi_dir() . '/fnotify');
    print FILE $text . "\n";
    close (FILE);
}

#--------------------------------------------------------------------
# Irssi::signal_add_last / Irssi::command_bind
#--------------------------------------------------------------------

Irssi::signal_add_first("message private", "priv_msg_winhook");
Irssi::signal_add_last("message private", "priv_msg");
Irssi::signal_add_last("print text", "hilight");

Irssi::command_bind('bnotify add', \&cmd_add);
Irssi::command_bind('bnotify del', \&cmd_del);
bnotify_init();

#- end