I want to efficiently check for all local address book contacts if they have a jabber account. The users are registered on the XMPP server with the mobile phone number.
I currently send the following XEP-0055 stanza to the ejabberd server and evaluate the result.
<iq type="set" id="searchByUserName" to="vjud.ecoimac1.local" from="+1222333444@ecoimac1.local">
<query xmlns="jabber:iq:search">
<x xmlns="jabber:x:data" type="submit">
<field type="hidden" var="FORM_TYPE">
<value>jabber:iq:search</value>
</field>
<field var="user">
<value>+123456789</value>
</field>
</x>
</query>
</iq>
This works but is not efficient. For each number a query has to be sent. Other apps like WhatsApp do the same job. Is there a more efficient way?
I have now following efficient solution:
On server side I did set up a MySQL database backend for eJabberd and on the web server a PHP interface provides a user lookup for the client application.
The app sends a HTTP POST request with a set of jabber user names to check. The PHP script looks up which users are registered and returns a response containing the indices of the registered users.
<?php
// HTTP POST sent from client app
// Do authentication here...
// Read all parameters
$users = array();
$user = $_POST["username0"];
for ($i = 1; !empty($user); $i++)
{
// Add user
$users[] = $user;
// Next parameter
$user = $_POST["username".$i];
}
if (empty($users))
{
echo "Success"; // nothing to do
exit;
}
// Create mySQL connection
$db = mysqli_connect("localhost", "user", "password", "database");
if (!$db)
{
$descr = mysqli_connect_error();
echo "Error: ".$descr;
exit;
}
// Prepare query
// SELECT username FROM users WHERE username IN ('+41796548345','+41786547544',...)
$query = "SELECT username FROM users WHERE username IN(";
foreach ($users as $user)
{
$query .= "'".$user."',";
}
$query = rtrim($query, ",");
$query .= ")";
// Execute query and generate result
$result = "Success";
$recset = mysqli_query($db, $query); // [+41786547544,+41763428566,...]
while ($rec = mysqli_fetch_object($recset))
{
$index = find_user($rec->username, $users);
if ($index >= 0)
{
$result .= "\t".$index;
}
}
echo $result;
function find_user($username, $users)
{
for ($idx = 0; $idx < count($users); $idx++)
{
if ($users[$idx] == $username)
{
return $idx; // found at index
}
}
return -1; // not found
}
?>