util  Diff

Differences From Artifact [589ffdd50a]:

To Artifact [08bfad81fd]:


     8      8   ;    simple nonce authentication is used but the
     9      9   ;    clipboard is sent IN THE CLEAR 
    10     10   
    11     11   do read/text %ansi.b
    12     12   
    13     13   verbose: no
    14     14   readonly: no
           15  +to-stdout: no
    15     16   port: "42069"
    16     17   key: #{981A7879}
           18  +
           19  +setter: case [
           20  +	getenv "ANDROID_ROOT" "termux-clipboard-set"
           21  +	true "xsel -bi"
           22  +]
           23  +
           24  +getter: case [
           25  +	getenv "ANDROID_ROOT" "termux-clipboard-get"
           26  +	true "xsel -bo"
           27  +]
    17     28   
    18     29   proto: context [
    19     30   	writecb: #{3E}
    20     31   	readcb: #{C5}
           32  +	stopserver: #{69}
    21     33   ]
    22     34   
    23     35   debug: func [x] [
    24     36   	if verbose [
    25     37   		x: either block? x [rejoin x] [x]
    26     38   
    27     39   		write stderr fmt [
................................................................................
    40     52   debug "parsing arguments"
    41     53   
    42     54   usage: does [
    43     55   	write stderr fmt [[(style hl)"usage:"]
    44     56   		" ./clip.b get <hostname> | send <hostname>" ]
    45     57   ]
    46     58   
    47         -flag: func [f /extern verbose readonly] [
           59  +flag: func [f /extern verbose readonly to-stdout] [
    48     60   	debug ["found flag " i]
    49     61   	do select [
    50     62   		"-v" [ verbose: yes ]
    51     63   		"-q" [ verbose: no ]
    52     64   		"-r" [ readonly: yes ]
           65  +		"-o" [ to-stdout: yes ]
    53     66   	] f
    54     67   ]
    55     68   
    56         -set-clipboard: func[c] [ execute/in "xsel -bi" c ]
           69  +set-clipboard: func[c] [
           70  +	either to-stdout [ print c ] [
           71  +		execute/in setter c
           72  +	]
           73  +]
    57     74   
    58     75   get-clipboard: func[/local] [ 
    59     76   	clip-out: ""
    60         -	execute/out "xsel -bo" clip-out
           77  +	execute/out getter clip-out
    61     78   	clip-out
    62     79   ]
    63     80   
    64     81   start-server: func[/local] [
    65     82   	debug ["serving clipboard from " hostname none ]
           83  +	debug [{using setter "} setter {" and getter "} getter {"}]
    66     84   	sock: open join "tcp://:" port
    67     85   	forever [
    68     86   		connection: read wait sock
    69     87   		nonce: rand-block 4
    70     88   		expect-key: (key-hash nonce key)
    71     89   		write connection nonce
    72     90   		debug [ "sent nonce " nonce ";"
................................................................................
    87    105   					write connection get-clipboard
    88    106   				]
    89    107   				eq? client-command proto/writecb [
    90    108   					new-clipboard: to-string client-body
    91    109   					debug ["got cb " new-clipboard]
    92    110   					set-clipboard new-clipboard
    93    111   				]
          112  +				eq? client-command proto/stopserver [break]
    94    113   				true [ write connection "bad command" ]
    95    114   			]
    96    115   		] [
    97    116   			debug "denied access"
    98    117   			write connection "access denied"
    99    118   		]
   100    119   		close connection
   101    120   		debug [ "closed connection" ]
   102    121   	]
   103    122   ]
   104    123   
   105    124   send-clip: func[host] [
   106         -	debug ["sending clipboard to " host ]
   107         -	sock: open rejoin ["tcp://" host ":" port]
   108         -	nonce: read wait sock
   109         -	debug [ "got nonce " nonce ]
   110         -	client-key: key-hash nonce key
   111         -	debug [ "sending password " client-key ]
   112         -	msg: rejoin [
   113         -		client-key
   114         -		proto/writecb
   115         -		get-clipboard #{00}
          125  +	catch [
          126  +		debug [{using getter "} getter {"}]
          127  +		debug ["sending clipboard to " host ]
          128  +		sock: open rejoin ["tcp://" host ":" port]
          129  +		nonce: read wait sock
          130  +		debug [ "got nonce " nonce ]
          131  +		client-key: key-hash nonce key
          132  +		debug [ "sending password " client-key ]
          133  +		msg: rejoin [
          134  +			client-key
          135  +			proto/writecb
          136  +			get-clipboard #{00}
          137  +		]
          138  +		write sock msg
          139  +		close sock
          140  +		true
   116    141   	]
   117         -	write sock msg
   118    142   ]
   119    143   get-clip: func[host] [
   120         -	debug ["getting clipboard from " host ]
   121         -	sock: open rejoin ["tcp://" host ":" port]
   122         -	nonce: read wait sock
   123         -	debug [ "got nonce " nonce ]
   124         -	client-key: key-hash nonce key
   125         -	debug [ "sending password " client-key ]
   126         -	msg: rejoin [ client-key proto/readcb ]
   127         -	write sock msg
   128         -	clipboard: read wait sock
   129         -	print ["server response: " to-string clipboard]
          144  +	catch [
          145  +		debug [{using setter "} setter {"}]
          146  +		debug ["getting clipboard from " host ]
          147  +		sock: open rejoin ["tcp://" host ":" port]
          148  +		nonce: read wait sock
          149  +		debug [ "got nonce " nonce ]
          150  +		client-key: key-hash nonce key
          151  +		debug [ "sending password " client-key ]
          152  +		msg: rejoin [ client-key proto/readcb ]
          153  +		write sock msg
          154  +		clipboard: read wait sock
          155  +		debug ["server response: " to-string clipboard]
          156  +		set-clipboard to-string clipboard
          157  +		close sock
          158  +		true
          159  +	]
          160  +]
          161  +stop-clip: func[host] [
          162  +	catch [
          163  +		debug ["stopping clipserv on " host ]
          164  +		sock: open rejoin ["tcp://" host ":" port]
          165  +		nonce: read wait sock
          166  +		debug [ "got nonce " nonce ]
          167  +		client-key: key-hash nonce key
          168  +		debug [ "sending password " client-key ]
          169  +		msg: rejoin [ client-key proto/stopserver ]
          170  +		write sock msg
          171  +		close sock
          172  +		true
          173  +	]
   130    174   ]
   131    175   
   132    176   main: func [argv checked-flags] [
   133    177   	case [
   134    178   		none? argv [ usage ]
   135    179   		parse argv ["get"  host: string!] [ get-clip  host ]
   136    180   		parse argv ["send" host: string!] [ send-clip host ]
          181  +		parse argv ["stop" host: string!] [ stop-clip host ]
   137    182   		parse argv ["serve"] [ start-server ]
   138    183   		not checked-flags [
   139    184   			new-args: []
   140    185   			foreach i argv [
   141    186   				either eq? first i '-' [ flag i ] [
   142    187   					new-args: join new-args i ] ]
   143    188   			main new-args yes
   144    189   		]
   145    190   		true [ usage ]
   146    191   	]
   147    192   ]
   148    193   
   149         -main args no
          194  +quit/return main args no