Skip to content

Instantly share code, notes, and snippets.

@SgtPooki
Last active February 7, 2022 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SgtPooki/7ade0dcb9d4273209ebcce2dcbdde1a1 to your computer and use it in GitHub Desktop.
Save SgtPooki/7ade0dcb9d4273209ebcce2dcbdde1a1 to your computer and use it in GitHub Desktop.
Simple terminal chat app using experimental ipfs
#
##
### PUBLIC INTERFACE
##
#
# Set up ipfs-chat
function chat-setup() {
# Create folders
mkdir -p $(chat-prefix)
mkfifo $(chat-paths-cmd-fifo)
}
# Start the ipfs-chat terminal "app"
function chat-start() {
# set up stdin > cmd fifo redirection
}
function chat() {
! (($#)) && return 1
local MSG="${USER}: $@"
ipfs pubsub pub $(chat-channel-current) <<<"$MSG"
}
function chat-prefix() {
echo ~/ipfs-chat
}
# Join a particular channel/topic
function chat-join() {
local CHANNEL="$1" && shift
local CHANNEL_SUB_PID
local CHANNEL_FIFO
local CHANNEL_LOG
local CHANNEL_LOG_PID
CHANNEL_FIFO=$(chat-util-mkfifo $(chat-paths-channel-fifo $CHANNEL))
CHANNEL_LOG="$(chat-paths-channel-log $CHANNEL)"
# create fifo for receiving messges from pubsub
# subscribe to channel send messages to previously created fifo
ipfs pubsub sub $CHANNEL > $(chat-paths-channel-log $CHANNEL)
# save PID of subscription process
CHANNEL_SUB_PID="$!"
chat-pid-channel-save "$CHANNEL" "$CHANNEL_SUB_PID"
# listen to fifo and output to channel log (in the background)
tail -f $CHANNEL_FIFO >> $CHANNEL_LOG &
# save PID of logging process
CHANNEL_LOG_PID="$!"
chat-pid-channel-save "$CHANNEL" "$CHANNEL_LOG_PID"
}
# List all channels (as well as a count of peers currently subscribed to that channel)
function chat-ls() {
local PEER_COUNT=0
for CHANNEL in $(ipfs pubsub ls); do
PEER_COUNT=$(ipfs pubsub peers $CHANNEL | wc -l | awk '{print $1}')
echo "$CHANNEL (Peers: $PEER_COUNT)"
PEER_COUNT=0
done
}
# Switch the chat's view to a particular channel/topic.
function chat-switch() {
local CHANNEL="$1" && shift
# set particular channel as active
echo $CHANNEL > $(chat-paths-channel-active)
chat-view-update
}
# Unsubscribe from a particular channel/topic
function chat-leave() {
local CHANNEL="$1" && shift
chat-pid-kill "$(chat-channel-pid $CHANNEL)"
}
#
##
### PRIVATE INTERFACE
##
#
#
## chat-pid: Process commands
#
function chat-pid-kill() {
local PID_FILE_PATH="$1" && shift
local PID
PID=$(cat $PID_FILE_PATH)
kill $PID
}
function chat-pid-channel-save() {
local CHANNEL="$1" && shift
local CHANNEL_PID="$1" && shift
echo "$CHANNEL_PID" >> $(chat-paths-channel-pids $CHANNEL)
chat-switch $CHANNEL
}
function chat-pid-channel-kill() {
local CHANNEL="$1" && shift
for PID in $(cat $(chat-paths-channel-pids $CHANNEL)); do
kill $PID
done
}
# # The pid of the ipfs subscription for the requested channel
# function chat-pid-channel() {
# local CHANNEL="$1" && shift
# cat $(chat-paths-channel-pids $CHANNEL)
# }
# the pid of
function chat-pid-cmd() {
}
#
## chat-fifo: Fifo functions
#
#
## chat-paths: Pathing functions
#
# chat-paths-channel
function chat-paths-channel() {
local CHAT_CHANNEL_PATH="$(chat-prefix)/$1"
mkdir -p $CHAT_CHANNEL_PATH
echo $CHAT_CHANNEL_PATH
}
function chat-paths-channel-active() {
echo $(chat-prefix)/active-channel
}
function chat-paths-channel-fifo() {
local CHANNEL="$1" && shift
local CHANNEL_FIFO="$(chat-paths-channel $CHANNEL)/fifo"
echo $CHANNEL_FIFO
# chat-util-mkfifo $CHANNEL_FIFO
}
function chat-paths-channel-index() {
echo $(chat-prefix)/active-channel
}
function chat-paths-channel-log() {
local CHANNEL="$1" && shift
echo $(chat-paths-channel $CHANNEL)/chat.log
}
# path to the PID for the process of the ipfs sub for the requested channel
function chat-paths-channel-pids() {
local CHANNEL="$1" && shift
echo $(chat-paths-channel $CHANNEL)/pids
}
# chat-paths-cmd
# The path to the fifo that receives all command inputs
function chat-paths-cmd-fifo() {
echo $(chat-prefix)/cmd-fifo
}
#
## chat-fifo: Fifo functions
#
#
## chat-channel: Channel functions
#
function chat-channel-current() {
cat $(chat-paths-channel-active)
}
#
## chat-view: View/Rendering functions
#
function chat-view-update() {
echo "TBD..."
}
#
## chat-util: Utility and miscellaneous functions
#
function chat-util-mkfifo() {
local FIFO_PATH="$1" && shift
ifft $FIFO_PATH || mkfifo $FIFO_PATH
echo $FIFO_PATH
}
function chat-util-clear-previous-line() {
echo -e '\e[1A\e[K'
}
# if file then (if f t => ifft)
function ifft() {
local FILE="$1" && shift
test -e $FILE && {
if (($#)); then
$@
fi
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment