#!/system/bin/sh
######################################################################
#Copyright (C) 2023  Kris Occhipinti
#https://filmsbykris.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation version 3 of the License.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
######################################################################

#Sends SMS messages to contact
#fzf required to contact filter
log="/data/local/tmp/sms_recent.log"

function contacts(){
  [[ -f "$log" ]] && sort -u "$log"
  content query --uri content://com.android.contacts/data --projection display_name:data4:contact_id|grep '=+'|
  sed 's/display_name=/|/g;s/, data4=/|/g;s/,/|/g'|cut -d\| -f2,3
}

contact="$(contacts|fzf)"
[[ $contact ]] || exit 1
# log contact to recent log
echo "$contact" >> "$log"

name="${contact%\|*}"
number="${contact##*\|}"

[[ $number ]] || exit 1

echo -e "\n\n\n"
while [ 1 ]
do
  echo -n "Sending to $name at $number: "
  read msg
  [[ $msg ]] || exit
  #older versions of android
  #service call isms 5 i32 0 s16 "com.android.mms.service" s16 "null" s16 "$number" s16 "null" s16 "$msg" s16 "null" s16 "null" s16 "null" s16 "null" > /dev/null
  #Android 10 and newer
  adb shell service call isms 5 i32 1 s16 "com.android.mms" s16 "null" s16 "$number" s16 "null" s16 "$msg" s16 "null" s16 "null" i32 0 i64 0
done