~mil/sxmo-utils

ref: c769ef15182fd6092e115266754e74c39c4f7631 sxmo-utils/scripts/modem/sxmo_modemtext.sh -rwxr-xr-x 5.9 KiB
c769ef15Stacy Harper Allow multikeys without bonsaictl 2 months 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

# 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

set -e

err() {
	sxmo_log "$1"
	echo "$1" | dmenu
	kill $$
}

choosenumbermenu() {
	# Prompt for number
	NUMBER="$(
		printf %b "\n$icon_cls Cancel\n$icon_grp More contacts\n$(sxmo_contacts.sh | grep -E "\+[0-9]+$")" |
		awk NF |
		sxmo_dmenu_with_kb.sh -p "Number" -i |
		cut -d: -f2 |
		tr -d -- '- '
	)"

	if echo "$NUMBER" | grep -q "Morecontacts"; then
		NUMBER="$( #joined words without space is not a bug
			printf %b "\nCancel\n$(sxmo_contacts.sh --all)" |
				grep . |
				sxmo_dmenu_with_kb.sh -p "Number" -i |
				cut -d: -f2 |
				tr -d -- '- '
		)"
	fi

	if printf %s "$NUMBER" | grep -q "Cancel"; then
		exit 1
	elif NUMBER="$(sxmo_validnumber.sh "$NUMBER")"; then
		printf %s "$NUMBER"
	else
		sxmo_notify_user.sh "That doesn't seem like a valid number"
	fi
}

sendtextmenu() {
	if [ -n "$1" ]; then
		NUMBER="$1"
	else
		NUMBER="$(choosenumbermenu)"
	fi

	[ -z "$NUMBER" ] && exit 1

	DRAFT="$SXMO_LOGDIR/$NUMBER/draft.txt"
	if [ ! -f "$DRAFT" ]; then
		mkdir -p "$(dirname "$DRAFT")"
		echo 'Enter text message here' > "$DRAFT"
	fi

	# shellcheck disable=SC2086
	sxmo_terminal.sh $EDITOR "$DRAFT"

	while true
	do
		ATTACHMENTS=
		if [ -f "$SXMO_LOGDIR/$NUMBER/draft.attachments.txt" ]; then
			# shellcheck disable=SC2016
			ATTACHMENTS="$(
				tr '\n' '\0' < "$SXMO_LOGDIR/$NUMBER/draft.attachments.txt" |
				xargs -0 -I{} sh -c 'printf "%s %s %s :: %s\n" "" "" "$(basename "{}")" "{}"'
			)"
		fi

		RECIPIENTS=
		if [ "$(printf %s "$NUMBER" | xargs pn find | wc -l)" -gt 1 ]; then
			# shellcheck disable=SC2016
			RECIPIENTS="$(printf %s "$NUMBER" | xargs pn find | xargs -I{} sh -c 'printf "  "$(sxmo_contacts.sh --name {})" :: {}\n"')"
		fi

		CHOICES="$(printf "%s Send to %s (%s)\n%b\n%s Add Recipient\n%b\n%s Add Attachment\n%s Edit '%s'\n%s Cancel\n" \
			"$icon_snd" "$(sxmo_contacts.sh --name "$NUMBER")" "$NUMBER" "$RECIPIENTS" "$icon_usr" "$ATTACHMENTS" "$icon_att" "$icon_edt" \
			"$(cat "$SXMO_LOGDIR/$NUMBER/draft.txt")" "$icon_cls" \
			| awk NF
		)"

		CONFIRM="$(printf %b "$CHOICES" | dmenu -i -p "Confirm")"
		case "$CONFIRM" in
			*"Send"*)
				if sxmo_modemsendsms.sh "$NUMBER" - < "$DRAFT"; then
					rm "$DRAFT"
					sxmo_log "Sent text to $NUMBER"
					exit 0
				else
					err "Failed to send txt to $NUMBER"
				fi
				;;
			# Remove Attachment
			" "*)
				FILE="$(printf %s "$CONFIRM" | awk -F' :: ' '{print $2}')"  
				sed -i "\|$FILE|d" "$SXMO_LOGDIR/$NUMBER/draft.attachments.txt"
				if [ ! -s "$SXMO_LOGDIR/$NUMBER/draft.attachments.txt" ] ; then
					rm "$SXMO_LOGDIR/$NUMBER/draft.attachments.txt"
				fi
				;;
			# Remove Recipient
			" "*)
				if [ "$(printf %s "$NUMBER" | xargs pn find | wc -l)" -gt 1 ]; then 
					OLDNUMBER="$NUMBER"
					RECIPIENT="$(printf %s "$CONFIRM" | awk -F' :: ' '{print $2}')"
					NUMBER="$(printf %s "$OLDNUMBER" | sed "s/$RECIPIENT//")"
					mkdir -p "$SXMO_LOGDIR/$NUMBER"
					DRAFT="$SXMO_LOGDIR/$NUMBER/draft.txt"
					if [ -f "$SXMO_LOGDIR/$OLDNUMBER/draft.txt" ]; then
						# TODO: if there is already a DRAFT warn the user?
						mv "$SXMO_LOGDIR/$OLDNUMBER/draft.txt" "$DRAFT"
					fi
					if [ -f "$SXMO_LOGDIR/$OLDNUMBER/draft.attachments.txt" ]; then
						mv "$SXMO_LOGDIR/$OLDNUMBER/draft.attachments.txt" \
							"$SXMO_LOGDIR/$NUMBER/draft.attachments.txt"
					fi
					kill "$(lsof | grep "/$OLDNUMBER/sms.txt" | cut -f1)"
					[ -e "$SXMO_LOGDIR/$NUMBER/sms.txt" ] || touch "$SXMO_LOGDIR/$NUMBER/sms.txt"
					sxmo_hook_tailtextlog.sh "$NUMBER" &
				fi
				;;
			*"Edit"*)
				sendtextmenu "$NUMBER"
				;;
			*"Add Attachment")
				ATTACHMENT="$(sxmo_files.sh "$HOME" --select-only)"
				if [ -f "$ATTACHMENT" ]; then
					printf "%s\n" "$ATTACHMENT" >> "$SXMO_LOGDIR/$NUMBER/draft.attachments.txt"
				fi
				;;
			*"Add Recipient")
				OLDNUMBER="$NUMBER"
				ADDEDNUMBER="$(choosenumbermenu)"

				if ! echo "$ADDEDNUMBER" | grep -q '^+'; then
					echo "We can't add numbers that don't start with +"
				elif echo "$OLDNUMBER" | grep -q "$ADDEDNUMBER"; then
					echo "Number already a recipient."
				else
					NUMBER="$(printf %s%s "$NUMBER" "$ADDEDNUMBER" | xargs pn find | sort -u | tr -d '\n')"
					mkdir -p "$SXMO_LOGDIR/$NUMBER"
					DRAFT="$SXMO_LOGDIR/$NUMBER/draft.txt"
					if [ -f "$SXMO_LOGDIR/$OLDNUMBER/draft.txt" ]; then
						# TODO: if there is already a DRAFT warn the user?
						mv "$SXMO_LOGDIR/$OLDNUMBER/draft.txt" "$DRAFT"
					fi
					if [ -f "$SXMO_LOGDIR/$OLDNUMBER/draft.attachments.txt" ]; then
						mv "$SXMO_LOGDIR/$OLDNUMBER/draft.attachments.txt" \
						"$SXMO_LOGDIR/$NUMBER/draft.attachments.txt"
					fi
					kill "$(lsof | grep "/$OLDNUMBER/sms.txt" | cut -f1)"
					[ -e "$SXMO_LOGDIR/$NUMBER/sms.txt" ] || touch "$SXMO_LOGDIR/$NUMBER/sms.txt"
					sxmo_hook_tailtextlog.sh "$NUMBER" &
				fi
				;;
			*"Cancel")
				exit 1
				;;
		esac
	done
}

