Advertisement
metalx1000

Android Last 50 SMS Messages with Names from Contacts

Feb 26th, 2023 (edited)
1,481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.92 KB | None | 0 0
  1. #!/system/bin/sh
  2. ######################################################################
  3. #Copyright (C) 2023  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation version 3 of the License.
  9.  
  10. #This program is distributed in the hope that it will be useful,
  11. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #GNU General Public License for more details.
  14.  
  15. #You should have received a copy of the GNU General Public License
  16. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. ######################################################################
  18.  
  19. # Device needs to be ROOTED
  20. # jq and sqlite3 are needed (you can get these compiled already for Termux)
  21. # use Android Shell or ADB Shell to check the last 50 Text Messages
  22.  
  23. sms_db="/data/data/com.android.providers.telephony/databases/mmssms.db"
  24. contact_db="/data/data/com.android.providers.contacts/databases/contacts2.db"
  25.  
  26. sql_cmd="SELECT * FROM sms ORDER BY date DESC LIMIT 50"
  27. sql_cmd="SELECT * FROM ($sql_cmd) ORDER BY date ASC;"
  28.  
  29. #this function will check you contacts for the contacts name
  30. #based on the phone number in the message
  31.  
  32. function insert_name(){
  33. sqlite3 -json $sms_db "$sql_cmd"|tr -d "["|tr -d "]"|while read l
  34. do
  35.   type="$(echo "$l"|sed 's/,$//g'|jq ".type")"
  36.   if [[ $type == "2" ]]
  37.   then
  38.     name="You"
  39.   else
  40.     number="$(echo "$l"|sed 's/,$//g'|jq ".address")"
  41.     sql_cmd="SELECT raw_contact_id FROM phone_lookup WHERE normalized_number = $number"
  42.     sql_cmd="SELECT display_name FROM raw_contacts WHERE _id = ($sql_cmd);"
  43.     name="$(sqlite3 $contact_db "$sql_cmd")"
  44.   fi
  45.  
  46.   [[ $name ]] || name="Unkown"
  47.   echo "$l"|sed "s/^{/{ \"name\":\"$name\",/g"
  48. done
  49. }
  50.  
  51. echo "[$(insert_name)]"|jq .
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement