]> git.defcon.no Git - hermes/blob - guc-clients/listsipusers
fccedb0e57a022170bed2d156a014f967f693073
[hermes] / guc-clients / listsipusers
1 #!/usr/bin/perl
2 use strict;
3
4 #TODO: Add support for assigning phone number
5 #TODO: Add support for overriding default domain ...
6
7 use Getopt::Long;
8 use Net::LDAP;
9 use Net::LDAP::Control::Paged;
10 use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED );
11 use LWP;
12 use JSON;
13 use AppConfig;
14 use Text::Iconv;
15
16 my $api_key;
17
18 my $help;
19 my $username;
20 my $configfile = undef;
21 my $dryrun = 0;
22 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
23 my ($user, $displayname, $phone, $mail, $sipuser, $linetext);
24
25 my $config = AppConfig->new({ CREATE => 1 });
26
27 $config->define("api_url=s");
28 $config->define("api_keyfile=s");
29
30 foreach (
31 "/usr/local/etc/hermes/hermes_config",
32 "/usr/local/etc/hermes/config",
33 "/etc/hermes/config",
34 $ENV{"HOME"} . "/.hermes/config",
35 $ENV{"HOME"} . "/.hermes_config",
36 ) { $configfile = $_ if ( -f $_ ); }
37
38 GetOptions(
39 "help" => \$help,
40 "configfile=s" => \$configfile,
41 );
42
43 if (
44 (not $configfile) ||
45 (( $configfile ) && ( not -f $configfile ))
46 )
47 {
48 $help = 1;
49 }
50
51 $config->file( $configfile );
52
53 if ( ( not $config->api_url ) ||
54 ( not $config->api_keyfile ) ||
55 ( $config->api_keyfile && not -f $config->api_keyfile )
56 )
57 {
58 $help = 1;
59 }
60
61 if ( $help ) {
62 print <<END_HELP;
63 Verify that the following options are set:
64 --configfile=s
65
66 Verify the contents of the configuration file.
67 Verify that the key-file exists.
68 END_HELP
69 exit; }
70
71 open KEY, "<" . $config->api_keyfile;
72 chomp( $api_key = <KEY> );
73 close KEY;
74
75 $g_ua = LWP::UserAgent->new;
76 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
77
78 login_apikey();
79
80 $data = exec_apinode("user/list", { });
81 if ( not $data->{'response'} eq 'ok' )
82 {
83 printf("Unable to fetch user list: %s\n", $data->{'cause'});
84 exit;
85 }
86
87 my $t = $data->{'list'};
88 my @users = @$t;
89
90 foreach my $u ( @users )
91 {
92 printf("%-24s", $u->{'user'});
93 my $alias_data = exec_apinode("alias/list", { 'destination' => $u->{'user'} });
94 if ( $alias_data->{'response'} eq 'ok' )
95 {
96 #print Dumper($alias_data);
97 my $t = $alias_data->{'aliases'};
98 foreach my $a ( sort {lc $a->{'alias'} cmp lc $b->{'alias'}} @$t )
99 {
100 printf("\t%-24s", $a->{'alias'});
101 }
102 }
103 else
104 {
105 }
106 print "\n";
107 }
108
109 undef $data;
110
111 logout();
112 ################################################################################################
113 sub exec_apinode($$)
114 {
115 my $node = shift;
116 my $param = shift;
117
118 my ( $response, $data );
119
120 $session = "" if not defined $session;
121 $auth_key = "" if not defined $auth_key;
122 my $url = $config->api_url . "/" . $node;
123
124 $param->{'session'} = $session;
125 $param->{'auth_key'} = $auth_key;
126
127 $response = $g_ua->post( $url, $param );
128 if ( $response->is_success )
129 {
130 if ( $response->content =~ m/\s*{/ )
131 {
132 $data = decode_json( $response->content);
133 }
134 else
135 {
136 $data = $response->content;
137 }
138
139 }
140 return $data;
141 }
142
143 sub login_apikey
144 {
145 my $response = $g_ua->post( $config->api_url . "/auth/login",
146 [ "api_key" => $api_key ] );
147
148 my $data = decode_json( $response->content) if $response->is_success;
149 die("HTTP error") unless $response->is_success;
150
151 if ( $data->{'response'} eq "ok" )
152 {
153 $session = $data->{'session'};
154 $auth_key = $data->{'auth_key'};
155 }
156 else
157 {
158 print "Unable to log in to Hermes API\n";
159 exit;
160 }
161 undef $data; undef $response;
162 }
163
164 sub logout
165 {
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;
170 }