freedombone-addemail 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Adds an email address rule to the email configuration
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. MYUSERNAME=$USER
  30. EMAILADDRESS=
  31. GROUP_NAME=
  32. function show_help {
  33. echo ''
  34. echo 'freedombone-addemail -u [username] -e [email address] -g [group name]'
  35. echo ''
  36. exit 0
  37. }
  38. while [[ $# > 1 ]]
  39. do
  40. key="$1"
  41. case $key in
  42. -h|--help)
  43. show_help
  44. ;;
  45. -u|--user)
  46. shift
  47. MYUSERNAME="$1"
  48. ;;
  49. -e|--email)
  50. shift
  51. EMAILADDRESS="$1"
  52. ;;
  53. -g|--group)
  54. shift
  55. GROUP_NAME="$1"
  56. ;;
  57. *)
  58. # unknown option
  59. ;;
  60. esac
  61. shift
  62. done
  63. if ! [[ $MYUSERNAME && $EMAILADDRESS && $GROUP_NAME ]]; then
  64. show_help
  65. fi
  66. MUTTRC=/home/$MYUSERNAME/.muttrc
  67. PM=/home/$MYUSERNAME/.procmailrc
  68. LISTDIR=/home/$MYUSERNAME/Maildir/$GROUP_NAME
  69. if [ ! -d "$LISTDIR" ]; then
  70. mkdir -m 700 $LISTDIR
  71. mkdir -m 700 $LISTDIR/tmp
  72. mkdir -m 700 $LISTDIR/new
  73. mkdir -m 700 $LISTDIR/cur
  74. fi
  75. chown -R $MYUSERNAME:$MYUSERNAME $LISTDIR
  76. if ! grep -q "Email rule for $EMAILADDRESS -> $GROUP_NAME" $PM; then
  77. echo "# Email rule for $EMAILADDRESS -> $GROUP_NAME" >> $PM
  78. echo ":0" >> $PM
  79. echo " * ^From:.*$EMAILADDRESS" >> $PM
  80. echo "$LISTDIR/new" >> $PM
  81. echo "# End of rule" >> $PM
  82. chown $MYUSERNAME:$MYUSERNAME $PM
  83. fi
  84. if [ ! -f "$MUTTRC" ]; then
  85. cp /etc/Muttrc $MUTTRC
  86. chown $MYUSERNAME:$MYUSERNAME $MUTTRC
  87. fi
  88. PROCMAILLOG=/home/$MYUSERNAME/log
  89. if [ ! -d $PROCMAILLOG ]; then
  90. mkdir $PROCMAILLOG
  91. chown -R $MYUSERNAME:$MYUSERNAME $PROCMAILLOG
  92. fi
  93. MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC)
  94. if [[ $MUTT_MAILBOXES != *$GROUP_NAME* ]]; then
  95. if ! grep -q "=$GROUP_NAME" $MUTTRC; then
  96. sed -i "s|$MUTT_MAILBOXES|$MUTT_MAILBOXES =$GROUP_NAME|g" $MUTTRC
  97. chown $MYUSERNAME:$MYUSERNAME $MUTTRC
  98. fi
  99. fi
  100. exit 0