File: sxmo_contactmenu.sh

package info (click to toggle)
sxmo-utils 1.14.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,016 kB
  • sloc: sh: 9,166; ansic: 117; makefile: 68
file content (193 lines) | stat: -rwxr-xr-x 4,150 bytes parent folder | download
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
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2022 Sxmo Contributors

# 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

# shellcheck disable=SC2120
newcontact() {
	name="$(printf "" | sxmo_dmenu_with_kb.sh -p "$icon_usr Name")"

	number="$1"
	if [ -n "$number" ]; then
		number="$(sxmo_validnumber.sh "$number")" || return
	fi

	while [ -z "$number" ]; do
		number="$(sxmo_contacts.sh --unknown | sxmo_dmenu_with_kb.sh -p "$icon_phl Number")"
		number="$(sxmo_validnumber.sh "$number")" || continue
	done

	# we store as NUMBER\tNAME but display as NAME\tNUMBER
	printf "%s\n" "$number	$name" >> "$SXMO_CONTACTFILE"
	PICKED="$name	$number"
}

editcontactname() {
	oldnumber="$(printf %s "$1" | cut -d"	" -f2)"
	oldname="$(printf %s "$1" | cut -d"	" -f1)"

	ENTRIES="$(printf %b "Old name: $oldname")"
	PICKED="$(
		printf %b "$ENTRIES" |
		sxmo_dmenu_with_kb.sh -p "$icon_edt Edit Contact"
	)"

	if ! printf %s "$PICKED" | grep -q "^Old name: "; then
		newcontact="$oldnumber	$PICKED"
		sed -i "s/^$oldnumber	$oldname$/$newcontact/" "$SXMO_CONTACTFILE"
		set -- "$newcontact"
	fi

	editcontact "$PICKED	$oldnumber"
}

editcontactnumber() {
	oldnumber="$(printf %s "$1" | cut -d"	" -f2)"
	oldname="$(printf %s "$1" | cut -d"	" -f1)"

	ENTRIES="$(sxmo_contacts.sh --unknown | xargs -0 printf "%b (Old number)\n%b" "$oldnumber")"
	PICKED= # already used var name
	while [ -z "$PICKED" ]; do
		PICKED="$(
			printf %b "$ENTRIES" |
			sxmo_dmenu_with_kb.sh -p "$icon_edt Edit Contact"
		)"
		if printf %s "$PICKED" | grep -q "(Old number)$"; then
			editcontact "$1"
			return
		fi
		PICKED="$(sxmo_validnumber.sh "$PICKED")" || continue
	done

	newcontact="$PICKED	$oldname"

	# reverse them
	sed -i "s/^$number	$name$/$newcontact/" "$SXMO_CONTACTFILE"
	editcontact "$oldname	$PICKED"
}

deletecontact() {
	number="$(printf %s "$1" | cut -d"	" -f2)"
	name="$(printf %s "$1" | cut -d"	" -f1)"

	# shellcheck disable=SC2059
	ENTRIES="$(printf "$icon_cls No\n$icon_chk Yes")"
	PICKED="$(
		printf %b "$ENTRIES" |
		dmenu -p "$icon_del Delete $name?"
	)"

	# reverse them
	printf %s "$PICKED" | grep -q "Yes" && sed -i "/^$number	$name$/d" "$SXMO_CONTACTFILE"
}

editcontact() {
	number="$(printf %s "$1" | cut -d"	" -f2)"
	name="$(printf %s "$1" | cut -d"	" -f1)"
	ENTRIES="$(printf %b "$icon_ret Cancel\n$icon_usr Name: $name\n$icon_phl Number: $number")"

	PICKED="$(
		printf %b "$ENTRIES" |
		dmenu -p "$icon_edt Edit Contact"
	)"

	case "$PICKED" in
		*"Name: "*)
			editcontactname "$1"
			;;
		*"Number: "*)
			editcontactnumber "$1"
			;;
		*)
			showcontact "$1"
			;;
	esac

}

showcontact() {
	number="$(printf %s "$1" | cut -d"	" -f2)"
	name="$(printf %s "$1" | cut -d"	" -f1)"

	PICKED="$(dmenu -p "$icon_usr $name" <<EOF
$icon_ret Cancel
$icon_lst List Messages
$icon_msg Send a Message
$icon_phn Call
$icon_itm View
$icon_edt Edit
$icon_del Delete
EOF
	)"

	case "$PICKED" in
		*"List Messages")
			sxmo_hook_tailtextlog.sh  "$number"
			exit
			;;
		*"Send a Message")
			sxmo_modemtext.sh sendtextmenu  "$number"
			exit
			;;
		*"Call")
			sxmo_modemdial.sh "$number"
			exit
			;;
		*"Edit")
			editcontact "$1"
			;;
		*"View")
			if formatted="$(pnc format -f nat "$number" 2>/dev/null)"; then
				text="$formatted"
			else
				text="$number"
			fi

			# shellcheck disable=SC2016
			sxmo_terminal.sh sh -c 'printf "%s\n%s\n" "$1" "$2"; read -r _line' \
				"show_number" "$name" "$text" || true
			;;
		*"Delete")
			deletecontact "$1" || showcontact "$1"
			;;
	esac
}

main() {
	while true; do
		CONTACTS="$(sxmo_contacts.sh --all)"
		ENTRIES="$(printf %b "$CONTACTS" | xargs -0 printf "$icon_ret Close Menu\n$icon_pls New Contact\n%s")"

		PICKED="$(
			printf %b "$ENTRIES" |
			sxmo_dmenu_with_kb.sh -i -p "$icon_lst Contacts"
		)"

		case "$PICKED" in
			"$icon_ret Close Menu")
				exit
				;;
			"$icon_pls New Contact")
				newcontact || continue
				;;
			*)
		esac

		showcontact "$(printf %s "$PICKED" | sed 's/: /\t/g')"
	done
}

if [ -n "$1" ]; then
	cmd="$1"
	shift
else
	cmd=main
fi

"$cmd" "$@"