Skip to content

Instantly share code, notes, and snippets.

@grawity
Created Jan 23, 2018
Embed
What would you like to do?

wallnotify — 'wall' to libnotify bridge

This script listens for messages sent out by wall, shutdown, or write and displays them as graphical libnotify notifications.

Requirements

The IO::Tty and Net::DBus Perl modules are required. On Debian they're packaged as libio-pty-perl and libnet-dbus-perl. On Arch Linux, the packages are perl-io-tty and perl-net-dbus. They can also be installed from CPAN.

The sessreg tool is required for the script to add itself to utmp. On Debian, it comes as part of x11-xserver-utils package; on Arch Linux, xorg-sessreg.

The user must be allowed to run sessreg through sudo:

ALL		ALL=(root) NOPASSWD: /usr/bin/sessreg

(This sudo thing is a BIG UGLY HACK and could be made unnecessary if there was a better way to update utmp without having full write permissions. Suggestions welcome.)

Installation

git clone git://github.com/grawity/wallnotify.git
cd wallnotify

sudo make install

Usage

The script will run automatically on logon, if you have wallnotify.desktop installed to /etc/xdg/autostart or ~/.config/autostart. This is done by make install.

To run the script manually, use:

./wallnotify &

Known issues

  • On Ubuntu 11.10, sessreg adds two utmp entries, causing the message to appear twice. This doesn't happen with sessreg 1.0.7 on Arch. Not sure why.
MIT Expat License
-----------------
(c) 2011 Mantas Mikulėnas <grawity@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
all:
@echo 'Nothing happens. (Use "make install" to install.)'
install:
install -Dm0755 wallnotify $(DESTDIR)/usr/local/bin/wallnotify
install -Dm0644 wallnotify.desktop $(DESTDIR)/etc/xdg/autostart/wallnotify.desktop
.PHONY: all install
#!/usr/bin/env perl
use strict;
use Sys::Utmp;
use Data::Dumper;
my @types = qw(
unknown runlvl boot newtime oldtime init login user dead acct
);
my $utmp = Sys::Utmp->new;
printf "%-8s|%-4s|%-32s|%-32s|%-8s\n",
"TYPE", "ID", "LINE", "USER", "PID";
while (my $ut = $utmp->getutent) {
printf "%-8s|%-4s|%-32s|%-32s|%-8d\n",
$types[$ut->ut_type],
substr($ut->ut_id, 0, 4),
$ut->ut_line,
$ut->ut_user,
$ut->ut_pid,
;
}
#!/usr/bin/env perl
# wallnotify v1.0 - 'wall' to libnotify bridge
# © 2011 Mantas Mikulėnas <grawity@gmail.com>
# Released under the MIT Expat License
use warnings;
use strict;
use IO::Pty;
use Net::DBus;
my $timeout = 15;
sub getusername {
getlogin
// (getpwuid $<)[0]
// (getpwuid $>)[0]
// die "unable to determine your username";
}
sub utmp_reg {
my $ttyname = shift;
$ttyname =~ s|^/dev/||;
system("sudo",
"sessreg",
"-a",
"-l" => $ttyname,
getusername);
}
sub utmp_unreg {
my $ttyname = shift;
$ttyname =~ s|^/dev/||;
system("sudo",
"sessreg",
"-d",
"-l" => $ttyname,
getusername);
}
sub listen_walls {
my $pty = IO::Pty->new;
warn "Obtained ".$pty->ttyname."\n";
utmp_reg($pty->ttyname);
$SIG{INT} = $SIG{TERM} = sub {
utmp_unreg($pty->ttyname);
exit(0);
};
$pty->set_raw();
$pty->slave->set_raw();
while (my $line = $pty->getline) {
chomp($line);
$line =~ s/[\x07\x0d]//g; # remove bells and CRs
notify($line);
}
}
sub notify {
my $text = shift;
my $summary;
our $last_time //= 0;
our $last_id //= 0;
our $body //= "";
our $dbus //= Net::DBus->session
or die "DBus session bus not accessible\n";
our $notify //= $dbus->get_service("org.freedesktop.Notifications")
->get_object("/org/freedesktop/Notifications");
$summary = "System message";
if (time-$last_time < $timeout) {
$body .= "\n".$text;
} else {
$body = $text;
}
# ignore leading empty lines
return unless length($body);
$last_time = time;
$last_id = $notify->Notify(
"System",
$last_id,
"dialog-information",
$summary,
$body,
[],
{},
$timeout*1000,
);
}
listen_walls;
[Desktop Entry]
Name=wallnotify
Comment='wall' to libnotify bridge
Icon=dialog-information
Exec=wallnotify
Terminal=false
Type=Application
X-GNOME-Autostart-enabled=true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment