#!/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 .
######################################################################
# Device needs to be ROOTED
# jq and sqlite3 are needed (you can get these compiled already for Termux)
# use Android Shell or ADB Shell to check the last 50 Text Messages
sms_db="/data/data/com.android.providers.telephony/databases/mmssms.db"
contact_db="/data/data/com.android.providers.contacts/databases/contacts2.db"
sql_cmd="SELECT * FROM sms ORDER BY date DESC LIMIT 50"
sql_cmd="SELECT * FROM ($sql_cmd) ORDER BY date ASC;"
#this function will check you contacts for the contacts name
#based on the phone number in the message
function insert_name(){
sqlite3 -json $sms_db "$sql_cmd"|tr -d "["|tr -d "]"|while read l
do
type="$(echo "$l"|sed 's/,$//g'|jq ".type")"
if [[ $type == "2" ]]
then
name="You"
else
number="$(echo "$l"|sed 's/,$//g'|jq ".address")"
sql_cmd="SELECT raw_contact_id FROM phone_lookup WHERE normalized_number = $number"
sql_cmd="SELECT display_name FROM raw_contacts WHERE _id = ($sql_cmd);"
name="$(sqlite3 $contact_db "$sql_cmd")"
fi
[[ $name ]] || name="Unkown"
echo "$l"|sed "s/^{/{ \"name\":\"$name\",/g"
done
}
echo "[$(insert_name)]"|jq .