freedombone-app-scuttlebot 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # scuttlebot pub application
  12. # https://scuttlebot.io
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. VARIANTS='full full-vim chat'
  32. IN_DEFAULT_INSTALL=0
  33. SHOW_ON_ABOUT=0
  34. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  35. SCUTTLEBOT_VERSION='9.8.0'
  36. SCUTTLEBOT_PORT=8008
  37. SCUTTLEBOT_ONION_PORT=8118
  38. scuttlebot_variables=(MY_USERNAME
  39. DEFAULT_DOMAIN_NAME
  40. SYSTEM_TYPE)
  41. function scuttlebot_create_invite {
  42. invite_string=$(su -c "sbot invite.create 1" - scuttlebot | sed 's/"//g')
  43. clear
  44. echo -e '\n\nYour Scuttlebot invite code is:\n\n'${invite_string}'\n\n'
  45. read -n1 -r -p $"Press any key to continue..." key
  46. }
  47. function configure_interactive_scuttlebot {
  48. while true
  49. do
  50. data=$(tempfile 2>/dev/null)
  51. trap "rm -f $data" 0 1 2 5 15
  52. dialog --backtitle $"Freedombone Control Panel" \
  53. --title $"Scuttlebot" \
  54. --radiolist $"Choose an operation:" 10 50 2 \
  55. 1 $"Create an invite" off \
  56. 2 $"Exit" on 2> $data
  57. sel=$?
  58. case $sel in
  59. 1) return;;
  60. 255) return;;
  61. esac
  62. case $(cat $data) in
  63. 1) scuttlebot_create_invite;;
  64. 2) break;;
  65. esac
  66. done
  67. }
  68. function remove_user_scuttlebot {
  69. remove_username="$1"
  70. }
  71. function add_user_scuttlebot {
  72. new_username="$1"
  73. new_user_password="$2"
  74. echo '0'
  75. }
  76. function install_interactive_scuttlebot {
  77. echo -n ''
  78. APP_INSTALLED=1
  79. }
  80. function change_password_scuttlebot {
  81. new_username="$1"
  82. new_user_password="$2"
  83. echo '0'
  84. }
  85. function reconfigure_scuttlebot {
  86. if [ -d /etc/scuttlebot/.ssb ]; then
  87. systemctl stop scuttlebot
  88. rm -rf /etc/scuttlebot/.ssb
  89. systemctl start scuttlebot
  90. fi
  91. }
  92. function upgrade_scuttlebot {
  93. if ! grep -q 'scuttlebot version:' $COMPLETION_FILE; then
  94. return
  95. fi
  96. CURR_SCUTTLEBOT_VERSION=$(get_completion_param "scuttlebot version")
  97. echo "scuttlebot current version: ${CURR_SCUTTLEBOT_VERSION}"
  98. echo "scuttlebot app version: ${SCUTTLEBOT_VERSION}"
  99. if [[ "${CURR_SCUTTLEBOT_VERSION}" == "${SCUTTLEBOT_VERSION}" ]]; then
  100. return
  101. fi
  102. npm upgrade -g scuttlebot@${SCUTTLEBOT_VERSION} --save
  103. if [ ! "$?" = "0" ]; then
  104. return
  105. fi
  106. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  107. }
  108. function backup_local_scuttlebot {
  109. if [ -d /etc/scuttlebot/.ssb ]; then
  110. systemctl stop scuttlebot
  111. function_check backup_directory_to_usb
  112. backup_directory_to_usb /etc/scuttlebot/.ssb scuttlebot
  113. systemctl start scuttlebot
  114. fi
  115. }
  116. function restore_local_scuttlebot {
  117. if [ -d /etc/scuttlebot ]; then
  118. systemctl stop scuttlebot
  119. temp_restore_dir=/root/tempscuttlebot
  120. function_check restore_directory_from_usb
  121. restore_directory_from_usb $temp_restore_dir scuttlebot
  122. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  123. systemctl start scuttlebot
  124. fi
  125. }
  126. function backup_remote_scuttlebot {
  127. if [ -d /etc/scuttlebot/.ssb ]; then
  128. systemctl stop scuttlebot
  129. function_check backup_directory_to_friend
  130. backup_directory_to_friend /etc/scuttlebot/.ssb scuttlebot
  131. systemctl start scuttlebot
  132. fi
  133. }
  134. function restore_remote_scuttlebot {
  135. if [ -d /etc/scuttlebot ]; then
  136. systemctl stop scuttlebot
  137. temp_restore_dir=/root/tempscuttlebot
  138. function_check restore_directory_from_friend
  139. restore_directory_from_friend $temp_restore_dir scuttlebot
  140. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  141. systemctl start scuttlebot
  142. fi
  143. }
  144. function remove_scuttlebot {
  145. systemctl stop scuttlebot
  146. systemctl disable scuttlebot
  147. rm /etc/systemd/system/scuttlebot.service
  148. function_check remove_onion_service
  149. remove_onion_service scuttlebot ${SCUTTLEBOT_ONION_PORT}
  150. userdel -r scuttlebot
  151. if [ -d /etc/scuttlebot ]; then
  152. rm -rf /etc/scuttlebot
  153. fi
  154. remove_completion_param install_scuttlebot
  155. sed -i '/scuttlebot /d' $COMPLETION_FILE
  156. }
  157. function install_scuttlebot {
  158. function_check install_nodejs
  159. install_nodejs scuttlebot
  160. npm install -g scuttlebot@${SCUTTLEBOT_VERSION}
  161. if [ ! -f /usr/local/bin/sbot ]; then
  162. exit 528253
  163. fi
  164. if [ ! -d /etc/scuttlebot ]; then
  165. mkdir -p /etc/scuttlebot
  166. fi
  167. # an unprivileged user to run as
  168. useradd -d /etc/scuttlebot/ scuttlebot
  169. # daemon
  170. echo '[Unit]' > /etc/systemd/system/scuttlebot.service
  171. echo 'Description=Scuttlebot (messaging system)' >> /etc/systemd/system/scuttlebot.service
  172. echo 'After=syslog.target' >> /etc/systemd/system/scuttlebot.service
  173. echo 'After=network.target' >> /etc/systemd/system/scuttlebot.service
  174. echo '' >> /etc/systemd/system/scuttlebot.service
  175. echo '[Service]' >> /etc/systemd/system/scuttlebot.service
  176. echo 'Type=simple' >> /etc/systemd/system/scuttlebot.service
  177. echo 'User=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  178. echo 'Group=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  179. echo "WorkingDirectory=/etc/scuttlebot" >> /etc/systemd/system/scuttlebot.service
  180. echo 'ExecStart=/usr/local/bin/sbot server' >> /etc/systemd/system/scuttlebot.service
  181. echo 'Restart=always' >> /etc/systemd/system/scuttlebot.service
  182. echo 'Environment="USER=scuttlebot"' >> /etc/systemd/system/scuttlebot.service
  183. echo '' >> /etc/systemd/system/scuttlebot.service
  184. echo '[Install]' >> /etc/systemd/system/scuttlebot.service
  185. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/scuttlebot.service
  186. chown -R scuttlebot:scuttlebot /etc/scuttlebot
  187. # files gw_name myhostname mdns4_minimal [NOTFOUND=return] dns
  188. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  189. # start the daemon
  190. systemctl enable scuttlebot.service
  191. systemctl daemon-reload
  192. systemctl start scuttlebot.service
  193. sleep 3
  194. SCUTTLEBOT_ONION_HOSTNAME=$(add_onion_service scuttlebot ${SCUTTLEBOT_PORT} ${SCUTTLEBOT_ONION_PORT})
  195. if [ ! -d /etc/scuttlebot/.ssb ]; then
  196. echo $'Scuttlebot config not generated'
  197. exit 73528
  198. fi
  199. echo '{' > /etc/scuttlebot/.ssb/config
  200. echo " \"host\": \"${SCUTTLEBOT_ONION_HOSTNAME}\"," >> /etc/scuttlebot/.ssb/config
  201. echo " \"port\": ${SCUTTLEBOT_PORT}," >> /etc/scuttlebot/.ssb/config
  202. echo ' "timeout": 30000,' >> /etc/scuttlebot/.ssb/config
  203. echo ' "pub": true,' >> /etc/scuttlebot/.ssb/config
  204. echo ' "local": true,' >> /etc/scuttlebot/.ssb/config
  205. echo ' "friends": {' >> /etc/scuttlebot/.ssb/config
  206. echo ' "dunbar": 150,' >> /etc/scuttlebot/.ssb/config
  207. echo ' "hops": 3' >> /etc/scuttlebot/.ssb/config
  208. echo ' },' >> /etc/scuttlebot/.ssb/config
  209. echo ' "gossip": {' >> /etc/scuttlebot/.ssb/config
  210. echo ' "connections": 2' >> /etc/scuttlebot/.ssb/config
  211. echo ' },' >> /etc/scuttlebot/.ssb/config
  212. echo ' "master": [],' >> /etc/scuttlebot/.ssb/config
  213. echo ' "logging": {' >> /etc/scuttlebot/.ssb/config
  214. echo ' "level": "error"' >> /etc/scuttlebot/.ssb/config
  215. echo ' }' >> /etc/scuttlebot/.ssb/config
  216. echo '}' >> /etc/scuttlebot/.ssb/config
  217. chown scuttlebot:scuttlebot /etc/scuttlebot/.ssb/config
  218. systemctl restart scuttlebot.service
  219. if ! grep -q "scuttlebot version:" ${COMPLETION_FILE}; then
  220. echo "scuttlebot version:${SCUTTLEBOT_VERSION}" >> ${COMPLETION_FILE}
  221. else
  222. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  223. fi
  224. APP_INSTALLED=1
  225. }
  226. # NOTE: deliberately no exit 0