Subversion Repositories Koakuma

Rev

Rev 3 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 4
Line 1... Line 1...
1
# $Id: rpc.tcl 3 2024-10-01 14:31:23Z nishi $
1
# $Id: rpc.tcl 4 2024-10-01 15:42:33Z nishi $
-
 
2
package require http
-
 
3
package require base64
-
 
4
package require term::ansi::ctrl::unix
-
 
5
 
-
 
6
namespace eval rpc {
-
 
7
	proc require-auth {} {
-
 
8
		global RPC_URL
-
 
9
		set tok [::http::geturl "$RPC_URL"]
-
 
10
		set code [::http::ncode $tok]
-
 
11
		::http::cleanup $tok
-
 
12
		if { $code == 401 } {
-
 
13
			return 1
-
 
14
		} elseif { $code == 403 } {
-
 
15
			return -1
-
 
16
		} else {
-
 
17
			return 0
-
 
18
		}
-
 
19
	}
-
 
20
	set username ""
-
 
21
	set password ""
-
 
22
	proc ask-auth {} {
-
 
23
		upvar 1 username username
-
 
24
		upvar 1 password password
-
 
25
		puts -nonewline "Username: "
-
 
26
		flush stdout
-
 
27
		set username "[gets stdin]"
-
 
28
		puts -nonewline "Password: "
-
 
29
		flush stdout
-
 
30
		exec stty -echo
-
 
31
		set password "[gets stdin]"
-
 
32
		exec stty echo
-
 
33
		puts ""
-
 
34
 
-
 
35
		set headers ""
-
 
36
		lappend headers "Authorization"
-
 
37
		lappend headers "Basic [::base64::encode -wrapchar "" "$username:$password"]"
-
 
38
 
-
 
39
		global RPC_URL
-
 
40
		set tok [::http::geturl "$RPC_URL" -headers $headers]
-
 
41
		set code [::http::ncode $tok]
-
 
42
		::http::cleanup $tok
-
 
43
 
-
 
44
		if { $code == 200 } {
-
 
45
			return 1
-
 
46
		}
-
 
47
		return 0
-
 
48
	}
-
 
49
}