This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection-options.scm
82 lines (69 loc) · 3 KB
/
connection-options.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(defstruct connection-options
host
port
authenv
username
password
help
rest)
(define-syntax eprint
(syntax-rules ()
((eprint arg ...)
(with-output-to-port (current-output-port) (lambda () (print arg ...))))))
(define *connection-opts*
(cling
(lambda (ret . rest)
(update-connection-options ret #:rest (cadr rest)))
(arg '((--help -h -?))
#:help "Show this help text."
#:kons (lambda (ret _ _) (update-connection-options ret #:help #t)))
(arg '((--host) . host)
#:help "The host of the transmission instance."
#:kons (lambda (ret _ host) (update-connection-options ret #:host host)))
(arg '((--port) . port)
#:help "The port of the transmission instance."
#:kons (lambda (ret _ port) (update-connection-options ret #:port port)))
(arg '((--username) . username)
#:help "The username to access the transmission instance."
#:kons (lambda (ret _ username) (update-connection-options ret #:username username)))
(arg '((--password) . password)
#:help "The password to access the transmission instance."
#:kons (lambda (ret _ password) (update-connection-options ret #:password password)))
(arg '((--authenv -ne))
#:help "Like transmission-remote, use the environment variable TR_AUTH."
#:kons (lambda (ret _ _) (update-connection-options ret #:authenv #t)))))
(define (set-parameters! #!key host port authenv username password)
(when host (*host* host))
(when port (*port* port))
; Update authentication variables
(cond
(authenv
(let ((tr_auth (get-environment-variable "TR_AUTH")))
(unless tr_auth
(eprint "TR_AUTH not set"))
(let ((username/password (string-split tr_auth ":")))
(unless (= (length username/password) 2)
(eprint "Splitting TR_AUTH by ':' didn't result in 2 elements; authentication will most likely fail."))
(let ((username (car username/password))
(password (cadr username/password)))
(*username* username)
(*password* password)))))
((or username password)
(unless (and username password)
(eprint "Did you forget to set both username and password? Authentication may fail."))
(when username (*username* username))
(when password (*password* password)))
(else #f))
#t)
(define (update-connection-options! args)
(let ((options (process-arguments *connection-opts* (make-connection-options) args)))
(set-parameters!
#:host (connection-options-host options)
#:port (connection-options-port options)
#:authenv (connection-options-authenv options)
#:username (connection-options-username options)
#:password (connection-options-password options))
(values (connection-options-rest options)
(connection-options-help options))))
(define (parse-torrent-ids-string str)
(error 'parse-torrent-ids-string "UNIMPLEMENTED!"))