fix(newsbin): Fix RPCD handler for BusyBox sh compatibility

- Use functions instead of inline local vars (not allowed in case)
- Use 1/0 instead of true/false for json_add_boolean
- Use full paths for lxc-info and curl

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
CyberMind-FR 2026-03-14 17:36:46 +01:00
parent 00da717ea4
commit 169b39ed57

View File

@ -13,26 +13,20 @@ get_sab_api() {
grep "^api_key" "$SAB_CONFIG" 2>/dev/null | cut -d'=' -f2 | tr -d ' ' grep "^api_key" "$SAB_CONFIG" 2>/dev/null | cut -d'=' -f2 | tr -d ' '
} }
case "$1" in do_status() {
list)
echo '{"status":{},"queue":{},"history":{},"search":{"query":"string"},"add_nzb":{"url":"string","category":"string"},"pause":{},"resume":{},"remove":{"nzo_id":"string"},"config":{}}'
;;
call)
case "$2" in
status)
json_init json_init
# SABnzbd status # SABnzbd status
local sab_running="false" sab_running=0
local sab_speed="0" sab_speed="0"
local sab_queue="0" sab_queue="0"
local sab_disk="0" sab_disk="0"
if lxc-info -n sabnzbd 2>/dev/null | grep -q "RUNNING"; then if /usr/bin/lxc-info -n sabnzbd 2>/dev/null | grep -q "RUNNING"; then
sab_running="true" sab_running=1
local api_key=$(get_sab_api) api_key=$(get_sab_api)
if [ -n "$api_key" ]; then if [ -n "$api_key" ]; then
local sab_data=$(curl -s "http://$SAB_IP:$SAB_PORT/api?mode=queue&output=json&apikey=$api_key" 2>/dev/null) sab_data=$(/usr/bin/curl -s "http://$SAB_IP:$SAB_PORT/api?mode=queue&output=json&apikey=$api_key" 2>/dev/null)
if [ -n "$sab_data" ]; then if [ -n "$sab_data" ]; then
sab_speed=$(echo "$sab_data" | jsonfilter -e '@.queue.speed' 2>/dev/null || echo "0") sab_speed=$(echo "$sab_data" | jsonfilter -e '@.queue.speed' 2>/dev/null || echo "0")
sab_queue=$(echo "$sab_data" | jsonfilter -e '@.queue.noofslots' 2>/dev/null || echo "0") sab_queue=$(echo "$sab_data" | jsonfilter -e '@.queue.noofslots' 2>/dev/null || echo "0")
@ -42,9 +36,9 @@ case "$1" in
fi fi
# NZBHydra status # NZBHydra status
local hydra_running="false" hydra_running=0
if lxc-info -n nzbhydra 2>/dev/null | grep -q "RUNNING"; then if /usr/bin/lxc-info -n nzbhydra 2>/dev/null | grep -q "RUNNING"; then
hydra_running="true" hydra_running=1
fi fi
json_add_object "sabnzbd" json_add_object "sabnzbd"
@ -61,15 +55,15 @@ case "$1" in
json_close_object json_close_object
json_dump json_dump
;; }
queue) do_queue() {
json_init json_init
json_add_array "items" json_add_array "items"
local api_key=$(get_sab_api) api_key=$(get_sab_api)
if [ -n "$api_key" ]; then if [ -n "$api_key" ]; then
local queue_data=$(curl -s "http://$SAB_IP:$SAB_PORT/api?mode=queue&output=json&apikey=$api_key" 2>/dev/null) queue_data=$(/usr/bin/curl -s "http://$SAB_IP:$SAB_PORT/api?mode=queue&output=json&apikey=$api_key" 2>/dev/null)
if [ -n "$queue_data" ]; then if [ -n "$queue_data" ]; then
echo "$queue_data" | python3 -c " echo "$queue_data" | python3 -c "
import sys, json import sys, json
@ -87,15 +81,13 @@ try:
except: except:
pass pass
" 2>/dev/null | while read item; do " 2>/dev/null | while read item; do
echo "$item" | {
read line
json_add_object "" json_add_object ""
local nzo_id=$(echo "$line" | jsonfilter -e '@.nzo_id') nzo_id=$(echo "$item" | jsonfilter -e '@.nzo_id')
local filename=$(echo "$line" | jsonfilter -e '@.filename') filename=$(echo "$item" | jsonfilter -e '@.filename')
local size=$(echo "$line" | jsonfilter -e '@.size') size=$(echo "$item" | jsonfilter -e '@.size')
local percentage=$(echo "$line" | jsonfilter -e '@.percentage') percentage=$(echo "$item" | jsonfilter -e '@.percentage')
local status=$(echo "$line" | jsonfilter -e '@.status') status=$(echo "$item" | jsonfilter -e '@.status')
local timeleft=$(echo "$line" | jsonfilter -e '@.timeleft') timeleft=$(echo "$item" | jsonfilter -e '@.timeleft')
json_add_string "nzo_id" "$nzo_id" json_add_string "nzo_id" "$nzo_id"
json_add_string "filename" "$filename" json_add_string "filename" "$filename"
json_add_string "size" "$size" json_add_string "size" "$size"
@ -103,22 +95,21 @@ except:
json_add_string "status" "$status" json_add_string "status" "$status"
json_add_string "timeleft" "$timeleft" json_add_string "timeleft" "$timeleft"
json_close_object json_close_object
}
done done
fi fi
fi fi
json_close_array json_close_array
json_dump json_dump
;; }
history) do_history() {
json_init json_init
json_add_array "items" json_add_array "items"
local api_key=$(get_sab_api) api_key=$(get_sab_api)
if [ -n "$api_key" ]; then if [ -n "$api_key" ]; then
local hist_data=$(curl -s "http://$SAB_IP:$SAB_PORT/api?mode=history&limit=20&output=json&apikey=$api_key" 2>/dev/null) hist_data=$(/usr/bin/curl -s "http://$SAB_IP:$SAB_PORT/api?mode=history&limit=20&output=json&apikey=$api_key" 2>/dev/null)
if [ -n "$hist_data" ]; then if [ -n "$hist_data" ]; then
echo "$hist_data" | python3 -c " echo "$hist_data" | python3 -c "
import sys, json import sys, json
@ -135,28 +126,25 @@ try:
except: except:
pass pass
" 2>/dev/null | while read item; do " 2>/dev/null | while read item; do
echo "$item" | {
read line
json_add_object "" json_add_object ""
local nzo_id=$(echo "$line" | jsonfilter -e '@.nzo_id') nzo_id=$(echo "$item" | jsonfilter -e '@.nzo_id')
local name=$(echo "$line" | jsonfilter -e '@.name') name=$(echo "$item" | jsonfilter -e '@.name')
local size=$(echo "$line" | jsonfilter -e '@.size') size=$(echo "$item" | jsonfilter -e '@.size')
local status=$(echo "$line" | jsonfilter -e '@.status') status=$(echo "$item" | jsonfilter -e '@.status')
json_add_string "nzo_id" "$nzo_id" json_add_string "nzo_id" "$nzo_id"
json_add_string "name" "$name" json_add_string "name" "$name"
json_add_string "size" "$size" json_add_string "size" "$size"
json_add_string "status" "$status" json_add_string "status" "$status"
json_close_object json_close_object
}
done done
fi fi
fi fi
json_close_array json_close_array
json_dump json_dump
;; }
search) do_search() {
read -r input read -r input
json_load "$input" json_load "$input"
json_get_var query query json_get_var query query
@ -165,8 +153,8 @@ except:
json_add_array "results" json_add_array "results"
if [ -n "$query" ]; then if [ -n "$query" ]; then
local search_url="http://$HYDRA_IP:$HYDRA_PORT/api?t=search&q=$(echo "$query" | sed 's/ /%20/g')" search_url="http://$HYDRA_IP:$HYDRA_PORT/api?t=search&q=$(echo "$query" | sed 's/ /%20/g')"
local result=$(curl -s "$search_url" 2>/dev/null) result=$(/usr/bin/curl -s "$search_url" 2>/dev/null)
if [ -n "$result" ]; then if [ -n "$result" ]; then
echo "$result" | python3 -c " echo "$result" | python3 -c "
@ -182,26 +170,23 @@ try:
except: except:
pass pass
" 2>/dev/null | while read item; do " 2>/dev/null | while read item; do
echo "$item" | {
read line
json_add_object "" json_add_object ""
local title=$(echo "$line" | jsonfilter -e '@.title') title=$(echo "$item" | jsonfilter -e '@.title')
local link=$(echo "$line" | jsonfilter -e '@.link') link=$(echo "$item" | jsonfilter -e '@.link')
local size=$(echo "$line" | jsonfilter -e '@.size') size=$(echo "$item" | jsonfilter -e '@.size')
json_add_string "title" "$title" json_add_string "title" "$title"
json_add_string "link" "$link" json_add_string "link" "$link"
json_add_int "size" "$size" json_add_int "size" "$size"
json_close_object json_close_object
}
done done
fi fi
fi fi
json_close_array json_close_array
json_dump json_dump
;; }
add_nzb) do_add_nzb() {
read -r input read -r input
json_load "$input" json_load "$input"
json_get_var url url json_get_var url url
@ -213,11 +198,11 @@ except:
json_add_boolean "success" 0 json_add_boolean "success" 0
json_add_string "error" "URL required" json_add_string "error" "URL required"
else else
local api_key=$(get_sab_api) api_key=$(get_sab_api)
local add_url="http://$SAB_IP:$SAB_PORT/api?mode=addurl&name=$(echo "$url" | sed 's/&/%26/g')&apikey=$api_key" add_url="http://$SAB_IP:$SAB_PORT/api?mode=addurl&name=$(echo "$url" | sed 's/&/%26/g')&apikey=$api_key"
[ -n "$category" ] && add_url="$add_url&cat=$category" [ -n "$category" ] && add_url="$add_url&cat=$category"
local result=$(curl -s "$add_url" 2>/dev/null) result=$(/usr/bin/curl -s "$add_url" 2>/dev/null)
if echo "$result" | grep -q "ok"; then if echo "$result" | grep -q "ok"; then
json_add_boolean "success" 1 json_add_boolean "success" 1
json_add_string "message" "NZB added to queue" json_add_string "message" "NZB added to queue"
@ -228,38 +213,38 @@ except:
fi fi
json_dump json_dump
;; }
pause) do_pause() {
local api_key=$(get_sab_api) api_key=$(get_sab_api)
curl -s "http://$SAB_IP:$SAB_PORT/api?mode=pause&apikey=$api_key" >/dev/null 2>&1 /usr/bin/curl -s "http://$SAB_IP:$SAB_PORT/api?mode=pause&apikey=$api_key" >/dev/null 2>&1
json_init json_init
json_add_boolean "success" 1 json_add_boolean "success" 1
json_dump json_dump
;; }
resume) do_resume() {
local api_key=$(get_sab_api) api_key=$(get_sab_api)
curl -s "http://$SAB_IP:$SAB_PORT/api?mode=resume&apikey=$api_key" >/dev/null 2>&1 /usr/bin/curl -s "http://$SAB_IP:$SAB_PORT/api?mode=resume&apikey=$api_key" >/dev/null 2>&1
json_init json_init
json_add_boolean "success" 1 json_add_boolean "success" 1
json_dump json_dump
;; }
remove) do_remove() {
read -r input read -r input
json_load "$input" json_load "$input"
json_get_var nzo_id nzo_id json_get_var nzo_id nzo_id
local api_key=$(get_sab_api) api_key=$(get_sab_api)
curl -s "http://$SAB_IP:$SAB_PORT/api?mode=queue&name=delete&value=$nzo_id&apikey=$api_key" >/dev/null 2>&1 /usr/bin/curl -s "http://$SAB_IP:$SAB_PORT/api?mode=queue&name=delete&value=$nzo_id&apikey=$api_key" >/dev/null 2>&1
json_init json_init
json_add_boolean "success" 1 json_add_boolean "success" 1
json_dump json_dump
;; }
config) do_config() {
json_init json_init
# NNTP servers # NNTP servers
@ -277,11 +262,24 @@ except:
json_close_array json_close_array
json_dump json_dump
;; }
*) case "$1" in
echo '{"error":"Unknown method"}' list)
echo '{"status":{},"queue":{},"history":{},"search":{"query":"string"},"add_nzb":{"url":"string","category":"string"},"pause":{},"resume":{},"remove":{"nzo_id":"string"},"config":{}}'
;; ;;
call)
case "$2" in
status) do_status ;;
queue) do_queue ;;
history) do_history ;;
search) do_search ;;
add_nzb) do_add_nzb ;;
pause) do_pause ;;
resume) do_resume ;;
remove) do_remove ;;
config) do_config ;;
*) echo '{"error":"Unknown method"}' ;;
esac esac
;; ;;
esac esac