]>
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 printf("%-12s", $u->{'status'});
94 my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} });
95 if ( $alias_data->{'response'} eq 'ok' )
97 #print Dumper($alias_data);
98 my $t = $alias_data->{'aliases'};
99 foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t )
101 printf("\t%-24s", $a->{'alias'});
113 ################################################################################################
119 my ( $response, $data );
121 $session = "" if not defined $session;
122 $auth_key = "" if not defined $auth_key;
123 my $url = $config->api_url . "/" . $node;
125 $param->{'session'} = $session;
126 $param->{'auth_key'} = $auth_key;
128 $response = $g_ua->post( $url, $param );
129 if ( $response->is_success )
131 if ( $response->content =~ m/\s*{/ )
133 $data = decode_json( $response->content);
137 $data = $response->content;
146 my $response = $g_ua->post( $config->api_url . "/auth/login",
147 [ "api_key" => $api_key ] );
149 my $data = decode_json( $response->content) if $response->is_success;
150 die("HTTP error") unless $response->is_success;
152 if ( $data->{'response'} eq "ok" )
154 $session = $data->{'session'};
155 $auth_key = $data->{'auth_key'};
159 print "Unable to log in to Hermes API\n";
162 undef $data; undef $response;
167 my $response = $g_ua->post( $config->api_url . "/auth/logout",
168 [ "session" => $session ] );
169 die("HTTP error") unless $response->is_success;
170 undef $session; undef $auth_key;