freedombone-app-scuttlebot 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_variables=(MY_USERNAME
  38. DEFAULT_DOMAIN_NAME
  39. SYSTEM_TYPE)
  40. function scuttlebot_create_invite {
  41. invite_string=$(su -c "sbot invite.create 1" - scuttlebot | sed 's/"//g')
  42. clear
  43. echo -e '\n\nYour Scuttlebot invite code is:\n\n'${invite_string}'\n\n'
  44. read -n1 -r -p $"Press any key to continue..." key
  45. }
  46. function configure_interactive_scuttlebot {
  47. while true
  48. do
  49. data=$(tempfile 2>/dev/null)
  50. trap "rm -f $data" 0 1 2 5 15
  51. dialog --backtitle $"Freedombone Control Panel" \
  52. --title $"Scuttlebot" \
  53. --radiolist $"Choose an operation:" 10 50 2 \
  54. 1 $"Create an invite" off \
  55. 2 $"Exit" on 2> $data
  56. sel=$?
  57. case $sel in
  58. 1) return;;
  59. 255) return;;
  60. esac
  61. case $(cat $data) in
  62. 1) scuttlebot_create_invite;;
  63. 2) break;;
  64. esac
  65. done
  66. }
  67. function remove_user_scuttlebot {
  68. remove_username="$1"
  69. }
  70. function add_user_scuttlebot {
  71. new_username="$1"
  72. new_user_password="$2"
  73. echo '0'
  74. }
  75. function install_interactive_scuttlebot {
  76. echo -n ''
  77. APP_INSTALLED=1
  78. }
  79. function change_password_scuttlebot {
  80. new_username="$1"
  81. new_user_password="$2"
  82. echo '0'
  83. }
  84. function reconfigure_scuttlebot {
  85. if [ -d /etc/scuttlebot/.ssb ]; then
  86. systemctl stop scuttlebot
  87. rm -rf /etc/scuttlebot/.ssb
  88. systemctl start scuttlebot
  89. fi
  90. }
  91. function upgrade_scuttlebot {
  92. if ! grep -q 'scuttlebot version:' $COMPLETION_FILE; then
  93. return
  94. fi
  95. CURR_SCUTTLEBOT_VERSION=$(get_completion_param "scuttlebot version")
  96. echo "scuttlebot current version: ${CURR_SCUTTLEBOT_VERSION}"
  97. echo "scuttlebot app version: ${SCUTTLEBOT_VERSION}"
  98. if [[ "${CURR_SCUTTLEBOT_VERSION}" == "${SCUTTLEBOT_VERSION}" ]]; then
  99. return
  100. fi
  101. npm upgrade -g scuttlebot@${SCUTTLEBOT_VERSION} --save
  102. if [ ! "$?" = "0" ]; then
  103. return
  104. fi
  105. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  106. }
  107. function backup_local_scuttlebot {
  108. if [ -d /etc/scuttlebot/.ssb ]; then
  109. systemctl stop scuttlebot
  110. function_check backup_directory_to_usb
  111. backup_directory_to_usb /etc/scuttlebot/.ssb scuttlebot
  112. systemctl start scuttlebot
  113. fi
  114. }
  115. function restore_local_scuttlebot {
  116. if [ -d /etc/scuttlebot ]; then
  117. systemctl stop scuttlebot
  118. temp_restore_dir=/root/tempscuttlebot
  119. function_check restore_directory_from_usb
  120. restore_directory_from_usb $temp_restore_dir scuttlebot
  121. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  122. systemctl start scuttlebot
  123. fi
  124. }
  125. function backup_remote_scuttlebot {
  126. if [ -d /etc/scuttlebot/.ssb ]; then
  127. systemctl stop scuttlebot
  128. function_check backup_directory_to_friend
  129. backup_directory_to_friend /etc/scuttlebot/.ssb scuttlebot
  130. systemctl start scuttlebot
  131. fi
  132. }
  133. function restore_remote_scuttlebot {
  134. if [ -d /etc/scuttlebot ]; then
  135. systemctl stop scuttlebot
  136. temp_restore_dir=/root/tempscuttlebot
  137. function_check restore_directory_from_friend
  138. restore_directory_from_friend $temp_restore_dir scuttlebot
  139. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  140. systemctl start scuttlebot
  141. fi
  142. }
  143. function remove_scuttlebot {
  144. firewall_remove ${SCUTTLEBOT_PORT}
  145. systemctl stop scuttlebot
  146. systemctl disable scuttlebot
  147. rm /etc/systemd/system/scuttlebot.service
  148. userdel -r scuttlebot
  149. if [ -d /etc/scuttlebot ]; then
  150. rm -rf /etc/scuttlebot
  151. fi
  152. remove_completion_param install_scuttlebot
  153. sed -i '/scuttlebot /d' $COMPLETION_FILE
  154. }
  155. function install_scuttlebot {
  156. function_check install_nodejs
  157. install_nodejs scuttlebot
  158. npm install -g scuttlebot@${SCUTTLEBOT_VERSION}
  159. if [ ! -f /usr/local/bin/sbot ]; then
  160. exit 528253
  161. fi
  162. if [ ! -d /etc/scuttlebot ]; then
  163. mkdir -p /etc/scuttlebot
  164. fi
  165. # an unprivileged user to run as
  166. useradd -d /etc/scuttlebot/ scuttlebot
  167. # daemon
  168. echo '[Unit]' > /etc/systemd/system/scuttlebot.service
  169. echo 'Description=Scuttlebot (messaging system)' >> /etc/systemd/system/scuttlebot.service
  170. echo 'After=syslog.target' >> /etc/systemd/system/scuttlebot.service
  171. echo 'After=network.target' >> /etc/systemd/system/scuttlebot.service
  172. echo '' >> /etc/systemd/system/scuttlebot.service
  173. echo '[Service]' >> /etc/systemd/system/scuttlebot.service
  174. echo 'Type=simple' >> /etc/systemd/system/scuttlebot.service
  175. echo 'User=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  176. echo 'Group=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  177. echo "WorkingDirectory=/etc/scuttlebot" >> /etc/systemd/system/scuttlebot.service
  178. echo 'ExecStart=/usr/local/bin/sbot server' >> /etc/systemd/system/scuttlebot.service
  179. echo 'Restart=always' >> /etc/systemd/system/scuttlebot.service
  180. echo 'Environment="USER=scuttlebot"' >> /etc/systemd/system/scuttlebot.service
  181. echo '' >> /etc/systemd/system/scuttlebot.service
  182. echo '[Install]' >> /etc/systemd/system/scuttlebot.service
  183. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/scuttlebot.service
  184. chown -R scuttlebot:scuttlebot /etc/scuttlebot
  185. # files gw_name myhostname mdns4_minimal [NOTFOUND=return] dns
  186. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  187. # start the daemon
  188. systemctl enable scuttlebot.service
  189. systemctl daemon-reload
  190. systemctl start scuttlebot.service
  191. sleep 3
  192. if [ ! -d /etc/scuttlebot/.ssb ]; then
  193. echo $'Scuttlebot config not generated'
  194. exit 73528
  195. fi
  196. echo '{' > /etc/scuttlebot/.ssb/config
  197. echo " \"host\": \"${DEFAULT_DOMAIN_NAME}\"," >> /etc/scuttlebot/.ssb/config
  198. echo " \"port\": ${SCUTTLEBOT_PORT}," >> /etc/scuttlebot/.ssb/config
  199. echo ' "timeout": 30000,' >> /etc/scuttlebot/.ssb/config
  200. echo ' "pub": true,' >> /etc/scuttlebot/.ssb/config
  201. echo ' "local": true,' >> /etc/scuttlebot/.ssb/config
  202. echo ' "friends": {' >> /etc/scuttlebot/.ssb/config
  203. echo ' "dunbar": 150,' >> /etc/scuttlebot/.ssb/config
  204. echo ' "hops": 3' >> /etc/scuttlebot/.ssb/config
  205. echo ' },' >> /etc/scuttlebot/.ssb/config
  206. echo ' "gossip": {' >> /etc/scuttlebot/.ssb/config
  207. echo ' "connections": 2' >> /etc/scuttlebot/.ssb/config
  208. echo ' },' >> /etc/scuttlebot/.ssb/config
  209. echo ' "master": [],' >> /etc/scuttlebot/.ssb/config
  210. echo ' "logging": {' >> /etc/scuttlebot/.ssb/config
  211. echo ' "level": "error"' >> /etc/scuttlebot/.ssb/config
  212. echo ' }' >> /etc/scuttlebot/.ssb/config
  213. echo '}' >> /etc/scuttlebot/.ssb/config
  214. chown scuttlebot:scuttlebot /etc/scuttlebot/.ssb/config
  215. systemctl restart scuttlebot.service
  216. firewall_add scuttlebot ${SCUTTLEBOT_PORT}
  217. if ! grep -q "scuttlebot version:" ${COMPLETION_FILE}; then
  218. echo "scuttlebot version:${SCUTTLEBOT_VERSION}" >> ${COMPLETION_FILE}
  219. else
  220. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  221. fi
  222. APP_INSTALLED=1
  223. }
  224. # NOTE: deliberately no exit 0