freedombone-controlpanel-user 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # User control panel
  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. PROJECT_NAME='freedombone'
  31. export TEXTDOMAIN=${PROJECT_NAME}-controlpanel-user
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. MY_EMAIL_ADDRESS=$USER@$HOSTNAME
  34. GPG_ID=$(gpg --fingerprint $MY_EMAIL_ADDRESS | grep -i "pub" | head -n 1 | awk -F '/' '{print $2}' | awk -F ' ' '{print $1}')
  35. SYNCTHING_CONFIG_FILE=~/.syncthingids
  36. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  37. function any_key {
  38. echo ' '
  39. read -n1 -r -p $"Press any key to continue..." key
  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. i=0
  44. W=()
  45. list_name=()
  46. while read -r listname; do
  47. i=$((i+1))
  48. W+=($i "$listname")
  49. list_name+=("$listname")
  50. echo $listname
  51. done <<< "$USER_MAILING_LISTS"
  52. i=$((i+1))
  53. W+=($i $"Exit back to user mainenance")
  54. 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)
  55. if [ $? -eq 0 ]; then # Exit with OK
  56. if [ ${list_selected} -ne ${i} ]; then
  57. remove_list_name="${list_name[$((list_selected-1))]}"
  58. # find the line number where the list is defined
  59. line_number=0
  60. i=0
  61. while read -r line
  62. do
  63. if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
  64. line_number=${i}
  65. fi
  66. i=$((i+1))
  67. done < "/home/$USER/.procmailrc"
  68. if [ ${line_number} -eq 0 ]; then
  69. # no match was found
  70. return
  71. fi
  72. # recreate the file
  73. if [ -f /home/${USER}/.procmailrc_new ]; then
  74. rm /home/${USER}/.procmailrc_new
  75. fi
  76. i=0
  77. clip=0
  78. while read -r line
  79. do
  80. i=$((i+1))
  81. if [ ${i} -gt $((line_number-1)) ]; then
  82. if [ ${clip} -eq 0 ]; then
  83. clip=1
  84. fi
  85. if [ ${clip} -eq 1 ]; then
  86. if [ ${i} -lt $((line_number+2)) ]; then
  87. continue
  88. else
  89. if [ ${#line} -lt 1 ]; then
  90. clip=2
  91. continue
  92. fi
  93. if [[ "$line" == ":"* || "$line" == "#"* ]]; then
  94. clip=2
  95. else
  96. continue
  97. fi
  98. fi
  99. fi
  100. fi
  101. echo "$line" >> /home/${USER}/.procmailrc_new
  102. if [[ "$line" == *"\[${remove_list_name}\\]"* ]]; then
  103. line_number=${i}
  104. fi
  105. done < "/home/$USER/.procmailrc"
  106. cp /home/${USER}/.procmailrc_new /home/${USER}/.procmailrc
  107. rm /home/${USER}/.procmailrc_new
  108. chown ${USER}:${USER} /home/${USER}/.procmailrc
  109. dialog --title $"Remove yourself from mailing list" \
  110. --msgbox $"You have been removed from ${remove_list_name}" 6 50
  111. fi
  112. fi
  113. }
  114. function add_to_mailing_list {
  115. data=$(tempfile 2>/dev/null)
  116. trap "rm -f $data" 0 1 2 5 15
  117. dialog --backtitle $"Freedombone User Control Panel" \
  118. --title $"Subscribe to a mailing list" \
  119. --form $"You can either enter a subject or an email address\n" 11 68 4 \
  120. $"List folder name:" 1 1 "" 1 35 26 25 \
  121. $"Name between [] on subject line:" 2 1 "" 2 35 26 25 \
  122. $"List email address:" 3 1 "" 3 35 26 25 \
  123. $"Public:" 4 1 $"yes" 4 35 4 25 \
  124. 2> $data
  125. sel=$?
  126. case $sel in
  127. 1) return;;
  128. 255) return;;
  129. esac
  130. LIST_NAME=$(cat $data | sed -n 1p)
  131. LIST_SUBJECT=$(cat $data | sed -n 2p)
  132. LIST_EMAIL=$(cat $data | sed -n 3p)
  133. LIST_PUBLIC=$(cat $data | sed -n 4p)
  134. if [ ${#LIST_PUBLIC} -lt 1 ]; then
  135. LIST_PUBLIC='no'
  136. fi
  137. if [[ $LIST_PUBLIC == $'y' || $LIST_PUBLIC == $'Y' || $LIST_PUBLIC == $'true' || $LIST_PUBLIC == $'True' || $LIST_PUBLIC == $'yes' || $LIST_PUBLIC == $'Yes' || $LIST_PUBLIC == $'YES' ]]; then
  138. LIST_PUBLIC='yes'
  139. else
  140. LIST_PUBLIC='no'
  141. fi
  142. if [ ${#LIST_NAME} -lt 2 ]; then
  143. dialog --title $"Add mailing list" \
  144. --msgbox $"No mailing list name was given" 6 40
  145. return
  146. fi
  147. if [ ${#LIST_SUBJECT} -lt 2 ]; then
  148. if [ ${#LIST_EMAIL} -lt 2 ]; then
  149. dialog --title $"Add mailing list" \
  150. --msgbox $"No mailing list subject or address was given" 6 40
  151. return
  152. fi
  153. fi
  154. if [ ${#LIST_SUBJECT} -gt 1 ]; then
  155. ${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
  156. -s "$LIST_SUBJECT" --public $LIST_PUBLIC
  157. else
  158. if [[ "$LIST_EMAIL" != *"@"* || "$LIST_EMAIL" != *"."* ]]; then
  159. dialog --title $"Add mailing list" \
  160. --msgbox $"Unrecognised email address" 6 40
  161. return
  162. else
  163. ${PROJECT_NAME}-addlist -u $USER -l "$LIST_NAME" \
  164. -e "$LIST_EMAIL" --public $LIST_PUBLIC
  165. fi
  166. fi
  167. dialog --title $"Add mailing list" \
  168. --msgbox $"$LIST_NAME list was added" 6 40
  169. }
  170. function email_rule_address {
  171. data=$(tempfile 2>/dev/null)
  172. trap "rm -f $data" 0 1 2 5 15
  173. dialog --backtitle $"Freedombone User Control Panel" \
  174. --title $"Create an email rule" \
  175. --form "\n" 9 65 4 \
  176. $"When email arrives from address:" 1 1 "" 1 35 24 28 \
  177. $"Move to folder:" 2 1 "" 2 35 24 28 \
  178. $"Public:" 3 1 $"no" 3 35 4 25 \
  179. 2> $data
  180. sel=$?
  181. case $sel in
  182. 1) return;;
  183. 255) return;;
  184. esac
  185. RULE_EMAIL=$(cat $data | sed -n 1p)
  186. RULE_FOLDER=$(cat $data | sed -n 2p)
  187. RULE_PUBLIC=$(cat $data | sed -n 3p)
  188. if [ ${#RULE_PUBLIC} -lt 1 ]; then
  189. RULE_PUBLIC='no'
  190. fi
  191. if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
  192. RULE_PUBLIC='yes'
  193. else
  194. RULE_PUBLIC='no'
  195. fi
  196. if [ ${#RULE_EMAIL} -lt 2 ]; then
  197. dialog --title $"Create an email rule" \
  198. --msgbox $"No email address was given" 6 40
  199. return
  200. fi
  201. if [ ${#RULE_FOLDER} -lt 2 ]; then
  202. dialog --title $"Create an email rule" \
  203. --msgbox $"No folder name was given" 6 40
  204. return
  205. fi
  206. if [[ "$RULE_EMAIL" != *"@"* || "$RULE_EMAIL" != *"."* ]]; then
  207. dialog --title $"Create an email rule" \
  208. --msgbox $"Unrecognised email address" 6 40
  209. return
  210. fi
  211. ${PROJECT_NAME}-addemail -u $USER -e "$RULE_EMAIL" \
  212. -g "$RULE_FOLDER" --public $RULE_PUBLIC
  213. dialog --title $"Create an email rule" \
  214. --msgbox $"Email rule for $RULE_EMAIL was added" 6 40
  215. }
  216. function email_rule_subject {
  217. data=$(tempfile 2>/dev/null)
  218. trap "rm -f $data" 0 1 2 5 15
  219. dialog --backtitle $"Freedombone User Control Panel" \
  220. --title $"Create an email rule" \
  221. --form "\n" 9 75 4 \
  222. $"When email arrives with subject containing:" 1 1 "" 1 45 24 28 \
  223. $"Move to folder:" 2 1 "" 2 45 24 28 \
  224. $"Public:" 3 1 $"no" 3 45 4 25 \
  225. 2> $data
  226. sel=$?
  227. case $sel in
  228. 1) return;;
  229. 255) return;;
  230. esac
  231. RULE_SUBJECT=$(cat $data | sed -n 1p)
  232. RULE_FOLDER=$(cat $data | sed -n 2p)
  233. RULE_PUBLIC=$(cat $data | sed -n 3p)
  234. if [ ${#RULE_PUBLIC} -lt 1 ]; then
  235. RULE_PUBLIC='no'
  236. fi
  237. if [[ $RULE_PUBLIC == $'y' || $RULE_PUBLIC == $'Y' || $RULE_PUBLIC == $'true' || $RULE_PUBLIC == $'True' || $RULE_PUBLIC == $'yes' || $RULE_PUBLIC == $'Yes' || $RULE_PUBLIC == $'YES' ]]; then
  238. RULE_PUBLIC='yes'
  239. else
  240. RULE_PUBLIC='no'
  241. fi
  242. if [ ${#RULE_SUBJECT} -lt 2 ]; then
  243. dialog --title $"Create an email rule" \
  244. --msgbox $"No subject text was given" 6 40
  245. return
  246. fi
  247. if [ ${#RULE_FOLDER} -lt 2 ]; then
  248. dialog --title $"Create an email rule" \
  249. --msgbox $"No folder name was given" 6 40
  250. return
  251. fi
  252. ${PROJECT_NAME}-addemail -u $USER -s "$RULE_SUBJECT" \
  253. -g "$RULE_FOLDER" --public $RULE_PUBLIC
  254. dialog --title $"Create an email rule" \
  255. --msgbox $"Email rule for subject '$RULE_SUBJECT' was added" 6 40
  256. }
  257. function block_unblock_email {
  258. blockstr=$"Block or unblock emails from a given address"
  259. data=$(tempfile 2>/dev/null)
  260. trap "rm -f $data" 0 1 2 5 15
  261. dialog --backtitle $"Freedombone User Control Panel" \
  262. --title "$blockstr" \
  263. --form "\n" 8 65 3 \
  264. $"When email arrives from address:" 1 1 "" 1 35 24 100 \
  265. $"Block it:" 2 1 "yes" 2 35 4 4 \
  266. 2> $data
  267. sel=$?
  268. case $sel in
  269. 1) return;;
  270. 255) return;;
  271. esac
  272. BLOCK_EMAIL=$(cat $data | sed -n 1p)
  273. BLOCK=$(cat $data | sed -n 2p)
  274. if [ ${#BLOCK_EMAIL} -lt 2 ]; then
  275. dialog --title "$blockstr" \
  276. --msgbox $"No email address was given" 6 40
  277. return
  278. fi
  279. if [[ "$BLOCK_EMAIL" != *"@"* || "$BLOCK_EMAIL" != *"."* ]]; then
  280. dialog --title "$blockstr" \
  281. --msgbox $"Unrecognised email address" 6 40
  282. return
  283. fi
  284. if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
  285. ${PROJECT_NAME}-ignore -u $USER -e "$BLOCK_EMAIL"
  286. dialog --title $"Block an email" \
  287. --msgbox "Email from $BLOCK_EMAIL is now blocked" 6 75
  288. else
  289. ${PROJECT_NAME}-unignore -u $USER -e "$BLOCK_EMAIL"
  290. dialog --title $"Unblock an email" \
  291. --msgbox "Email from $BLOCK_EMAIL is now unblocked" 6 75
  292. fi
  293. }
  294. function block_unblock_subject {
  295. blockstr=$"Block or unblock emails with text in the subject line"
  296. data=$(tempfile 2>/dev/null)
  297. trap "rm -f $data" 0 1 2 5 15
  298. dialog --backtitle $"Freedombone User Control Panel" \
  299. --title "$blockstr" \
  300. --form "\n" 8 70 3 \
  301. $"When email arrives with subject text:" 1 1 "" 1 40 24 28 \
  302. $"Block it:" 2 1 "yes" 2 40 4 4 \
  303. 2> $data
  304. sel=$?
  305. case $sel in
  306. 1) return;;
  307. 255) return;;
  308. esac
  309. BLOCK_SUBJECT=$(cat $data | sed -n 1p)
  310. BLOCK=$(cat $data | sed -n 2p)
  311. if [ ${#BLOCK_SUBJECT} -lt 2 ]; then
  312. dialog --title "$blockstr" \
  313. --msgbox $"No subject was given" 6 40
  314. return
  315. fi
  316. if [[ $BLOCK == "y"* || $BLOCK == "Y"* ]]; then
  317. ${PROJECT_NAME}-ignore -u $USER -t "$BLOCK_SUBJECT"
  318. dialog --title $"Block an email" \
  319. --msgbox $"Email with subject $BLOCK_SUBJECT is now blocked" 6 40
  320. else
  321. ${PROJECT_NAME}-unignore -u $USER -t "$BLOCK_SUBJECT"
  322. dialog --title $"Unblock an email" \
  323. --msgbox $"Email with subject $BLOCK_SUBJECT is now unblocked" 6 40
  324. fi
  325. }
  326. function show_gpg_key {
  327. GPG_FINGERPRINT=$(gpg --fingerprint $MY_EMAIL_ADDRESS | grep -i "key fingerprint" | head -n 1 | awk -F '= ' '{print $2}')
  328. GPG_DATE=$(gpg --fingerprint $MY_EMAIL_ADDRESS | grep -i "pub" | head -n 1 | awk -F '/' '{print $2}' | awk -F ' ' '{print $2}')
  329. dialog --title $"Your PGP/GPG Key" \
  330. --backtitle $"Freedombone User Control Panel" \
  331. --msgbox $"Email Address: $MY_EMAIL_ADDRESS\n\nKey ID: $GPG_ID\n\nFingerprint: $GPG_FINGERPRINT\n\nCreated: $GPG_DATE" 12 70
  332. }
  333. function publish_gpg_key {
  334. gpg --send-key $GPG_ID
  335. dialog --title $"Publish your PGP/GPG key" \
  336. --msgbox $"Your key has now been published" 6 40
  337. }
  338. function add_gpg_key {
  339. data=$(tempfile 2>/dev/null)
  340. trap "rm -f $data" 0 1 2 5 15
  341. dialog --title $"Add someone's PGP/GPG key" \
  342. --backtitle $"Freedombone User Control Panel" \
  343. --inputbox $"Enter their email address below" 8 60 2>$data
  344. sel=$?
  345. case $sel in
  346. 0)
  347. ADD_EMAIL_ADDRESS=$(<$data)
  348. if [ ${#ADD_EMAIL_ADDRESS} -gt 2 ]; then
  349. if [[ $ADD_EMAIL_ADDRESS == *"@"* && $ADD_EMAIL_ADDRESS == *"."* ]]; then
  350. clear
  351. gpg --search-keys $ADD_EMAIL_ADDRESS
  352. else
  353. dialog --title $"Unrecognised email address" \
  354. --backtitle $"Freedombone User Control Panel" \
  355. --msgbox $"This doesn't look like an email address" 6 50
  356. fi
  357. fi
  358. ;;
  359. esac
  360. }
  361. function remove_gpg_key {
  362. data=$(tempfile 2>/dev/null)
  363. trap "rm -f $data" 0 1 2 5 15
  364. dialog --title $"Remove someone's PGP/GPG key" \
  365. --backtitle $"Freedombone User Control Panel" \
  366. --inputbox $"Enter their email address below" 8 60 2>$data
  367. sel=$?
  368. case $sel in
  369. 0)
  370. REMOVE_EMAIL_ADDRESS=$(<$data)
  371. if [ ${#REMOVE_EMAIL_ADDRESS} -gt 2 ]; then
  372. if [[ $REMOVE_EMAIL_ADDRESS == *"@"* && $REMOVE_EMAIL_ADDRESS == *"."* ]]; then
  373. if [[ $REMOVE_EMAIL_ADDRESS != $MY_EMAIL_ADDRESS ]]; then
  374. clear
  375. gpg --delete-key $REMOVE_EMAIL_ADDRESS
  376. else
  377. dialog --title $"Remove someone's PGP/GPG key" \
  378. --backtitle $"Freedombone User Control Panel" \
  379. --msgbox $"It's not a good idea to remove your own encryption key" 6 65
  380. fi
  381. else
  382. dialog --title $"Unrecognised email address" \
  383. --backtitle $"Freedombone User Control Panel" \
  384. --msgbox $"This doesn't look like an email address" 6 50
  385. fi
  386. fi
  387. ;;
  388. esac
  389. }
  390. function add_ssh_key {
  391. data=$(tempfile 2>/dev/null)
  392. trap "rm -f $data" 0 1 2 5 15
  393. dialog --title $"Add an ssh key for logging in" \
  394. --backtitle $"Freedombone User Control Panel" \
  395. --inputbox $"This will allow you to log into ${PROJECT_NAME} if you have an ssh key on your system, and provides much stronger security than simply using a login password.\n\nWARNING: If you make any mistakes here then you may not be able to log in and will need to get the administrator to clear your ssh authorized_keys file." 15 60 2>$data
  396. sel=$?
  397. case $sel in
  398. 0)
  399. SSH_PUBLIC_KEY=$(<$data)
  400. if [ ${#SSH_PUBLIC_KEY} -gt 20 ]; then
  401. if [[ "$SSH_PUBLIC_KEY" == "ssh-"* ]]; then
  402. if [ ! -d /home/$USER/.ssh ]; then
  403. mkdir /home/$USER/.ssh
  404. fi
  405. if [ ! -f /home/$USER/.ssh/authorized_keys ]; then
  406. touch /home/$USER/.ssh/authorized_keys
  407. fi
  408. if ! grep -q "$SSH_PUBLIC_KEY" /home/$USER/.ssh/authorized_keys; then
  409. echo "$SSH_PUBLIC_KEY" >> /home/$USER/.ssh/authorized_keys
  410. dialog --title $"New ssh key added" \
  411. --backtitle $"Freedombone User Control Panel" \
  412. --msgbox $"Your ssh key has now been added" 6 50
  413. else
  414. dialog --title $"ssh key already added" \
  415. --backtitle $"Freedombone User Control Panel" \
  416. --msgbox $"That ssh key has already been added" 6 50
  417. fi
  418. else
  419. dialog --title $"Unrecognised ssh public key" \
  420. --backtitle $"Freedombone User Control Panel" \
  421. --msgbox $"This doesn't look like an ssh key" 6 50
  422. fi
  423. fi
  424. ;;
  425. esac
  426. }
  427. function remove_ssh_key {
  428. data=$(tempfile 2>/dev/null)
  429. trap "rm -f $data" 0 1 2 5 15
  430. dialog --title $"Remove an ssh key for logging in" \
  431. --backtitle $"Freedombone User Control Panel" \
  432. --inputbox $"Enter the ssh public key which is to be removed. This can be just the address at the end.\n\nWARNING: If you make any mistakes here then you may not be able to log in and will need to get the administrator to clear your ssh authorized_keys file." 15 60 2>$data
  433. sel=$?
  434. case $sel in
  435. 0)
  436. SSH_PUBLIC_KEY=$(<$data)
  437. if [ ${#SSH_PUBLIC_KEY} -gt 5 ]; then
  438. if [ -f /home/$USER/.ssh/authorized_keys ]; then
  439. sed -i "s|.*${SSH_PUBLIC_KEY}.*||g" /home/$USER/.ssh/authorized_keys
  440. dialog --title $"Remove an ssh public key" \
  441. --backtitle $"Freedombone User Control Panel" \
  442. --msgbox $"The ssh key has been removed" 6 50
  443. fi
  444. fi
  445. ;;
  446. esac
  447. }
  448. function smtp_proxy {
  449. MUTTRC_FILE=/home/$USER/.muttrc
  450. if [ ! -f $MUTTRC_FILE ]; then
  451. return
  452. fi
  453. SMTP_PROXY_ENABLE=$'no'
  454. SMTP_PROXY_PROTOCOL='smtps'
  455. SMTP_PROXY_SERVER='mail.myispdomain'
  456. SMTP_PROXY_PORT=465
  457. SMTP_PROXY_USERNAME=''
  458. SMTP_PROXY_PASSWORD=''
  459. if grep -q "set smtp_url" $MUTTRC_FILE; then
  460. if grep -q "#set smtp_url" $MUTTRC_FILE; then
  461. SMTP_PROXY_ENABLE=$'no'
  462. else
  463. SMTP_PROXY_ENABLE=$'yes'
  464. fi
  465. SMTP_PROXY_PROTOCOL=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F ':' '{print $1}')
  466. SMTP_PROXY_SERVER=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $2}' | awk -F '@' '{print $2}')
  467. SMTP_PROXY_PORT=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F ':' '{print $4}' | awk -F '/' '{print $1}')
  468. SMTP_PROXY_USERNAME=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $1}')
  469. SMTP_PROXY_PASSWORD=$(cat $MUTTRC_FILE | grep "set smtp_url" | awk -F '"' '{print $2}' | awk -F '/' '{print $3}' | awk -F ':' '{print $2}' | awk -F '@' '{print $1}')
  470. fi
  471. data=$(tempfile 2>/dev/null)
  472. trap "rm -f $data" 0 1 2 5 15
  473. dialog --backtitle $"Freedombone Control Panel" \
  474. --title $"SMTP Proxy for $USER" \
  475. --form $"You may need to proxy outgoing email via your ISP's mail server. If so enter the details below." 14 75 6 \
  476. $"Enable proxy:" 1 1 "$SMTP_PROXY_ENABLE" 1 24 5 5 \
  477. $"Protocol (smtp/smtps):" 2 1 "$SMTP_PROXY_PROTOCOL" 2 24 5 5 \
  478. $"ISP mail server:" 3 1 "$SMTP_PROXY_SERVER" 3 24 40 10000 \
  479. $"Port:" 4 1 "$SMTP_PROXY_PORT" 4 24 5 5 \
  480. $"Username:" 5 1 "$SMTP_PROXY_USERNAME" 5 24 40 10000 \
  481. $"Password:" 6 1 "$SMTP_PROXY_PASSWORD" 6 24 40 10000 \
  482. 2> $data
  483. sel=$?
  484. case $sel in
  485. 1) return;;
  486. 255) return;;
  487. esac
  488. SMTP_PROXY_ENABLE=$(cat $data | sed -n 1p)
  489. SMTP_PROXY_PROTOCOL=$(cat $data | sed -n 2p)
  490. SMTP_PROXY_SERVER=$(cat $data | sed -n 3p)
  491. SMTP_PROXY_PORT=$(cat $data | sed -n 4p)
  492. SMTP_PROXY_USERNAME=$(cat $data | sed -n 5p)
  493. SMTP_PROXY_PASSWORD=$(cat $data | sed -n 6p)
  494. # change muttrc
  495. if [ $SMTP_PROXY_ENABLE != $'no' ]; then
  496. if ! grep -q "set smtp_url" $MUTTRC_FILE; then
  497. echo "set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"" >> $MUTTRC_FILE
  498. else
  499. sed -i "s|set smtp_url=.*|set smtp_url=\"${SMTP_PROXY_PROTOCOL}://${SMTP_PROXY_USERNAME}:${SMTP_PROXY_PASSWORD}@${SMTP_PROXY_SERVER}:${SMTP_PROXY_PORT}/\"|g" $MUTTRC_FILE
  500. fi
  501. sed -i 's|#set smtp_url|set smtp_url|g' $MUTTRC_FILE
  502. else
  503. if grep "set smtp_url" $MUTTRC_FILE; then
  504. sed -i 's|set smtp_url|#set smtp_url|g' $MUTTRC_FILE
  505. fi
  506. fi
  507. }
  508. function syncthing_create_ids_file {
  509. if [ ! -f ~/.syncthing-server-id ]; then
  510. return
  511. fi
  512. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  513. if [ ! -f $SYNCTHING_CONFIG_FILE ]; then
  514. echo $'# Your syncthing configuration file' > $SYNCTHING_CONFIG_FILE
  515. echo '#' >> $SYNCTHING_CONFIG_FILE
  516. echo $"# The ${PROJECT_NAME} syncthing ID is: $SYNCTHING_ID" >> $SYNCTHING_CONFIG_FILE
  517. echo '#' >> $SYNCTHING_CONFIG_FILE
  518. echo '# Paste the IDs of your devices below' >> $SYNCTHING_CONFIG_FILE
  519. echo '#' >> $SYNCTHING_CONFIG_FILE
  520. fi
  521. }
  522. function syncthing_manual_edit {
  523. if [ ! -f ~/.syncthing-server-id ]; then
  524. return
  525. fi
  526. syncthing_create_ids_file
  527. editor $SYNCTHING_CONFIG_FILE
  528. # force an update of the configuration
  529. touch ~/.syncthing-update
  530. }
  531. function syncthing_show_id {
  532. if [ ! -f ~/.syncthing-server-id ]; then
  533. return
  534. fi
  535. SYNCTHING_ID=$(cat ~/.syncthing-server-id)
  536. dialog --title $"Device ID for ${PROJECT_NAME}" \
  537. --backtitle $"Freedombone User Control Panel" \
  538. --msgbox $"In a desktop terminal press shift and select the ID below,\nthen right click and copy.\n\nWithin Connectbot select Menu/Copy and then highlight the ID below\n\n$SYNCTHING_ID" 12 78
  539. }
  540. function syncthing_add_id {
  541. if [ ! -f ~/.syncthing-server-id ]; then
  542. return
  543. fi
  544. syncthing_create_ids_file
  545. data=$(tempfile 2>/dev/null)
  546. trap "rm -f $data" 0 1 2 5 15
  547. dialog --backtitle $"Freedombone User Control Panel" \
  548. --title $"Add a Syncthing device ID" \
  549. --form $"Paste the device ID for your laptop/desktop/netbook/phone/tablet below" 9 80 2 \
  550. $"Device ID:" 1 1 "" 1 26 80 80 \
  551. $"Description (optional):" 2 1 "" 2 26 80 80 \
  552. 2> $data
  553. sel=$?
  554. case $sel in
  555. 1) return;;
  556. 255) return;;
  557. esac
  558. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  559. SYNCTHING_DESCRIPTION=$(cat $data | sed -n 2p)
  560. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  561. return
  562. fi
  563. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  564. dialog --title $"Add a Syncthing device ID" \
  565. --backtitle $"Freedombone User Control Panel" \
  566. --msgbox $"That doesn't look like a device ID" 6 50
  567. return
  568. fi
  569. if grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  570. dialog --title $"Add a Syncthing device ID" \
  571. --backtitle $"Freedombone User Control Panel" \
  572. --msgbox $"That ID has already been added" 6 50
  573. return
  574. fi
  575. if [ ${#SYNCTHING_DESCRIPTION} -gt 0 ]; then
  576. echo "# $SYNCTHING_DESCRIPTION" >> $SYNCTHING_CONFIG_FILE
  577. fi
  578. echo "$SYNCTHING_DEVICE_ID" >> $SYNCTHING_CONFIG_FILE
  579. # force an update of the configuration
  580. touch ~/.syncthing-update
  581. dialog --title $"Add a Syncthing device ID" \
  582. --backtitle $"Freedombone User Control Panel" \
  583. --msgbox $"The ID was added" 6 50
  584. }
  585. function syncthing_remove_id {
  586. if [ ! -f ~/.syncthing-server-id ]; then
  587. return
  588. fi
  589. syncthing_create_ids_file
  590. data=$(tempfile 2>/dev/null)
  591. trap "rm -f $data" 0 1 2 5 15
  592. dialog --backtitle $"Freedombone User Control Panel" \
  593. --title $"Remove a Syncthing device ID" \
  594. --form $"Paste the device ID which is to be removed below" 8 80 1 \
  595. $"Device ID:" 1 1 "" 1 14 80 80 \
  596. 2> $data
  597. sel=$?
  598. case $sel in
  599. 1) return;;
  600. 255) return;;
  601. esac
  602. SYNCTHING_DEVICE_ID=$(cat $data | sed -n 1p)
  603. if [ ${#SYNCTHING_DEVICE_ID} -lt 10 ]; then
  604. return
  605. fi
  606. if [[ $SYNCTHING_DEVICE_ID == *"#"* || $SYNCTHING_DEVICE_ID == *"*"* || $SYNCTHING_DEVICE_ID == *'/'* || $SYNCTHING_DEVICE_ID != *"-"* ]]; then
  607. dialog --title $"Remove a Syncthing device ID" \
  608. --backtitle $"Freedombone User Control Panel" \
  609. --msgbox $"That doesn't look like a device ID" 6 50
  610. return
  611. fi
  612. if ! grep -q "$SYNCTHING_DEVICE_ID" $SYNCTHING_CONFIG_FILE; then
  613. dialog --title $"Remove a Syncthing device ID" \
  614. --backtitle $"Freedombone User Control Panel" \
  615. --msgbox $"That ID wasn't registered anyway" 6 50
  616. return
  617. fi
  618. sed -i "/$SYNCTHING_DEVICE_ID/d" $SYNCTHING_CONFIG_FILE
  619. # force an update of the configuration
  620. touch ~/.syncthing-update
  621. dialog --title $"Remove a Syncthing device ID" \
  622. --backtitle $"Freedombone User Control Panel" \
  623. --msgbox $"The ID was removed" 6 50
  624. }
  625. function menu_encryption_keys {
  626. while true
  627. do
  628. data=$(tempfile 2>/dev/null)
  629. trap "rm -f $data" 0 1 2 5 15
  630. dialog --backtitle $"Freedombone User Control Panel" \
  631. --title $"Your Encryption Keys" \
  632. --radiolist $"Choose an operation:" 15 70 7 \
  633. 1 $"Show your PGP/GPG key" off \
  634. 2 $"Publish your PGP/GPG key so that others can find it" off \
  635. 3 $"Add someone's PGP/GPG key" off \
  636. 4 $"Remove someone's PGP/GPG key" off \
  637. 5 $"Add an ssh key for logging in" off \
  638. 6 $"Remove an ssh key for logging in" off \
  639. 7 $"Back to main menu" on 2> $data
  640. sel=$?
  641. case $sel in
  642. 1) break;;
  643. 255) break;;
  644. esac
  645. case $(cat $data) in
  646. 1) show_gpg_key;;
  647. 2) publish_gpg_key;;
  648. 3) add_gpg_key;;
  649. 4) remove_gpg_key;;
  650. 5) add_ssh_key;;
  651. 6) remove_ssh_key;;
  652. 7) break;;
  653. esac
  654. done
  655. }
  656. function menu_email {
  657. while true
  658. do
  659. data=$(tempfile 2>/dev/null)
  660. trap "rm -f $data" 0 1 2 5 15
  661. dialog --backtitle $"Freedombone User Control Panel" \
  662. --title $"Change Email Filtering Rules" \
  663. --radiolist $"Choose an operation:" 14 70 7 \
  664. 1 $"Add yourself to a mailing list" off \
  665. 2 $"Remove yourself from a mailing list" off \
  666. 3 $"Add an email rule for an address" off \
  667. 4 $"Add an email rule for a subject" off \
  668. 5 $"Block or unblock an email address" off \
  669. 6 $"Block or unblock email with subject text" off \
  670. 7 $"Back to main menu" on 2> $data
  671. sel=$?
  672. case $sel in
  673. 1) break;;
  674. 255) break;;
  675. esac
  676. case $(cat $data) in
  677. 1) add_to_mailing_list;;
  678. 2) remove_user_from_mailing_list;;
  679. 3) email_rule_address;;
  680. 4) email_rule_subject;;
  681. 5) block_unblock_email;;
  682. 6) block_unblock_subject;;
  683. 7) break;;
  684. esac
  685. done
  686. }
  687. function menu_syncthing {
  688. while true
  689. do
  690. data=$(tempfile 2>/dev/null)
  691. trap "rm -f $data" 0 1 2 5 15
  692. dialog --backtitle $"Freedombone User Control Panel" \
  693. --title $"File Synchronization" \
  694. --radiolist $"Choose an operation:" 12 70 6 \
  695. 1 $"Show device ID for ${PROJECT_NAME}" off \
  696. 2 $"Add an ID for another machine or device" off \
  697. 3 $"Remove an ID for another machine or device" off \
  698. 4 $"Manually edit device IDs" off \
  699. 5 $"Back to main menu" on 2> $data
  700. sel=$?
  701. case $sel in
  702. 1) break;;
  703. 255) break;;
  704. esac
  705. case $(cat $data) in
  706. 1) syncthing_show_id;;
  707. 2) syncthing_add_id;;
  708. 3) syncthing_remove_id;;
  709. 4) syncthing_manual_edit;;
  710. 5) break;;
  711. esac
  712. done
  713. }
  714. function menu_admin {
  715. if [ ! -f /etc/sudoers ]; then
  716. clear
  717. exit 0
  718. fi
  719. if grep -q "$USER ALL=(ALL) ALL" /etc/sudoers; then
  720. sudo freedombone-controlpanel
  721. else
  722. clear
  723. exit 0
  724. fi
  725. }
  726. function menu_top_level {
  727. while true
  728. do
  729. data=$(tempfile 2>/dev/null)
  730. trap "rm -f $data" 0 1 2 5 15
  731. dialog --backtitle $"Freedombone User Control Panel" \
  732. --title $"User Control Panel" \
  733. --radiolist $"Choose an operation:" 19 50 12 \
  734. 1 $"Use Email" off \
  735. 2 $"Change Email Filtering Rules" off \
  736. 3 $"Use Tox Chat" off \
  737. 4 $"Use XMPP Chat" off \
  738. 5 $"Use IRC" off \
  739. 6 $"Browse the Web" off \
  740. 7 $"File Synchronization" off \
  741. 8 $"Your Encryption Keys" off \
  742. 9 $"Set an outgoing email proxy" off \
  743. 10 $"Administrator controls" off \
  744. 11 $"Exit to the command line" off \
  745. 12 $"Log out" on 2> $data
  746. sel=$?
  747. case $sel in
  748. 1) exit 1;;
  749. 255) exit 1;;
  750. esac
  751. case $(cat $data) in
  752. 1) mutt;;
  753. 2) menu_email;;
  754. 3) toxic --force-tcp --SOCKS5-proxy 127.0.0.1 9050;;
  755. 4) torify profanity;;
  756. 5) torify irssi;;
  757. 6) torify elinks;;
  758. 7) menu_syncthing;;
  759. 8) menu_encryption_keys;;
  760. 9) smtp_proxy;;
  761. 10) menu_admin;;
  762. 11) break;;
  763. 12) kill -HUP `pgrep -s 0 -o`;;
  764. esac
  765. done
  766. }
  767. menu_top_level
  768. clear
  769. . ~/.bashrc
  770. exit 0