]>
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 );
20 my $configfile = undef;
22 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
23 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
25 my $config = AppConfig
->new({ CREATE
=> 1 });
27 $config->define("api_url=s");
28 $config->define("api_keyfile=s");
31 "/usr/local/etc/hermes/hermes_config",
32 "/usr/local/etc/hermes/config",
34 $ENV{"HOME"} . "/.hermes/config",
35 $ENV{"HOME"} . "/.hermes_config",
36 ) { $configfile = $_ if ( -f
$_ ); }
40 "configfile=s" => \
$configfile,
45 (( $configfile ) && ( not -f
$configfile ))
51 $config->file( $configfile );
53 if ( ( not $config->api_url ) ||
54 ( not $config->api_keyfile ) ||
55 ( $config->api_keyfile && not -f
$config->api_keyfile )
63 Verify that the following options are set:
66 Verify the contents of the configuration file.
67 Verify that the key-file exists.
71 open KEY, "<" . $config->api_keyfile;
72 chomp( $api_key = <KEY> );
75 $g_ua = LWP::UserAgent->new;
76 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
80 $data = exec_apinode("user/list", { });
81 if ( not $data->{'response'} eq 'ok' )
83 printf("Unable to fetch user list: %s\n", $data->{'cause'});
87 my $t = $data->{'list'};
90 foreach my $u ( @users )
92 printf("%-24s", $u->{'user'});
93 my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} });
94 if ( $alias_data->{'response'} eq 'ok' )
96 #print Dumper($alias_data);
97 my $t = $alias_data->{'aliases'};
98 foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t )
100 printf("\t%-24s", $a->{'alias'});
112 ################################################################################################
118 my ( $response, $data );
120 $session = "" if not defined $session;
121 $auth_key = "" if not defined $auth_key;
122 my $url = $config->api_url . "/" . $node;
124 $param->{'session'} = $session;
125 $param->{'auth_key'} = $auth_key;
127 $response = $g_ua->post( $url, $param );
128 if ( $response->is_success )
130 if ( $response->content =~ m/\s*{/ )
132 $data = decode_json( $response->content);
136 $data = $response->content;
145 my $response = $g_ua->post( $config->api_url . "/auth/login",
146 [ "api_key" => $api_key ] );
148 my $data = decode_json( $response->content) if $response->is_success;
149 die("HTTP error") unless $response->is_success;
151 if ( $data->{'response'} eq "ok" )
153 $session = $data->{'session'};
154 $auth_key = $data->{'auth_key'};
158 print "Unable to log in to Hermes API\n";
161 undef $data; undef $response;
166 my $response = $g_ua->post( $config->api_url . "/auth/logout",
167 [ "session" => $session ] );
168 die("HTTP error") unless $response->is_success;
169 undef $session; undef $auth_key;