-
Notifications
You must be signed in to change notification settings - Fork 3
/
notifications.pl
198 lines (155 loc) · 6.42 KB
/
notifications.pl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# notifications, an Irssi script to notify the user about incoming messages
# Copyright (C) 2012, 2013, 2014, 2019 Jaromir Hradilek
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABI-
# LITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use Encode;
use Net::DBus;
use Irssi;
# The character encoding of the Irssi client:
use constant ENCODING => "UTF-8";
# General script information:
our $VERSION = '0.9.3';
our %IRSSI = (
name => 'notifications',
description => 'Notify the user about incoming messages.',
authors => 'Jaromir Hradilek',
contact => 'jhradilek@gmail.com',
url => 'https://github.com/jhradilek/irssi-notifications',
license => 'GNU General Public License, version 3',
changed => '2019-06-18',
);
# Get the session message bus handle:
our $bus = Net::DBus->session;
# Get the notifications service handle:
our $service = $bus->get_service('org.freedesktop.Notifications');
# Get the relevant object handle:
our $object = $service->get_object('/org/freedesktop/Notifications');
# Display a GTK notification:
sub display_notification {
my ($summary, $body) = @_;
# Convert the strings to Perl's internal representation:
$summary = decode(ENCODING, $summary);
$body = decode(ENCODING, $body);
# Display the notification:
$object->Notify('irssi-notifications', 0, 'im-message-new',
$summary, $body, [], {}, 0);
}
# Handle incoming public messages:
sub message_public {
my ($server, $message, $nick, $address, $target) = @_;
# Check whether to notify the user about public messages:
return unless (Irssi::settings_get_bool('notifications_public_messages'));
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('notifications_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return if ($window eq $target);
}
# Get the user's nick name:
my $user = $server->{nick};
# Check whether to notify the user about indirect messages:
unless (Irssi::settings_get_bool('notifications_indirect_messages')) {
# Ignore messages that are not explicitly addressed to the user:
return if ($message !~ /^$user[\s:,]/);
}
else {
# Ignore messages that do not mention the user:
return if ($message !~ /\b$user\b/);
}
# Get the server's tag:
my $tag = $server->{tag};
# Prepare the message body:
(my $body = $message) =~ s/^$user[\s:,]\s*//;
# Notify the user about the incoming public message:
display_notification("Message from $nick/$tag on $target:", $body);
}
# Handle incoming private messages:
sub message_private {
my ($server, $message, $nick, $address) = @_;
# Check whether to notify the user about public messages:
return unless (Irssi::settings_get_bool('notifications_public_messages'));
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('notifications_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return if ($window eq $nick);
}
# Get the server's tag:
my $tag = $server->{tag};
# Notify the user about the incoming private message:
display_notification("Message from $nick/$tag:", $message);
}
# Handle incoming DCC requests:
sub dcc_request {
my ($dcc, $sendaddr) = @_;
# Check whether to notify the user about DCC requests:
return unless (Irssi::settings_get_bool('notifications_dcc_messages'));
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('notifications_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return unless ($window);
}
# Get the request type:
my $type = $dcc->{type};
# Get the sender's nick:
my $nick = $dcc->{nick};
# Get the server's tag:
my $tag = $dcc->{server}->{tag};
# Check the request type:
if ($type eq 'GET') {
# Get the file name and size:
my $name = $dcc->{arg};
my $size = $dcc->{size};
# Notify the user about the incoming SEND request:
display_notification("$nick/$tag offers a file:", "$name ($size B)");
}
elsif ($type eq 'CHAT') {
# Notify the user about the incoming CHAT request:
display_notification("$nick/$tag offers a DCC chat.", "");
}
}
# Handle incoming DCC CHAT messages:
sub dcc_chat_message {
my ($dcc, $message) = @_;
# Check whether to notify the user about DCC requests:
return unless (Irssi::settings_get_bool('notifications_dcc_messages'));
# Get the sender's nick:
my $nick = $dcc->{id};
# Check whether to notify the user about messages in the active window:
unless (Irssi::settings_get_bool('notifications_active_window')) {
# Get the name of the active window:
my $window = Irssi::active_win()->{active}->{name} || '';
# Ignore messages in the active window:
return if ($window eq "=$nick");
}
# Get the server's tag:
my $tag = $dcc->{server}->{tag};
# Notify the user about the incoming CHAT message:
display_notification("DCC chat message from $nick/$tag:", $message);
}
# Register configuration options:
Irssi::settings_add_bool('notifications', 'notifications_private_messages', 1);
Irssi::settings_add_bool('notifications', 'notifications_public_messages', 1);
Irssi::settings_add_bool('notifications', 'notifications_indirect_messages',0);
Irssi::settings_add_bool('notifications', 'notifications_active_window', 0);
Irssi::settings_add_bool('notifications', 'notifications_dcc_messages', 1);
# Register signals:
Irssi::signal_add('message public', 'message_public');
Irssi::signal_add('message private', 'message_private');
Irssi::signal_add('dcc request', 'dcc_request');
Irssi::signal_add('dcc chat message', 'dcc_chat_message');