This file is indexed.

/usr/share/perl5/Swatch/Actions.pm is in swatch 3.2.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package Swatch::Actions;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;

@ISA = qw(Exporter AutoLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
             ring_bell
	     echo
             exec_command
	     send_message_to_pipe
);
$VERSION = '20060502';

# Preloaded methods go here.

# Autoload methods go after =cut, and are processed by the autosplit program.

################################################################
# "bell" action
#
# ring_bell(args) -- ring the terminal bell some number 
#    of times (default is 1)
################################################################
use Time::HiRes qw(sleep);
sub ring_bell {
  my %args = (
              'RINGS' => 1,
              'DELAY' => 0.2,
              @_
             );
  
  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  return if (exists($args{'THRESHOLDING'})
	     and $args{'THRESHOLDING'} eq 'on'
	     and not &Swatch::Threshold::threshold(%args));
  
  my $bells = $args{'RINGS'};
  for ( ; $bells > 0 ; $bells-- ) {
    print "\a";
    sleep($args{'DELAY'});
  }
}

################################################################
# "echo" Action
################################################################
use Term::ANSIColor;
sub echo {
  my %args = (
              'MODES' => [ ],
              @_
             );
  return if (exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'}));

  return if (exists($args{'THRESHOLDING'})
	     and $args{'THRESHOLDING'} eq 'on'
	     and not &Swatch::Threshold::threshold(%args));
  
  if (${$args{'MODES'}}[0] =~ /^normal$/i) { # for backward compatability
    print "$args{'MESSAGE'}\n";
  } else {
    print colored("$args{'MESSAGE'}\n", @{$args{'MODES'}});
  }
}

################################################################
# "exec" Action
#
# exec_command(args) -- fork and execute a command
################################################################
use POSIX ":sys_wait_h";

sub exec_command {
  my %args = (@_);
  my $exec_pid;
  my $command;

  if (exists $args{'COMMAND'}) {
    $command = $args{'COMMAND'};
  } else {
    warn "$0: No command was specified in exec action.\n";
    return 1;
  }

  return 0 if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  return if (exists($args{'THRESHOLDING'})
	     and $args{'THRESHOLDING'} eq 'on'
	     and not &Swatch::Threshold::threshold(%args));

 EXECFORK: {
    if ($exec_pid = fork) {
      waitpid(-1, WNOHANG);
      return 0;
    } elsif (defined $exec_pid) {
      exec($command);
      } elsif ($! =~ /No more processes/) {
        # EAGAIN, supposedly recoverable fork error
        sleep 5;
        redo EXECFORK;
      } else {
        warn "$0: Can't fork to exec $command: $!\n";
        return 1;
      }
  }
  return 0;
}

################################################################
# "mail" Action
#
# send_email -- send some mail using $MAILER.
#
# usage: &send_email(%options);
#
################################################################

sub send_email {
  my $login = (getpwuid($<))[0];
  my %args = (
              'ADDRESSES' => $login,
              'SUBJECT' => 'Message from Swatch',
              @_
             );

  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  return if (exists($args{'THRESHOLDING'})
	     and $args{'THRESHOLDING'} eq 'on'
	     and not &Swatch::Threshold::threshold(%args));

  if (! $args{'MAILER'} ) {
    foreach my $mailer (qw(/usr/lib/sendmail /usr/sbin/sendmail)) {
      $args{'MAILER'} = $mailer if ( -x $mailer );
    }
    if ($args{'MAILER'} ne '') {
      $args{'MAILER'} .= ' -oi -t -odq';
    }
  }

  (my $to_line = $args{'ADDRESSES'}) =~ s/:/,/g;

  local $SIG{CHLD} = 'default';
  open(MAIL_PIPE, "| $args{'MAILER'}") 
    or (warn "$0: cannot open pipe to $args{MAILER}: $!\n" and  return);

  print MAIL_PIPE <<"EOF";
To: $to_line
Subject: $args{SUBJECT}

$args{'MESSAGE'}
EOF

  close(MAIL_PIPE);
}

################################################################
# "pipe" Action
#
# send_message_to_pipe -- send text to a pipe.
#
# usage: &send_message_to_pipe(
#               $program_to_pipe_to_including_the_vertical_bar_symbol,
#               $message_to_send_to_the_pipe);
# 
################################################################
{
  my $pipe_is_open;
  my $current_command_name;

  sub send_message_to_pipe {
    my %args = (@_);
    my $command;

    if (exists $args{'COMMAND'}) {
      $command = $args{'COMMAND'};
    } else {
      warn "$0: No command was specified in pipe action.\n";
      return;
    }

    return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

    return if (exists($args{'THRESHOLDING'})
	       and $args{'THRESHOLDING'} eq 'on'
	       and not &Swatch::Threshold::threshold(%args));

    # open a new pipe if necessary
    if ( !$pipe_is_open or $current_command_name ne $command ) {
      # first close an open pipe
      close(PIPE) if $pipe_is_open;
      $pipe_is_open = 0;
      open(PIPE, "| $command") 
        or warn "$0: cannot open pipe to $command: $!\n" && return;
      PIPE->autoflush(1);
      $pipe_is_open = 1;
      $current_command_name = $command;
    }
    # send the text
    print PIPE "$args{'MESSAGE'}";

    if (not exists $args{'KEEP_OPEN'}) {
      close(PIPE) if $pipe_is_open;
      $pipe_is_open = 0;
    }
  }

  #
  # close_pipe_if_open -- used at the end of a script to close a pipe
  #     opened by &pipe_it().
  #
  # usage: &close_pipe_if_open();
  #
  sub close_pipe_if_open {
    if ($pipe_is_open) {
      close(PIPE);
    }
  }
}


################################################################
# "write" Action
#
# write_message -- send a message logged on users.
#
################################################################
sub write_message {
  my %args = (WRITE => '/usr/bin/write',
	      @_);

  return if exists($args{'WHEN'}) and not inside_time_window($args{'WHEN'});

  return if (exists($args{'THRESHOLDING'})
	     and $args{'THRESHOLDING'} eq 'on'
	     and not &Swatch::Threshold::threshold(%args));

  if ($args{WRITE} eq '') {
    warn "ERROR: $0 cannot find the write(1) program\n";
    return;
  }

  if (exists($args{'USERS'})) {
    foreach my $user (split(/:/, $args{'USERS'})) {
      send_message_to_pipe(COMMAND => "$args{'WRITE'} $user 2>/dev/null", 
                           MESSAGE => "$args{'MESSAGE'}\n");
    }
  }
}

################################################################
# in_range($range, $number) 
# returns 1 if $number is inside $range, 0 if not
#
################################################################
sub in_range {
  my $range = shift;
  my $num = shift;

  foreach my $f (split(/,/, $range)) {
    if ($f =~ /-/) {
      my ($low,$high) = split(/-/, $f);
      return 1 if ($low <= $num and $num <= $high);
    } elsif ($f == $num) {
      return 1;
    }
  }
  return 0;
}

################################################################
# inside_time_window($days,$hours)
# returns 1 if inside window, 0 if outside window
#
################################################################
sub inside_time_window {
  my $range = shift;
  my($days, $hours) = split(/:/, $range);

  my ($hr, $wday) = (localtime(time))[2,6];

  if (($days eq '*' or in_range($days, $wday))
      and ($hours eq '*' or in_range($hours, $hr))) {
    return 1;
  } else {
    return 0;
  }
}


1;

__END__

################################################################
# Perl Documentation
################################################################

=head1 NAME

Swatch::Actions - actions for swatch(1)

=head1 SYNOPSIS

  use Swatch::Actions

  ring_bell(RINGS => $number_of_times_to_ring,
            DELAY => $delay_in_seconds,
            WHEN => $time_window);

  echo(MESSAGE => 'some text', MODES => @modes);

  exec(COMMAND => $command_string,
       WHEN => $time_window);


=head1 DESCRIPTION

=head1 AUTHOR

E. Todd Atkins - Todd.Atkins@StanfordAlumni.ORG

=head1 SEE ALSO

swatch(1), Term::ANSIColor(1), perl(1).

=cut