#!/bin/sh # SPDX-License-Identifier: AGPL-3.0-only # Copyright 2022 Sxmo Contributors export HOME="${HOME:-/home/user}" export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/10000}" export PATH="${XDG_CONFIG_HOME:-$HOME/.config}/sxmo/hooks:/usr/share/sxmo/default_hooks:/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin:$PATH" # include common definitions # shellcheck source=configs/default_hooks/sxmo_hook_icons.sh . sxmo_hook_icons.sh # shellcheck source=scripts/core/sxmo_common.sh . sxmo_common.sh # We use this directory to store states, so it must exist mkdir -p "$XDG_RUNTIME_DIR/sxmo_calls" set -e vid_to_number() { case "$1" in sub1_*|sub0_*) doas -n /usr/libexec/msm-modem-uim-selection call-list 2>/dev/null | awk -F'\t' -v id="$1" '$1 == id {print $4; exit}' ;; *) mmcli -m any -o "$1" -K 2>/dev/null | \ grep call.properties.number | \ cut -d ':' -f2 | \ tr -d ' ' ;; esac } log_event() { EVT_HANDLE="$1" EVT_VID="$2" NUM="$(vid_to_number "$EVT_VID" 2>/dev/null || true)" TIME="$(date +%FT%H:%M:%S%z)" # Logging must never abort call control under `set -e` (for example when # the log directory is not writable): a failed write would otherwise skip # the actual accept/hangup that follows. { mkdir -p "$SXMO_LOGDIR" && printf %b "$TIME\t$EVT_HANDLE\t$NUM\n" >> "$SXMO_LOGDIR/modemlog.tsv" } 2>/dev/null || sxmo_log "Could not write $EVT_HANDLE to $SXMO_LOGDIR/modemlog.tsv" return 0 } pickup() { CALLID="$1" NUM="$(vid_to_number "$CALLID" 2>/dev/null || true)" case "$CALLID" in sub1_*|sub0_*) sxmo_log "Invoking pickup hook for $CALLID" sxmo_hook_pickup.sh || true doas -n dbus-send --system --type=signal / org.ofono.VoiceCallManager.CallAdded >/dev/null 2>&1 || true sxmo_modemaudio.sh setup_audio || true doas -n /usr/libexec/msm-modem-uim-selection call-accept "$CALLID" >/dev/null 2>&1 || \ /usr/libexec/msm-modem-uim-selection call-accept "$CALLID" >/dev/null 2>&1 || true touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.pickedupcall" if command -v xcallscreen >/dev/null 2>&1; then xcallscreen active "$CALLID" "$NUM" >/dev/null 2>&1 || true fi return 0 ;; esac DIRECTION="$( (mmcli -m any -o "$CALLID" -K 2>/dev/null || mmcli -o "$CALLID" -K 2>/dev/null || mmcli --voice-status -o "$CALLID" -K 2>/dev/null) | grep call.properties.direction | cut -d: -f2 | tr -d " " )" case "$DIRECTION" in outgoing) if ! sxmo_modemaudio.sh setup_audio; then sxmo_notify_user.sh --urgency=critical "We failed to setup call audio" return 1 fi if ! mmcli -m any -o "$CALLID" --start; then sxmo_notify_user.sh --urgency=critical "We failed to start the call" return 1 fi touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.initiatedcall" log_event "call_start" "$CALLID" if command -v xcallscreen >/dev/null 2>&1; then xcallscreen outgoing "$CALLID" "$NUM" >/dev/null 2>&1 || true else sxmo_notify_user.sh "Started call" fi ;; incoming) sxmo_log "Invoking pickup hook" sxmo_hook_pickup.sh if ! sxmo_modemaudio.sh setup_audio; then sxmo_notify_user.sh --urgency=critical "We failed to setup call audio" return 1 fi if ! mmcli -m any -o "$CALLID" --accept 2>/dev/null && \ ! doas -n /usr/libexec/msm-modem-uim-selection call-accept "$CALLID" >/dev/null 2>&1; then sxmo_notify_user.sh --urgency=critical "We failed to accept the call" return 1 fi touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.pickedupcall" log_event "call_pickup" "$CALLID" if command -v xcallscreen >/dev/null 2>&1; then xcallscreen active "$CALLID" "$NUM" >/dev/null 2>&1 || true else sxmo_notify_user.sh "Picked up call" fi ;; *) sxmo_notify_user.sh --urgency=critical "Couldn't initialize call with callid <$CALLID>; unknown direction <$DIRECTION>" rm "$XDG_RUNTIME_DIR/sxmo_calls/"* 2>/dev/null || true rm -f "$XDG_RUNTIME_DIR"/sxmo.ring.pid 2>/dev/null rm -f "$SXMO_NOTIFDIR"/incomingcall* 2>/dev/null if command -v xcallscreen >/dev/null 2>&1; then xcallscreen ended >/dev/null 2>&1 || true fi return 1 ;; esac } hangup() { CALLID="${1:-all}" touch /tmp/sxmo_hangup_requested 2>/dev/null || true dpids="$(pgrep -f '[s]xmo_modemdial\.sh' 2>/dev/null || true)" [ -n "$dpids" ] && kill -TERM $dpids 2>/dev/null || true case "$CALLID" in ""|"-"|"all") for f in "$XDG_RUNTIME_DIR"/sxmo_calls/*.initiatedcall "$XDG_RUNTIME_DIR"/sxmo_calls/*.pickedupcall; do [ -e "$f" ] || continue base="${f%.*}" rm -f "$f" 2>/dev/null || true touch "${base}.hangedupcall" 2>/dev/null || true done for f in "$XDG_RUNTIME_DIR"/sxmo_calls/*.monitoredcall; do [ -e "$f" ] || continue base="${f%.*}" rm -f "$f" 2>/dev/null || true touch "${base}.discardedcall" 2>/dev/null || true done rm -f "$XDG_RUNTIME_DIR"/sxmo_calls/sub1_* "$XDG_RUNTIME_DIR"/sxmo_calls/sub0_* /tmp/sxmo_call_ringing /tmp/sxmo_active_call 2>/dev/null || true doas -n /usr/libexec/msm-modem-uim-selection call-hangup all >/dev/null 2>&1 || \ /usr/libexec/msm-modem-uim-selection call-hangup all >/dev/null 2>&1 || true mmcli -m any --voice-hangup-all >/dev/null 2>&1 || true for cid in $(mmcli -m any --voice-list-calls 2>/dev/null | grep -oE 'Call/[0-9]+' | cut -d/ -f2); do mmcli -m any -o "$cid" --hangup >/dev/null 2>&1 || true mmcli -m any --voice-delete-call="$cid" >/dev/null 2>&1 || true done mm_calls="$(mmcli -m any --voice-list-calls 2>/dev/null || true)" for f in "$XDG_RUNTIME_DIR"/sxmo_calls/*; do [ -e "$f" ] || continue bn="${f##*/}" cid="${bn%%.*}" case "$cid" in ''|*[!0-9]*) continue ;; esac printf '%s\n' "$mm_calls" | grep -qE "/Call/${cid}([[:space:]]|\$)" || rm -f "$f" 2>/dev/null || true done doas -n dbus-send --system --type=signal / org.ofono.VoiceCallManager.CallRemoved >/dev/null 2>&1 || true sxmo_hook_hangup.sh || true return 0 ;; sub1_*|sub0_*) rm -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}."* "$XDG_RUNTIME_DIR/sxmo_calls/sub1_dialing."* /tmp/sxmo_call_ringing /tmp/sxmo_active_call 2>/dev/null || true doas -n /usr/libexec/msm-modem-uim-selection call-hangup "$CALLID" >/dev/null 2>&1 || \ /usr/libexec/msm-modem-uim-selection call-hangup "$CALLID" >/dev/null 2>&1 || \ doas -n /usr/libexec/msm-modem-uim-selection call-hangup all >/dev/null 2>&1 || true doas -n dbus-send --system --type=signal / org.ofono.VoiceCallManager.CallRemoved >/dev/null 2>&1 || true sxmo_hook_hangup.sh || true return 0 ;; esac # Mark the call as hung up / discarded first (sxmo_modem.sh reads these # markers when ModemManager reports the call as terminated), then end the # call on the network right away, and only afterwards log and run hooks so # a failing log write or hook can never leave the caller ringing. WAS_PICKED_UP=0 if [ -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.pickedupcall" ] || \ [ -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.initiatedcall" ]; then WAS_PICKED_UP=1 rm -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.pickedupcall" \ "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.initiatedcall" \ "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.monitoredcall" touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.hangedupcall" || true else # this call was never picked up and hung up immediately, so it is a discarded call rm -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.monitoredcall" touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.discardedcall" || true fi rm -f /tmp/sxmo_call_ringing /tmp/sxmo_active_call 2>/dev/null || true HANGUP_FAILED=0 if ! mmcli -m any -o "$CALLID" --hangup 2>/dev/null && \ ! mmcli -m any --voice-hangup-all 2>/dev/null && \ ! doas -n /usr/libexec/msm-modem-uim-selection call-hangup "$CALLID" >/dev/null 2>&1 && \ ! doas -n /usr/libexec/msm-modem-uim-selection call-hangup all >/dev/null 2>&1; then mmcli -m any --voice-delete-call="$CALLID" >/dev/null 2>&1 || true # we ignore already closed calls if list_active_calls | grep -q "/$CALLID "; then HANGUP_FAILED=1 fi fi if [ "$WAS_PICKED_UP" = "1" ]; then log_event "call_hangup" "$CALLID" sxmo_log "sxmo_modemcall: Invoking hangup hook" sxmo_hook_hangup.sh || true else log_event "call_discard" "$CALLID" sxmo_log "sxmo_modemcall: Invoking discard hook" sxmo_hook_discard.sh || true fi if ! mmcli -m any --voice-list-calls 2>/dev/null | grep -qE "/Call/${CALLID}([[:space:]]|\$)"; then rm -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}."* 2>/dev/null || true fi if [ "$HANGUP_FAILED" = "1" ]; then sxmo_notify_user.sh --urgency=critical "We failed to hangup the call" return 1 fi } # We shallow muted/blocked and terminated calls list_active_calls() { mmcli -m any --voice-list-calls 2>/dev/null | \ awk '$1=$1' | \ grep -v terminated | \ grep -v "(unknown)" | \ grep -v "No calls were found" | while read -r line; do CALLID="$(printf "%s\n" "$line" | awk '$1=$1' | cut -d" " -f1 | xargs basename)" if [ -e "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.mutedring" ]; then continue # we shallow muted calls fi printf "%s\n" "$line" done for f in "$XDG_RUNTIME_DIR"/sxmo_calls/sub1_*.*; do [ -e "$f" ] || continue base="${f##*/}" cid="${base%%.*}" st="${base#*.}" case "$st" in ringing-in|dialing|ringing-out|active|held|waiting) if [ ! -e "$XDG_RUNTIME_DIR/sxmo_calls/${cid}.mutedring" ]; then printf "/org/freedesktop/ModemManager1/Call/%s (%s)\n" "$cid" "$st" fi ;; esac done } incall_menu() { if command -v xcallscreen >/dev/null 2>&1; then ACTIVE_LINE="$(list_active_calls | head -n1)" if [ -n "$ACTIVE_LINE" ]; then CALLID="$(printf %s "$ACTIVE_LINE" | cut -d" " -f1 | xargs basename)" NUMBER="$(vid_to_number "$CALLID" 2>/dev/null || true)" case "$ACTIVE_LINE" in *"(ringing-in)"*) xcallscreen incoming "$CALLID" "$NUMBER" >/dev/null 2>&1 || true ;; *"(dialing)"*|*"(ringing-out)"*) xcallscreen outgoing "$CALLID" "$NUMBER" >/dev/null 2>&1 || true ;; *) xcallscreen active "$CALLID" "$NUMBER" >/dev/null 2>&1 || true ;; esac fi return 0 fi } mute() { CALLID="$1" # this signals that we muted this ring touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.mutedring" sxmo_log "Invoking mute_ring hook" sxmo_hook_mute_ring.sh log_event "ring_mute" "$1" } incoming_call_menu() { case "$1" in sub1_*|sub0_*) [ -e "$XDG_RUNTIME_DIR/sxmo_calls/${1}.ringing-in" ] || return 0 ;; *) mmcli -m any -o "$1" -K 2>/dev/null | \ grep -qE 'call\.properties\.state[[:space:]]*:[[:space:]]*(ringing-in|waiting)' || return 0 ;; esac NUMBER="$(vid_to_number "$1")" NUMBER="$(sxmo_modem.sh cleanupnumber "$NUMBER")" if command -v xcallscreen >/dev/null 2>&1; then xcallscreen incoming "$1" "$NUMBER" >/dev/null 2>&1 || true return 0 fi } killed() { sxmo_dmenu.sh close } if [ "$1" = "incall_menu" ] || [ "$1" = "incoming_call_menu" ]; then trap 'killed' TERM INT fi "$@"