'kamailio_subscriber_table' => 'subscriber',
'kamailio_alias_table' => 'dbaliases',
'kamailio_domain_table' => 'domain',
+ 'kamailio_location_table' => 'location',
'hermes_users_table' => 'users',
'hermes_phones_table' => 'phones',
'auth_backend' => 'permitall',
'sessionkey_lifetime' => 5, // Minutes
- 'standard_dialplan' => '(*xx*.|xxx.)',
+ 'standard_dialplan' => '(**x*x.|*xx*|*xx*x.|*xx*x.*|*xx|*xx.|xxx.)',
'default_domain' => 'hig.no',
'permit_multiple_e164alias' => false,
'standard_password_length' => 10,
function list_users ( $search = null )
{
global $config;
- $query = sprintf("SELECT CONCAT(username, '@', domain), displayname FROM %s ORDER BY username,domain", $config['hermes_users_table'] );
+ $query = sprintf("SELECT CONCAT(username, '@', domain), displayname, username, domain FROM %s ORDER BY username,domain", $config['hermes_users_table'] );
if ( array_key_exists ( 'search', $_POST ) )
{
$search = $_POST['search']; // TODO: Add some sanitation and input validation!
- $query = sprintf("SELECT CONCAT(username, '@', domain) FROM %s WHERE CONCAT(username, '@', domain) LIKE '%%%s%%' ORDER BY username,domain", $config['hermes_users_table'], sql_clean( $search ) );
+ $query = sprintf("SELECT CONCAT(username, '@', domain), displayname, username, domain FROM %s WHERE CONCAT(username, '@', domain) LIKE '%%%s%%' ORDER BY username,domain", $config['hermes_users_table'], sql_clean( $search ) );
}
$result = sql_dbquery( $config['hermes_db'], $query );
$list = array();
while ( $row = @mysql_fetch_row( $result ) )
{
- array_push( $list, array( "user" => $row[0], "displayname" => $row[1] ) );
+ $online = is_online( $row[2], $row[3] ) ? online : offline;
+ array_push( $list, array( "user" => $row[0], "displayname" => $row[1], "status" => $online ) );
}
return $list;
print json_encode( array( 'response' => 'ok', 'list' => $list ));
return $user;
}
-
+
+function get_locations( $username, $domain )
+{
+ global $config;
+
+ $query = sprintf ("SELECT contact, UNIX_TIMESTAMP(expires) as expires, user_agent, socket FROM %s WHERE username = '%s' AND (domain = '%s' OR domain is NULL)",
+ $config['kamailio_location_table'],
+ sql_clean($username),
+ sql_clean($domain));
+
+ $result = sql_dbquery( $config['kamailio_db'], $query );
+ if ( !$result ) return null;
+ $list = array();
+ while ( $row = @mysql_fetch_row( $result ) )
+ {
+ array_push( $list, array( "contact" => $row[0], "expires" => $row[1], "useragent" => $row[2] ) );
+ }
+ return $list;
+}
+
+function is_online( $username, $domain )
+{
+ if ( get_locations( $username, $domain ) != null ) return true;
+ else return false;
+}
+
?>
%EXPORT_TAGS = ( 'all' => [ qw( ) ] );
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
}
-our $api_base = "http://10.0.2.5/hermes/api/";
-our $api_key = "4hErgw3QFJLeuXGW";
+our $api_base = "http://sippbx.hig.no/hermes/api/";
+our $api_key = "e065gBpUwqzY0a5o";
our $invalid_key = "invalidkey" . int(rand(255));
our $test_username = "testauth-" . int(rand(255));
undef $data;
$data = exec_apinode("user/gen_pw", undef);
-ok( $data =~ m/[a-zA-Z0-9]{24}/, 'user/gen_pw format OK');
+ok( $data =~ m/[a-zA-Z0-9]{8}/, 'user/gen_pw format OK');
$temp = $data;
undef $data;
else
print json_encode ( array( 'response' => 'invalid') );
break;
+
+ case "/location":
+ if ( array_key_exists('user', $_POST) ||
+ ( array_key_exists('username', $_POST) && array_key_exists('domain', $_POST )))
+ {
+ $username = "";
+ $domain = "";
+ if ( array_key_exists('username', $_POST) )
+ {
+ $username = $_POST['username'];
+ $domain = $_POST['domain'];
+ }
+ else
+ {
+ $user = split_sipaddress($_POST['user']);
+ if ( !$user )
+ {
+ print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
+ break;
+ }
+ list ( $username, $domain ) = $user;
+ }
+ $location = get_locations( $username, $domain );
+ if ( $location == null )
+ {
+ print json_encode( array( 'response' => 'failed', 'cause' => 'offline' ));
+ break;
+ }
+ print json_encode( array( 'response' => 'ok', 'locations' => $location ));
+ }
+ else
+ print json_encode ( array( 'response' => 'invalid') );
+ break;
+
case "/list":
/*
Simply list all users in user@domain format
Returns 'failed' with 'cause' = 'nonexistant' if user does not exist..
Returns 'failed' with 'cause' = 'invalid' if SIP address is malformed.
+user/location
+-------------------
+ Required:
+ user=user@bar.bz *
+
+ Description:
+ Return registered user agent locations for a given user, with
+ contact and user agent information.
+
+ Return:
+ On success, returns the array 'locations', each element contianing:
+ 'contact' is the registration contact (the location)
+ 'useragent' is the UA string from the client
+ 'expires' is the expiration time of the registration, as unix timestamp
+
+ Returns 'failed' with 'cause' = 'offline' if user is not registered ...
+ Returns 'failed' with 'cause' = 'invalid' if SIP address is malformed.
+
+ TODO:
+ Add a return code for non-existant subscriber?
+
user/list
-------------------
Optional:
TODO list:
---------------------
+user/forward To set up permanent forwarding. Call forward is stored as a user
+ preference value in the kamailio database. Evample of diverting calls to
+ 'jonl@hig.no' to the number +4761135280':
+insert into usr_preferences (username, domain, attribute, value, last_modified) values ( 'jonl', 'hig.no', 'cfu', '+4761135280', now());
+
Some TODO's listed above ...
Permissions! (user/permissions?user=...)
foreach my $u ( @users )
{
printf("%-24s", $u->{'user'});
+ printf("%-12s", $u->{'status'});
my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} });
if ( $alias_data->{'response'} eq 'ok' )
{