[How-to] Saving SMS / text conversations
asked 2014-08-22 18:50:00 +0200
This post is a wiki. Anyone with karma >75 is welcome to improve it.
Here is a script to save SMS (text) messages on a Jolla mobile.
- You can run the script from either Terminal or logged in through SSH.
- Run the script as a regular user (do not use "devel-su")
- This script will print the messages to stdout (after nicely formatting them).
- Although this script may have bugs, it will not damage or delete any SMS messages. It only reads the messages from the internal communication database.
You can choose to name the script anything you like (for example, "sms-dump"). You can save the output to a text file by redirecting the output (for example, "sms-dump > sms-with-mom.txt"). Make the file executable ("chmod 755 sms-dump"). A convenient place to save it is in "/home/nemo/bin", which is already in the default path.
NOTE: This is a wiki page. Please feel free to contribute documentation, bug fixes, and any other improvements to this script.
#!/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="Me"
# 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
Usage
sms-dump <phone-number> [Name]
sms-dump 4565551234 John # Example
Known Issues
- Messages that have a newline character will still be printed but will cause an error message to appear.
Thanks for this, much appreciated! It would be great if someone could wrap a simple UI around it as I suppose that would be trivial.
nthn ( 2014-08-22 20:32:29 +0200 )editThanks!
Now I wanted to dump all my sms conversations, with all senders/recipients, to one text file. I managed to do this by modifying your script (removing the
remoteUid LIKE '%$1'
condition). But I'd like the script to also output the sender's number (so that every message would be output like[number] [timestamp] [message]
). How should I modify the script to achieve this? (I have a hunch, but I'm really not good at this, so if you can easily give an answer I'd appreciate it :)Update: Never mind, I managed to do it. Learned something about
sed
andcut
while I was at it :) I'll soon post the modified script as an answer below in case someone's interested.(Why I wanted to do this? My Jolla is going to care and I'm using another phone for the time being, so I wanted to save all my sms's in case it gets flashed. I also have the .vault backup stored on my computer, but I wanted to have the sms's in plain text also.)
ssahla ( 2014-08-22 21:52:07 +0200 )editThis script does not support MMS. Since I wanted those as well, I made https://github.com/tkalliom/convert-to-sbr-xml (though I didn’t figure out how to access MMS without the parts stored locally, so even my script misses some MMS). Use mine at your own risk.
Timo K ( 2017-09-02 17:57:21 +0200 )edit