Skip to content

Instantly share code, notes, and snippets.

@christf
Created January 25, 2023 23:35
Show Gist options
  • Save christf/bfecda0d60a055d45ca2990a955a76f5 to your computer and use it in GitHub Desktop.
Save christf/bfecda0d60a055d45ca2990a955a76f5 to your computer and use it in GitHub Desktop.
this conveys a directory of vcf files (as obtained by vdirsyncer) into the friends.db sqlite database from linphone such that the contacts are visible in linphones addressbook
#!/bin/bash
dbfile=~/.local/share/linphone/friends.db # the sqlite3 database from linphone used for the address book
contactsdir=~/.contacts # the directory that is used by vdirsyncer to place the vcf files
asterisk_hostname=ned # use your asterisks hostname here
# vcard parser
# sed 's/.\{75\}/&\n /g'
# get all the lines that begin with space and join them to the previous one
# all the lines end up in an array called lines
concatenator () {
local i=0
lines=()
while read -r || [[ $REPLY ]]; do
if [[ ${REPLY:0:1} = " " ]]; then
lines[i]+=${REPLY:1}
else
lines[++i]+=$REPLY
fi
done
}
# stores the fields in vcard
parser () {
concatenator
for line in "${lines[@]}"; do
vcard[${line%%:*}]=${line#*:}
done
}
declare -A vcard names linphonevcf
vcfparse(){
vcard=()
parser < $1
unset "vcard[BEGIN]" "vcard[END]" "vcard[VERSION]" "vcard[UID]"
linphonevcf=$(
printf "BEGIN:VCARD\n"
printf "VERSION:4.0\n"
printf "FN:${vcard[FN]}\n"
for line in "${lines[@]}"
do
if [[ ${line:0:3} == "TEL" ]]
then
sip="sip:$(strings <<< ${line#*:}|sed -e 's/-//g' -e 's/+/00/' -e 's/ //g' |tr -d '()')@$asterisk_hostname"
printf "IMPP:$sip\n"
fi
done
printf "END:VCARD\n\n"
)
}
runsql() {
sqlite3 "$dbfile" "$@"
}
runsql "delete from friends;"
for contact in $contactsdir/*.vcf $contactsdir/*/*.vcf
do
vcfparse "$contact"
sipid=sip:$(grep -m1 -E IMPP:sip:[0-9]+ <<< $vcf | cut -d: -f3)
runsql "INSERT INTO friends (friend_list_id, sip_uri, subscribe_policy, send_subscribe, ref_key, vCard, vCard_etag, vCard_url, presence_received) VALUES (1, NULL, 1, 0, NULL, \"${linphonevcf}
\", NULL, NULL, 0);"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment