ircscripts

collection of scripts to enhance irc experience
Log | Files | Refs

notifications.pl (6572B)


      1 # notifications, an Irssi script to notify the user about incoming messages
      2 # Copyright (C) 2012, 2013, 2014, 2019 Jaromir Hradilek
      3 
      4 # This program is free software;  you can redistribute it  and/or modify it
      5 # under the  terms of the  GNU General Public License  as published  by the
      6 # Free Software Foundation; version 3 of the License.
      7 #
      8 # This  program is  distributed  in the  hope that  it will be useful,  but
      9 # WITHOUT ANY WARRANTY;  without even the implied warranty of  MERCHANTABI-
     10 # LITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See  the  GNU General Public
     11 # License for more details.
     12 #
     13 # You should have received a copy of the  GNU General Public License  along
     14 # with this program. If not, see <http://www.gnu.org/licenses/>.
     15 
     16 use strict;
     17 use warnings;
     18 use Encode;
     19 use Net::DBus;
     20 use Irssi;
     21 
     22 # The character encoding of the Irssi client:
     23 use constant ENCODING => "UTF-8";
     24 
     25 # General script information:
     26 our $VERSION  = '0.9.3';
     27 our %IRSSI    = (
     28   name        => 'notifications',
     29   description => 'Notify the user about incoming messages.',
     30   authors     => 'Jaromir Hradilek',
     31   contact     => 'jhradilek@gmail.com',
     32   url         => 'https://github.com/jhradilek/irssi-notifications',
     33   license     => 'GNU General Public License, version 3',
     34   changed     => '2019-06-18',
     35 );
     36 
     37 # Get the session message bus handle:
     38 our $bus = Net::DBus->session;
     39 
     40 # Get the notifications service handle:
     41 our $service = $bus->get_service('org.freedesktop.Notifications');
     42 
     43 # Get the relevant object handle:
     44 our $object = $service->get_object('/org/freedesktop/Notifications');
     45 
     46 # Display a GTK notification:
     47 sub display_notification {
     48   my ($summary, $body) = @_;
     49 
     50   # Convert the strings to Perl's internal representation:
     51   $summary = decode(ENCODING, $summary);
     52   $body = decode(ENCODING, $body);
     53 
     54   # Display the notification:
     55   $object->Notify('irssi-notifications', 0, 'im-message-new',
     56                   $summary, $body, [], {}, 0);
     57 }
     58 
     59 # Handle incoming public messages:
     60 sub message_public {
     61   my ($server, $message, $nick, $address, $target) = @_;
     62 
     63   # Check whether to notify the user about public messages:
     64   return unless (Irssi::settings_get_bool('notifications_public_messages'));
     65 
     66   # Check whether to notify the user about messages in the active window:
     67   unless (Irssi::settings_get_bool('notifications_active_window')) {
     68     # Get the name of the active window:
     69     my $window = Irssi::active_win()->{active}->{name} || '';
     70 
     71     # Ignore messages in the active window:
     72     return if ($window eq $target);
     73   }
     74 
     75   # Get the user's nick name:
     76   my $user = $server->{nick};
     77 
     78   # Check whether to notify the user about indirect messages:
     79   unless (Irssi::settings_get_bool('notifications_indirect_messages')) {
     80     # Ignore messages that are not explicitly addressed to the user:
     81     return if ($message !~ /^$user[\s:,]/);
     82   }
     83   else {
     84     # Ignore messages that do not mention the user:
     85     return if ($message !~ /\b$user\b/);
     86   }
     87 
     88   # Get the server's tag:
     89   my $tag = $server->{tag};
     90 
     91   # Prepare the message body:
     92   (my $body = $message) =~ s/^$user[\s:,]\s*//;
     93 
     94   # Notify the user about the incoming public message:
     95   display_notification("Message from $nick/$tag on $target:", $body);
     96 }
     97 
     98 # Handle incoming private messages:
     99 sub message_private {
    100   my ($server, $message, $nick, $address) = @_;
    101 
    102   # Check whether to notify the user about public messages:
    103   return unless (Irssi::settings_get_bool('notifications_public_messages'));
    104 
    105   # Check whether to notify the user about messages in the active window:
    106   unless (Irssi::settings_get_bool('notifications_active_window')) {
    107     # Get the name of the active window:
    108     my $window = Irssi::active_win()->{active}->{name} || '';
    109 
    110     # Ignore messages in the active window:
    111     return if ($window eq $nick);
    112   }
    113 
    114   # Get the server's tag:
    115   my $tag = $server->{tag};
    116 
    117   # Notify the user about the incoming private message:
    118   display_notification("Message from $nick/$tag:", $message);
    119 }
    120 
    121 # Handle incoming DCC requests:
    122 sub dcc_request {
    123   my ($dcc, $sendaddr) = @_;
    124 
    125   # Check whether to notify the user about DCC requests:
    126   return unless (Irssi::settings_get_bool('notifications_dcc_messages'));
    127 
    128   # Check whether to notify the user about messages in the active window:
    129   unless (Irssi::settings_get_bool('notifications_active_window')) {
    130     # Get the name of the active window:
    131     my $window = Irssi::active_win()->{active}->{name} || '';
    132 
    133     # Ignore messages in the active window:
    134     return unless ($window);
    135   }
    136 
    137   # Get the request type:
    138   my $type = $dcc->{type};
    139 
    140   # Get the sender's nick:
    141   my $nick = $dcc->{nick};
    142 
    143   # Get the server's tag:
    144   my $tag  = $dcc->{server}->{tag};
    145 
    146   # Check the request type:
    147   if ($type eq 'GET') {
    148     # Get the file name and size:
    149     my $name = $dcc->{arg};
    150     my $size = $dcc->{size};
    151 
    152     # Notify the user about the incoming SEND request:
    153     display_notification("$nick/$tag offers a file:", "$name ($size B)");
    154   }
    155   elsif ($type eq 'CHAT') {
    156     # Notify the user about the incoming CHAT request:
    157     display_notification("$nick/$tag offers a DCC chat.", "");
    158   }
    159 }
    160 
    161 # Handle incoming DCC CHAT messages:
    162 sub dcc_chat_message {
    163   my ($dcc, $message) = @_;
    164 
    165   # Check whether to notify the user about DCC requests:
    166   return unless (Irssi::settings_get_bool('notifications_dcc_messages'));
    167 
    168   # Get the sender's nick:
    169   my $nick = $dcc->{id};
    170 
    171   # Check whether to notify the user about messages in the active window:
    172   unless (Irssi::settings_get_bool('notifications_active_window')) {
    173     # Get the name of the active window:
    174     my $window = Irssi::active_win()->{active}->{name} || '';
    175 
    176     # Ignore messages in the active window:
    177     return if ($window eq "=$nick");
    178   }
    179 
    180   # Get the server's tag:
    181   my $tag  = $dcc->{server}->{tag};
    182 
    183   # Notify the user about the incoming CHAT message:
    184   display_notification("DCC chat message from $nick/$tag:", $message);
    185 }
    186 
    187 # Register configuration options:
    188 Irssi::settings_add_bool('notifications', 'notifications_private_messages', 1);
    189 Irssi::settings_add_bool('notifications', 'notifications_public_messages',  1);
    190 Irssi::settings_add_bool('notifications', 'notifications_indirect_messages',0);
    191 Irssi::settings_add_bool('notifications', 'notifications_active_window', 0);
    192 Irssi::settings_add_bool('notifications', 'notifications_dcc_messages',  1);
    193 
    194 # Register signals:
    195 Irssi::signal_add('message public',   'message_public');
    196 Irssi::signal_add('message private',  'message_private');
    197 Irssi::signal_add('dcc request',      'dcc_request');
    198 Irssi::signal_add('dcc chat message', 'dcc_chat_message');