about summary refs log tree commit diff stats
path: root/bonus
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-11 10:46:58 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-11 11:03:15 +0100
commite419b5881c8ca99a36962145f9647d2c37e624e2 (patch)
tree2df1bd4ed0c120b317b2bb2cabd0541e4e0a41ba /bonus
parentd2451e51ede8a733c22fbe0b285ccb341e272b16 (diff)
downloadchawan-e419b5881c8ca99a36962145f9647d2c37e624e2.tar.gz
bonus: add magnet.cgi
Diffstat (limited to 'bonus')
-rwxr-xr-xbonus/magnet.cgi78
-rwxr-xr-x[-rw-r--r--]bonus/trans.cgi0
2 files changed, 78 insertions, 0 deletions
diff --git a/bonus/magnet.cgi b/bonus/magnet.cgi
new file mode 100755
index 00000000..085d252b
--- /dev/null
+++ b/bonus/magnet.cgi
@@ -0,0 +1,78 @@
+#!/bin/sh
+# Add magnet: links to transmission using transmission-remote.
+#
+# Usage: place magnet.cgi in your cgi-bin directory (don't forget to set
+# the executable bit, e.g. chmod +x magnet.cgi), then add the following line
+# to your urimethodmap:
+#
+# magnet: /cgi-bin/magnet.cgi?%s
+#
+# Then, set the remote transmission session's address using the
+# CHA_TRANSMISSION_ADDRESS environment variable, and if needed, the
+# authentication data using CHA_TRANSMISSION_AUTH. Alternatively, uncomment
+# the following lines and set them there:
+
+#CHA_TRANSMISSION_ADDRESS=localhost:9091
+#CHA_TRANSMISSION_AUTH=username:password
+
+#TODO: add a way to authenticate without exposing the credentials as an
+# environment variable
+
+die() {
+	printf "Content-Type: text/plain\n\n%s" "$1"
+	exit 1
+}
+
+decode() {
+	# URL-decode the string passed as the first parameter
+	printf '%s\n' "$1" | \
+		sed 's/+/ /g;s/%/\\x/g' | \
+		xargs -0 printf "%b"
+}
+
+html_quote() {
+	sed 's/&/&amp;/g;s/</&lt;/g;s/>/&gt;/g;s/'\''/&apos;/g;s/"/&quot;/g'
+}
+
+
+test -n "$QUERY_STRING" || die "URL expected"
+type transmission-remote >/dev/null || die "transmission-remote not found"
+
+case "$REQUEST_METHOD" in
+GET)	URL_HTML_QUOTED="$(printf '%s' "$QUERY_STRING" | html_quote)"
+	printf 'Content-Type: text/html\n\n
+<!DOCTYPE HTML>
+<HEAD>
+<TITLE>Add magnet URL</TITLE>
+</HEAD>
+<H1>Add magnet URL</H1>
+<P>
+Add the following magnet URL to transmission?
+<PRE>%s</PRE>
+<FORM METHOD=POST>
+<INPUT TYPE=SUBMIT NAME=ADD_URL VALUE=OK>
+<INPUT type=HIDDEN NAME=URL VALUE='%s'>
+</FORM>
+' "$URL_HTML_QUOTED" "$URL_HTML_QUOTED"
+	;;
+POST)	read line
+	case $line in
+	'ADD_URL=OK&'*) line="${line#*&}" ;;
+	*) die 'Invalid POST 1; this is probably a bug in the magnet script.' ;;
+	esac
+	case $line in
+	URL=*) line="${line#*=}" ;;
+	*) die 'Invalid POST 2; this is probably a bug in the magnet script.'"$line" ;;
+	esac
+	line="$(decode "$line")"
+	if test -n "$CHA_TRANSMISSION_AUTH"
+	then	authparam="--auth=$CHA_TRANSMISSION_AUTH"
+	fi
+	if test -n "$authparam"
+	then	output="$(transmission-remote "${CHA_TRANSMISSION_ADDRESS:-localhost:9091}" "$authparam" -a "$line" 2>&1)"
+	else	output="$(transmission-remote "${CHA_TRANSMISSION_ADDRESS:-localhost:9091}" -a "$line" 2>&1)"
+	fi
+	printf 'Content-Type: text/plain\n\n%s' "$output"
+	;;
+*)	die "Unrecognized HTTP method $HTTP_METHOD" ;;
+esac
diff --git a/bonus/trans.cgi b/bonus/trans.cgi
index bfaca713..bfaca713 100644..100755
--- a/bonus/trans.cgi
+++ b/bonus/trans.cgi