summaryrefslogtreecommitdiff
path: root/localmb2md
blob: fcdbdb32b8da6c213cb02eb2d3193d47308f1f32 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/adduser.local
  4. # Copyright 2005-2006 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmb2md,v 1.4 2006-01-18 11:25:34 jonas Exp $
  7. #
  8. # Convert user account from mailboxes below /var/mail/ and ~/mail/ to
  9. # maildirs below ~/Maildir/ .
  10. #
  11. # If from a different account, execute it like this:
  12. #
  13. # su -s /bin/sh -c localmb2md - <user>
  14. #
  15. # FIXME: Make sure no MUAs or pop/imap daemons are running
  16. # TODO: Add --help (and perhaps --force) options
  17. #
  18. set -e
  19. for prg in mb2md procmail mail-lock mail-touchlock mail-unlock; do
  20. if [ ! -f "/usr/bin/$prg" ]; then
  21. echo "ERROR: $prg not installed!"
  22. exit 1
  23. fi
  24. done
  25. # Make sure Maildir is used if procmailrc exists already
  26. if [ -f ~/.procmailrc ] && egrep -v -q '^DEFAULT=\$HOME/Maildir/$' ~/.procmailrc; then
  27. echo 'ERROR: non-Maildir-aware ~/.procmailrc exists already.'
  28. echo ' Please edit manually to include the following line:'
  29. echo
  30. echo ' DEFAULT=$HOME/Maildir/'
  31. echo
  32. echo ' Make sure other rules use Maildirs too, if needed.'
  33. exit 1
  34. fi
  35. # Avoid incoming mail while messing with INBOX
  36. mail-lock
  37. mail-touchlock &
  38. # Save the PID of the mail-touchlock process
  39. BADGER="$!"
  40. # Convert INBOX and purge if not empty
  41. if [ -s "/var/mail/$USER" ]; then
  42. mb2md -m
  43. echo -n '' > "/var/mail/$USER"
  44. else
  45. # Create Maildir for INBOX if not there already
  46. mkdir -p ~/Maildir/cur ~/Maildir/new ~/Maildir/tmp
  47. chmod -R u=rw,go=,u+X ~/Maildir
  48. fi
  49. # Reroute future incoming mail to Maildir
  50. echo 'DEFAULT=$HOME/Maildir/' > ~/.procmailrc
  51. # Allow incoming mail again
  52. kill "${BADGER}"
  53. mail-unlock
  54. # Convert secondary mailboxes
  55. if [ -d ~/mail ]; then
  56. mv ~/mail ~/mail.abandoned
  57. mb2md -s mail.abandoned
  58. # Grab dovecot subscriptions
  59. if [ -f ~/mail/.subscriptions ]; then
  60. mv ~/mail/.subscriptions ~/Maildir/.subscriptions
  61. # ...or UW-imap ones
  62. elif [ -f ~/.mailboxlist ]; then
  63. mv ~/.mailboxlist ~/Maildir/.subscriptions
  64. fi
  65. fi