-
Notifications
You must be signed in to change notification settings - Fork 0
/
tel-dialer
executable file
·169 lines (151 loc) · 4.75 KB
/
tel-dialer
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
#!/usr/bin/env bash
# TEL Dialer #
# ---------- #
# sealyj
# sidd_dino
version=0.2
# location of the contacts file
contactsfile="$HOME/.tel/data/.contacts";
print_ERR_and_die() {
# Error printing function
printf "\e[33;5;1m[!]%s\n\e[m"\
"$*" 1>&2
exit 1
}
show_help() {
printf "%s\n"\
"call [OPTIONS] [PATTERN]"\
"__________________________"\
"OPTIONS"\
" -a 'name' 'number' Add a new contact"\
" -s Share contacts number"\
" -o Output contacts number"\
" -h Display this help"\
" -u Update contact list"
exit 0
}
update_contacts() {
# Empty contacts file
:> "$contactsfile"
:> ~/.temp
# move termux-contact-list output to
# file descriptor 9
termux-contact-list > ~/.temp
# delay for 2 seconds
read -rt "2" <> <(:) || :
while IFS=$'\n' read -sr line; do
[[ ${line} == " {" ]] && {
read -r line
name=${line:9:-2}
read -r line
number=${line:11:-1}
echo "$name | $number" >> "$contactsfile"
}
done <~/.temp
rm ~/.temp
}
add_contact(){
if [ $# -eq 2 ] ; then
printf "\e[33;5;2m Dialer\e[m: Adding $2 to contacts \n"
add_contact_name=$2
echo 'Contact number:'
read add_contact_number
elif [ $# -eq 1 ] ; then
printf "\e[33;5;2m Dialer\e[m: Adding a new contact \n"
echo 'Contact name:'
read add_contact_name
echo 'Contact number:'
read add_contact_number
else
add_contact_name=$2
add_contact_number=$3
fi
am start -a android.intent.action.INSERT -t vnd.android.cursor.dir/contact -e name $add_contact_name -e phone $add_contact_number > /dev/null 2>&1
printf "\e[33;5;2m Dialer\e[m: Added $add_contact_name to contacts%s\n"
touch ~/.dialer_flag
#update_contacts
exit 0
}
main() {
request_output="false"
if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then
show_help
elif [ "$1" == "-v" ] || [ "$1" == "--version" ] ; then
printf "\e[33;5;2m Dialer\e[m: version $version\n"
elif [ "$1" == "-a" ] || [ "$1" == "--add" ] ; then
add_contact "$@"
elif [ "$1" == "-o" ] || [ "$1" == "--output" ] ; then
# output_contact "$@"
request_output="true"
share="false"
shift
elif [ "$1" == "-s" ] || [ "$1" == "--share" ] ; then
request_output="true"
share="true"
shift
fi
[[ ! -a "$contactsfile" || "$1" == '-u' || -f ~/.dialer_flag ]] && {
rm -f ~/.dialer_flag
printf "\e[33;5;2m Dialer\e[m: Generating contacts list...\n"
update_contacts # if contacts file missing - generate one
[[ "$1" == '-u' ]] && exit 0
}
# fuzzy search in $contactsfile to get the number and
# name in the format
# Name | Number
fzf_output=$(fzf --print-query --color=16 --prompt=" Contact: " --query="$*" --select-1 -0 < "$contactsfile")
case "$?" in
# No contact found
"1" )
if [ "$request_output" == "true" ] ; then
exit 1
fi
IFS=$'\n' read -d '' -r -a selected_contact <<< "$fzf_output"
if [ ${#selected_contact[@]} -gt 1 ] ; then #no match found assume output is printed number
unknown_number="${selected_contact[0]}"
contact="${selected_contact[1]}"
else
contact="${selected_contact[0]}"
fi
if [ ${#selected_contact[@]} -gt 0 ] ; then #no match found assume output is printed number
unknown_number=${selected_contact[0]}
if [ ${#unknown_number} -gt 2 ] ; then
#fail to get matching contact trying calling number
printf "\e[33;5;2m Calling:\e[m %s\n"\
"[${unknown_number}]"
termux-telephony-call "${unknown_number}"
fi
fi
exit 1
;;
# If Ctrl+C is used
"130")
exit 1
;;
esac
IFS=$'\n' read -d '' -r -a selected_contact <<< "$fzf_output"
if [ ${#selected_contact[@]} -gt 1 ] ; then #no match found assume output is printed number
unknown_number="${selected_contact[0]}"
contact="${selected_contact[1]}"
else
contact="${selected_contact[0]}"
fi
# get the contact's name
contact_name=$(echo "${contact}" | cut -d "|" -f1)
# get the contact's number
contact_number=$(echo "${contact}" | cut -d "|" -f2 | tr -d '[:space:]')
#kills the program if termux-telephony-call command is not available
( command -v termux-telephony-call >/dev/null 2>&1 ) || {
print_ERR_and_die "termux-telephony-call not found, have you installed termux-api?"
}
if [ "$request_output" == "true" ] ; then
echo "$contact_number"
elif [ "$share" == "true" ] ; then
am start -a android.intent.action.SEND -t text/plain -e android.intent.extra.TEXT "$contact_number" > /dev/null 2>&1
else
printf "\e[33;5;2m Calling:\e[m %s\n"\
"$contact_name[$contact_number]"
termux-telephony-call "$contact_number"
fi
}
main "$@"