conversationloop() {
	if [ -n "$1" ]; then
		NUMBER="$1"
	else
		NUMBER="$(choosenumbermenu)"
	fi

	set -e

	sxmo_keyboard.sh open

	while true; do
		DRAFT="$SXMO_LOGDIR/$NUMBER/draft.txt"
		if [ ! -f "$DRAFT" ]; then
			mkdir -p "$(dirname "$DRAFT")"
			touch "$DRAFT"
		fi

		# shellcheck disable=SC2086
		$EDITOR "$DRAFT"
		sxmo_modemsendsms.sh "$NUMBER" - < "$DRAFT" || continue
		rm "$DRAFT"
	done
}

readtextmenu() {
	# E.g. only display logfiles for directories that exist and join w contact name
	ENTRIES="$(
	printf %b "$icon_cls Close Menu\n$icon_edt Send a Text\n";
		sxmo_contacts.sh --texted | xargs -IL echo "L logfile"
	)"
	PICKED="$(printf %b "$ENTRIES" | sxmo_dmenu_with_kb.sh -p "Texts" -i)" || exit

	if echo "$PICKED" | grep "Close Menu"; then
		exit 1
	elif echo "$PICKED" | grep "Send a Text"; then
		sendtextmenu
	else
		sxmo_hook_tailtextlog.sh "$(echo "$PICKED" | cut -d: -f2 | sed 's/^ //' | cut -d' ' -f1 )"
	fi
}

if [ -z "$*" ]; then
	readtextmenu
else
	"$@"
fi