freedombone-mesh-visit-site 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Visit ipfs sites by entering a username
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-mesh-visit-site
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. IPFS_URL='http://127.0.0.1:8080/ipns'
  32. # The browser application to use
  33. BROWSER=firefox
  34. BROWSER_OPTIONS='-url'
  35. # An optional suffix to be appended to the URL
  36. SUFFIX=$1
  37. IPFS_USERS_FILE=/tmp/.ipfs-users
  38. if [ ! -f $IPFS_USERS_FILE ]; then
  39. exit 0
  40. fi
  41. USERS_FILE=/tmp/Users.txt
  42. if [ ! -f $USERS_FILE ]; then
  43. exit 0
  44. fi
  45. USERS_FILE_LINES=$(wc -l $USERS_FILE | awk -F ' ' '{print $1}')
  46. if [ "$USERS_FILE_LINES" -gt 200 ]; then
  47. # If there are more than a Dunbar number of peers then ask for the peer name or ID
  48. data=$(zenity --entry --title="Visit IPFS site" --text="Enter the username or Tox ID for the site you wish to visit")
  49. sel=$?
  50. case $sel in
  51. 0)
  52. TOX_USERNAME_OR_ID="$data"
  53. if [ ${#TOX_USERNAME_OR_ID} -gt 0 ]; then
  54. if ! grep -q "$TOX_USERNAME_OR_ID" $USERS_FILE; then
  55. TOX_ID="$TOX_USERNAME_OR_ID"
  56. else
  57. TOX_ID=$(grep "$TOX_USERNAME_OR_ID" "$USERS_FILE" | head -n 1 | sed "s|$TOX_USERNAME_OR_ID ||g" | sed -e 's/^[[:space:]]*//')
  58. fi
  59. if [ ${#TOX_ID} -gt 5 ]; then
  60. if ! grep -q "$TOX_ID" $IPFS_USERS_FILE; then
  61. zenity --info --title $"Visit a site" --text $"An IPFS site was not found for the user '$TOX_USERNAME_OR_ID'" --width 500
  62. exit 3
  63. fi
  64. IPFS_FULL_URL=${IPFS_URL}/$(grep "$TOX_ID" "$IPFS_USERS_FILE" | head -n 1 | awk -F ':' '{print $2}')
  65. pkill $BROWSER
  66. setsid sh -c "$BROWSER $BROWSER_OPTIONS $IPFS_FULL_URL$SUFFIX" > /dev/null 2>&1 < /dev/null &
  67. # Need to wait a while for the browser to begin opening
  68. sleep 3
  69. fi
  70. else
  71. exit 1
  72. fi
  73. ;;
  74. esac
  75. else
  76. # If there are a relatively small number of users then choose from a list
  77. TOX_ID=$(
  78. # shellcheck disable=SC2002
  79. cat "$USERS_FILE" | \
  80. awk -F ' ' '{
  81. for(i=1;i<=NF;i++){
  82. print $i;
  83. }
  84. }' | \
  85. zenity --list \
  86. --title='Visit the site of another user' \
  87. --column='Username' --column='Tox ID' \
  88. --print-column=2 --hide-column=2 --width=300 --height=400)
  89. if [ ! "$TOX_ID" ]; then
  90. exit 0
  91. fi
  92. IPFS_FULL_URL=${IPFS_URL}/$(grep "$TOX_ID" "$IPFS_USERS_FILE" | head -n 1 | awk -F ':' '{print $2}')
  93. pkill $BROWSER
  94. setsid sh -c "$BROWSER $BROWSER_OPTIONS $IPFS_FULL_URL$SUFFIX" > /dev/null 2>&1 < /dev/null &
  95. # Need to wait a while for the browser to begin opening
  96. sleep 3
  97. fi
  98. exit 0