summaryrefslogtreecommitdiff
path: root/localrmstaleaccounts
blob: dbb92f0959e0d8264cb0209bcf85260cf7e5b98b (plain)
  1. #!/bin/sh
  2. set -e
  3. PRG=$(basename "$0")
  4. exit1() {
  5. echo >&2 "ERROR: $1"
  6. exit 1
  7. }
  8. TEMP=$(getopt -s sh -o vqnh --long verbose,quiet,dry-run,help -n "$PRG" -- "$@") || exit1 "Internal getopt error"
  9. eval set -- "$TEMP"
  10. usage() {
  11. cat <<EOF >&2
  12. Usage: $PRG [opts...] USER [USER...]
  13. -v, --verbose increase verbosity
  14. -q, --quiet suppress non-error messages
  15. -n, --dry-run perform a trial run with no changes made
  16. -h, --help show this help
  17. EOF
  18. exit 0
  19. }
  20. VERBOSE=
  21. QUIET=
  22. DRY_RUN=
  23. while true ; do
  24. case "$1" in
  25. -v|--verbose) VERBOSE=1; shift;;
  26. -q|--quiet) QUIET=1; shift;;
  27. -n|--dry-run) DRY_RUN=1; shift;;
  28. -h|--help) usage;;
  29. --) shift; break;;
  30. *) exit1 "Internal getopt parsing error";;
  31. esac
  32. done
  33. warn() {
  34. [ -n "$QUIET" ] || echo >&2 "WARNING: $1"
  35. }
  36. info() {
  37. [ -n "$QUIET" ] || [ -z "$VERBOSE" ] || echo >&2 "INFO: $1"
  38. }
  39. remove_account() {
  40. [ -n "$DRY_RUN" ] || localrmaccount "$1"
  41. }
  42. for user in $@; do
  43. home=$(getent passwd "$user" | cut -d: -f6)
  44. if [ -z "$home" ]; then
  45. warn "Skipping user $user: failed resolving homedir"
  46. elif [ -e "$home/.forward" ]; then
  47. warn "Skipping user $user: Email gets forwarded"
  48. elif [ ! -d "$home/Maildir" ]; then
  49. warn "Skipping user $user: Missing Maildir"
  50. elif find "$home/Maildir/new" -maxdepth 0 -type d -mtime +365 | grep -q .; then
  51. info "Removing user $user: Maildir INBOX/new untouched for a year"
  52. remove_account "$user"
  53. elif find "$home/Maildir/cur" -maxdepth 0 -type d -mtime +365 | grep -q .; then
  54. info "Removing user $user: Maildir INBOX/cur untouched for a year"
  55. remove_account "$user"
  56. elif find "$home/Maildir/new" -type f -mtime +365 | grep -q .; then
  57. info "Removing user $user: new mail in INBOX untouched for a year"
  58. remove_account "$user"
  59. else
  60. info "Skipping user $user"
  61. fi
  62. done