]> git.defcon.no Git - hermes/blobdiff - guc-clients/assignphone
Updates: showsipuser tool to show a sip user, listphones to list registered phones...
[hermes] / guc-clients / assignphone
index 074ab1fd8b32853453e6e17a898d7d0d4d68235e..bd8a623b8676ab11f3cd72dba9565ffe858acd88 100755 (executable)
@@ -13,7 +13,9 @@ my $api_key;
 
 my $help;
 my $username;
-my $configfile;
+my $configfile = undef;
+my $remove = 0;
+my $dryrun = 0;
 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
 my ($user, $sipuser, $mac);
 
@@ -22,11 +24,21 @@ my $config = AppConfig->new({ CREATE => 1 });
 $config->define("api_url=s");
 $config->define("api_keyfile=s");
 
+foreach ( 
+       "/usr/local/etc/hermes/hermes_config",
+       "/usr/local/etc/hermes/config",
+       "/etc/hermes/config",
+       $ENV{"HOME"} . "/.hermes/config",
+       $ENV{"HOME"} . "/.hermes_config",
+) { $configfile = $_ if ( -f $_ ); }
+
 GetOptions(
        "help"          => \$help,
        "macaddress=s"  => \$mac,
        "username=s"    => \$username,
        "configfile=s"  => \$configfile,
+       "remove"        => \$remove,
+       "dryrun"        => \$dryrun,
 );
 
 if (
@@ -57,6 +69,8 @@ Verify that the following options are set:
        --configfile=s|--config|-c
        --username=s|--user|-u
        --macaddress=s|--mac|-m
+       --remove|-r
+       --dryrun|--dry|-d
 
 Verify the contents of the configuration file.
 Verify that the key-file exists.
@@ -105,15 +119,41 @@ if ( not $data->{'response'} eq 'ok' )
        logout();
        exit;
 }
+if ( $remove )
+{
+       $data = exec_apinode("phone/get", { 'user' => $sipuser, 'mac' => $mac });
+       if ( not $data->{'response'} eq 'ok' )
+       {
+               printf("Unable to remove phone+user, lookup gave: '%s'\n", $data->{'cause'});
+               logout();
+               exit;
+       }
+}
+if ( $dryrun ) {
+       print("Dryrun specified. All OK so far, stopping before add.\n");
+       logout();
+       exit;
+}
+if ( $remove )
+{
+       $data = exec_apinode("phone/remove", { 'user' => $sipuser, 'mac' => $mac });
 
-$data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
+       printf("Failed to remove phone+user, cause given: '%s'\n", $data->{'cause'})
+               if ( not $data->{'response'} eq 'ok' );
 
-printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
-       if ( not $data->{'response'} eq 'ok' );
+       printf("Removed assigned phone with mac '%s' from user '%s'\n", $mac, $sipuser)
+               if ( $data->{'response'} eq 'ok' );
+}
+else
+{
+       $data = exec_apinode("phone/add", { 'user' => $sipuser, 'mac' => $mac });
 
-printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
-       if ( $data->{'response'} eq 'ok' );
+       printf("Failed to add phone+user, cause given: '%s'\n", $data->{'cause'})
+               if ( not $data->{'response'} eq 'ok' );
 
+       printf("Assigned phone with mac '%s' to user '%s'\n", $mac, $sipuser)
+               if ( $data->{'response'} eq 'ok' );
+}
 logout();
 ################################################################################################
 sub exec_apinode($$)