~mil/sxmo-utils

ref: fc051045c98e6ec4fae39800f68830661fb596e9 sxmo-utils/scripts/modem/sxmo_modemmonitor.sh -rwxr-xr-x 4.5 KiB
fc051045Anjandev Momi added aerc to app menu 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env sh
LOGDIR="$XDG_DATA_HOME"/sxmo/modem
NOTIFDIR="$XDG_DATA_HOME"/sxmo/notifications
trap "gracefulexit" INT TERM

err() {
	notify-send "$1"
	gracefulexit
}

gracefulexit() {
	echo "gracefully exiting $0!"
	kill -9 0
}

modem_n() {
	MODEMS="$(mmcli -L)"
	echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' > /dev/null || err "Couldn't find modem - is your modem enabled?\nDisabling modem monitor"
	echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2
}

lookupnumberfromcallid() {
	VOICECALLID=$1
	mmcli -m "$(modem_n)" --voice-list-calls -o "$VOICECALLID" -K |
		grep call.properties.number |
		cut -d ':' -f 2 |
		tr -d ' '
}

checkformissedcalls() {
	if pgrep -vf sxmo_modemcall.sh; then
		# Add a notification for every missed call
		# Note sxmo_modemcall.sh cleanups/delete the callid from the modem; so
		# effectivly any incoming call thats terminated here is a missed call!
		for MISSEDCALLID in $(
			mmcli -m "$(modem_n)" --voice-list-calls |
			grep incoming |
			grep terminated |
			grep -oE "Call\/[0-9]+" |
			cut -d'/' -f2
		); do
			MISSEDNUMBER="$(lookupnumberfromcallid "$MISSEDCALLID")"
			mmcli -m "$(modem_n)" --voice-delete-call "$MISSEDCALLID"
			rm -f "$NOTIFDIR/incomingcall_${MISSEDCALLID}_notification"


			TIME="$(date --iso-8601=seconds)"
			mkdir -p "$LOGDIR"
			printf %b "$TIME\tcall_missed\t$MISSEDNUMBER\n" >> "$LOGDIR/modemlog.tsv"

			CONTACT="$(sxmo_contacts.sh | grep -F "^${MISSEDNUMBER//+/\\+}:")"
			sxmo_notificationwrite.sh \
				random \
				"st -f Terminus-20 -e sh -c \"echo 'Missed call from $CONTACT at $(date)' && read\"" \
				none \
				"Missed call - $CONTACT"
		done
	fi
}

checkforincomingcalls() {
	VOICECALLID="$(
		mmcli -m "$(modem_n)" --voice-list-calls -a |
		grep -Eo '[0-9]+ incoming \(ringing-in\)' |
		grep -Eo '[0-9]+'
	)"
	echo "$VOICECALLID" | grep -v . && rm -f "$NOTIFDIR/incomingcall*" && return

	# Determine the incoming phone number
	echo "Incoming Call:"
	INCOMINGNUMBER=$(lookupnumberfromcallid "$VOICECALLID")

	if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/ring" ]; then
		"$XDG_CONFIG_HOME/sxmo/hooks/ring" "$(sxmo_contacts.sh | grep -E "^${INCOMINGNUMBER//+/\\+}:")" &
	else
		sxmo_vibratepine 2500 &
	fi

	TIME="$(date --iso-8601=seconds)"
	mkdir -p "$LOGDIR"
	printf %b "$TIME\tcall_ring\t$INCOMINGNUMBER\n" >> "$LOGDIR/modemlog.tsv"

	sxmo_notificationwrite.sh \
		"$NOTIFDIR/incomingcall_${VOICECALLID}_notification" \
		"sxmo_modemcall.sh pickup $VOICECALLID" \
		none \
		"Pickup - $(sxmo_contacts.sh | grep -E "^${INCOMINGNUMBER//+/\\+}:")" &

	echo "Number: $INCOMINGNUMBER (VOICECALLID: $VOICECALLID)"
}

checkfornewtexts() {
	TEXTIDS="$(
		mmcli -m "$(modem_n)" --messaging-list-sms |
		grep -Eo '/SMS/[0-9]+ \(received\)' |
		grep -Eo '[0-9]+'
	)"
	echo "$TEXTIDS" | grep -v . && return

	# Loop each textid received and read out the data into appropriate logfile
	for TEXTID in $TEXTIDS; do
		TEXTDATA="$(mmcli -m "$(modem_n)" -s "$TEXTID" -K)"
		TEXT="$(echo "$TEXTDATA" | grep sms.content.text | sed -E 's/^sms\.content\.text\s+:\s+//')"
		NUM="$(
			echo "$TEXTDATA" |
			grep sms.content.number |
			sed -E 's/^sms\.content\.number\s+:\s+//'
		)"
		TIME="$(echo "$TEXTDATA" | grep sms.properties.timestamp | sed -E 's/^sms\.properties\.timestamp\s+:\s+//')"

		mkdir -p "$LOGDIR/$NUM"
		printf %b "Received from $NUM at $TIME:\n$TEXT\n\n" >> "$LOGDIR/$NUM/sms.txt"
		printf %b "$TIME\trecv_txt\t$NUM\t${#TEXT} chars\n" >> "$LOGDIR/modemlog.tsv"
		mmcli -m "$(modem_n)" --messaging-delete-sms="$TEXTID"

		sxmo_notificationwrite.sh \
			random \
			"st -e tail -n9999 -f '$LOGDIR/$NUM/sms.txt'" \
			"$LOGDIR/$NUM/sms.txt" \
			"Message - $(sxmo_contacts.sh | grep -E "^${NUM//+/\\+}:"): $TEXT"

		if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/sms" ]; then
			"$XDG_CONFIG_HOME/sxmo/hooks/sms" "$(sxmo_contacts.sh | grep -E "^${NUM//+/\\+}:")" "$TEXT"
		fi
	done
}

mainloop() {
	checkformissedcalls
	checkforincomingcalls
	checkfornewtexts

	# Monitor for incoming calls
	dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Voice',type='signal',member='CallAdded'" | \
		while read -r; do
			checkforincomingcalls
		done &

	# Monitor for incoming texts
	dbus-monitor --system "interface='org.freedesktop.ModemManager1.Modem.Messaging',type='signal',member='Added'" | \
		while read -r; do
			checkfornewtexts
		done &

	# Monitor for missed calls
	dbus-monitor --system "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.ModemManager1.Call'" | \
		while read -r; do
			checkformissedcalls
		done &

	wait
	wait
	wait
}

mainloop