meshavahi 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # A script for using avahi to discover peers and update tox/ipfs
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=$PROJECT_NAME-meshavahi
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. MESH_USERNAME='fbone'
  33. MY_USERNAME=$MESH_USERNAME
  34. IPFS_PORT=4001
  35. IPFS_PATH=/usr/bin
  36. IPFS_COMMAND=$IPFS_PATH/ipfs
  37. IPFS_USERS_FILE=/tmp/.ipfs-users
  38. IPFS_PUBLIC=/home/$MY_USERNAME/.ipfs-public
  39. # contains the output of the avahi command
  40. TEMPFILE_BASE=$(mktemp /tmp/meshavahibase.XXXXXX)
  41. TEMPFILE=$(mktemp /tmp/meshavahi.XXXXXX)
  42. # List of tox users previously seen
  43. PREV_TOX_USERS_FILE=/root/.prev_tox_users
  44. function ipfs_publish {
  45. # Publishes anything within the ~/Public directory
  46. DIR_TO_CHECK=/home/$MY_USERNAME/Public
  47. if [ ! -d $DIR_TO_CHECK ]; then
  48. return
  49. fi
  50. OLD_STAT_FILE=/home/$MY_USERNAME/.old_stat.txt
  51. if [ -e $OLD_STAT_FILE ]
  52. then
  53. OLD_STAT=$(cat $OLD_STAT_FILE)
  54. else
  55. OLD_STAT="nothing"
  56. fi
  57. NEW_STAT=$(stat -t $DIR_TO_CHECK)
  58. # include some subdirectories
  59. for dir in $DIR_TO_CHECK/*/
  60. do
  61. REALLY_NEW_STAT="$NEW_STAT$(stat -t $dir)"
  62. NEW_STAT="$REALLY_NEW_STAT"
  63. for dir2 in "$dir"/*/
  64. do
  65. REALLY_NEW_STAT="$NEW_STAT$(stat -t $dir2)"
  66. NEW_STAT="$REALLY_NEW_STAT"
  67. for dir3 in "$dir2"/*/
  68. do
  69. REALLY_NEW_STAT="$NEW_STAT$(stat -t $dir3)"
  70. NEW_STAT="$REALLY_NEW_STAT"
  71. done
  72. done
  73. done
  74. if [ "$OLD_STAT" != "$NEW_STAT" ]; then
  75. su -c "echo \$($IPFS_COMMAND add -rq /home/$MY_USERNAME/Public | tail -n 1) > $IPFS_PUBLIC" - $MY_USERNAME
  76. echo "$NEW_STAT" > $OLD_STAT_FILE
  77. chown $MY_USERNAME:$MY_USERNAME $OLD_STAT_FILE
  78. fi
  79. if [ -f $IPFS_PUBLIC ]; then
  80. IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
  81. su -c "$IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID" - $MY_USERNAME
  82. fi
  83. }
  84. function ipfs_bootstrap {
  85. cat $TEMPFILE_BASE | grep "ipfs_id\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  86. state=0
  87. address=""
  88. peer=""
  89. if [ -d /home/$MY_USERNAME/Desktop ]; then
  90. echo -n '' > ${IPFS_USERS_FILE}.new
  91. fi
  92. while IFS='' read -r line || [[ -n "$line" ]]; do
  93. if [ ${state} -eq "3" ]; then
  94. if [[ $line == *"txt ="* ]]; then
  95. ipfs_txt=$(echo "$line" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}')
  96. ipfs_peer_id=$(echo "$ipfs_txt" | awk -F ':' '{print $1}')
  97. ipfs_tox_id=$(echo "$ipfs_txt" | awk -F ':' '{print $2}')
  98. su -c "$IPFS_COMMAND bootstrap add /ip4/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}" - $MY_USERNAME
  99. if [ -d /home/$MY_USERNAME/Desktop ]; then
  100. if [[ $ipfs_tox_id != 'none' ]]; then
  101. echo "$ipfs_tox_id:$ipfs_peer_id" >> ${IPFS_USERS_FILE}.new
  102. fi
  103. fi
  104. state=0
  105. fi
  106. fi
  107. if [ ${state} -eq "2" ]; then
  108. if [[ $line == *"address ="* ]]; then
  109. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  110. state=3
  111. fi
  112. fi
  113. if [ ${state} -eq "1" ]; then
  114. if [[ $line == *"hostname ="* ]]; then
  115. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  116. state=2
  117. fi
  118. fi
  119. if [[ $line == *"ipfs_id"* && $line == "= "* ]]; then
  120. state=1
  121. fi
  122. done < "$TEMPFILE"
  123. # Create a list of user sites, in alphabetical order by Tox nick
  124. if [ -d /home/$MY_USERNAME/Desktop ]; then
  125. if [ -f ${IPFS_USERS_FILE}.new ]; then
  126. sites_list=$(cat ${IPFS_USERS_FILE}.new | sort -d)
  127. echo "${sites_list}" > ${IPFS_USERS_FILE}
  128. chown $MY_USERNAME:$MY_USERNAME ${IPFS_USERS_FILE}
  129. rm ${IPFS_USERS_FILE}.new
  130. fi
  131. fi
  132. }
  133. function detect_new_tox_users {
  134. CURRENT_USERS_FILE=$1
  135. if [ ! -f $CURRENT_USERS_FILE ]; then
  136. return
  137. fi
  138. # Check that this is a GUI installation with a desktop
  139. if [ ! -d /home/$MESH_USERNAME/Desktop ]; then
  140. return
  141. fi
  142. # Produce notifications if new users appear
  143. if [ -f $PREV_TOX_USERS_FILE ]; then
  144. while IFS='' read -r line || [[ -n "$line" ]]; do
  145. if [[ $line != "Failed*" && $line != "data "* && $line != "Anon "* && $line != "anon "* && $line != "anonymous "* && $line != "Anonymous "* ]]; then
  146. if ! grep -q "$line" $PREV_TOX_USERS_FILE; then
  147. # get the nick of the user
  148. toxidstr=$(echo "$line" | awk -F ' ' '{print $(NF)}')
  149. toxuser=$(echo "$line" | sed "s| $toxidstr||g")
  150. if [ -r "/home/$MESH_USERNAME/.dbus/Xdbus" ]; then
  151. . "/home/$MESH_USERNAME/.dbus/Xdbus"
  152. fi
  153. export DISPLAY=:0.0
  154. export XAUTHORITY=/home/$MESH_USERNAME/.Xauthority
  155. sudo -u $MESH_USERNAME /usr/bin/notify-send $"$toxuser" $"has joined the mesh" --icon=dialog-information
  156. break
  157. fi
  158. fi
  159. done < "$CURRENT_USERS_FILE"
  160. fi
  161. # Store the previous tox users list
  162. cp -f $CURRENT_USERS_FILE $PREV_TOX_USERS_FILE
  163. }
  164. function detect_tox_users {
  165. # don't show the first peer field
  166. lstox | awk -F ' ' '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//' | sort -d > $TEMPFILE
  167. detect_new_tox_users $TEMPFILE
  168. }
  169. function avahi_extract_info {
  170. # Create a list of bootstrap nodes
  171. avahi-browse -atr > $TEMPFILE_BASE
  172. cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE
  173. if [ ! -f $TEMPFILE ]; then
  174. exit 1
  175. fi
  176. }
  177. function avahi_remove_info {
  178. rm -f $TEMPFILE_BASE
  179. rm -f $TEMPFILE
  180. }
  181. if [ ! -d /etc/avahi ]; then
  182. exit 0
  183. fi
  184. avahi_extract_info
  185. ipfs_bootstrap
  186. ipfs_publish
  187. detect_tox_users
  188. avahi_remove_info
  189. exit 0