Skip to content
Snippets Groups Projects
meshavahi 6.94 KiB
Newer Older
#!/bin/bash
Bob Mottram's avatar
Bob Mottram committed
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
Bob Mottram's avatar
Bob Mottram committed
#                              Freedom in the Cloud
# A script for using avahi to discover peers and update tox/ipfs
Bob Mottram's avatar
Bob Mottram committed
#
# License
# =======
#
Bob Mottram's avatar
Bob Mottram committed
# Copyright (C) 2015-2019 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
Bob Mottram's avatar
Bob Mottram committed
# it under the terms of the GNU Affero General Public License as published by
# 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
Bob Mottram's avatar
Bob Mottram committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
Bob Mottram's avatar
Bob Mottram committed
# 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/>.
Bob Mottram's avatar
Bob Mottram committed
PROJECT_NAME='freedombone'

export TEXTDOMAIN=$PROJECT_NAME-meshavahi
Bob Mottram's avatar
Bob Mottram committed
export TEXTDOMAINDIR="/usr/share/locale"

Bob Mottram's avatar
Bob Mottram committed
MESH_USERNAME='fbone'
Bob Mottram's avatar
Bob Mottram committed
MY_USERNAME=$MESH_USERNAME
Bob Mottram's avatar
Bob Mottram committed
IPFS_PORT=4001
IPFS_PATH=/usr/bin
IPFS_COMMAND=$IPFS_PATH/ipfs
Bob Mottram's avatar
Bob Mottram committed
IPFS_USERS_FILE=/tmp/.ipfs-users
Bob Mottram's avatar
Bob Mottram committed
IPFS_PUBLIC=/home/$MY_USERNAME/.ipfs-public
Bob Mottram's avatar
Bob Mottram committed

# contains the output of the avahi command
TEMPFILE_BASE=$(mktemp /tmp/meshavahibase.XXXXXX)
TEMPFILE=$(mktemp /tmp/meshavahi.XXXXXX)
# List of tox users previously seen
PREV_TOX_USERS_FILE=/root/.prev_tox_users

Bob Mottram's avatar
Bob Mottram committed
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)

    # include some subdirectories
    for dir in $DIR_TO_CHECK/*/
    do
Bob Mottram's avatar
Bob Mottram committed
        REALLY_NEW_STAT="$NEW_STAT$(stat -t "$dir")"
        NEW_STAT="$REALLY_NEW_STAT"

        for dir2 in "$dir"/*/
        do
