]>
git.defcon.no Git - hermes/blob - guc-clients/listsipusers
4 #TODO: Add support for assigning phone number
5 #TODO: Add support for overriding default domain ...
9 use Net
::LDAP
::Control
::Paged
;
10 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
18 my $utf2iso = Text
::Iconv
->new("utf-8","latin1");
25 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
26 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
28 my $config = AppConfig
->new({ CREATE
=> 1 });
30 $config->define("api_url=s");
31 $config->define("api_keyfile=s");
33 #TODO: Move this to config file.
34 $config->default_domain("hig.no");
38 "configfile=s" => \
$configfile,
43 (( $configfile ) && ( not -f
$configfile ))
49 $config->file( $configfile );
51 if ( ( not $config->api_url ) ||
52 ( not $config->api_keyfile ) ||
53 ( $config->api_keyfile && not -f
$config->api_keyfile )
61 Verify that the following options are set:
64 Verify the contents of the configuration file.
65 Verify that the key-file exists.
69 open KEY, "<" . $config->api_keyfile;
70 chomp( $api_key = <KEY> );
73 $g_ua = LWP::UserAgent->new;
74 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
78 $data = exec_apinode("user/list", { });
79 if ( not $data->{'response'} eq 'ok' )
81 printf("Unable to fetch user list: %s\n", $data->{'cause'});
85 my $t = $data->{'list'};
88 foreach my $u ( @users )
90 printf("%-24s", $u->{'user'});
91 my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} });
92 if ( $alias_data->{'response'} eq 'ok' )
94 #print Dumper($alias_data);
95 my $t = $alias_data->{'aliases'};
96 foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t )
98 printf("\t%-24s", $a->{'alias'});
110 ################################################################################################
116 my ( $response, $data );
118 $session = "" if not defined $session;
119 $auth_key = "" if not defined $auth_key;
120 my $url = $config->api_url . "/" . $node;
122 $param->{'session'} = $session;
123 $param->{'auth_key'} = $auth_key;
125 $response = $g_ua->post( $url, $param );
126 if ( $response->is_success )
128 if ( $response->content =~ m/\s*{/ )
130 $data = decode_json( $response->content);
134 $data = $response->content;
143 my $response = $g_ua->post( $config->api_url . "/auth/login",
144 [ "api_key" => $api_key ] );
146 my $data = decode_json( $response->content) if $response->is_success;
147 die("HTTP error") unless $response->is_success;
149 if ( $data->{'response'} eq "ok" )
151 $session = $data->{'session'};
152 $auth_key = $data->{'auth_key'};
156 print "Unable to log in to Hermes API\n";
159 undef $data; undef $response;
164 my $response = $g_ua->post( $config->api_url . "/auth/logout",
165 [ "session" => $session ] );
166 die("HTTP error") unless $response->is_success;
167 undef $session; undef $auth_key;