Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.5 KiB

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use 5.10.0;
  5. # we'll use this to filter out people who haven't logged in.
  6. # pretty silly!
  7. my %whitelist;
  8. my (@lastlog) = split /\n/, `lastlog | grep -v Never | awk '{ print \$1; }'`;
  9. foreach my $user (@lastlog) {
  10. $whitelist{$user} = 1;
  11. }
  12. my $hostname = `hostname`;
  13. chomp $hostname;
  14. opendir(my $dh, '/home/')
  15. or die "could not open /home/: $!";
  16. my %dirs;
  17. my %titles;
  18. while (my $dir = readdir $dh) {
  19. next if $dir =~ /^[.]/;
  20. next unless $whitelist{$dir};
  21. my $index_html_path = "/home/$dir/public_html/index.html";
  22. if (-e $index_html_path) {
  23. $dirs{$dir} = (stat $index_html_path)[9]; # mtime
  24. $titles{$dir} = get_title_from_file($index_html_path);
  25. }
  26. }
  27. close $dh;
  28. sub sort_by_time {
  29. $dirs{$b} <=> $dirs{$a};
  30. }
  31. my $list = "<table>\n";
  32. foreach my $key (sort sort_by_time (keys(%dirs))) {
  33. $list .= ' <tr>'
  34. . '<td><a href="//' . $hostname . '/~' . $key . '/">~' . $key . '</a></td>'
  35. . '<td>' . $titles{$key} . '</td>'
  36. . '<td class=tiny>' . $dirs{$key} . '</td>'
  37. . "</tr>\n";
  38. }
  39. $list .= "</table>";
  40. say $list;
  41. sub get_title_from_file {
  42. my ($filespec) = @_;
  43. my $html = slurp($filespec);
  44. if ($html =~ m{<title>(.*?)</title>}is) {
  45. return $1;
  46. }
  47. return '';
  48. }
  49. sub slurp {
  50. my ($file) = @_;
  51. my $everything;
  52. open my $fh, '<', $file
  53. or die "Couldn't open $file: $!\n";
  54. # line separator:
  55. local $/ = undef;
  56. $everything = <$fh>;
  57. close $fh
  58. or die "Couldn't close $file: $!";
  59. return $everything;
  60. }