You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.2 KiB

#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source "$DIR/common"
usage(){
echo "checkCall [options] [message|-]"
echo "Options:"
echo " -h|--help Display this help text"
echo " -m|--modem Specify a modem"
echo " --dry-run Don't actually send the message"
}
# Stolen from sxmo
lookupnumberfromcallid() {
local id=$1
mmcli -m "$modem" --voice-list-calls -o "$id" -K |
grep call.properties.number |
cut -d ':' -f 2 |
tr -d ' '
}
lookupcontact(){
echo "$1" |
# Remove the +44 and replace with 0
sed 's/^\+44/0/'
# This will eventually work with abook but for now just return the number
}
answer-call(){
export DISPLAY=:0.0
echo "answer call $1" > /dev/tty
mmcli -m "$modem" -o "$1" --accept
}
reject-call(){
mmcli -m "$modem" -o "$1" --hangup
}
prompt-incoming(){
export DISPLAY=:0.0
local answer=""
local reject=""
local choice=''
local contact="$1"
local prompt="$(echo -en "Incoming Call\n$contact")"
choice="$(echo -e "$answer\n$reject" | rofi -dmenu -i \
-theme themes/incoming-call.rasi -a 0 -u 1 \
-p "$prompt" \
-window-title "call-from-$contact" \
-me-select-entry '' -me-accept-entry MousePrimary)"
case "$choice" in
"$answer") echo "accept" ;;
"$reject") echo "reject" ;;
esac
}
checkIncoming(){
export DISPLAY=:0.0
if [ "$dryrun" = "true" ]; then
prompt-incoming "+441234567890"
return
fi
local id="$( mmcli -m "$modem" --voice-list-calls |
grep -Eo '[0-9]+ incoming \(ringing-in\)' | grep -Eo '[0-9]+' )"
local count="$(echo "$id" | deleteEmptyLines | wc -l)"
[ "$count" -eq 0 ] && return
local number="$(lookupnumberfromcallid "$id")"
local contact="$(lookupcontact "$number")"
local action=""
while mmcli -m "$modem" --voice-list-calls | grep -Eoq "$id"' incoming \(ringing-in\)' && [ -z "$action" ]; do
action="$(prompt-incoming "$contact")"
done
case "$action" in
"accept") answer-call "$id"; break ;;
"reject") reject-call "$id"; break ;;
esac
}
checkFinished(){
local ids="$( mmcli -m "$modem" --voice-list-calls |
grep -Eo '[0-9]+ incoming \(terminated\)' | grep -Eo '[0-9]+' )"
local count="$(echo "$ids" | deleteEmptyLines | wc -l)"
[ "$count" -eq 0 ] && return
echo "$ids" | while read -r id; do
# Delete from the modem
local number="$(lookupnumberfromcallid "$id")"
local contact="$(lookupcontact "$number")"
# If there is a rofi process with the title of "call-from-number", then
# it hasn't been answerd yet.
# Treat as a missed call
if ps aux | grep -E '\Wrofi' | grep -q "call-from-$number"; then
echo "Missed call from $contact" >> "$CALL_DIR/missed-calls"
mmcli -m "$modem" --voice-delete-call "$id"
ps aux | grep -E '\Wrofi' | grep "call-from-$number" |
awk '{print $2}' | xargs kill
fi
done
}
mkdir -p "$CALL_DIR"
dryrun="false"
# Assume we want the first modem
# can be overwritten by the -m option
modem="$(mmcli -L | grep -oE 'Modem\/[0-9]+' | head -n 1 | cut -d'/' -f2)"
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage; exit;;
-m|--modem) modem="$2"; shift ;;
--dry-run) dryrun="true" ;;
--) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
checkIncoming &
checkFinished &