Debian Codegraph @

contrib/mailnoise

raw
1 #!/usr/local/bin/perl
2 #
3 sub Usage { print <<USAGE; # -----------------------
4
5 mailnoise -- play a noise when mail arrives
6
7 usage: mailnoise [-user username]
8
9 Create a file called ~/.mailnoise, looking like this:
10
11 host: overload
12 volume: 100
13 noise: drum.au
14
15 It accepts fields of the format "X: y", where X is one
16 of "host", "volume", "priority" and "noise". You can leave out
17 all of these except for noise. Volume defaults to $vol,
18 priority defaults to 255, and the default host is determined
19 using rwho and some trickery.
20
21 If you use MH's slocal(8) mechanism, add the following line to
22 your ~/.maildelivery file:
23
24 * - | R "/path/to/this/script/mailnoise"
25
26 If you use Elm, Pine, or almost any other mail reader, just put
27 this line in your ~/.forward:
28
29 "|/path/to/this/script/mailnoise -user <username>"
30
31 where username is your username.
32
33 USAGE
34 exit 1; } # ----------------------------------------
35 #
36 # uses the rptp command-line interface
37 # Justin Mason <[email protected]> 20/12/93.
38
39 $RPTP_PATH = "/usr/local/bin"; # Where rptp(1) can be found
40
41 # default values (in rplay(1) terms)
42 $vol = 40;
43 $pri = 0;
44
45 # if someone has a display on these hosts, map it to somewhere
46 # else that has an rplayd daemon.
47
48 %mappings = (
49 "operation", "class",
50 "stream", "class",
51 "exception", "class"
52 );
53
54 # you probably won't need to change this.
55 $ENV{'PATH'} = "$RPTP_PATH:/usr/ucb:/usr/local/bin" . $ENV{'PATH'};
56
57 # end of config section ----------------------------
58
59 require 'newgetopt.pl';
60 &NGetOpt("user:s") || &Usage;
61
62 &main;
63 while (<>) { ; } # eat the input
64 exit 0;
65
66 sub gethost {
67 $host = $DEFAULTHOST;
68 $smallest = 9999;
69
70 if ($opt_user eq '') {
71 $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
72 (getpwuid($<))[7] || die "You have no \$HOME!\n";
73 $user = $ENV{'USER'} || getlogin ||
74 (getpwuid($<))[0] || "someone";
75 } else {
76 $user = $opt_user;
77 $home = (getpwnam ($user))[7] || die "$user has no password entry!";
78 }
79
80 if (-r "$home/.display") {
81 open (H, "< $home/.display");
82 chop ($host = <H>);
83 close H;
84
85 } else {
86 # assume that the least idle is on the user's display (er ;)
87 open (RWHO, "rwho |");
88 while (<RWHO>) {
89 next unless /^${user}\s/; # username
90 next unless ($' =~ /^\s+([^:]+):/); # hostname
91 $h = $1;
92 next unless /[A-Z][a-z][a-z] [ \d]\d \d\d:\d\d/; # date
93 $_ = $'; # get the idle, if it exists
94 if ($_ =~ /:/) {
95 ($host = $h) if (($`*60 + $') < $smallest);
96 } else { # not idle
97 $host = $h;
98 }
99 }
100 close RWHO;
101 }
102 $host;
103 }
104
105 sub main {
106 $host = &gethost;
107
108 if (-r "$home/.mailnoise") {
109 open (N, "< $home/.mailnoise");
110 while (<N>) {
111 tr/A-Z/a-z/; chop;
112 s/^\s+//g;
113 /^priority: / && ($pri = $');
114 /^host: / && ($host = $');
115 /^volume: / && ($vol = $');
116 /^noise: / && ($noise .= " $'");
117 }
118 close N;
119 }
120
121 $noise =~ s/^ //;
122 open (RPTP, "echo find \"$noise\" \| rptp 2>&1 |");
123 while (<RPTP>) { /^$noise/ && $valid++; }
124 close RPTP;
125
126 if ($valid) {
127 if ($host eq '') {
128 # print STDERR "rplay -v $vol -P $pri ", $noise;
129 system ("/usr/local/bin/rplay", "-v", $vol, "-P", $pri, $noise);
130 } else {
131 # print STDERR "rplay -h $host -v $vol -P $pri ", $noise;
132 system ("/usr/local/bin/rplay", "-h", $host, "-v", $vol, "-P", $pri, $noise);
133 }
134 }
135 }