6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 02:31:08 +00:00
prologic-saltyim/bin/salty-chat.sh
xuu 7261dd085c feat: add CNAME Discovery to script implementation (#44)
implements https://git.mills.io/prologic/salty.im/pulls/9

Co-authored-by: Jon Lundy <jon@xuu.cc>
Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/44
Co-authored-by: xuu <xuu@noreply@mills.io>
Co-committed-by: xuu <xuu@noreply@mills.io>
2022-03-22 22:59:45 +00:00

325 lines
6.6 KiB
Bash
Executable File

#!/bin/sh
set -e
# Validate environment
if ! command -v jq > /dev/null; then
printf "missing jq command. Please install it via your system's package manager\n"
exit 1
fi
if ! command -v msgbus > /dev/null; then
printf "missing msgbus command. Use: go install git.mills.io/prologic/msgbus/cmd/msgbus@master\n"
exit 1
fi
for cmd in salty salty-keygen; do
if ! command -v $cmd > /dev/null; then
printf "missing %s command. Use go install go.mills.io/salty/cmd/$cmd@master\n" "$cmd"
exit 1
fi
done
if [ -n "$XDG_CONFIG_HOME" ]; then
data_path="$XDG_CONFIG_HOME/salty"
else
data_path="$HOME/.config/salty"
fi
if [ -z "$SALTY_IDENTITY" ]; then
export SALTY_IDENTITY="$data_path/$USER.key"
fi
clear_line() {
printf '\r\033[2K'
}
move_cursor_up() {
printf '\033[1A'
}
get_user () {
user=$(grep user: "$SALTY_IDENTITY" | awk '{print $3}')
if [ -z "$user" ]; then
user="$USER"
fi
echo "$user"
}
format_message() {
me="$1"
message="$2"
printf "%s\t(%s)\t%s" "$(date -u +%FT%TZ)" "$me" "$message"
}
stream () {
if [ -z "$SALTY_IDENTITY" ]; then
echo "SALTY_IDENTITY not set"
return 2
fi
sig=$(mktemp /tmp/salty.XXXXXX)
msg="$(jq -r '.payload' | base64 -d | salty -i "$SALTY_IDENTITY" -d 2> "$sig")"
if [ -n "$SALTY_CHATWITH" ]; then
if ! echo "$msg" | grep -q "\t($SALTY_CHATWITH)" > /dev/null; then
exit
fi
fi
echo '\007'
if [ -n "$SALTY_CHATKEY" ]; then
if ! grep -q "$SALTY_CHATKEY" "$sig" > /dev/null; then
echo "[warning key does not match!] $(cat "$sig") != $SALTY_CHATKEY"
fi
fi
rm "$sig"
move_cursor_up
clear_line
echo "$msg" # | lextwt-cli -salty | jq -r '[.created,.twter.nick,.text]|@csv'
printf ">"
}
lookup () {
if [ $# -lt 1 ]; then
printf "lookup takes 1 arugment %d given\n" "$#"
printf "Try %s lookup nick@domain\n" "$(basename "$0")"
return 1
fi
user="$1"
nick="$(echo "$user" | awk -F@ '{ print $1 }')"
domain="$(echo "$user" | awk -F@ '{ print $2 }')"
hash="$(printf "%s" "$user" | sha256sum | cut -f 1 -d ' ')"
discovery_host="$(dig +short SRV _salty._tcp."$domain" | cut -f 4 -d' ')"
if [ -z "$discovery_host" ]; then
discovery_host="$domain"
fi
info=$(mktemp /tmp/salty.XXXXXX)
if ! curl -qfsSL "https://$discovery_host/.well-known/salty/${hash}.json" > "$info" 2> /dev/null; then
if ! curl -qfsSL "https://$discovery_host/.well-known/salty/${nick}.json" > "$info"; then
rm "$info"
echo "error: lookup failed"
return 1
fi
fi
cat "$info"
rm "$info"
}
readmsgs () {
topic="$1"
if [ -z "$topic" ]; then
me="$(get_user)"
topic="$(echo "$me" | awk -F@ '{ print $1 }')"
fi
export SALTY_IDENTITY="$data_path/$topic.key"
if [ ! -f "$SALTY_IDENTITY" ]; then
echo "identity file missing for user $topic" >&2
return 1
fi
msgbus sub "$topic" "$0"
}
sendmsg () {
if [ $# -lt 2 ]; then
printf "sendmsg requires 2 arguments %d provided\n" "$#"
printf "Try %s send nick@domain message\n" "$(basename "$0")"
return 1
fi
if [ -z "$SALTY_IDENTITY" ]; then
echo "SALTY_IDENTITY not set"
return 2
fi
user="$1"
message="$2"
if [ -z "$message" ]; then
echo "error: empty message"
exit 2
fi
salty_json="$(mktemp /tmp/salty.XXXXXX)"
lookup "$user" > "$salty_json"
endpoint="$(jq -r '.endpoint' < "$salty_json")"
key="$(jq -r '.key' < "$salty_json")"
# XXX: Deprecated as of Spec v1.1
# XXX: There is only an Endpoint
if topic="$(jq -e -r '.topic' < "$salty_json")"; then
endpoint="$endpoint/$topic"
fi
rm "$salty_json"
me="$(get_user)"
format_message "$me" "$message" \
| salty -i "$SALTY_IDENTITY" -r "$key" \
| curl -fqsS --data-binary @- "$endpoint"
}
chatwith() {
if [ $# -lt 1 ]; then
printf "chatwith requires 1 arguments %d provided\n" "$#"
printf "Try %s chat nick@domain\n" "$(basename "$0")"
return 1
fi
if [ -z "$SALTY_IDENTITY" ]; then
echo "SALTY_IDENTITY not set"
return 2
fi
user="$1"
salty_json="$(mktemp /tmp/salty.XXXXXX)"
lookup "$user" > "$salty_json"
endpoint="$(jq -r '.endpoint' < "$salty_json")"
key="$(jq -r '.key' < "$salty_json")"
# XXX: Deprecated as of Spec v1.1
# XXX: There is only an Endpoint
if topic="$(jq -e -r '.topic' < "$salty_json")"; then
endpoint="$endpoint/$topic"
fi
rm "$salty_json"
me="$(get_user)"
nick="$(echo "$me" | awk -F@ '{ print $1 }')"
domain="$(echo "$me" | awk -F@ '{ print $2 }')"
export SALTY_CHATWITH="$user"
export SALTY_CHATKEY="$key"
echo chat with "$SALTY_CHATWITH" via "$endpoint" key "$key"
readmsgs "$nick" &
READ_PID=$$
trap 'kill $READ_PID' EXIT
while true; do
printf "> "
read -r message
clear_line
if [ -z "$message" ]; then
continue
fi
format_message "$me" "$message" \
| salty -i "$SALTY_IDENTITY" -r "$key" \
| curl -fqsS --data-binary @- "$endpoint"
move_cursor_up
clear_line
format_message "$me" "$message"
echo
done
}
make_user () {
mkdir -p "$data_path"
if [ $# -lt 1 ]; then
user=$USER
else
user=$1
fi
identity_file="$data_path/$user.key"
if [ -f "$identity_file" ]; then
printf "user key already exists!"
return 1
fi
# Check for msgbus env.. probably can make it fallback to looking for a config file?
if [ -z "$MSGBUS_URI" ]; then
printf "missing MSGBUS_URI in environment"
return 1
fi
salty-keygen -o "$identity_file"
echo "# user: $user" >> "$identity_file"
pubkey=$(grep key: "$identity_file" | awk '{print $4}')
hash="$(printf "%s" "$user" | sha256sum | cut -f 1 -d ' ')"
cat <<- EOF
Create this file in your webserver well-known folder. https://hostname.tld/.well-known/salty/$hash.json
{
"endpoint": "$MSGBUS_URI/$user",
"key": "$pubkey"
}
EOF
}
show_help() {
printf "Usage: %s [options] <command> [arguments]\n" "$(basename "$0")"
printf "\n"
printf "Options:\n"
printf " -h/--help Show this help"
printf "\n"
printf "\n"
printf "Commands:\n"
printf " help -- Display this help message\n"
printf " chat -- Chat with a user by nick@domain\n"
printf " lookup -- Lookup a user by nick@domain\n"
printf " make-user -- Generate a new user key pair\n"
printf " read -- Reads your messages\n"
printf " send -- Sends a message to nick@domain\n"
}
# check if streaming
if [ ! -t 1 ]; then
stream
exit 0
fi
# Show Help
if [ $# -lt 1 ]; then
show_help
exit 1
fi
CMD=$1
shift
case $CMD in
-h|--help|help)
show_help
;;
chat)
chatwith "$@"
;;
send)
sendmsg "$@"
;;
read)
readmsgs "$@"
;;
lookup)
lookup "$@"
;;
make-user)
make_user "$@"
;;
esac