freedomboneeee/src/freedombone-controlpanel-user

414 lines
14 KiB
Plaintext
Raw Normal View History

2016-02-25 12:26:33 +01:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# User control panel for email
#
# License
# =======
#
# Copyright (C) 2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-controlpanel-user
export TEXTDOMAINDIR="/usr/share/locale"
function any_key {
echo ' '
read -n1 -r -p $"Press any key to continue..." key
}
function remove_user_from_mailing_list {
USER_MAILING_LISTS=$(cat "/home/$USER/.procmailrc" | grep '\[' | grep '\]' | awk -F '\[' '{print $2}' | awk -F '\\' '{print $1}')
i=0
W=()
list_name=()
while read -r listname; do
i=$((i+1))
W+=($i "$listname")
list_name+=("$listname")
echo $listname
done <<< "$USER_MAILING_LISTS"
i=$((i+1))
W+=($i $"Exit back to user mainenance")
2016-02-25 21:43:30 +01:00
list_selected=$(dialog --default-item "$i" --backtitle $"Freedombone User Control Panel" --title $"Remove yourself from a mailing list" --menu $"Select one of the following:" 24 50 17 "${W[@]}" 3>&2 2>&1 1>&3)
2016-02-25 12:26:33 +01:00
if [ $? -eq 0 ]; then # Exit with OK
if [ ${list_selected} -ne ${i} ]; then
remove_list_name="${list_name[$((list_selected-1))]}"
# find the line number where the list is defined
line_number=0
i=0
while read -r line
do
if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
line_number=${i}
fi
i=$((i+1))
done < "/home/$USER/.procmailrc"
if [ ${line_number} -eq 0 ]; then
# no match was found
return
fi
# recreate the file
if [ -f /home/${USER}/.procmailrc_new ]; then
rm /home/${USER}/.procmailrc_new
fi
i=0
clip=0
while read -r line
do
i=$((i+1))
if [ ${i} -gt $((line_number-1)) ]; then
if [ ${clip} -eq 0 ]; then
clip=1
fi
if [ ${clip} -eq 1 ]; then
if [ ${i} -lt $((line_number+2)) ]; then
continue
else
if [ ${#line} -lt 1 ]; then
clip=2
continue
fi
if [[ "$line" == ":"* || "$line" == "#"* ]]; then
clip=2
else
continue
fi
fi
fi
fi
echo "$line" >> /home/${USER}/.procmailrc_new
if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
line_number=${i}
fi
done < "/home/$USER/.procmailrc"
cp /home/${USER}/.procmailrc_new /home/${USER}/.procmailrc
rm /home/${USER}/.procmailrc_new
chown ${USER}:${USER} /home/${USER}/.procmailrc
2016-02-25 21:43:30 +01:00
dialog --title $"Remove yourself from mailing list" \
--msgbox $"You have been removed from ${remove_list_name}" 6 50
2016-02-25 12:26:33 +01:00
fi
fi
}
function add_to_mailing_list {
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"Subscribe to a mailing list" \
--form $"You can either enter a subject or an email address\n" 11 68 4 \
$"List folder name:" 1 1 "" 1 35 26 25 \
$"Name between [] on subject line:" 2 1 "" 2 35 26 25 \
$"List email address:" 3 1 "" 3 35 26 25 \
$"Public:" 4 1 $"yes" 4 35 4 25 \
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
LIST_NAME=$(cat $data | sed -n 1p)
LIST_SUBJECT=$(cat $data | sed -n 2p)
LIST_EMAIL=$(cat $data | sed -n 3p)
LIST_PUBLIC=$(cat $data | sed -n 4p)
if [ ${#LIST_PUBLIC} -lt 1 ]; then
LIST_PUBLIC='no'
fi
if [[ $LIST_PUBLIC == $'y' || $LIST_PUBLIC == $'Y' || $LIST_PUBLIC == $'true' || $LIST_PUBLIC == $'True' || $LIST_PUBLIC == $'yes' || $LIST_PUBLIC == $'Yes' || $LIST_PUBLIC == $'YES' ]]; then
LIST_PUBLIC='yes'
else
LIST_PUBLIC='no'
fi
if [ ${#LIST_NAME} -lt 2 ]; then
dialog --title $"Add mailing list" \
--msgbox $"No mailing list name was given" 6 40
return
fi
if [ ${#LIST_SUBJECT} -lt 2 ]; then
if [ ${#LIST_EMAIL} -lt 2 ]; then
dialog --title $"Add mailing list" \
--msgbox $"No mailing list subject or address was given" 6 40
return
fi
fi
if [ ${#LIST_SUBJECT} -gt 1 ]; then
${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
-s "$LIST_SUBJECT" --public $LIST_PUBLIC
else
if [[ "$LIST_EMAIL" != *"@"* || "$LIST_EMAIL" != *"."* ]]; then
dialog --title $"Add mailing list" \
--msgbox $"Unrecognised email address" 6 40
return
else
${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
-e "$LIST_EMAIL" --public $LIST_PUBLIC
fi
fi
dialog --title $"Add mailing list" \
--msgbox $"$LIST_NAME list was added" 6 40
}
2016-02-25 22:12:19 +01:00
function email_rule_address {
2016-02-25 12:26:33 +01:00
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
2016-02-25 21:43:30 +01:00
--title $"Create an email rule" \
2016-02-25 12:26:33 +01:00
--form "\n" 9 65 4 \
$"When email arrives from address:" 1 1 "" 1 35 24 28 \
$"Move to folder:" 2 1 "" 2 35 24 28 \
$"Public:" 3 1 $"no" 3 35 4 25 \
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
RULE_EMAIL=$(cat $data | sed -n 1p)
RULE_FOLDER=$(cat $data | sed -n 2p)
RULE_PUBLIC=$(cat $data | sed -n 3p)
if [ ${#RULE_PUBLIC} -lt 1 ]; then
RULE_PUBLIC='no'
fi
if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
RULE_PUBLIC='yes'
else
RULE_PUBLIC='no'
fi
if [ ${#RULE_EMAIL} -lt 2 ]; then
2016-02-25 21:43:30 +01:00
dialog --title $"Create an email rule" \
2016-02-25 12:26:33 +01:00
--msgbox $"No email address was given" 6 40
return
fi
if [ ${#RULE_FOLDER} -lt 2 ]; then
2016-02-25 21:43:30 +01:00
dialog --title $"Create an email rule" \
2016-02-25 12:26:33 +01:00
--msgbox $"No folder name was given" 6 40
return
fi
if [[ "$RULE_EMAIL" != *"@"* || "$RULE_EMAIL" != *"."* ]]; then
2016-02-25 21:43:30 +01:00
dialog --title $"Create an email rule" \
2016-02-25 12:26:33 +01:00
--msgbox $"Unrecognised email address" 6 40
return
fi
${PROJECT_NAME}-addemail -u $USER -e "$RULE_EMAIL" \
-g "$RULE_FOLDER" --public $RULE_PUBLIC
2016-02-25 21:43:30 +01:00
dialog --title $"Create an email rule" \
2016-02-25 12:26:33 +01:00
--msgbox $"Email rule for $RULE_EMAIL was added" 6 40
}
2016-02-25 22:12:19 +01:00
function email_rule_subject {
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"Create an email rule" \
2016-02-25 22:15:30 +01:00
--form "\n" 9 75 4 \
$"When email arrives with subject containing:" 1 1 "" 1 45 24 28 \
$"Move to folder:" 2 1 "" 2 45 24 28 \
$"Public:" 3 1 $"no" 3 45 4 25 \
2016-02-25 22:12:19 +01:00
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
RULE_SUBJECT=$(cat $data | sed -n 1p)
RULE_FOLDER=$(cat $data | sed -n 2p)
RULE_PUBLIC=$(cat $data | sed -n 3p)
if [ ${#RULE_PUBLIC} -lt 1 ]; then
RULE_PUBLIC='no'
fi
if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
RULE_PUBLIC='yes'
else
RULE_PUBLIC='no'
fi
if [ ${#RULE_SUBJECT} -lt 2 ]; then
dialog --title $"Create an email rule" \
--msgbox $"No subject text was given" 6 40
return
fi
if [ ${#RULE_FOLDER} -lt 2 ]; then
dialog --title $"Create an email rule" \
--msgbox $"No folder name was given" 6 40
return
fi
${PROJECT_NAME}-addemail -u $USER -s "$RULE_SUBJECT" \
-g "$RULE_FOLDER" --public $RULE_PUBLIC
dialog --title $"Create an email rule" \
--msgbox $"Email rule for subject '$RULE_SUBJECT' was added" 6 40
}
2016-02-25 12:26:33 +01:00
function block_unblock_email {
2016-02-25 21:43:30 +01:00
blockstr=$"Block or unblock emails from a given address"
2016-02-25 12:26:33 +01:00
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
2016-02-25 21:43:30 +01:00
--title "$blockstr" \
2016-02-25 12:26:33 +01:00
--form "\n" 8 65 3 \
$"When email arrives from address:" 1 1 "" 1 35 24 28 \
$"Block it:" 2 1 "yes" 2 35 4 4 \
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
BLOCK_EMAIL=$(cat $data | sed -n 1p)
BLOCK=$(cat $data | sed -n 2p)
if [ ${#BLOCK_EMAIL} -lt 2 ]; then
2016-02-25 21:43:30 +01:00
dialog --title "$blockstr" \
2016-02-25 12:26:33 +01:00
--msgbox $"No email address was given" 6 40
return
fi
if [[ "$BLOCK_EMAIL" != *"@"* || "$BLOCK_EMAIL" != *"."* ]]; then
2016-02-25 21:43:30 +01:00
dialog --title "$blockstr" \
2016-02-25 12:26:33 +01:00
--msgbox $"Unrecognised email address" 6 40
return
fi
if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
${PROJECT_NAME}-ignore -u $USER -e "$BLOCK_EMAIL"
dialog --title $"Block an email" \
2016-02-25 21:43:30 +01:00
--msgbox "Email from $BLOCK_EMAIL is now blocked" 6 40
2016-02-25 12:26:33 +01:00
else
${PROJECT_NAME}-unignore -u $USER -e "$BLOCK_EMAIL"
dialog --title $"Unblock an email" \
2016-02-25 21:43:30 +01:00
--msgbox "Email from $BLOCK_EMAIL is now unblocked" 6 40
2016-02-25 12:26:33 +01:00
fi
}
function block_unblock_subject {
2016-02-25 21:43:30 +01:00
blockstr=$"Block or unblock emails with text in the subject line"
2016-02-25 12:26:33 +01:00
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
2016-02-25 21:43:30 +01:00
--title "$blockstr" \
2016-02-25 12:26:33 +01:00
--form "\n" 8 70 3 \
$"When email arrives with subject text:" 1 1 "" 1 40 24 28 \
$"Block it:" 2 1 "yes" 2 40 4 4 \
2> $data
sel=$?
case $sel in
1) return;;
255) return;;
esac
BLOCK_SUBJECT=$(cat $data | sed -n 1p)
BLOCK=$(cat $data | sed -n 2p)
if [ ${#BLOCK_SUBJECT} -lt 2 ]; then
2016-02-25 21:43:30 +01:00
dialog --title "$blockstr" \
2016-02-25 12:26:33 +01:00
--msgbox $"No subject was given" 6 40
return
fi
if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
${PROJECT_NAME}-ignore -u $USER -t "$BLOCK_SUBJECT"
dialog --title $"Block an email" \
2016-02-25 21:43:30 +01:00
--msgbox "Email with subject $BLOCK_SUBJECT is now blocked" 6 40
2016-02-25 12:26:33 +01:00
else
${PROJECT_NAME}-unignore -u $USER -t "$BLOCK_SUBJECT"
dialog --title $"Unblock an email" \
2016-02-25 21:43:30 +01:00
--msgbox "Email with subject $BLOCK_SUBJECT is now unblocked" 6 40
2016-02-25 12:26:33 +01:00
fi
}
function menu_email {
while true
do
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"Change Email Filtering Rules" \
2016-02-25 22:12:19 +01:00
--radiolist $"Choose an operation:" 14 70 7 \
2016-02-25 12:35:17 +01:00
1 $"Add yourself to a mailing list" off \
2 $"Remove yourself from a mailing list" off \
2016-02-25 22:12:19 +01:00
3 $"Add an email rule for an address" off \
4 $"Add an email rule for a subject" off \
5 $"Block or unblock an email address" off \
6 $"Block or unblock email with subject text" off \
7 $"Back to main menu" on 2> $data
2016-02-25 12:26:33 +01:00
sel=$?
case $sel in
1) break;;
255) break;;
esac
case $(cat $data) in
1) add_to_mailing_list;;
2) remove_user_from_mailing_list;;
2016-02-25 22:12:19 +01:00
3) email_rule_address;;
4) email_rule_subject;;
5) block_unblock_email;;
6) block_unblock_subject;;
7) break;;
2016-02-25 12:26:33 +01:00
esac
done
}
function menu_top_level {
while true
do
data=$(tempfile 2>/dev/null)
trap "rm -f $data" 0 1 2 5 15
dialog --backtitle $"Freedombone User Control Panel" \
--title $"User Control Panel" \
--radiolist $"Choose an operation:" 13 50 6 \
2016-02-25 12:47:55 +01:00
1 $"Use Email" off \
2016-02-25 12:26:33 +01:00
2 $"Change Email Filtering Rules" off \
2016-02-25 12:47:55 +01:00
3 $"Use Chat" off \
4 $"Use IRC" off \
5 $"Exit to the command line" off \
6 $"Log out" on 2> $data
2016-02-25 12:26:33 +01:00
sel=$?
case $sel in
1) exit 1;;
255) exit 1;;
esac
case $(cat $data) in
2016-02-25 12:47:55 +01:00
1) mutt;;
2016-02-25 12:26:33 +01:00
2) menu_email;;
2016-02-25 12:47:55 +01:00
3) toxic;;
4) irssi;;
5) break;;
6) kill -HUP `pgrep -s 0 -o`;;
2016-02-25 12:26:33 +01:00
esac
done
}
menu_top_level
clear
. ~/.bashrc
exit 0