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