Bob Mottram's avatar
Bob Mottram committed
            REALLY_NEW_STAT="$NEW_STAT$(stat -t "$dir2")"
            NEW_STAT="$REALLY_NEW_STAT"

            for dir3 in "$dir2"/*/
            do
Bob Mottram's avatar
Bob Mottram committed
                REALLY_NEW_STAT="$NEW_STAT$(stat -t "$dir3")"
                NEW_STAT="$REALLY_NEW_STAT"
            done
        done
    done

Bob Mottram's avatar
Bob Mottram committed
    if [ "$OLD_STAT" != "$NEW_STAT" ]; then
        su -c "echo \$($IPFS_COMMAND add -rq /home/$MY_USERNAME/Public | tail -n 1) > $IPFS_PUBLIC" - $MY_USERNAME
Bob Mottram's avatar
Bob Mottram committed
        echo "$NEW_STAT" > $OLD_STAT_FILE
        chown $MY_USERNAME:$MY_USERNAME $OLD_STAT_FILE
Bob Mottram's avatar
Bob Mottram committed
    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
}

function ipfs_bootstrap {
    # TODO switch to ipv6
Bob Mottram's avatar
Bob Mottram committed
    grep "ipfs_id\\|hostname =\\|address =\\|port =\\|txt =" "$TEMPFILE_BASE" > "$TEMPFILE"
Bob Mottram's avatar
Bob Mottram committed
    if [ -d /home/$MY_USERNAME/Desktop ]; then
        echo -n '' > ${IPFS_USERS_FILE}.new
    fi
    while IFS='' read -r line || [[ -n "$line" ]]; do
        if [ ${state} -eq "3" ]; then
            if [[ $line == *"txt ="* ]]; then
Bob Mottram's avatar
Bob Mottram committed
                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}')
                su -c "$IPFS_COMMAND bootstrap add /ip6/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}" - $MY_USERNAME
Bob Mottram's avatar
Bob Mottram committed
                if [ -d /home/$MY_USERNAME/Desktop ]; then
                    if [[ $ipfs_tox_id != 'none' ]]; then
                        echo "$ipfs_tox_id:$ipfs_peer_id" >> ${IPFS_USERS_FILE}.new
                    fi
Bob Mottram's avatar
Bob Mottram committed
                fi
                state=0
            fi
        fi
        if [ ${state} -eq "2" ]; then
            if [[ $line == *"address ="* ]]; then
Bob Mottram's avatar
Bob Mottram committed
                address=$(echo "$line" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
                state=3
            fi
        fi
        if [ ${state} -eq "1" ]; then
            if [[ $line == *"hostname ="* ]]; then
Bob Mottram's avatar
Bob Mottram committed
                peer=$(echo "$line" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
Bob Mottram's avatar
Bob Mottram committed
        if [[ $line == *"ipfs_id"* && $line == "= "* ]]; then
            state=1
        fi
    done < "$TEMPFILE"
Bob Mottram's avatar
Bob Mottram committed

    # 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
Bob Mottram's avatar
Bob Mottram committed
            sites_list=$(sort -d < "${IPFS_USERS_FILE}.new")
Bob Mottram's avatar
Bob Mottram committed
            echo "${sites_list}" > ${IPFS_USERS_FILE}
            chown $MY_USERNAME:$MY_USERNAME ${IPFS_USERS_FILE}
            rm ${IPFS_USERS_FILE}.new
        fi
    fi
}

function detect_new_tox_users {
    CURRENT_USERS_FILE=$1

Bob Mottram's avatar
Bob Mottram committed
    if [ ! -f "$CURRENT_USERS_FILE" ]; then
        return
    fi

    # Check that this is a GUI installation with a desktop
Bob Mottram's avatar
Bob Mottram committed
    if [ ! -d /home/$MESH_USERNAME/Desktop ]; then
        return
    fi

    # Produce notifications if new users appear
    if [ -f $PREV_TOX_USERS_FILE ]; then
        while IFS='' read -r line || [[ -n "$line" ]]; do
            if [[ $line != "Failed*" && $line != "data "* && $line != "Anon "* && $line != "anon "* && $line != "anonymous "* && $line != "Anonymous "* ]]; then
Bob Mottram's avatar
Bob Mottram committed
                if ! grep -q "$line" $PREV_TOX_USERS_FILE; then
                    # get the nick of the user
Bob Mottram's avatar
Bob Mottram committed
                    toxidstr=$(awk -F ' ' '{print $(NF)}' < "$line")
                    toxuser=$(sed "s| $toxidstr||g" < "$line")
                    if [ -r "/home/$MESH_USERNAME/.dbus/Xdbus" ]; then
Bob Mottram's avatar
Bob Mottram committed
                        # shellcheck disable=SC1090
                        . "/home/$MESH_USERNAME/.dbus/Xdbus"
                    fi
                    export DISPLAY=:0.0
                    export XAUTHORITY=/home/$MESH_USERNAME/.Xauthority
Bob Mottram's avatar
Bob Mottram committed
                    sudo -u $MESH_USERNAME /usr/bin/notify-send $"$toxuser" $"has joined the mesh" --icon=dialog-information
            fi
        done < "$CURRENT_USERS_FILE"
    fi

    # Store the previous tox users list
Bob Mottram's avatar
Bob Mottram committed
    cp -f "$CURRENT_USERS_FILE" "$PREV_TOX_USERS_FILE"
}

function detect_tox_users {
    # don't show the first peer field
Bob Mottram's avatar
Bob Mottram committed
    lstox | awk -F ' ' '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//' | sort -d > "$TEMPFILE"
Bob Mottram's avatar
Bob Mottram committed
    detect_new_tox_users "$TEMPFILE"
function avahi_extract_info {
    # Create a list of bootstrap nodes
Bob Mottram's avatar
Bob Mottram committed
    avahi-browse -atr > "$TEMPFILE_BASE"
    grep "hostname =\\|address =\\|port =" "$TEMPFILE_BASE" > "$TEMPFILE"
    if [ ! -f "$TEMPFILE" ]; then
function avahi_remove_info {
Bob Mottram's avatar
Bob Mottram committed
    rm -f "$TEMPFILE_BASE"
    rm -f "$TEMPFILE"
if [ ! -d /etc/avahi ]; then
avahi_extract_info
ipfs_bootstrap
Bob Mottram's avatar
Bob Mottram committed
ipfs_publish
Bob Mottram's avatar
Bob Mottram committed
detect_tox_users
avahi_remove_info