From 0f6b80ae70f9978080aeaf3f155bab79ded1916f Mon Sep 17 00:00:00 2001 From: Jon Langseth Date: Thu, 19 Apr 2012 15:24:11 +0200 Subject: [PATCH 1/1] Added tool to list users... ALL users... --- guc-clients/listsipusers | 168 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100755 guc-clients/listsipusers diff --git a/guc-clients/listsipusers b/guc-clients/listsipusers new file mode 100755 index 0000000..3648cd0 --- /dev/null +++ b/guc-clients/listsipusers @@ -0,0 +1,168 @@ +#!/usr/bin/perl +use strict; + +#TODO: Add support for assigning phone number +#TODO: Add support for overriding default domain ... + +use Getopt::Long; +use Net::LDAP; +use Net::LDAP::Control::Paged; +use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); +use LWP; +use JSON; +use AppConfig; +use Text::Iconv; + +use Data::Dumper; + +my $utf2iso = Text::Iconv->new("utf-8","latin1"); +my $api_key; + +my $help; +my $username; +my $configfile; +my $dryrun = 0; +my ($g_ua, $session, $api_key, $auth_key, $data, $domain); +my ($user, $displayname, $phone, $mail, $sipuser, $linetext); + +my $config = AppConfig->new({ CREATE => 1 }); + +$config->define("api_url=s"); +$config->define("api_keyfile=s"); + +#TODO: Move this to config file. +$config->default_domain("hig.no"); + +GetOptions( + "help" => \$help, + "configfile=s" => \$configfile, +); + +if ( + (not $configfile) || + (( $configfile ) && ( not -f $configfile )) +) +{ + $help = 1; +} + +$config->file( $configfile ); + +if ( ( not $config->api_url ) || + ( not $config->api_keyfile ) || + ( $config->api_keyfile && not -f $config->api_keyfile ) +) +{ + $help = 1; +} + +if ( $help ) { +print <api_keyfile; +chomp( $api_key = ); +close KEY; + +$g_ua = LWP::UserAgent->new; +$g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant + +login_apikey(); + +$data = exec_apinode("user/list", { }); +if ( not $data->{'response'} eq 'ok' ) +{ + printf("Unable to fetch user list: %s\n", $data->{'cause'}); + exit; +} + +my $t = $data->{'list'}; +my @users = @$t; + +foreach my $u ( @users ) +{ + printf("%-24s", $u->{'user'}); + my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} }); + if ( $alias_data->{'response'} eq 'ok' ) + { + #print Dumper($alias_data); + my $t = $alias_data->{'aliases'}; + foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t ) + { + printf("\t%-24s", $a->{'alias'}); + } + } + else + { + } + print "\n"; +} + +undef $data; + +logout(); +################################################################################################ +sub exec_apinode($$) +{ + my $node = shift; + my $param = shift; + + my ( $response, $data ); + + $session = "" if not defined $session; + $auth_key = "" if not defined $auth_key; + my $url = $config->api_url . "/" . $node; + + $param->{'session'} = $session; + $param->{'auth_key'} = $auth_key; + + $response = $g_ua->post( $url, $param ); + if ( $response->is_success ) + { + if ( $response->content =~ m/\s*{/ ) + { + $data = decode_json( $response->content); + } + else + { + $data = $response->content; + } + + } + return $data; +} + +sub login_apikey +{ + my $response = $g_ua->post( $config->api_url . "/auth/login", + [ "api_key" => $api_key ] ); + + my $data = decode_json( $response->content) if $response->is_success; + die("HTTP error") unless $response->is_success; + + if ( $data->{'response'} eq "ok" ) + { + $session = $data->{'session'}; + $auth_key = $data->{'auth_key'}; + } + else + { + print "Unable to log in to Hermes API\n"; + exit; + } + undef $data; undef $response; +} + +sub logout +{ + my $response = $g_ua->post( $config->api_url . "/auth/logout", + [ "session" => $session ] ); + die("HTTP error") unless $response->is_success; + undef $session; undef $auth_key; +} -- 2.39.2