summaryrefslogtreecommitdiff
path: root/localmb2md
blob: 0979ef574f654f58cac22cf965ef8f1fe20e5e73 (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.8 2006-04-25 19:09:38 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. helpers="/usr/bin/mb2md /usr/bin/mail-lock /usr/bin/mail-touchlock /usr/bin/mail-unlock"
  20. # Unset these if the MDA delivers to Maildir without being told explicitly
  21. mkprocmailrc="yes"
  22. helpers="$helpers /usr/bin/procmail"
  23. # Change this to ".subscriptions" if using pre-1.xx Dovecot
  24. subscriptions="subscriptions"
  25. if [ -e /etc/local/mb2md.conf ]; then
  26. . /etc/local/mb2md.conf
  27. fi
  28. # Make sure all required tools are available
  29. for prg in $helpers; do
  30. if [ ! -x "$prg" ]; then
  31. echo "ERROR: $prg not installed!"
  32. exit 1
  33. fi
  34. done
  35. # Bail out if custom procmailrc is in the way
  36. if [ -n "$mkprocmailrc" ] && [ -f ~/.procmailrc ]; then
  37. if egrep -v -q '^DEFAULT=\$HOME/Maildir/$' ~/.procmailrc; then
  38. cat <<EOF
  39. ERROR: non-Maildir-aware ~/.procmailrc exists already.
  40. Please edit manually to include the following line:
  41. DEFAULT=$HOME/Maildir/
  42. Make sure other rules use Maildirs too, if needed.
  43. EOF
  44. exit 1
  45. else
  46. mkprocmailrc=""
  47. fi
  48. fi
  49. # Avoid incoming mail while messing with INBOX
  50. if [ -e "/var/mail/$USER" ]; then
  51. echo "Locking INBOX"
  52. mail-lock
  53. mail-touchlock &
  54. # Save the PID of the mail-touchlock process
  55. BADGER="$!"
  56. # Create Maildir for INBOX if not there already
  57. mkdir -p ~/Maildir/cur ~/Maildir/new ~/Maildir/tmp
  58. chmod -R u=rw,go=,u+X ~/Maildir
  59. # Convert INBOX if not empty
  60. if [ -s "/var/mail/$USER" ]; then
  61. mb2md -m
  62. fi
  63. # Reroute future incoming mail to Maildir
  64. if [ -n "$mkprocmailrc" ]; then
  65. echo 'DEFAULT=$HOME/Maildir/' > ~/.procmailrc
  66. fi
  67. # Purge old INBOX
  68. echo -n '' > "/var/mail/$USER"
  69. # Allow incoming mail again
  70. kill "${BADGER}"
  71. mail-unlock
  72. echo "INBOX unlocked again"
  73. fi
  74. # Convert secondary mailboxes
  75. if [ -d ~/mail ]; then
  76. mv ~/mail ~/mail.abandoned
  77. mb2md -R -s mail.abandoned
  78. # Grab dovecot subscriptions
  79. if [ -f ~/mail/.subscriptions ]; then
  80. mv ~/mail/.subscriptions ~/Maildir/"$subscriptions"
  81. # ...or UW-imap ones
  82. elif [ -f ~/.mailboxlist ]; then
  83. mv ~/.mailboxlist ~/Maildir/"$subscriptions"
  84. fi
  85. fi