Skip to content

Instantly share code, notes, and snippets.

@wraithgar
Created November 12, 2015 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wraithgar/26002103cf94107a94de to your computer and use it in GitHub Desktop.
Save wraithgar/26002103cf94107a94de to your computer and use it in GitHub Desktop.
Maildir capable finger utility
#!/usr/bin/perl
####################################################
# Maildir Capable Finger Utility #
# v2.0 #
# by Michael Garvin <gar@comrade.us> #
# original can be found at #
# http://sysadminco.com/postfix/scripts/mfinger.pl #
# doesn't check .plan or .project files #
# doesn't check for ~/.nofinger #
# intended to run suid as root, so use at #
# your own risk #
# ONLY checks Maildir/cur and Maildir/new. #
# Will NOT check other subdirectories #
# Assumes you are using courier-imap #
####################################################
#3/19/2004 - 1.0
# Initial Release
#4/22/2004 - 1.1
# separated newest and oldest between new and cur
# "mail last read" now uses atime of courierpop3dsizelist
#9/02/2004 - 2.0
# rewrote logic to use ctime of new and cur instead of statting EACH file
# ...much faster now
# separated pop vs imap for "Best guess", added "Never checked" test
use strict;
$ENV{PATH} = "/bin"; #suid protection
my ($newest_new,$oldest_new,$newest_cur,$oldest_cur,$newcount,$curcount,@m);
if (!$ARGV[0]) {
print "Usage: $0 username\n";
} else {
my ($user) = $ARGV[0] =~ /^([A-Za-z0-9@._\-]+)$/; #suid protection
if (my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $homedir, $shell) = getpwnam($user)) {
print "Login: $name\tName: $gcos\n";
print "Directory: $homedir\tShell: $shell\n";
if ($comment) { print "Comment: $comment\n"; }
print "\n";
if (opendir (MAILDIR, $homedir."/Maildir")) {
close MAILDIR;
if (opendir (NEWDIR, $homedir."/Maildir/new")) {
while (readdir(NEWDIR)) {
$newcount++;
}
close NEWDIR;
$newcount--;
$newcount--;
@m = stat $homedir."/Maildir/new";
print "$newcount new messages\n";
if ($newcount) {
print "Newest mail recieved: ";
print scalar localtime $m[10];
print "\n";
}
} else {
print "User does not appear to have a valid maildir directory\n";
}
if (opendir (CURDIR, $homedir."/Maildir/cur")) {
while (readdir(CURDIR)) {
$curcount++;
}
close CURDIR;
$curcount--;
$curcount--;
@m = stat $homedir."/Maildir/cur";
my $cur_ctime = $m[10];
if ($curcount) {
print "$curcount read messages\n";
print "Last new mail was read: ";
print scalar localtime $cur_ctime;
print "\n";
}
@m = stat $homedir."Maildir/courierpop3dsizelist";
my $pop = $m[8];
@m = stat $homedir."Maildir/courierimapuiddb";
my $imap = $m[8];
if ($pop || $imap) {
if ($pop > $imap) {
print "\nBest guess: Mail was last popped: ";
print scalar localtime $pop;
} else {
print "\nBest guess: Mail was last imapped: ";
print scalar localtime $imap;
}
} else {
print "\nBest guess: Mail has never been checked\n";
print "Mailbox created: ";
print scalar localtime $cur_ctime;
print "\n";
}
print "\n";
}
} else {
print "User does not appear to have a maildir directory\n";
}
} else {
print "mfinger: $user: no such user\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment