Skip to content

Instantly share code, notes, and snippets.

@bodqhrohro
Last active June 25, 2024 20:19
Show Gist options
  • Save bodqhrohro/f065a3f2dbede786bcc9ed865db8119e to your computer and use it in GitHub Desktop.
Save bodqhrohro/f065a3f2dbede786bcc9ed865db8119e to your computer and use it in GitHub Desktop.
ICQ backup
#!/bin/bash
# requires: curl, uuidgen, jq, coreutils
# Pass the aimsid argument to it; open the network tab in devtools and obtain from some request, lots of it contain it.
# The rest is done automatically, it would make a local copy of your contact list, contact profiles, chat history in the current directory.
#
# I don't guarantee this script would work for you and fetch ALL your valuable data, use with caution.
aimsid="$1"
req()
{
uuid=$(uuidgen)
curl -# 'https://u.icq.net/api/v92'"$1" -X POST -H 'Content-Type: application/json' --data-raw '{"reqId":"'$uuid'","aimsid":"'"$aimsid"'","params":'"$2"'}'
}
if [[ -f events0.json ]]
then
read -p "This directory already contains an ICQ backup. Overwrite? (y/n)" y
if [[ $y != y ]]
then
echo "Aborting"
exit
fi
fi
curl -# 'https://u.icq.net/api/v92/bos/nicq-b023s/aim/fetchEvents?aimsid='"$aimsid"'&first=1&rnd='"$RANDOM.$RANDOM"'&timeout=500&supportedSuggestTypes=text-smartreply%2Csticker-smartreply' > events0.json
echo "Fetched events0"
buddies=$(cat events0.json|jq -r '.response.data.events|map(select(.type=="buddylist").eventData.groups|map(.buddies|map(.aimId)))|flatten'|grep '"'|cut -d '"' -f 2)
mkdir -p buddies
mkdir -p avatars
mkdir -p chatlogs
echo "$buddies"|while read buddy
do
name=buddies/"$buddy".json
req /rapi/getUserInfo '{"sn":"'"$buddy"'"}' > $name
echo "Fetched buddy $buddy"
curl -# 'https://ub.icq.net/api/v92/files/avatar/get?targetSn='"$buddy"'&aimsid='"$aimsid"'&size=256' > avatars/"$buddy".jpg
echo "Fetched avatar of $buddy"
chatlogsdir=chatlogs/"$buddy"
mkdir -p "$chatlogsdir"
fromMsgId=-1
for (( i=0; ; i++ ))
do
iname=$chatlogsdir/$i.json
req /rapi/getHistory '{"sn":"'"$buddy"'","fromMsgId":"'"$fromMsgId"'","count":-20,"lang":"en","patchVersion":"7372581143364763697"}' > $iname
[[ -f "$iname" ]] || break
olderMsgId=$(cat $iname |jq -r '.results.olderMsgId')
if [[ $olderMsgId != null ]]
then
lastMsgId=$(cat $iname |jq -r '.results.messages[-1].msgId')
if [[ $lastMsgId != null ]]
then
fromMsgId="$lastMsgId"
else break; fi
else break; fi
done
echo "Fetched chatlogs of $buddy"
done
echo "Done querying buddies"
mkdir -p persons
cat chatlogs/**/*.json|jq '.results.persons|map(.sn)|flatten' 2>/dev/null|grep '"'|cut -d '"' -f 2|sort|uniq|while read buddy
do
if ! [[ -f "buddies/$buddy.json" ]]
then
req /rapi/getUserInfo '{"sn":"'"$buddy"'"}' > "persons/$buddy.json"
echo "Fetched person $buddy"
curl -# 'https://ub.icq.net/api/v92/files/avatar/get?targetSn='"$buddy"'&aimsid='"$aimsid"'&size=256' > "persons/$buddy.jpg"
echo "Fetched avatar of $buddy"
fi
done
echo "Done querying persons"
mkdir -p stickers
cat chatlogs/**/*.json|jq '.results.messages|map(select(.sticker)|.sticker.id)' 2>/dev/null|grep '"'|cut -d '"' -f 2|sort|uniq|while read sticker
do
packid=$(echo "$sticker"|cut -d ':' -f 2)
sid=$(echo "$sticker"|cut -d ':' -f 4)
curl -# "https://icq.com/store/stickers/$packid/$sid/large.png" > "stickers/${packid}_${sid}.png"
done
echo "Done fetching stickers"
mkdir -p fileinfo
mkdir -p files
cat chatlogs/**/*.json|jq .|grep -o 'https://files\.icq\.net/get/.\{33\}'|sort|uniq|while read fileurl
do
fileid=${fileurl:26}
finame="fileinfo/$fileid.json"
curl -# 'https://u.icq.net/api/v92/files/info/'"$fileid"'/?aimsid='"$aimsid"'&previews=192%2C600%2C800%2Cxlarge' > "$finame"
dlink=$(cat "$finame"|jq -r '.result.info.dlink')
curl -# -o "files/$fileid" "$dlink"
echo "Fetched file $fileid"
done
echo "All done"
@bodqhrohro
Copy link
Author

Important note: it's better to take the aimsid token from a freshly logged in session (not necessarily on the same IP). The events request returns something weird with a token from my old one.

@S0m3Th1nG-AwFul
Copy link

S0m3Th1nG-AwFul commented Jun 16, 2024

Hello! It does giving me some strange output endlessly even with fresh aimsid with no final result at all:
image
Creates only empty events0.json and nothing more. Changing %3A to : doesn't make any difference.
Tested on Ubuntu 22.04 LTS.

@bodqhrohro
Copy link
Author

Well, I can't help much with no thorough debugging. Try to add the -v flag to curl and look for the result, most likely the server returns some error. I've got 504 right now just when logging in to the web UI. If it became so unreliable, fetching the data would become complicated already, as this script is simple and doesn't repeat any failed queries.

@bodqhrohro
Copy link
Author

And BTW, I was also suggested with using Miranda-NG for making a backup (but that would probably do only for the chat history, not for complete profile information or files).

@bodqhrohro
Copy link
Author

@S0m3Th1nG-AwFul I changed one letter in the endpoint name, try again. Seems like they upgraded it, and both the old one and new one worked for a while, but not for everyone. Hurry up, as it can go down at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment