this script extract from the (uncompressed] date base all the SMS related to a given phone number
#!/bin/bash
# A script to dump text messages to stdout
#
# Usage:
# sms-dump <phone-number> [Name]
#
# phone-number : A phone number (example: 4565551234)
#
# Name : (Option) The contact's name, as it will appear in the output.
# If not provided then it will use the phone number.
# My name, as it will appear in the output
me="jpa"
# The database with the SMS messages
sql_database="/home/nemo/.local/share/commhistory/commhistory.db"
# The SQL command to select which messages to retrieve
# direction : Who sent the message, can be 1 (them) or 2 (me)
# startTime : When the message was sent
# freeText : The text of the actual message
# remoteUid : The contact's phone number
# type : Messages are of type 2
sql_command="SELECT direction, startTime, freeText FROM Events WHERE remoteUid LIKE '%$1' AND type=2;"
# Don't let root run this
if [ $UID -eq 0 ]; then
echo "Do not run as root."
exit 1
fi
# Check for a phone number as a command line argument
if [ $# -lt 1 ]; then
echo "usage: $(basename $0) <phone-number> [Name]"
exit 1
fi
# Set the contact's name, if provided
if [ $# -gt 1 ]; then
contact="$2"
else
contact="$1"
fi
# Get the message data from the phone's database
# Parse each line and print it, nice and pretty
sqlite3 "$sql_database" "$sql_command" | while read line; do
# Check for the desired format...
format=`echo $line | sed -e 's/^[12]|[0-9].*|.*/CORRECTFORMAT/'`
if [ "$format" == "CORRECTFORMAT" ]; then
# Get the name of the person who's texting
fromwhom=`echo $line | cut -d '|' -f 1`
if [ $fromwhom -eq 1 ]; then
fromwhom="$contact"
else
fromwhom="$me"
fi
# Convert the Unix timestamp to a human readable format
unixtime=`echo $line | cut -d '|' -f 2`
datetime=`date -d@${unixtime} "+%Y-%m-%d %H:%M"`
# Get the actual text message
message=`echo $line | cut -d '|' -f 3`
# Copy all the information into a new file
echo "$fromwhom (${datetime}): $message"
else
# ...Fallback, just copy the line
echo "$line"
fi
done