repo: janus-extras
action: blob
revision: 
path_from: janus-presence
revision_from: refs/heads/master:
path_to: 
revision_to: 
git.thebackupbox.net
janus-extras
git clone git://git.thebackupbox.net/janus-extras

blob of:

janus-extras

/ janus-presence

blob_plain of this file

refs/heads/master:/janus-presence

 #!/usr/bin/env bash

 logger -p crit "started janus-presence"

 ### first thing janusweb sends is:
 # {"method":"logon","data":{"userId":"UgliestZebra524","version":"janusweb-1.2","roomId":"fc7ad5d16c6a365557175c3518d5f856"}}
 # {"method":"logon","data":{"userId":"DrabCow704","version":"janusweb-1.2","roomId":"261c7138091e51e30ecd7c21df42fd9b"}}
 ## this one is using just the URL: https://thebackupbox.net/~epoch/janus/

 ### https://github.com/janusvr/janus-server/blob/master/API%20Documentation.md

 ### in addition to keeping a loop handling data from the client, I need to be sending out data to everyone
 ### about everything everyone else is doing. might just make this use IRC behind the scenes for lulz. heh.
 printf '{"method":"user_chat","data":{"message":{"data":"%s"},"userId":"void"}}\n' "welcome to epoch's janus presence server. WIP."

 PREFIX="${PREFIX:-/usr/local}"

 mkdir -p "${PREFIX}/cache/janus/"
 irc_pipe="${PREFIX}/cache/janus/$$.irc"
 move_pipe="${PREFIX}/cache/janus/$$.move"
 mkfifo "${irc_pipe}"
 mkfifo "${move_pipe}"

 prev=0
 userId=""
 jq -Rnc 'inputs | fromjson? | select(.method != "pos")' | while read -r msg;do
 	method="$(jq -r .method <<< "$msg")"
 	data="$(jq -r .data <<< "$msg")"
 	if [ "$(date +%M)" != "${prev}" ];then
 		printf '{"method":"user_chat","data":{"message":{"data":"%s"},"userId":"void"}}\n' "$(date)"
 		prev="$(date +%M)"
 	fi
 	case "$method" in
 		logon)
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			# {"method":"logon","data":{"userId":"CalmFly290","version":"janusweb-1.2","roomId":"9831af0c1a978a8d9537711c8c70214f"}}
 			roomId="$(jq -r .data.roomId <<< "$msg")"
 			userId="$(jq -r .data.userId <<< "$msg")"
 			userId_js="$(jq .data.userId <<< "$msg")"
 			stdbuf -oL tail -f "${irc_pipe}" | stdbuf -o0 "${PREFIX}/libexec/janus-irc-hack-part1" "$userId" &
 			printf '{"method":"okay"}\n'
 			### start the background process that shows log messages :)
 #			stdbuf -oL tail -n0 -f /var/cache/janus.log \
 #				| stdbuf -oL jq -Rnc 'inputs | fromjson? | select(.method = "chat") | select(.data.userId != '"${userId_js}"')' &
 		;;
 		enter_room)
 			printf '{"method":"user_enter", "data":{"userId":"%s","roomId":"%s"}}\n' "$userId" "$roomId" >> "${move_pipe}"
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			printf '{"method":"okay"}\n'
 		;;
 		move)
 			# don't care to see these messages atm
 			### example incoming move message:
 			# {
 			#   "method":"move",
 			#   "data": {
 			#     "pos":"0 0 0",
 			#     "vel":"0 0 0",
 			#     "scale":"1 1 1",
 			#     "rotvel":"0 0 0",
 			#     "dir":"-0.00009999999957112646 0 -0.999999995",
 			#     "up_dir":"9.999999914225293e-9 0.999999995 0.00009999999907112646",
 			#     "view_dir":"0.00009999999907112646 -0.00009999999957112646 0.9999999900000001",
 			#     "head_pos":[0,0,0],
 			#     "anim_id":"idle",
 			#     "speaking":false
 			#   }
 			# }
 			### example outgoing move message:
 			# {
 			#   "method":"user_moved",
 			#   "data": {
 			#     "roomId":"e562b2e1339fc08d635d28481121857c",
 			#     "userId":"ProudMinna333",
 			#     "position": {
 			#       "pos":"8.38889 -0.267704 -5.83333",
 			#       "dir":"-1 -1.33e-06 9.42e-07",
 			#       "view_dir":"-1 -1.33e-06 9.42e-07",
 			#       "up_dir":"-1.33e-06 1 1.25e-12",
 			#       "head_pos":"0 0 0"
 			#     }
 			#   }
 			# }

 			### so, the client->server message put the move data into the position thing
 			data="$(jq -c '.data' <<< "${msg}")"
 			printf '{"method":"user_moved","data":{"roomId":"%s","userId":"%s","position":%s}}\n' "${roomId}" "${userId}" "${data}" \
 			  >> "${move_pipe}"
 #			printf '%s\n' "${msg}" >> ${move_pipe}
 			#printf '{"method":"okay"}\n'
 		;;
 		chat)
 			logger -p crit "msg: ${msg}"
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			printf '{"method":"okay"}\n'
 			### example incoming chat message: {"method":"chat","data":"Hello?"}
 			printf '%s\n' "${msg}" > "${irc_pipe}"
 		;;
 		subscribe)
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			stdbuf -oL tail -n0 -f "${move_pipe}" | ncat 127.0.0.1 1234 &
 			printf '{"method":"okay"}\n'
 		;;
 		unsubscribe)
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			printf '{"method":"okay"}\n'
 		;;
 		portal)
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			printf '{"method":"okay"}\n'
 		;;
 		users_online)
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ msg: $msg"
 			printf '{"method":"users_online","data":{"results":2,"roomId":"%s","users":["void","%s"]}\n' "$roomId" "$userId"
 		;;
 		*)
 			#logger -p crit "${REMOTE_URL}: janus-presence $$ SHIT msg: $msg"
 			printf '{"method":"error","data":{"message":"unexpected method."}\n'
 		;;
 	esac
 done