@@ -11,14 +11,26 @@ do read/text %ansi.b verbose: no readonly: no +to-stdout: no port: "42069" key: #{981A7879} + +setter: case [ + getenv "ANDROID_ROOT" "termux-clipboard-set" + true "xsel -bi" +] + +getter: case [ + getenv "ANDROID_ROOT" "termux-clipboard-get" + true "xsel -bo" +] proto: context [ writecb: #{3E} readcb: #{C5} + stopserver: #{69} ] debug: func [x] [ if verbose [ @@ -43,27 +55,33 @@ write stderr fmt [[(style hl)"usage:"] " ./clip.b get | send " ] ] -flag: func [f /extern verbose readonly] [ +flag: func [f /extern verbose readonly to-stdout] [ debug ["found flag " i] do select [ "-v" [ verbose: yes ] "-q" [ verbose: no ] "-r" [ readonly: yes ] + "-o" [ to-stdout: yes ] ] f ] -set-clipboard: func[c] [ execute/in "xsel -bi" c ] +set-clipboard: func[c] [ + either to-stdout [ print c ] [ + execute/in setter c + ] +] get-clipboard: func[/local] [ clip-out: "" - execute/out "xsel -bo" clip-out + execute/out getter clip-out clip-out ] start-server: func[/local] [ debug ["serving clipboard from " hostname none ] + debug [{using setter "} setter {" and getter "} getter {"}] sock: open join "tcp://:" port forever [ connection: read wait sock nonce: rand-block 4 @@ -90,8 +108,9 @@ new-clipboard: to-string client-body debug ["got cb " new-clipboard] set-clipboard new-clipboard ] + eq? client-command proto/stopserver [break] true [ write connection "bad command" ] ] ] [ debug "denied access" @@ -102,39 +121,65 @@ ] ] send-clip: func[host] [ - debug ["sending clipboard to " host ] - sock: open rejoin ["tcp://" host ":" port] - nonce: read wait sock - debug [ "got nonce " nonce ] - client-key: key-hash nonce key - debug [ "sending password " client-key ] - msg: rejoin [ - client-key - proto/writecb - get-clipboard #{00} + catch [ + debug [{using getter "} getter {"}] + debug ["sending clipboard to " host ] + sock: open rejoin ["tcp://" host ":" port] + nonce: read wait sock + debug [ "got nonce " nonce ] + client-key: key-hash nonce key + debug [ "sending password " client-key ] + msg: rejoin [ + client-key + proto/writecb + get-clipboard #{00} + ] + write sock msg + close sock + true ] - write sock msg ] get-clip: func[host] [ - debug ["getting clipboard from " host ] - sock: open rejoin ["tcp://" host ":" port] - nonce: read wait sock - debug [ "got nonce " nonce ] - client-key: key-hash nonce key - debug [ "sending password " client-key ] - msg: rejoin [ client-key proto/readcb ] - write sock msg - clipboard: read wait sock - print ["server response: " to-string clipboard] + catch [ + debug [{using setter "} setter {"}] + debug ["getting clipboard from " host ] + sock: open rejoin ["tcp://" host ":" port] + nonce: read wait sock + debug [ "got nonce " nonce ] + client-key: key-hash nonce key + debug [ "sending password " client-key ] + msg: rejoin [ client-key proto/readcb ] + write sock msg + clipboard: read wait sock + debug ["server response: " to-string clipboard] + set-clipboard to-string clipboard + close sock + true + ] +] +stop-clip: func[host] [ + catch [ + debug ["stopping clipserv on " host ] + sock: open rejoin ["tcp://" host ":" port] + nonce: read wait sock + debug [ "got nonce " nonce ] + client-key: key-hash nonce key + debug [ "sending password " client-key ] + msg: rejoin [ client-key proto/stopserver ] + write sock msg + close sock + true + ] ] main: func [argv checked-flags] [ case [ none? argv [ usage ] parse argv ["get" host: string!] [ get-clip host ] parse argv ["send" host: string!] [ send-clip host ] + parse argv ["stop" host: string!] [ stop-clip host ] parse argv ["serve"] [ start-server ] not checked-flags [ new-args: [] foreach i argv [ @@ -145,5 +190,5 @@ true [ usage ] ] ] -main args no +quit/return main args no