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