#!/usr/bin/env perl # # Example client for the Roller Network Account Control Center REST API. # # Documentation: https://acc.rollernet.us/help/rest-api.php # # Uses HTTP::Tiny and JSON::PP, which come with Perl. HTTPS also needs IO::Socket::SSL # and Net::SSLeay (Debian and Ubuntu: apt install libio-socket-ssl-perl). # # Set your token in the environment, not on the command line, where other users of the # system could see it: # # export RNACC_TOKEN='rnacc_...' # ./rest-api-example.pl domains # # Commands: # domains list mail domains # queue [DOMAIN] queued message counts, for all domains or one # flush DOMAIN deliver a domain's queued mail now (ETRN) # usertable DOMAIN show a domain's valid user table # adduser DOMAIN ENTRY add an entry to the valid user table # deluser DOMAIN ENTRY remove an entry from the valid user table # zones list Secondary DNS zones # retransfer ZONE transfer a Secondary DNS zone again # logs TYPE START END [DOMAIN] mail log entries as JSON, for all domains or one # logs-csv TYPE START END FILE [DOMAIN] mail log entries saved as a CSV file # # TYPE is incoming, delivery or submission; DOMAIN works with incoming and delivery only. # START and END are times such as 2026-11-19T00:00, in UTC unless RNACC_TZ is set, or with # their own offset such as 2026-11-19T00:00-08:00. Set RNACC_TZ to a time zone name such as # America/Los_Angeles to use that zone for START and END and for the times shown. use strict; use warnings; use HTTP::Tiny; use JSON::PP; my $API_URL = $ENV{RNACC_API_URL} || 'https://acc.rollernet.us/api/v1'; my $json = JSON::PP->new->utf8->canonical->pretty; # URL encode one path segment or query value, such as a domain or mail box address. sub encode { my ($value) = @_; $value =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge; return $value; } # Send a request and return the decoded data member (undef for a 204 response). # Options: body (a hash to send as JSON), query (a hash), accept, save_to (a file name). sub api_request { my ($method, $path, %opt) = @_; my $token = $ENV{RNACC_TOKEN} or die "Set RNACC_TOKEN to your API token.\n"; my $url = $API_URL . $path; if ($opt{query}) { $url .= '?' . join('&', map { encode($_) . '=' . encode($opt{query}{$_}) } sort keys %{ $opt{query} }); } my %headers = ( 'Authorization' => "Bearer $token", 'Accept' => $opt{accept} || 'application/json', ); my %args = (headers => \%headers); if ($opt{body}) { $headers{'Content-Type'} = 'application/json'; $args{content} = $json->encode($opt{body}); } my $out; if ($opt{save_to}) { # stream the file instead of holding it in memory; only a successful response is saved $args{data_callback} = sub { my ($chunk, $res) = @_; if ($res->{status} == 200) { unless ($out) { open($out, '>:raw', $opt{save_to}) or die "Unable to write $opt{save_to}: $!\n"; } print {$out} $chunk; } else { $res->{content} .= $chunk; } }; } my $http = HTTP::Tiny->new(timeout => 120, verify_SSL => 1); my $res = $http->request($method, $url, \%args); close($out) if $out; # status 599 is HTTP::Tiny's own error, such as a connection failure if ($res->{status} == 599) { die "Unable to reach the API: $res->{content}"; } if (!$res->{success}) { # errors are always JSON: {"error": {"status": ..., "message": ...}} my $message = eval { $json->decode($res->{content})->{error}{message} } || $res->{reason}; die "HTTP $res->{status}: $message\n"; } return undef if $opt{save_to} || $res->{status} == 204; my $result = $json->decode($res->{content}); # a large mail log result can fail after the response has started die "Incomplete result: $result->{error}{message}\n" if $result->{error}; return $result->{data}; } # The mail log path: one domain's log, or every domain's (and the submission log). sub log_path { my ($type, $domain) = @_; return '/mail/domains/' . encode($domain) . '/logs/' . encode($type) if defined $domain; return '/mail/logs/' . encode($type); } # The mail log query: the time range, and the time zone from RNACC_TZ when it is set. sub log_query { my ($start, $end) = @_; my %query = (start => $start, end => $end); $query{tz} = $ENV{RNACC_TZ} if $ENV{RNACC_TZ}; return \%query; } sub show { print $json->encode($_[0]); } sub usage { open(my $self, '<', $0) or die "Usage: see the comments at the top of this script.\n"; while (<$self>) { last if /^use /; print STDERR $1, "\n" if /^# ?(.*)$/ && $. > 1; } exit 2; } my ($command, @args) = @ARGV; usage() unless defined $command; eval { if ($command eq 'domains' && !@args) { show(api_request('GET', '/mail/domains')); } elsif ($command eq 'queue' && @args <= 1) { show(api_request('GET', @args ? '/mail/domains/' . encode($args[0]) . '/queue' : '/mail/queue')); } elsif ($command eq 'flush' && @args == 1) { api_request('POST', '/mail/domains/' . encode($args[0]) . '/queue/flush'); print "Queue flush requested for $args[0].\n"; } elsif ($command eq 'usertable' && @args == 1) { show(api_request('GET', '/mail/domains/' . encode($args[0]) . '/usertable')); } elsif ($command eq 'adduser' && @args == 2) { show(api_request('POST', '/mail/domains/' . encode($args[0]) . '/usertable/entries', body => { entry => $args[1] })); } elsif ($command eq 'deluser' && @args == 2) { api_request('DELETE', '/mail/domains/' . encode($args[0]) . '/usertable/entries/' . encode($args[1])); print "Removed $args[1] from the valid user table for $args[0].\n"; } elsif ($command eq 'zones' && !@args) { show(api_request('GET', '/dns/secondary/zones')); } elsif ($command eq 'retransfer' && @args == 1) { api_request('POST', '/dns/secondary/zones/' . encode($args[0]) . '/retransfer'); print "Zone transfer requested for $args[0].\n"; } elsif ($command eq 'logs' && (@args == 3 || @args == 4)) { show(api_request('GET', log_path($args[0], $args[3]), query => log_query($args[1], $args[2]))); } elsif ($command eq 'logs-csv' && (@args == 4 || @args == 5)) { api_request('GET', log_path($args[0], $args[4]), query => log_query($args[1], $args[2]), accept => 'text/csv', save_to => $args[3]); print "Saved $args[3].\n"; } else { usage(); } 1; } or do { print STDERR "Error: $@"; exit 1; };