#!/usr/bin/env php "Bearer $token", 'Accept' => $accept]; $data = []; if ($body !== null) { $headers['Content-Type'] = 'application/json'; $data = json_encode($body, JSON_THROW_ON_ERROR); } $options = ['timeout' => 120, 'connect_timeout' => 10]; $partFile = null; if ($saveTo !== null) { // Requests writes the body to this file as it arrives, whatever the status, so it goes to // a temporary file next to $saveTo and is renamed only when the request succeeds $partFile = tempnam(dirname($saveTo), 'rnacc') ?: throw new ApiError("Unable to write $saveTo."); $options['filename'] = $partFile; } try { $response = Requests::request($url, $headers, $data, $method, $options); } catch (RequestsException $e) { if ($partFile !== null) { unlink($partFile); } throw new ApiError("Unable to reach the API: " . $e->getMessage()); } if (!$response->success) { $content = $partFile !== null ? (string) file_get_contents($partFile) : $response->body; if ($partFile !== null) { unlink($partFile); } // errors are always JSON: {"error": {"status": ..., "message": ...}} $message = json_decode($content, true)['error']['message'] ?? 'no details'; throw new ApiError("HTTP {$response->status_code}: $message"); } if ($partFile !== null) { if (!rename($partFile, $saveTo)) { unlink($partFile); throw new ApiError("Unable to write $saveTo."); } return null; } if ($response->status_code === 204) { return null; } try { $result = json_decode($response->body, true, flags: JSON_THROW_ON_ERROR); } catch (JsonException) { throw new ApiError("The response is not valid JSON."); } // a large mail log result can fail after the response has started if (isset($result['error'])) { throw new ApiError("Incomplete result: " . $result['error']['message']); } return $result['data'] ?? null; } function show(mixed $data): void { echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), "\n"; } // URL encode one path segment, such as a domain or mail box address function seg(string $value): string { return rawurlencode($value); } // the mail log path: one domain's log, or every domain's (and the submission log) function logPath(string $type, ?string $domain): string { return $domain !== null ? '/mail/domains/' . seg($domain) . '/logs/' . seg($type) : '/mail/logs/' . seg($type); } // the mail log query: the time range, and the time zone from RNACC_TZ when it is set function logQuery(string $start, string $end): array { return ['start' => $start, 'end' => $end] + (getenv('RNACC_TZ') ? ['tz' => getenv('RNACC_TZ')] : []); } $args = array_slice($argv, 1); $command = array_shift($args); $count = count($args); try { if ($command === 'domains' && $count === 0) { show(apiRequest('GET', '/mail/domains')); } elseif ($command === 'queue' && $count <= 1) { show(apiRequest('GET', $count ? '/mail/domains/' . seg($args[0]) . '/queue' : '/mail/queue')); } elseif ($command === 'flush' && $count === 1) { apiRequest('POST', '/mail/domains/' . seg($args[0]) . '/queue/flush'); echo "Queue flush requested for {$args[0]}.\n"; } elseif ($command === 'usertable' && $count === 1) { show(apiRequest('GET', '/mail/domains/' . seg($args[0]) . '/usertable')); } elseif ($command === 'adduser' && $count === 2) { show(apiRequest('POST', '/mail/domains/' . seg($args[0]) . '/usertable/entries', body: ['entry' => $args[1]])); } elseif ($command === 'deluser' && $count === 2) { apiRequest('DELETE', '/mail/domains/' . seg($args[0]) . '/usertable/entries/' . seg($args[1])); echo "Removed {$args[1]} from the valid user table for {$args[0]}.\n"; } elseif ($command === 'zones' && $count === 0) { show(apiRequest('GET', '/dns/secondary/zones')); } elseif ($command === 'retransfer' && $count === 1) { apiRequest('POST', '/dns/secondary/zones/' . seg($args[0]) . '/retransfer'); echo "Zone transfer requested for {$args[0]}.\n"; } elseif ($command === 'logs' && ($count === 3 || $count === 4)) { show(apiRequest('GET', logPath($args[0], $args[3] ?? null), query: logQuery($args[1], $args[2]))); } elseif ($command === 'logs-csv' && ($count === 4 || $count === 5)) { apiRequest( 'GET', logPath($args[0], $args[4] ?? null), query: logQuery($args[1], $args[2]), accept: 'text/csv', saveTo: $args[3] ); echo "Saved {$args[3]}.\n"; } else { fwrite(STDERR, USAGE); exit(2); } } catch (ApiError | JsonException $e) { fwrite(STDERR, "Error: " . $e->getMessage() . "\n"); exit(1); }