]>
git.defcon.no Git - hermes/blob - guc-clients/sipalias
6 #TODO: Add support for assigning phone number
7 #TODO: Add support for overriding default domain ...
11 use Net
::LDAP
::Control
::Paged
;
12 use Net
::LDAP
::Constant
qw( LDAP_CONTROL_PAGED );
26 my $configfile = undef;
27 my ($g_ua, $session, $api_key, $auth_key, $data, $domain);
29 my $config = AppConfig
->new({ CREATE
=> 1 });
31 $config->define("api_url=s");
32 $config->define("api_keyfile=s");
34 $config->define("default_domain=s");
35 $config->default_domain("hig.no");
37 $config->define("numbers_local_prefix=s");
38 $config->define("numbers_local_series=s");
39 $config->define("numbers_countrycode=s");
42 "/usr/local/etc/hermes/hermes_config",
43 "/usr/local/etc/hermes/config",
45 $ENV{"HOME"} . "/.hermes/config",
46 $ENV{"HOME"} . "/.hermes_config",
47 ) { $configfile = $_ if ( -f
$_ ); }
51 "configfile=s" => \
$configfile,
52 "username=s" => \
$username,
54 "number=s" => \
$number,
63 ((not $alias) && (not $number)) ||
64 (($alias) && ($number)) ||
65 (( $configfile ) && ( not -f
$configfile ))
71 $config->file( $configfile );
73 if ( ( not $config->api_url ) ||
74 ( not $config->api_keyfile ) ||
75 ( $config->api_keyfile && not -f
$config->api_keyfile )
83 Verify that the following options are set:
84 --username=s|--user|-u
91 Verify the contents of the configuration file.
92 Verify that the key-file exists.
96 open KEY, "<" . $config->api_keyfile;
97 chomp( $api_key = <KEY> );
100 if ( not $username =~ m/\w+/ )
101 { print "Illegal username\n"; exit; }
103 $g_ua = LWP::UserAgent->new;
104 $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant
108 # First: fetch a supported domain from the API...
109 $data = exec_apinode("domain/list", undef);
110 if ( $data->{'response'} eq 'ok' )
112 $domain = $data->{'list'}[0];
116 printf("Unable to get domain name. Aborting\n");
121 if ( $username =~ /@/ )
123 ( $username, $domain ) = split /@/, $username;
126 $data = exec_apinode("user/available", { 'username' => $username, 'domain' => $domain });
127 if ( $data->{'response'} eq 'ok' )
129 printf("Username is not registered.\n");
134 if ( not $data->{'cause'} eq 'exists' )
136 printf("Username lookup failed, cause: %s.\n", $data->{'cause'});
147 if ( $number =~ m/^+.\d+$/)
149 # Prefix country-code unless number includes ....
150 $use_alias = $config->numbers_countrycode unless $number =~ m/^\+\d\d/;
152 # Add local prefix if local-series regex matches.
153 my $t = $config->numbers_local_series;
154 $use_alias .= $config->numbers_local_prefix if $number =~ m/^$t$/;
156 # Add supplied number
157 $use_alias .= $number;
159 # Finally, tack on domain to complete E164 SIP address:
160 $use_alias .= "@" . $domain;
164 printf("Failed number format test. Check input and retry\n");
171 # Add domain to given alias, unless it seems to contain one..
172 $alias .= "@" . $domain if not $alias =~ m/\@\w+/;
175 # A fairly naive email-format address checker...
176 if ( $alias =~ m/^([\w-_\+]+\.)*[\w-_\+]+\@([\w-_\+]+\.)+\w+$/ )
182 printf("Failed alias format test. Check input and retry\n");
187 printf("Alias after expanding options: %s\n", $use_alias);
189 # URL-encode any plus-signs in the address...
190 $use_alias =~ s/\+/\%2B/g;
193 # run alias/list with alias=$use_alias, expect an empty list
194 $data = exec_apinode("alias/list", { 'alias' => $use_alias });
195 if ( not $data->{'response'} eq 'ok' )
197 printf("Something failed trying to see if alias is in use...\n");
201 my $t = $data->{'aliases'}; my @aliases = @$t;
203 # End of common code, rest is different for add/remove
206 # if list is non-empty, fail/abort, alias already taken
207 if ( not $#aliases == -1 )
209 printf("Given alias/number already exists, unable to proceed\n");
213 # run alias/add with $username@$domain as destination and $use_alias as alias
215 $data = exec_apinode("alias/add", { 'alias' => $use_alias, 'destination' => $username . "@" . $domain });
216 # fail unless OK is returned.
217 if ( not $data->{'response'} eq 'ok' )
219 printf("Unable to add alias, Hermes response is: %s\n", $data->{'cause'});
223 printf("Alias after expansion '%s' added to user '%s'\n", $use_alias, $username . "@" . $domain );
227 if ( not $#aliases == 0 )
229 printf("Search for alias did not return correct number of results (%d != 1)\n", ($#aliases+1));
233 if ( not $aliases[0]->{'alias'} eq $use_alias )
235 printf("Not a match on alias: %s != %s\n", $aliases[0]->{'alias'}, $use_alias );
238 if ( not $aliases[0]->{'destination'} eq $username . "@" . $domain )
240 printf("Not a match on destination: %s != %s\n",
241 $aliases[0]->{'destination'}, $username . "@" . $domain
247 ################################################################################################
253 my ( $response, $data );
255 $session = "" if not defined $session;
256 $auth_key = "" if not defined $auth_key;
257 my $url = $config->api_url . "/" . $node;
259 $param->{'session'} = $session;
260 $param->{'auth_key'} = $auth_key;
262 $response = $g_ua->post( $url, $param );
263 if ( $response->is_success )
265 if ( $response->content =~ m/\s*{/ )
267 $data = decode_json( $response->content);
271 $data = $response->content;
280 my $response = $g_ua->post( $config->api_url . "/auth/login",
281 [ "api_key" => $api_key ] );
283 my $data = decode_json( $response->content) if $response->is_success;
284 die("HTTP error") unless $response->is_success;
286 if ( $data->{'response'} eq "ok" )
288 $session = $data->{'session'};
289 $auth_key = $data->{'auth_key'};
293 print "Unable to log in to Hermes API\n";
296 undef $data; undef $response;
301 my $response = $g_ua->post( $config->api_url . "/auth/logout",
302 [ "session" => $session ] );
303 die("HTTP error") unless $response->is_success;
304 undef $session; undef $auth_key;