Browse Source

User version of the control panel

Bob Mottram 9 years ago
parent
commit
b3a0655cc2
2 changed files with 425 additions and 0 deletions
  1. BIN
      man/freedombone-controlpanel-user.1.gz
  2. 425
    0
      src/freedombone-controlpanel-user

BIN
man/freedombone-controlpanel-user.1.gz View File


+ 425
- 0
src/freedombone-controlpanel-user View File

@@ -0,0 +1,425 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# User control panel for email
12
+#
13
+# License
14
+# =======
15
+#
16
+# Copyright (C) 2016 Bob Mottram <bob@robotics.uk.to>
17
+#
18
+# This program is free software: you can redistribute it and/or modify
19
+# it under the terms of the GNU Affero General Public License as published by
20
+# the Free Software Foundation, either version 3 of the License, or
21
+# (at your option) any later version.
22
+#
23
+# This program is distributed in the hope that it will be useful,
24
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
+# GNU Affero General Public License for more details.
27
+#
28
+# You should have received a copy of the GNU Affero General Public License
29
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
+
31
+PROJECT_NAME='freedombone'
32
+
33
+export TEXTDOMAIN=${PROJECT_NAME}-controlpanel-user
34
+export TEXTDOMAINDIR="/usr/share/locale"
35
+
36
+function any_key {
37
+    echo ' '
38
+    read -n1 -r -p $"Press any key to continue..." key
39
+}
40
+
41
+function remove_user_from_mailing_list {
42
+    USER_MAILING_LISTS=$(cat "/home/$USER/.procmailrc" | grep '\[' | grep '\]' | awk -F '\[' '{print $2}' | awk -F '\\' '{print $1}')
43
+
44
+    i=0
45
+    W=()
46
+    list_name=()
47
+    while read -r listname; do
48
+        i=$((i+1))
49
+        W+=($i "$listname")
50
+        list_name+=("$listname")
51
+        echo $listname
52
+    done <<< "$USER_MAILING_LISTS"
53
+
54
+    i=$((i+1))
55
+    W+=($i $"Exit back to user mainenance")
56
+
57
+    list_selected=$(dialog --default-item "$i" --backtitle $"Freedombone User Control Panel" --title $"Remove a mailing list for $USER" --menu $"Select one of the following:" 24 50 17 "${W[@]}" 3>&2 2>&1 1>&3)
58
+
59
+    if [ $? -eq 0 ]; then # Exit with OK
60
+        if [ ${list_selected} -ne ${i} ]; then
61
+            remove_list_name="${list_name[$((list_selected-1))]}"
62
+
63
+            # find the line number where the list is defined
64
+            line_number=0
65
+            i=0
66
+            while read -r line
67
+            do
68
+                if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
69
+                    line_number=${i}
70
+                fi
71
+                i=$((i+1))
72
+            done < "/home/$USER/.procmailrc"
73
+
74
+            if [ ${line_number} -eq 0 ]; then
75
+                # no match was found
76
+                return
77
+            fi
78
+
79
+            # recreate the file
80
+            if [ -f /home/${USER}/.procmailrc_new ]; then
81
+                rm /home/${USER}/.procmailrc_new
82
+            fi
83
+            i=0
84
+            clip=0
85
+            while read -r line
86
+            do
87
+                i=$((i+1))
88
+                if [ ${i} -gt $((line_number-1)) ]; then
89
+                    if [ ${clip} -eq 0 ]; then
90
+                        clip=1
91
+                    fi
92
+                    if [ ${clip} -eq 1 ]; then
93
+                        if [ ${i} -lt $((line_number+2)) ]; then
94
+                            continue
95
+                        else
96
+                            if [ ${#line} -lt 1 ]; then
97
+                                clip=2
98
+                                continue
99
+                            fi
100
+                            if [[ "$line" == ":"* || "$line" == "#"* ]]; then
101
+                                clip=2
102
+                            else
103
+                                continue
104
+                            fi
105
+                        fi
106
+                    fi
107
+                fi
108
+
109
+                echo "$line" >> /home/${USER}/.procmailrc_new
110
+
111
+                if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
112
+                    line_number=${i}
113
+                fi
114
+            done < "/home/$USER/.procmailrc"
115
+            cp /home/${USER}/.procmailrc_new /home/${USER}/.procmailrc
116
+            rm /home/${USER}/.procmailrc_new
117
+            chown ${USER}:${USER} /home/${USER}/.procmailrc
118
+            dialog --title $"Remove user from mailing list" \
119
+                   --msgbox $"${USER} has been removed from ${remove_list_name}" 6 50
120
+        fi
121
+    fi
122
+}
123
+
124
+function add_to_mailing_list {
125
+    data=$(tempfile 2>/dev/null)
126
+    trap "rm -f $data" 0 1 2 5 15
127
+    dialog --backtitle $"Freedombone User Control Panel" \
128
+           --title $"Subscribe to a mailing list" \
129
+           --form $"You can either enter a subject or an email address\n" 11 68 4 \
130
+              $"List folder name:" 1 1 "" 1 35 26 25 \
131
+              $"Name between [] on subject line:" 2 1 "" 2 35 26 25 \
132
+              $"List email address:" 3 1 "" 3 35 26 25 \
133
+              $"Public:" 4 1 $"yes" 4 35 4 25 \
134
+              2> $data
135
+    sel=$?
136
+    case $sel in
137
+        1) return;;
138
+        255) return;;
139
+    esac
140
+    LIST_NAME=$(cat $data | sed -n 1p)
141
+    LIST_SUBJECT=$(cat $data | sed -n 2p)
142
+    LIST_EMAIL=$(cat $data | sed -n 3p)
143
+    LIST_PUBLIC=$(cat $data | sed -n 4p)
144
+
145
+    if [ ${#LIST_PUBLIC} -lt 1 ]; then
146
+        LIST_PUBLIC='no'
147
+    fi
148
+    if [[ $LIST_PUBLIC == $'y' || $LIST_PUBLIC == $'Y' || $LIST_PUBLIC == $'true' || $LIST_PUBLIC == $'True' || $LIST_PUBLIC == $'yes' || $LIST_PUBLIC == $'Yes' || $LIST_PUBLIC == $'YES' ]]; then
149
+        LIST_PUBLIC='yes'
150
+    else
151
+        LIST_PUBLIC='no'
152
+    fi
153
+    if [ ${#LIST_NAME} -lt 2 ]; then
154
+        dialog --title $"Add mailing list" \
155
+               --msgbox $"No mailing list name was given" 6 40
156
+        return
157
+    fi
158
+    if [ ${#LIST_SUBJECT} -lt 2 ]; then
159
+        if [ ${#LIST_EMAIL} -lt 2 ]; then
160
+            dialog --title $"Add mailing list" \
161
+                   --msgbox $"No mailing list subject or address was given" 6 40
162
+            return
163
+        fi
164
+    fi
165
+    if [ ${#LIST_SUBJECT} -gt 1 ]; then
166
+        ${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
167
+                       -s "$LIST_SUBJECT" --public $LIST_PUBLIC
168
+    else
169
+        if [[ "$LIST_EMAIL" != *"@"* || "$LIST_EMAIL" != *"."* ]]; then
170
+            dialog --title $"Add mailing list" \
171
+                   --msgbox $"Unrecognised email address" 6 40
172
+            return
173
+        else
174
+            ${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
175
+                           -e "$LIST_EMAIL" --public $LIST_PUBLIC
176
+        fi
177
+    fi
178
+
179
+    dialog --title $"Add mailing list" \
180
+           --msgbox $"$LIST_NAME list was added" 6 40
181
+}
182
+
183
+function email_rule {
184
+    data=$(tempfile 2>/dev/null)
185
+    trap "rm -f $data" 0 1 2 5 15
186
+    dialog --backtitle $"Freedombone User Control Panel" \
187
+           --title $"Email rule for user $USER" \
188
+           --form "\n" 9 65 4 \
189
+              $"When email arrives from address:" 1 1 "" 1 35 24 28 \
190
+              $"Move to folder:" 2 1 "" 2 35 24 28 \
191
+              $"Public:" 3 1 $"no" 3 35 4 25 \
192
+              2> $data
193
+    sel=$?
194
+    case $sel in
195
+        1) return;;
196
+        255) return;;
197
+    esac
198
+    RULE_EMAIL=$(cat $data | sed -n 1p)
199
+    RULE_FOLDER=$(cat $data | sed -n 2p)
200
+    RULE_PUBLIC=$(cat $data | sed -n 3p)
201
+
202
+    if [ ${#RULE_PUBLIC} -lt 1 ]; then
203
+        RULE_PUBLIC='no'
204
+    fi
205
+    if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
206
+        RULE_PUBLIC='yes'
207
+    else
208
+        RULE_PUBLIC='no'
209
+    fi
210
+    if [ ${#RULE_EMAIL} -lt 2 ]; then
211
+        dialog --title $"Add email rule" \
212
+               --msgbox $"No email address was given" 6 40
213
+        return
214
+    fi
215
+    if [ ${#RULE_FOLDER} -lt 2 ]; then
216
+        dialog --title $"Add email rule" \
217
+               --msgbox $"No folder name was given" 6 40
218
+        return
219
+    fi
220
+    if [[ "$RULE_EMAIL" != *"@"* || "$RULE_EMAIL" != *"."* ]]; then
221
+        dialog --title $"Add email rule" \
222
+               --msgbox $"Unrecognised email address" 6 40
223
+        return
224
+    fi
225
+
226
+    ${PROJECT_NAME}-addemail -u $USER -e "$RULE_EMAIL" \
227
+                         -g "$RULE_FOLDER" --public $RULE_PUBLIC
228
+    dialog --title $"Add email rule" \
229
+           --msgbox $"Email rule for $RULE_EMAIL was added" 6 40
230
+}
231
+
232
+function block_unblock_email {
233
+    blockstr=$"Block/Unblock email going to"
234
+    data=$(tempfile 2>/dev/null)
235
+    trap "rm -f $data" 0 1 2 5 15
236
+    dialog --backtitle $"Freedombone User Control Panel" \
237
+           --title "$blockstr $USER" \
238
+           --form "\n" 8 65 3 \
239
+              $"When email arrives from address:" 1 1 "" 1 35 24 28 \
240
+              $"Block it:" 2 1 "yes" 2 35 4 4 \
241
+              2> $data
242
+    sel=$?
243
+    case $sel in
244
+        1) return;;
245
+        255) return;;
246
+    esac
247
+    BLOCK_EMAIL=$(cat $data | sed -n 1p)
248
+    BLOCK=$(cat $data | sed -n 2p)
249
+    if [ ${#BLOCK_EMAIL} -lt 2 ]; then
250
+        dialog --title $"Block/Unblock an email" \
251
+               --msgbox $"No email address was given" 6 40
252
+        return
253
+    fi
254
+    if [[ "$BLOCK_EMAIL" != *"@"* || "$BLOCK_EMAIL" != *"."* ]]; then
255
+        dialog --title $"Block/Unblock an email" \
256
+               --msgbox $"Unrecognised email address" 6 40
257
+        return
258
+    fi
259
+    if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
260
+        ${PROJECT_NAME}-ignore -u $USER -e "$BLOCK_EMAIL"
261
+        dialog --title $"Block an email" \
262
+               --msgbox "Email from $BLOCK_EMAIL to $USER blocked" 6 40
263
+    else
264
+        ${PROJECT_NAME}-unignore -u $USER -e "$BLOCK_EMAIL"
265
+        dialog --title $"Unblock an email" \
266
+               --msgbox "Email from $BLOCK_EMAIL to $USER unblocked" 6 40
267
+    fi
268
+}
269
+
270
+function block_unblock_subject {
271
+    blockstr=$"Block/Unblock email going to"
272
+    data=$(tempfile 2>/dev/null)
273
+    trap "rm -f $data" 0 1 2 5 15
274
+    dialog --backtitle $"Freedombone User Control Panel" \
275
+           --title "$blockstr $USER" \
276
+           --form "\n" 8 70 3 \
277
+              $"When email arrives with subject text:" 1 1 "" 1 40 24 28 \
278
+              $"Block it:" 2 1 "yes" 2 40 4 4 \
279
+              2> $data
280
+    sel=$?
281
+    case $sel in
282
+        1) return;;
283
+        255) return;;
284
+    esac
285
+    BLOCK_SUBJECT=$(cat $data | sed -n 1p)
286
+    BLOCK=$(cat $data | sed -n 2p)
287
+    if [ ${#BLOCK_SUBJECT} -lt 2 ]; then
288
+        dialog --title $"Block/Unblock an email" \
289
+               --msgbox $"No subject was given" 6 40
290
+        return
291
+    fi
292
+    if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
293
+        ${PROJECT_NAME}-ignore -u $USER -t "$BLOCK_SUBJECT"
294
+        dialog --title $"Block an email" \
295
+               --msgbox "Email with subject $BLOCK_SUBJECT to $USER blocked" 6 40
296
+    else
297
+        ${PROJECT_NAME}-unignore -u $USER -t "$BLOCK_SUBJECT"
298
+        dialog --title $"Unblock an email" \
299
+               --msgbox "Email with subject $BLOCK_SUBJECT to $USER unblocked" 6 40
300
+    fi
301
+}
302
+
303
+function create_keydrive_master {
304
+    select_user
305
+    if [ ! $USER ]; then
306
+        return
307
+    fi
308
+    dialog --title $"USB Master Keydrive" \
309
+           --msgbox $"Plug in a LUKS encrypted USB drive" 6 40
310
+    clear
311
+    ${PROJECT_NAME}-keydrive -u $USER --master 'yes'
312
+    any_key
313
+}
314
+
315
+function create_keydrive_fragment {
316
+    select_user
317
+    if [ ! $USER ]; then
318
+        return
319
+    fi
320
+    dialog --title $"USB Fragment Keydrive" \
321
+           --msgbox $"Plug in a LUKS encrypted USB drive" 6 40
322
+    clear
323
+    ${PROJECT_NAME}-keydrive -u $USER
324
+    any_key
325
+}
326
+
327
+function restore_gpg_key {
328
+    select_user
329
+    if [ ! $USER ]; then
330
+        return
331
+    fi
332
+    restorestr=$"Restore encryption key for user"
333
+    dialog --title "$restorestr $USER" \
334
+           --msgbox $"Plug in your USB keydrive" 6 40
335
+    clear
336
+    ${PROJECT_NAME}-recoverkey -u $USER
337
+    any_key
338
+}
339
+
340
+
341
+function menu_encryption_key {
342
+    while true
343
+    do
344
+        data=$(tempfile 2>/dev/null)
345
+        trap "rm -f $data" 0 1 2 5 15
346
+        dialog --backtitle $"Freedombone User Control Panel" \
347
+               --title $"Your Encryption Key" \
348
+               --radiolist $"Choose an operation:" 12 70 5 \
349
+               1 $"Restore encryption key from USB keydrive" off \
350
+               2 $"Backup encryption key to USB (master keydrive)" off \
351
+               3 $"Backup encryption key to USB (fragment keydrive)" off \
352
+               4 $"Back to main menu" on 2> $data
353
+        sel=$?
354
+        case $sel in
355
+            1) break;;
356
+            255) break;;
357
+        esac
358
+        case $(cat $data) in
359
+            1) restore_gpg_key;;
360
+            2) create_keydrive_master;;
361
+            3) create_keydrive_fragment;;
362
+            4) break;;
363
+        esac
364
+    done
365
+}
366
+
367
+function menu_email {
368
+    while true
369
+    do
370
+        data=$(tempfile 2>/dev/null)
371
+        trap "rm -f $data" 0 1 2 5 15
372
+        dialog --backtitle $"Freedombone User Control Panel" \
373
+               --title $"Change Email Filtering Rules" \
374
+               --radiolist $"Choose an operation:" 13 70 6 \
375
+               1 $"Add a user to a mailing list" off \
376
+               2 $"Remove a user from a mailing list" off \
377
+               3 $"Add an email rule" off \
378
+               4 $"Block/Unblock an email address" off \
379
+               5 $"Block/Unblock email with subject text" off \
380
+               6 $"Back to main menu" on 2> $data
381
+        sel=$?
382
+        case $sel in
383
+            1) break;;
384
+            255) break;;
385
+        esac
386
+        case $(cat $data) in
387
+            1) add_to_mailing_list;;
388
+            2) remove_user_from_mailing_list;;
389
+            3) email_rule;;
390
+            4) block_unblock_email;;
391
+            5) block_unblock_subject;;
392
+            6) break;;
393
+        esac
394
+    done
395
+}
396
+
397
+
398
+function menu_top_level {
399
+    while true
400
+    do
401
+        data=$(tempfile 2>/dev/null)
402
+        trap "rm -f $data" 0 1 2 5 15
403
+        dialog --backtitle $"Freedombone User Control Panel" \
404
+               --title $"User Control Panel" \
405
+               --radiolist $"Choose an operation:" 10 50 3 \
406
+               1 $"Your Encryption Key" off \
407
+               2 $"Change Email Filtering Rules" off \
408
+               3 $"Exit" on 2> $data
409
+        sel=$?
410
+        case $sel in
411
+            1) exit 1;;
412
+            255) exit 1;;
413
+        esac
414
+        case $(cat $data) in
415
+            1) menu_encryption_key;;
416
+            2) menu_email;;
417
+            3) break;;
418
+        esac
419
+    done
420
+}
421
+
422
+menu_top_level
423
+clear
424
+. ~/.bashrc
425
+exit 0