From 4ade1a6f91b591431b62c0062ce22d01600c5d15 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 25 Feb 2016 21:12:19 +0000 Subject: [PATCH] Email rule based on subject text --- locale/de/freedombone-addemail.json | 2 +- locale/de/freedombone-controlpanel-user.json | 6 +- locale/es/freedombone-addemail.json | 2 +- locale/es/freedombone-controlpanel-user.json | 6 +- locale/fr/freedombone-addemail.json | 2 +- locale/fr/freedombone-controlpanel-user.json | 6 +- man/freedombone-addemail.1.gz | Bin 683 -> 768 bytes src/freedombone-addemail | 39 ++++++++--- src/freedombone-controlpanel-user | 66 ++++++++++++++++--- 9 files changed, 104 insertions(+), 25 deletions(-) diff --git a/locale/de/freedombone-addemail.json b/locale/de/freedombone-addemail.json index 3967c3f8..8787e7dd 100644 --- a/locale/de/freedombone-addemail.json +++ b/locale/de/freedombone-addemail.json @@ -1,3 +1,3 @@ { - "${PROJECT_NAME}-addemail -u [username] -e [email address] -g [group name] --public [yes|no]": "" + "${PROJECT_NAME}-addemail -u [username] -e [email address] -s [subject text] -g [group name] --public [yes|no]": "" } \ No newline at end of file diff --git a/locale/de/freedombone-controlpanel-user.json b/locale/de/freedombone-controlpanel-user.json index d9ec92b9..1da26a4f 100644 --- a/locale/de/freedombone-controlpanel-user.json +++ b/locale/de/freedombone-controlpanel-user.json @@ -25,6 +25,9 @@ "No email address was given": "", "No folder name was given": "", "Email rule for $RULE_EMAIL was added": "", + "When email arrives with subject containing:": "", + "No subject text was given": "", + "Email rule for subject '$RULE_SUBJECT' was added": "", "Block or unblock emails from a given address": "", "Block it:": "", "Block an email": "", @@ -35,7 +38,8 @@ "Change Email Filtering Rules": "", "Choose an operation:": "", "Add yourself to a mailing list": "", - "Add an email rule": "", + "Add an email rule for an address": "", + "Add an email rule for a subject": "", "Block or unblock an email address": "", "Block or unblock email with subject text": "", "Back to main menu": "", diff --git a/locale/es/freedombone-addemail.json b/locale/es/freedombone-addemail.json index 3967c3f8..8787e7dd 100644 --- a/locale/es/freedombone-addemail.json +++ b/locale/es/freedombone-addemail.json @@ -1,3 +1,3 @@ { - "${PROJECT_NAME}-addemail -u [username] -e [email address] -g [group name] --public [yes|no]": "" + "${PROJECT_NAME}-addemail -u [username] -e [email address] -s [subject text] -g [group name] --public [yes|no]": "" } \ No newline at end of file diff --git a/locale/es/freedombone-controlpanel-user.json b/locale/es/freedombone-controlpanel-user.json index d9ec92b9..1da26a4f 100644 --- a/locale/es/freedombone-controlpanel-user.json +++ b/locale/es/freedombone-controlpanel-user.json @@ -25,6 +25,9 @@ "No email address was given": "", "No folder name was given": "", "Email rule for $RULE_EMAIL was added": "", + "When email arrives with subject containing:": "", + "No subject text was given": "", + "Email rule for subject '$RULE_SUBJECT' was added": "", "Block or unblock emails from a given address": "", "Block it:": "", "Block an email": "", @@ -35,7 +38,8 @@ "Change Email Filtering Rules": "", "Choose an operation:": "", "Add yourself to a mailing list": "", - "Add an email rule": "", + "Add an email rule for an address": "", + "Add an email rule for a subject": "", "Block or unblock an email address": "", "Block or unblock email with subject text": "", "Back to main menu": "", diff --git a/locale/fr/freedombone-addemail.json b/locale/fr/freedombone-addemail.json index 3967c3f8..8787e7dd 100644 --- a/locale/fr/freedombone-addemail.json +++ b/locale/fr/freedombone-addemail.json @@ -1,3 +1,3 @@ { - "${PROJECT_NAME}-addemail -u [username] -e [email address] -g [group name] --public [yes|no]": "" + "${PROJECT_NAME}-addemail -u [username] -e [email address] -s [subject text] -g [group name] --public [yes|no]": "" } \ No newline at end of file diff --git a/locale/fr/freedombone-controlpanel-user.json b/locale/fr/freedombone-controlpanel-user.json index d9ec92b9..1da26a4f 100644 --- a/locale/fr/freedombone-controlpanel-user.json +++ b/locale/fr/freedombone-controlpanel-user.json @@ -25,6 +25,9 @@ "No email address was given": "", "No folder name was given": "", "Email rule for $RULE_EMAIL was added": "", + "When email arrives with subject containing:": "", + "No subject text was given": "", + "Email rule for subject '$RULE_SUBJECT' was added": "", "Block or unblock emails from a given address": "", "Block it:": "", "Block an email": "", @@ -35,7 +38,8 @@ "Change Email Filtering Rules": "", "Choose an operation:": "", "Add yourself to a mailing list": "", - "Add an email rule": "", + "Add an email rule for an address": "", + "Add an email rule for a subject": "", "Block or unblock an email address": "", "Block or unblock email with subject text": "", "Back to main menu": "", diff --git a/man/freedombone-addemail.1.gz b/man/freedombone-addemail.1.gz index 642d3c22081356735881cd0b48c484b29c69227b..e25d4ee0364a2e1786702b94250162c69d4fdf49 100644 GIT binary patch literal 768 zcmV+b1ONOViwFP$ZqHT#1FclsZqq;zefL+4`~pbCDMd&SA=CsyTS42XNvR-IXuY1q z3%ed`FDaJzcxHW}7tt3)6eafV%$zgl%x<$bngXBYYY2( z&t9aE^7s8sAY3@Kj~twcDhQn>Nwz$K^mKYMPm{ma`#OSYQ8?({c-f+JV1q{R1ir+6 z$2HEQ>FX^&pPnt2$IAz5jsmO$^QeTuD;OcH?^Nd{2WuDd?3BoBK%4c<=0An0HKxgmz|ams z+=pvZyQE2y0Dm4*&oqO4QN7{$z%){aJZhE3hi?W=&b_kBOJV`A!X4ThT*W@3MCM*} zD%>xmJX|ZSVN0N@TkJpztvTr~N(RRpXea;~!KoV_RW_fqOkue zo{ynKhxrup!10?tMS8v`bi3!`cuTUE7*efPiL{q2iZQ%DVBqE)O5?6D+4#e{@o2#-!-SQ_~Kv-Ke?<~w&;h7ABAX-i#yc*8OVen7ApFJ2RiG}u!mJM19#!CdJ!R6TP z*vdLoo!;{9>Uy!9E#J`WdDwX3D44=~=pk%gsMAsgo6KlmLKo62eJllsO@;P%q3^po zU&hOeo7rMDyS@s829vdDyIk-boMDAc(O6I<%QCd&nN}2(i7ghddZKn{qpsRdwM@`N zU6t`8Q>8>Q3+VG!N2=PlRClz0K35f&KCU|o>xW+oRZK)Aw$y})1#Zy*+rtR*7C91L z(z3o55)O7kO2{;bqCsw9fio#7>1CA&W+_zKgI0<%>*IQ&)TI-;sMnew;NBQ4TocgN zh@0pnf)&--YLP&xtramDTpCS($~KwLM+G+5UM5gz2V579iGq5oz?O`oB}4Csg;Tdo zT1~6SMOxK1kc??ScdTeRySAvsy-;@t4oX?SIec`7pbepjEprG5Sa4I74wJ*yOdG+d zf&^3Mr6c3$+B0jtB9~mw>MG>6sPD{PqOJKJVo&jDm%<%QimYhU)S)dztuKlUq_D2F zu$Q0v1)7BJevZ}?nzS1%wQ+==5wi=T5#-L5cF^xIpMxki!D6!LPZpB_ zjE5i3PR1Y3n$?kT-Lws0H1D18Tc4}Z_Ir|=X@04HtP)#ZJbXj3(q@fzdD&#$VmxSo RiT7o#;19?aQp4*6005oYPlf;h diff --git a/src/freedombone-addemail b/src/freedombone-addemail index 7fd73fab..6f7b94e9 100755 --- a/src/freedombone-addemail +++ b/src/freedombone-addemail @@ -36,12 +36,13 @@ export TEXTDOMAINDIR="/usr/share/locale" MYUSERNAME=$USER EMAILADDRESS= +SUBJECT_TEXT= GROUP_NAME= PUBLIC='no' function show_help { echo '' - echo $"${PROJECT_NAME}-addemail -u [username] -e [email address] -g [group name] --public [yes|no]" + echo $"${PROJECT_NAME}-addemail -u [username] -e [email address] -s [subject text] -g [group name] --public [yes|no]" echo '' exit 0 } @@ -62,6 +63,10 @@ case $key in shift EMAILADDRESS="$1" ;; + -s|--subject) + shift + SUBJECT_TEXT="$1" + ;; -g|--group) shift GROUP_NAME="$1" @@ -77,14 +82,27 @@ esac shift done -if ! [[ $MYUSERNAME && $EMAILADDRESS && $GROUP_NAME ]]; then +if ! [[ $MYUSERNAME && $GROUP_NAME ]]; then show_help fi +if [ ${#EMAILADDRESS} -lt 2 ]; then + if [ ${#SUBJECT_TEXT} -lt 2 ]; then + show_help + fi +fi + MUTTRC=/home/$MYUSERNAME/.muttrc PM=/home/$MYUSERNAME/.procmailrc LISTDIR=/home/$MYUSERNAME/Maildir/$GROUP_NAME +proc_rule=" * ^From:.*$EMAILADDRESS" +proc_comment="# Email rule for $EMAILADDRESS -> $GROUP_NAME" +if [ ${#SUBJECT_TEXT} -gt 0 ]; then + proc_rule=" * ^From:.*$EMAILADDRESS" + proc_comment="# Email rule for '$SUBJECT_TEXT' -> $GROUP_NAME" +fi + if [ ! -d "$LISTDIR" ]; then mkdir -m 700 $LISTDIR mkdir -m 700 $LISTDIR/tmp @@ -92,40 +110,43 @@ if [ ! -d "$LISTDIR" ]; then mkdir -m 700 $LISTDIR/cur fi chown -R $MYUSERNAME:$MYUSERNAME $LISTDIR -if ! grep -q "Email rule for $EMAILADDRESS -> $GROUP_NAME" $PM; then +if ! grep -q "$proc_comment" $PM; then if [[ $PUBLIC != "yes" ]]; then # private emails go after the encryption stage echo '' >> $PM - echo "# Email rule for $EMAILADDRESS -> $GROUP_NAME" >> $PM + echo "$proc_comment" >> $PM echo ":0" >> $PM - echo " * ^From:.*$EMAILADDRESS" >> $PM + echo "$proc_rule" >> $PM echo "$LISTDIR/new" >> $PM echo "# End of rule" >> $PM else - # public emails are copied before hte encryption stage + # public emails are copied before the encryption stage if ! grep -q '# encrypt' $PM; then echo '' >> $PM - echo "# Email rule for $EMAILADDRESS -> $GROUP_NAME" >> $PM + echo "$proc_comment" >> $PM echo ":0" >> $PM - echo " * ^From:.*$EMAILADDRESS" >> $PM + echo "$proc_rule" >> $PM echo "$LISTDIR/new" >> $PM echo "# End of rule" >> $PM else - filter=$(echo "# Email rule for $EMAILADDRESS -> $GROUP_NAME\n:0\n * ^From:.*$EMAILADDRESS\n$LISTDIR/new\n# End of rule\n") + filter=$(echo "$proc_comment\n:0\n${proc_rule}\n$LISTDIR/new\n# End of rule\n") sed -i "/# encrypt/i ${filter}" $PM fi fi chown $MYUSERNAME:$MYUSERNAME $PM fi + if [ ! -f "$MUTTRC" ]; then cp /etc/Muttrc $MUTTRC chown $MYUSERNAME:$MYUSERNAME $MUTTRC fi + PROCMAILLOG=/home/$MYUSERNAME/log if [ ! -d $PROCMAILLOG ]; then mkdir $PROCMAILLOG chown -R $MYUSERNAME:$MYUSERNAME $PROCMAILLOG fi + MUTT_MAILBOXES=$(grep "mailboxes =" $MUTTRC) if [[ $MUTT_MAILBOXES != *$GROUP_NAME* ]]; then if ! grep -q "=$GROUP_NAME" $MUTTRC; then diff --git a/src/freedombone-controlpanel-user b/src/freedombone-controlpanel-user index 63620d8f..f222a533 100755 --- a/src/freedombone-controlpanel-user +++ b/src/freedombone-controlpanel-user @@ -180,7 +180,7 @@ function add_to_mailing_list { --msgbox $"$LIST_NAME list was added" 6 40 } -function email_rule { +function email_rule_address { data=$(tempfile 2>/dev/null) trap "rm -f $data" 0 1 2 5 15 dialog --backtitle $"Freedombone User Control Panel" \ @@ -229,6 +229,50 @@ function email_rule { --msgbox $"Email rule for $RULE_EMAIL was added" 6 40 } +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" \ + --form "\n" 9 65 4 \ + $"When email arrives with subject containing:" 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_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 +} + function block_unblock_email { blockstr=$"Block or unblock emails from a given address" data=$(tempfile 2>/dev/null) @@ -307,13 +351,14 @@ function menu_email { trap "rm -f $data" 0 1 2 5 15 dialog --backtitle $"Freedombone User Control Panel" \ --title $"Change Email Filtering Rules" \ - --radiolist $"Choose an operation:" 13 70 6 \ + --radiolist $"Choose an operation:" 14 70 7 \ 1 $"Add yourself to a mailing list" off \ 2 $"Remove yourself from a mailing list" off \ - 3 $"Add an email rule" off \ - 4 $"Block or unblock an email address" off \ - 5 $"Block or unblock email with subject text" off \ - 6 $"Back to main menu" on 2> $data + 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 sel=$? case $sel in 1) break;; @@ -322,10 +367,11 @@ function menu_email { case $(cat $data) in 1) add_to_mailing_list;; 2) remove_user_from_mailing_list;; - 3) email_rule;; - 4) block_unblock_email;; - 5) block_unblock_subject;; - 6) break;; + 3) email_rule_address;; + 4) email_rule_subject;; + 5) block_unblock_email;; + 6) block_unblock_subject;; + 7) break;; esac done }