]>
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;
191 # Seems to not be needed, am I doing this somewhere else?
194 # run alias/list with alias=$use_alias, expect an empty list
195 $data = exec_apinode("alias/list", { 'alias' => $use_alias });
196 if ( not $data->{'response'} eq 'ok' )
198 printf("Something failed trying to see if alias is in use...\n");
202 my $t = $data->{'aliases'}; my @aliases = @$t;
204 # End of common code, rest is different for add/remove
207 # if list is non-empty, fail/abort, alias already taken
208 if ( not $#aliases == -1 )
210 printf("Given alias/number already exists, unable to proceed\n");
214 # run alias/add with $username@$domain as destination and $use_alias as alias
216 $data = exec_apinode("alias/add", { 'alias' => $use_alias, 'destination' => $username . "@" . $domain });
217 # fail unless OK is returned.
218 if ( not $data->{'response'} eq 'ok' )
220 printf("Unable to add alias, Hermes response is: %s\n", $data->{'cause'});
224 printf("Alias after expansion '%s' added to user '%s'\n", $use_alias, $username . "@" . $domain );
228 if ( not $#aliases == 0 )
230 printf("Search for alias did not return correct number of results (%d != 1)\n", ($#aliases+1));
234 if ( not $aliases[0]->{'alias'} eq $use_alias )
236 printf("Not a match on alias: %s != %s\n", $aliases[0]->{'alias'}, $use_alias );
241 if ( not $aliases[0]->{'destination'} eq $username . "@" . $domain )
243 printf("Not a match on destination: %s != %s\n",
244 $aliases[0]->{'destination'}, $username . "@" . $domain
250 $data = exec_apinode("alias/remove", { 'alias' => $use_alias });
251 # fail unless OK is returned.
252 if ( not $data->{'response'} eq 'ok' )
254 printf("Unable to remove alias, Hermes response is: %s\n", $data->{'cause'});
258 printf("Removed alias %s.\n", $data->{'alias'} );
262 ################################################################################################
268 my ( $response, $data );
270 $session = "" if not defined $session;
271 $auth_key = "" if not defined $auth_key;
272 my $url = $config->api_url . "/" . $node;
274 $param->{'session'} = $session;
275 $param->{'auth_key'} = $auth_key;
277 $response = $g_ua->post( $url, $param );
278 if ( $response->is_success )
280 if ( $response->content =~ m/\s*{/ )
282 $data = decode_json( $response->content);
286 $data = $response->content;
295 my $response = $g_ua->post( $config->api_url . "/auth/login",
296 [ "api_key" => $api_key ] );
298 my $data = decode_json( $response->content) if $response->is_success;
299 die("HTTP error") unless $response->is_success;
301 if ( $data->{'response'} eq "ok" )
303 $session = $data->{'session'};
304 $auth_key = $data->{'auth_key'};
308 print "Unable to log in to Hermes API\n";
311 undef $data; undef $response;
316 my $response = $g_ua->post( $config->api_url . "/auth/logout",
317 [ "session" => $session ] );
318 die("HTTP error") unless $response->is_success;
319 undef $session; undef $auth_key;