2015-08-29 17:26:51 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# .---. . .
|
|
|
|
# | | |
|
|
|
|
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
|
|
|
|
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
|
|
|
|
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
|
|
|
|
#
|
|
|
|
# Freedom in the Cloud
|
|
|
|
#
|
2016-09-11 14:33:25 +02:00
|
|
|
# A script for using avahi to discover peers and update tox/ipfs
|
2015-09-03 12:21:39 +02:00
|
|
|
|
2015-08-29 17:26:51 +02:00
|
|
|
# License
|
|
|
|
# =======
|
|
|
|
#
|
2016-10-31 17:24:49 +01:00
|
|
|
# Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
|
2015-08-29 17:26:51 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2016-02-13 23:09:27 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2015-08-29 17:26:51 +02:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-02-13 23:09:27 +01:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
2015-08-29 17:26:51 +02:00
|
|
|
#
|
2016-02-13 23:09:27 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-08-29 17:26:51 +02:00
|
|
|
|
2015-11-27 12:42:16 +01:00
|
|
|
PROJECT_NAME='freedombone'
|
|
|
|
|
2016-09-11 14:33:25 +02:00
|
|
|
export TEXTDOMAIN=$PROJECT_NAME-meshavahi
|
2015-11-27 12:42:16 +01:00
|
|
|
export TEXTDOMAINDIR="/usr/share/locale"
|
|
|
|
|
2016-08-03 16:36:59 +02:00
|
|
|
MESH_USERNAME='fbone'
|
2016-08-03 23:05:41 +02:00
|
|
|
MY_USERNAME=$MESH_USERNAME
|
2015-08-29 17:26:51 +02:00
|
|
|
|
2015-09-30 21:46:18 +02:00
|
|
|
IPFS_PORT=4001
|
2016-08-14 23:04:34 +02:00
|
|
|
IPFS_PATH=/usr/bin
|
|
|
|
IPFS_COMMAND=$IPFS_PATH/ipfs
|
2016-09-12 16:21:03 +02:00
|
|
|
IPFS_USERS_FILE=/tmp/.ipfs-users
|
2016-09-04 17:12:12 +02:00
|
|
|
IPFS_PUBLIC=/home/$MY_USERNAME/.ipfs-public
|
2015-09-30 21:46:18 +02:00
|
|
|
|
2016-07-16 14:52:04 +02:00
|
|
|
# contains the output of the avahi command
|
2016-09-11 14:33:25 +02:00
|
|
|
TEMPFILE_BASE=$(mktemp /tmp/meshavahibase.XXXXXX)
|
|
|
|
TEMPFILE=$(mktemp /tmp/meshavahi.XXXXXX)
|
2016-07-16 14:52:04 +02:00
|
|
|
|
2016-08-03 16:31:49 +02:00
|
|
|
# List of tox users previously seen
|
|
|
|
PREV_TOX_USERS_FILE=/root/.prev_tox_users
|
|
|
|
|
2016-09-04 17:12:12 +02:00
|
|
|
function ipfs_publish {
|
|
|
|
# Publishes anything within the ~/Public directory
|
|
|
|
|
|
|
|
DIR_TO_CHECK=/home/$MY_USERNAME/Public
|
|
|
|
if [ ! -d $DIR_TO_CHECK ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
OLD_STAT_FILE=/home/$MY_USERNAME/.old_stat.txt
|
|
|
|
|
|
|
|
if [ -e $OLD_STAT_FILE ]
|
|
|
|
then
|
|
|
|
OLD_STAT=$(cat $OLD_STAT_FILE)
|
|
|
|
else
|
|
|
|
OLD_STAT="nothing"
|
|
|
|
fi
|
|
|
|
|
|
|
|
NEW_STAT=$(stat -t $DIR_TO_CHECK)
|
|
|
|
|
|
|
|
if [ "$OLD_STAT" != "$NEW_STAT" ]; then
|
|
|
|
su -c "echo \$($IPFS_COMMAND add -rq /home/$MY_USERNAME/Public | tail -n 1) > $IPFS_PUBLIC" - $MY_USERNAME
|
2016-09-09 00:18:01 +02:00
|
|
|
echo "$NEW_STAT" > $OLD_STAT_FILE
|
|
|
|
chown $MY_USERNAME:$MY_USERNAME $OLD_STAT_FILE
|
2016-09-04 17:12:12 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f $IPFS_PUBLIC ]; then
|
|
|
|
IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
|
|
|
|
su -c "$IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID" - $MY_USERNAME
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-07-16 14:52:04 +02:00
|
|
|
function ipfs_bootstrap {
|
2016-09-07 23:47:01 +02:00
|
|
|
cat $TEMPFILE_BASE | grep "ipfs_id\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
|
2016-08-03 16:31:49 +02:00
|
|
|
|
|
|
|
state=0
|
|
|
|
address=""
|
|
|
|
peer=""
|
2016-09-04 17:12:12 +02:00
|
|
|
if [ -d /home/$MY_USERNAME/Desktop ]; then
|
|
|
|
echo -n '' > ${IPFS_USERS_FILE}.new
|
|
|
|
fi
|
2016-08-03 16:31:49 +02:00
|
|
|
while IFS='' read -r line || [[ -n "$line" ]]; do
|
|
|
|
if [ ${state} -eq "3" ]; then
|
|
|
|
if [[ $line == *"txt ="* ]]; then
|
2016-09-04 11:55:24 +02:00
|
|
|
ipfs_txt=$(echo "$line" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}')
|
|
|
|
ipfs_peer_id=$(echo "$ipfs_txt" | awk -F ':' '{print $1}')
|
|
|
|
ipfs_tox_id=$(echo "$ipfs_txt" | awk -F ':' '{print $2}')
|
2016-09-08 11:31:05 +02:00
|
|
|
su -c "$IPFS_COMMAND bootstrap add /ip4/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}" - $MY_USERNAME
|
2016-09-04 17:12:12 +02:00
|
|
|
if [ -d /home/$MY_USERNAME/Desktop ]; then
|
2016-09-07 23:30:12 +02:00
|
|
|
if [[ $ipfs_tox_id != 'none' ]]; then
|
|
|
|
echo "$ipfs_tox_id:$ipfs_peer_id" >> ${IPFS_USERS_FILE}.new
|
|
|
|
fi
|
2016-09-04 17:12:12 +02:00
|
|
|
fi
|
2016-08-03 16:31:49 +02:00
|
|
|
state=0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ ${state} -eq "2" ]; then
|
|
|
|
if [[ $line == *"address ="* ]]; then
|
|
|
|
address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
|
|
|
|
state=3
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ ${state} -eq "1" ]; then
|
|
|
|
if [[ $line == *"hostname ="* ]]; then
|
|
|
|
peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
|
|
|
|
state=2
|
|
|
|
fi
|
|
|
|
fi
|
2016-09-08 11:02:02 +02:00
|
|
|
if [[ $line == *"ipfs_id"* && $line == "= "* ]]; then
|
2016-08-03 16:31:49 +02:00
|
|
|
state=1
|
|
|
|
fi
|
|
|
|
done < "$TEMPFILE"
|
2016-09-04 17:12:12 +02:00
|
|
|
|
|
|
|
# Create a list of user sites, in alphabetical order by Tox nick
|
|
|
|
if [ -d /home/$MY_USERNAME/Desktop ]; then
|
|
|
|
if [ -f ${IPFS_USERS_FILE}.new ]; then
|
|
|
|
sites_list=$(cat ${IPFS_USERS_FILE}.new | sort -d)
|
|
|
|
echo "${sites_list}" > ${IPFS_USERS_FILE}
|
|
|
|
chown $MY_USERNAME:$MY_USERNAME ${IPFS_USERS_FILE}
|
|
|
|
rm ${IPFS_USERS_FILE}.new
|
|
|
|
fi
|
|
|
|
fi
|
2016-08-03 16:31:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function detect_new_tox_users {
|
|
|
|
CURRENT_USERS_FILE=$1
|
|
|
|
|
|
|
|
if [ ! -f $CURRENT_USERS_FILE ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check that this is a GUI installation with a desktop
|
2016-08-03 16:36:59 +02:00
|
|
|
if [ ! -d /home/$MESH_USERNAME/Desktop ]; then
|
2016-08-03 16:31:49 +02:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Produce notifications if new users appear
|
|
|
|
if [ -f $PREV_TOX_USERS_FILE ]; then
|
|
|
|
while IFS='' read -r line || [[ -n "$line" ]]; do
|
2016-09-11 14:30:09 +02:00
|
|
|
if [[ $line != "Failed*" && $line != "data "* && $line != "Anon "* && $line != "anon "* ]]; then
|
2016-08-03 16:36:59 +02:00
|
|
|
if ! grep -q "$line" $PREV_TOX_USERS_FILE; then
|
|
|
|
# get the nick of the user
|
2016-09-12 01:15:54 +02:00
|
|
|
toxidstr=$(echo "$line" | awk -F ' ' '{print $(NF)}')
|
2016-09-12 01:13:36 +02:00
|
|
|
toxuser=$(echo "$line" | sed "s| $toxidstr||g")
|
2016-08-03 16:31:49 +02:00
|
|
|
|
2016-08-05 10:25:01 +02:00
|
|
|
if [ -r "/home/$MESH_USERNAME/.dbus/Xdbus" ]; then
|
|
|
|
. "/home/$MESH_USERNAME/.dbus/Xdbus"
|
|
|
|
fi
|
|
|
|
export DISPLAY=:0.0
|
|
|
|
export XAUTHORITY=/home/$MESH_USERNAME/.Xauthority
|
2016-08-07 23:46:51 +02:00
|
|
|
sudo -u $MESH_USERNAME /usr/bin/notify-send $"$toxuser" $"has joined the mesh" --icon=dialog-information
|
2016-08-04 10:08:15 +02:00
|
|
|
break
|
2016-08-03 16:36:59 +02:00
|
|
|
fi
|
2016-08-03 16:31:49 +02:00
|
|
|
fi
|
|
|
|
done < "$CURRENT_USERS_FILE"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Store the previous tox users list
|
|
|
|
cp -f $CURRENT_USERS_FILE $PREV_TOX_USERS_FILE
|
2016-07-16 14:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function detect_tox_users {
|
2016-08-03 16:31:49 +02:00
|
|
|
# don't show the first peer field
|
|
|
|
lstox | awk -F ' ' '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//' | sort -d > $TEMPFILE
|
|
|
|
|
|
|
|
detect_new_tox_users $TEMPFILE
|
2015-09-03 12:21:39 +02:00
|
|
|
}
|
2015-09-03 10:18:26 +02:00
|
|
|
|
2016-07-16 14:52:04 +02:00
|
|
|
function avahi_extract_info {
|
2016-08-03 16:31:49 +02:00
|
|
|
# Create a list of bootstrap nodes
|
|
|
|
avahi-browse -atr > $TEMPFILE_BASE
|
|
|
|
cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE
|
|
|
|
if [ ! -f $TEMPFILE ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-07-16 14:52:04 +02:00
|
|
|
}
|
2015-09-02 23:50:45 +02:00
|
|
|
|
2016-07-16 14:52:04 +02:00
|
|
|
function avahi_remove_info {
|
2016-08-03 16:31:49 +02:00
|
|
|
rm -f $TEMPFILE_BASE
|
|
|
|
rm -f $TEMPFILE
|
2016-07-16 14:52:04 +02:00
|
|
|
}
|
2015-09-04 22:28:11 +02:00
|
|
|
|
2016-07-16 14:52:04 +02:00
|
|
|
if [ ! -d /etc/avahi ]; then
|
2016-08-03 16:31:49 +02:00
|
|
|
exit 0
|
2015-08-31 13:20:57 +02:00
|
|
|
fi
|
2015-08-29 17:26:51 +02:00
|
|
|
|
2016-07-16 14:52:04 +02:00
|
|
|
avahi_extract_info
|
|
|
|
ipfs_bootstrap
|
2016-09-04 17:12:12 +02:00
|
|
|
ipfs_publish
|
2016-09-11 14:30:09 +02:00
|
|
|
detect_tox_users
|
2016-07-16 14:52:04 +02:00
|
|
|
avahi_remove_info
|
|
|
|
|
2015-08-29 17:26:51 +02:00
|
|
|
exit 0
|