#!/usr/bin/perl use strict; use Data::Dumper; #TODO: Add support for assigning phone number #TODO: Add support for overriding default domain ... use Getopt::Long; use Net::LDAP; use Net::LDAP::Control::Paged; use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); use LWP; use JSON; use AppConfig; use Text::Iconv; my $api_key; my $help; my $username; my $alias; my $number; my $remove = 0; my $dryrun = 0; my $configfile = undef; my ($g_ua, $session, $api_key, $auth_key, $data, $domain); my $config = AppConfig->new({ CREATE => 1 }); $config->define("api_url=s"); $config->define("api_keyfile=s"); $config->define("default_domain=s"); $config->default_domain("hig.no"); $config->define("numbers_local_prefix=s"); $config->define("numbers_local_series=s"); $config->define("numbers_countrycode=s"); foreach ( "/usr/local/etc/hermes/hermes_config", "/usr/local/etc/hermes/config", "/etc/hermes/config", $ENV{"HOME"} . "/.hermes/config", $ENV{"HOME"} . "/.hermes_config", ) { $configfile = $_ if ( -f $_ ); } GetOptions( "help" => \$help, "configfile=s" => \$configfile, "username=s" => \$username, "alias=s" => \$alias, "number=s" => \$number, "remove" => \$remove, "dryrun" => \$dryrun, ); if ( (not $username) || (not $configfile) || ((not $alias) && (not $number)) || (($alias) && ($number)) || (( $configfile ) && ( not -f $configfile )) ) { $help = 1; } $config->file( $configfile ); if ( ( not $config->api_url ) || ( not $config->api_keyfile ) || ( $config->api_keyfile && not -f $config->api_keyfile ) ) { $help = 1; } if ( $help ) { print <api_keyfile; chomp( $api_key = ); close KEY; if ( not $username =~ m/\w+/ ) { print "Illegal username\n"; exit; } $g_ua = LWP::UserAgent->new; $g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant login_apikey(); # First: fetch a supported domain from the API... $data = exec_apinode("domain/list", undef); if ( $data->{'response'} eq 'ok' ) { $domain = $data->{'list'}[0]; } else { printf("Unable to get domain name. Aborting\n"); logout(); exit; } if ( $username =~ /@/ ) { ( $username, $domain ) = split /@/, $username; } $data = exec_apinode("user/available", { 'username' => $username, 'domain' => $domain }); if ( $data->{'response'} eq 'ok' ) { printf("Username is not registered.\n"); logout(); exit; } if ( not $data->{'cause'} eq 'exists' ) { printf("Username lookup failed, cause: %s.\n", $data->{'cause'}); logout(); exit; } undef $data; my $use_alias; if ( $number ) { if ( $number =~ m/^+.\d+$/) { # Prefix country-code unless number includes .... $use_alias = $config->numbers_countrycode unless $number =~ m/^\+\d\d/; # Add local prefix if local-series regex matches. my $t = $config->numbers_local_series; $use_alias .= $config->numbers_local_prefix if $number =~ m/^$t$/; # Add supplied number $use_alias .= $number; # Finally, tack on domain to complete E164 SIP address: $use_alias .= "@" . $domain; } else { printf("Failed number format test. Check input and retry\n"); logout(); exit; } } elsif ( $alias ) { # Add domain to given alias, unless it seems to contain one.. $alias .= "@" . $domain if not $alias =~ m/\@\w+/; print $alias . "\n"; # A fairly naive email-format address checker... if ( $alias =~ m/^([\w-_\+]+\.)*[\w-_\+]+\@([\w-_\+]+\.)+\w+$/ ) { $use_alias = $alias; } else { printf("Failed alias format test. Check input and retry\n"); logout(); exit; } } printf("Alias after expanding options: %s\n", $use_alias); # URL-encode any plus-signs in the address... #$use_alias =~ s/\+/\%2B/g; # Seems to not be needed, am I doing this somewhere else? undef $data; # run alias/list with alias=$use_alias, expect an empty list $data = exec_apinode("alias/list", { 'alias' => $use_alias }); if ( not $data->{'response'} eq 'ok' ) { printf("Something failed trying to see if alias is in use...\n"); logout(); exit; } my $t = $data->{'aliases'}; my @aliases = @$t; # End of common code, rest is different for add/remove if ( not $remove ) { # if list is non-empty, fail/abort, alias already taken if ( not $#aliases == -1 ) { printf("Given alias/number already exists, unable to proceed\n"); logout(); exit; } # run alias/add with $username@$domain as destination and $use_alias as alias undef $data; $data = exec_apinode("alias/add", { 'alias' => $use_alias, 'destination' => $username . "@" . $domain }); # fail unless OK is returned. if ( not $data->{'response'} eq 'ok' ) { printf("Unable to add alias, Hermes response is: %s\n", $data->{'cause'}); logout(); exit; } printf("Alias after expansion '%s' added to user '%s'\n", $use_alias, $username . "@" . $domain ); } else { if ( not $#aliases == 0 ) { printf("Search for alias did not return correct number of results (%d != 1)\n", ($#aliases+1)); logout(); exit; } if ( not $aliases[0]->{'alias'} eq $use_alias ) { printf("Not a match on alias: %s != %s\n", $aliases[0]->{'alias'}, $use_alias ); } if ( not $aliases[0]->{'destination'} eq $username . "@" . $domain ) { printf("Not a match on destination: %s != %s\n", $aliases[0]->{'destination'}, $username . "@" . $domain ); } undef $data; $data = exec_apinode("alias/remove", { 'alias' => $use_alias }); # fail unless OK is returned. if ( not $data->{'response'} eq 'ok' ) { printf("Unable to remove alias, Hermes response is: %s\n", $data->{'cause'}); logout(); exit; } printf("Removed alias %s.\n", $data->{'alias'} ); } logout(); ################################################################################################ sub exec_apinode($$) { my $node = shift; my $param = shift; my ( $response, $data ); $session = "" if not defined $session; $auth_key = "" if not defined $auth_key; my $url = $config->api_url . "/" . $node; $param->{'session'} = $session; $param->{'auth_key'} = $auth_key; $response = $g_ua->post( $url, $param ); if ( $response->is_success ) { if ( $response->content =~ m/\s*{/ ) { $data = decode_json( $response->content); } else { $data = $response->content; } } return $data; } sub login_apikey { my $response = $g_ua->post( $config->api_url . "/auth/login", [ "api_key" => $api_key ] ); my $data = decode_json( $response->content) if $response->is_success; die("HTTP error") unless $response->is_success; if ( $data->{'response'} eq "ok" ) { $session = $data->{'session'}; $auth_key = $data->{'auth_key'}; } else { print "Unable to log in to Hermes API\n"; exit; } undef $data; undef $response; } sub logout { my $response = $g_ua->post( $config->api_url . "/auth/logout", [ "session" => $session ] ); die("HTTP error") unless $response->is_success; undef $session; undef $auth_key; }