freedombone-mesh-visit-site 3.6KB

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