baumi's blog

baumi's personal blog … Linux, OS X, Windows, Random things, …

LinPhone VOIP client softphone: Importing SIP contacts from Google Contacts via commandline using PHP on GNU/Linux

The following command will add contact “Demo” to your linphone contact database using sqlite3:

sqlite3 ~/.var/app/com.belledonnecommunications.linphone/data/linphone/friends.db "INSERT INTO friends (friend_list_id, sip_uri, subscribe_policy, send_subscribe, ref_key, vCard, vCard_etag, vCard_url, presence_received) VALUES (1, 'sip:+436641234567@fairytel.at', 1, 0, NULL, 'BEGIN:VCARD' || char(10) || 'VERSION:4.0' || char(10) || 'IMPP:sip:+4366412346567@fairytel.at' || char(10) || 'FN:Demo' || char(10) || 'IMPP:sip:+4366412346567@213.185.165.102' || char(10) || 'END:VCARD', NULL, NULL, 0);" ".exit"

Please note: ~/.var/app/com.belledonnecommunications.linphone/data/linphone/friends.db is the correct path for Linphone when installed using flatpak. You might need to adjust your path to ~/.local/linphone/ or ~/.config/linphone/ depending on how you’ve installed LinPhone, i.e. where LinPhone is storing its database.

Google Contacts export to CSV/VCF:
See details here.

You can export the whole Google Contacts list to .vcf or .csv and then use a small script to import your contacts into LinPhone. Be warned, the following script is pretty ugly, yet it’s a start:


<?php

$str = file_get_contents( "contacts.vcf" );
$tmp = explode( "\r\n", $str );
$number = $vcard = "";
foreach ( $tmp as $line )
{
if ( strpos($line, "N:;") !== false) continue;
if ( strpos($line, "ADR:;") !== false) continue;

$line = str_replace( "VERSION:3.0", "VERSION:4.0", $line );
$line = str_replace( "item1.ORG:", "FN:", $line );
$line = str_replace( "`", "''", $line );
$line = str_replace( ",", " ", $line );
$line = str_replace( "\\,", " ", $line );
$line = str_replace( "\\", " ", $line );

if ( strpos($line, "item1.") !== false) continue;
if ( strpos($line, "item2.") !== false) continue;
if ( strpos($line, "item3.") !== false) continue;
if ( strpos($line, "item4.") !== false) continue;

if ( $line == "BEGIN:VCARD" ) $vcard = "";
if ( strpos($line, "TEL;") !== false)
{
$number = explode(":", $line)[1];
if ( $number == "" ) $number = explode(";", $line)[1];

$number = str_replace( " ", "", $number );
$number = str_replace( "-", "", $number );
$number2 = str_replace( "+", "00", $number );

$vcard .= "IMPP:sip:" . $number . "@fairytel.at' || char(10) || '";
$vcard .= "IMPP:sip:" . $number2 . "@213.185.165.102' || char(10) || '";
$vcard .= "IMPP:sip:" . $number2 . "@213.185.165.103' || char(10) || '";
continue;
}
$vcard .= $line;

if ( $line == "END:VCARD" )
{
$sipuri = "sip:$number@fairytel.at";
$sipuri2 = "sip:$number2@fairytel.at";
$cmd = "sqlite3 ~/.var/app/com.belledonnecommunications.linphone/data/linphone/friends.db \"DELETE FROM friends WHERE sip_uri='$sipuri' OR sip_uri='$sipuri2'\" \".exit\" ";
shell_exec( $cmd );

$cmd = "sqlite3 ~/.var/app/com.belledonnecommunications.linphone/data/linphone/friends.db \"INSERT INTO friends (friend_list_id, sip_uri, subscribe_policy, send_subscribe, ref_key, vCard, vCard_etag, vCard_url, presence_received) VALUES (1, '$sipuri', 1, 0, NULL, '" . $vcard . "', NULL, NULL, 0);\" \".exit\" \n";
shell_exec( $cmd );
}
$vcard .= "' || char(10) || '";
}

?>

Comments are currently closed.