From 2dac13303d143a774e26b50614f43b25f88c6eaa Mon Sep 17 00:00:00 2001 From: bvaudour Date: Wed, 22 Jul 2020 11:24:43 +0200 Subject: [PATCH] maj de la configuration --- aliases/add_badips.elv | 28 +++++++++++++ aliases/clean_known_hosts.elv | 19 +++++---- aliases/dig.elv | 2 + aliases/digall.elv | 69 ++++++++++++++++++++++++++++++++ aliases/ip-of.elv | 5 +-- aliases/xaa | 1 + lib/moi/aliases.elv | 1 + lib/moi/completion/arc.elv | 2 +- lib/moi/completion/kcp.elv | 2 +- lib/moi/completion/pacman.elv | 7 ++-- lib/moi/completion/ssh.elv | 16 ++++---- lib/moi/completion/sudo.elv | 2 +- lib/moi/util/.list.elv.kate-swp | Bin 0 -> 332 bytes lib/moi/util/condition.elv | 2 +- lib/moi/util/file.elv | 3 +- lib/moi/util/ip.elv | 56 ++++++++++++++++++-------- lib/moi/util/list.elv | 32 ++++++++------- lib/moi/util/map.elv | 21 ++++++++-- lib/moi/util/option.elv | 6 +-- rc.elv | 5 ++- 20 files changed, 210 insertions(+), 69 deletions(-) create mode 100644 aliases/add_badips.elv create mode 100644 aliases/dig.elv create mode 100644 aliases/digall.elv create mode 100644 aliases/xaa create mode 100644 lib/moi/util/.list.elv.kate-swp diff --git a/aliases/add_badips.elv b/aliases/add_badips.elv new file mode 100644 index 0000000..a9d31f0 --- /dev/null +++ b/aliases/add_badips.elv @@ -0,0 +1,28 @@ +#alias:new add_badips e:echo +fn add_badips [@_args]{ + use moi/util/ip + use moi/util/list + use moi/util/map + use moi/util/number + use str + + local:f = ~/bad_ips.txt + local:ips = [ (cat $f) ] + local:m = (map:to-set $ips) + local:c = 0 + each [e]{ + local:cond = (and (not (has-key $m $e)) (ip:is-ipv4 $e)) + if $cond { + echo $e ajoutée + c = (number:++ $c) + m[$e] = $nil + ips = [ (all $ips) $e ] + } + } $_args + if (> $c 0) { + ips = [ (list:sort $ip:cmp4~ $ips) ] + } + local:result = (str:join "\n" $ips) + echo $result > $f + echo $c' IPs ajoutées ('(count $ips)' au total)' +} diff --git a/aliases/clean_known_hosts.elv b/aliases/clean_known_hosts.elv index 2e62cf5..7aaf64f 100644 --- a/aliases/clean_known_hosts.elv +++ b/aliases/clean_known_hosts.elv @@ -1,37 +1,36 @@ #alias:new clean_known_hosts e:cat fn clean_known_hosts [@_args]{ + use str use moi/util/condition use moi/util/list use moi/util/ip fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } fn sort-host [e1 e2]{ - local:i1 = (condition:set (ip:is-ipv4 $e1) 2 (condition:set (ip:is-ipv6 $e1) 1 0)) - local:i2 = (condition:set (ip:is-ipv4 $e2) 2 (condition:set (ip:is-ipv6 $e2) 1 0)) + local:i1 = (condition:set (ip:is-ip $e1) 1 0) + local:i2 = (condition:set (ip:is-ip $e2) 1 0) if (!= $i1 $i2) { < $i1 $i2 } elif (== $i1 0) { ~/.ssh/known_hosts + echo (str:join "\n" $lines) > ~/.ssh/known_hosts } diff --git a/aliases/dig.elv b/aliases/dig.elv new file mode 100644 index 0000000..497ddf6 --- /dev/null +++ b/aliases/dig.elv @@ -0,0 +1,2 @@ +#alias:new dig e:dig +noall +answer +fn dig [@_args]{ e:dig +noall +answer $@_args } diff --git a/aliases/digall.elv b/aliases/digall.elv new file mode 100644 index 0000000..cdea25d --- /dev/null +++ b/aliases/digall.elv @@ -0,0 +1,69 @@ +#alias:new digall e:dig ANY +noall +answer +fn digall [@_args]{ + use moi/util/list + use str + + local:default_records = [ + A + AAAA + CNAME + PTR + TXT + MX + SRV + NAPTR + NS + SOA + ] + local:records = [] + local:servers = [] + local:flags = [ + +noall + +answer + ] + local:domains = [] + + fn init []{ + each [e]{ + if (is $e[0] '+') { + flags = [ (all $flags) $e ] + } elif (is $e[0] @) { + servers = [ (all $servers) $e ] + } elif (list:includes $e $default_records) { + records = [ (all $records) $e ] + } else { + domains = [ (all $domains) $e ] + } + } $_args + if (== (count $records) 0) { + records = $default_records + } + } + + fn digr [d @args]{ + local:results = [ (each [r]{ + e:dig $@args $r $d $@flags + } $records) ] + results = [ (list:remove-duplicate $results) ] + echo (str:join "\n" $results) + } + + fn digs [s d]{ + echo (styled $s bright-yellow) + digr $d $s + } + + fn digd [d]{ + echo (styled 'Domaine: '$d bright-green) + if (> (count $servers) 0) { + each [s]{ + digs $s $d + } $servers + } else { + digr $d + } + } + + init + each $digd~ $domains +} diff --git a/aliases/ip-of.elv b/aliases/ip-of.elv index 68cda35..f08bc76 100644 --- a/aliases/ip-of.elv +++ b/aliases/ip-of.elv @@ -3,7 +3,6 @@ fn ip-of [@_args]{ use str use moi/util/condition use moi/util/ip - use moi/util/list fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } @@ -79,7 +78,7 @@ fn ip-of [@_args]{ if (has-key $hosts $d) { exists = $true echo (styled ' local:' bright-yellow) - keys $hosts[$d] | list:sort $sort-ip~ | each [ip]{ + keys $hosts[$d] | order &less-than=$sort-ip~ | each [ip]{ echo ' - '$ip } } @@ -88,7 +87,7 @@ fn ip-of [@_args]{ local:info = $remote[$d] echo (styled ' remote:' bright-yellow) echo ' IPs:' - keys $info[ips] | list:sort $sort-ip~ | each [ip]{ + keys $info[ips] | order &less-than=$sort-ip~ | each [ip]{ echo ' - '$ip } echo ' Resolvers: '$info[resolvers] diff --git a/aliases/xaa b/aliases/xaa new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/aliases/xaa @@ -0,0 +1 @@ + diff --git a/lib/moi/aliases.elv b/lib/moi/aliases.elv index 9759255..6e68e79 100644 --- a/lib/moi/aliases.elv +++ b/lib/moi/aliases.elv @@ -11,6 +11,7 @@ alias:new cat bat -pp alias:new midi e:fluidsynth -a pulseaudio /usr/share/soundfonts/FluidR3_GM.sf2 alias:new timidity e:timidity -a -K -1 alias:new git e:hub +alias:new dig e:dig +noall +answer #alias:new pacmanhisto cat /var/log/pacman.log | grep "ALPM" | grep alias:new pf e:pkgfile -vri diff --git a/lib/moi/completion/arc.elv b/lib/moi/completion/arc.elv index 2491a8d..3b701b4 100644 --- a/lib/moi/completion/arc.elv +++ b/lib/moi/completion/arc.elv @@ -11,7 +11,7 @@ commands = [ extensions = [ tar bz2 zip gz lz4 sz xz zst rar ] -fn -comp-commands { explode $commands } +fn -comp-commands { all $commands } fn -comp-archive-files [m]{ local:type = (condition:set (eq $m '') prefix deep-prefix) diff --git a/lib/moi/completion/kcp.elv b/lib/moi/completion/kcp.elv index 993d26a..13cd190 100644 --- a/lib/moi/completion/kcp.elv +++ b/lib/moi/completion/kcp.elv @@ -32,7 +32,7 @@ np = [ fn complete [@argv]{ local:c = (count $argv) if (== $c 2) { - explode $options + all $options } elif (and (== $c 3) (has-value $np $argv[-2])) { -remotes-packages } diff --git a/lib/moi/completion/pacman.elv b/lib/moi/completion/pacman.elv index c66d734..ee1a9bf 100644 --- a/lib/moi/completion/pacman.elv +++ b/lib/moi/completion/pacman.elv @@ -11,13 +11,14 @@ fn -repo-packages { put $p })] local:spackages = [&] + use str peach [p]{ - splits '/' $p + str:split '/' $p } $packages | peach [e]{ spackages[$e] = $nil } keys $spackages - explode $packages + all $packages } fn -downloaded-packages [m]{ @@ -83,7 +84,7 @@ fpack = [ fn complete [@argv]{ local:c = (count $argv) if (< $c 3) { - explode $options + all $options } else { local:cmd = $argv[1] if (and (== $c 3) (has-value $asdeps $cmd)) { diff --git a/lib/moi/completion/ssh.elv b/lib/moi/completion/ssh.elv index 94eeedf..2f700b4 100644 --- a/lib/moi/completion/ssh.elv +++ b/lib/moi/completion/ssh.elv @@ -44,11 +44,11 @@ fn -init-hosts { fn -init-known-hosts { local:khosts = [&22=[&]] cat ~/.ssh/known_hosts | peach [l]{ - put [(splits ' ' $l)] + put [(str:split ' ' $l)] } | peach [e]{ local:domains @local:id = $@e - id = (joins ' ' $id) - splits ',' $domains | peach [d]{ + id = (str:join ' ' $id) + str:split ',' $domains | peach [d]{ local:d = $d local:port = 22 if (eq $d[0] '[') { @@ -105,10 +105,10 @@ fn -init-history { if (and (< $i $c) (or (and (eq $cmd ssh) (eq $arg -p)) (eq $arg -P))) { port = $argv[(+ $i 1)] } else { - local:nd = [(splits '@' $arg)] + local:nd = [(str:split '@' $arg)] if (== (count $nd) 2) { name = $nd[0] - domain @_ = (splits ':' $nd[1]) + domain @_ = (str:split ':' $nd[1]) break } } @@ -207,14 +207,14 @@ fn -complete-args [cmd @argv]{ port = $port[-1] } local:larg = $argv[-1] - local:name @local:host = (splits '@' $larg) + local:name @local:host = (str:split '@' $larg) if (list:empty $host) { -known-names &port=$port | each [n]{ put $n'@' } if (eq $cmd 'scp') { file:match-files $larg } } else { - host @_ = (splits ':' $host[0]) + host @_ = (str:split ':' $host[0]) local:check = [h]{ has-prefix $h $host } local:khosts = [(-known-hosts &port=$port &name=$name)] if (not (list:contains $check $khosts)) { @@ -224,7 +224,7 @@ fn -complete-args [cmd @argv]{ } } each [h]{ put $name'@'$h } $khosts - explode $khosts + all $khosts } } diff --git a/lib/moi/completion/sudo.elv b/lib/moi/completion/sudo.elv index 86ecb99..1f68fa9 100644 --- a/lib/moi/completion/sudo.elv +++ b/lib/moi/completion/sudo.elv @@ -1,6 +1,6 @@ fn complete [@argv]{ if (and (> (count $argv) 2) (has-key $edit:completion:arg-completer $argv[1])) { - $edit:completion:arg-completer[$argv[1]] (explode $argv[1:]) + $edit:completion:arg-completer[$argv[1]] (all $argv[1:]) } else { edit:complete-sudo $@argv } diff --git a/lib/moi/util/.list.elv.kate-swp b/lib/moi/util/.list.elv.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..6e7f85879a50904c10c8695b99e432acb8fa98b1 GIT binary patch literal 332 zcmZQzU=Z?7EJ;-eE>A2_aLdd|RWQ;sU|?VnS^Qsm`)9A6_D6Po3TpiN`mJ=v_R3&S zpv*NO<^y2`g@V!&g_Qi<#LT>6t8l243`os&AO-=Tn#7zO1r?|wkQ$H_A4ts&AO-=b zIYp_-`9&!Zb3jsHH8;U35avJ?fy@C(fz{kXr~$dBIJKxOwFqJkND8dxHbM>9Jy1m; ob3jsHHFuzDcp&~t%SlX!m;#dE0lD}tLIv0quo93NAPF8<0E4Vb?*IS* literal 0 HcmV?d00001 diff --git a/lib/moi/util/condition.elv b/lib/moi/util/condition.elv index bf8f063..f527ca0 100644 --- a/lib/moi/util/condition.elv +++ b/lib/moi/util/condition.elv @@ -7,7 +7,7 @@ fn set [c t f]{ } fn mset [c t f]{ - explode (set $c $t $f) + all (set $c $t $f) } fn call [c t f @argv]{ diff --git a/lib/moi/util/file.elv b/lib/moi/util/file.elv index 9f02de7..1394118 100644 --- a/lib/moi/util/file.elv +++ b/lib/moi/util/file.elv @@ -20,8 +20,7 @@ fn -less [f1 f2]{ fn -get-results [sort result]{ if $sort { - use ./list - keys $result | list:sort $-less~ + keys $result | order $-less~ } else { keys $result } diff --git a/lib/moi/util/ip.elv b/lib/moi/util/ip.elv index 8c6f2da..655dce5 100644 --- a/lib/moi/util/ip.elv +++ b/lib/moi/util/ip.elv @@ -1,15 +1,16 @@ +use re +use str + fn is-ipv4 [arg]{ - use re if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) { put $false } else { use ./list - not (splits '.' $arg | list:contains [p]{ > $p 255 }) + not (str:split '.' $arg | list:contains [p]{ > $p 255 }) } } fn is-ipv6 [arg]{ - use re local:p = '[0-9a-fA-F]{1,4}' local:g1 local:g2 = '('$p':)' '(:'$p')' local:cases = [ @@ -24,15 +25,17 @@ fn is-ipv6 [arg]{ ':'$g2'{1,7}' '::' ] - local:r = '^('(joins '|' $cases)')$' + local:r = '^('(str:join '|' $cases)')$' put (re:match $r $arg) } +fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) } + fn -long-part6 [p]{ use str p = (str:to-lower $p) c = (- 4 (count $p)) - put (repeat $c '0' | joins '')$p + put (repeat $c '0' | str:join '')$p } fn -middle-part6 [p]{ @@ -68,25 +71,24 @@ fn long6 [ip]{ fail 'Not an IPv6' } if (eq $ip '::') { - repeat 8 '0000' | joins ':' + repeat 8 '0000' | str:join ':' return } - use str local:c = (- 7 (str:count $ip ':')) if (> $c 0) { local:i = (str:index $ip '::') - local:z = (repeat $c ':' | joins '') - ip = (joins '' [$ip[:$i] $z $ip[{$i}:]]) + local:z = (repeat $c ':' | str:join '') + ip = (str:join '' [$ip[:$i] $z $ip[{$i}:]]) } - splits ':' $ip | each $-long-part6~ | joins ':' + str:split ':' $ip | each $-long-part6~ | str:join ':' } fn middle6 [ip]{ - splits ':' (long6 $ip) | each $-middle-part6~ | joins ':' + str:split ':' (long6 $ip) | each $-middle-part6~ | str:join ':' } fn short6 [ip]{ - local:parts = [(splits ':' (middle6 $ip))] + local:parts = [(str:split ':' (middle6 $ip))] local:i local:s = (-find-max0 $parts) if (>= $i 0) { local:left local:right = $parts[:$i] $parts[(+ $i $s):] @@ -98,7 +100,7 @@ fn short6 [ip]{ } parts = [$@left '' $@right] } - joins ':' $parts + str:join ':' $parts } fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) } @@ -119,14 +121,34 @@ fn -cmp [e1 e2]{ fn cmp4 [ip1 ip2]{ if (or (not (is-ipv4 $ip1)) (not (is-ipv4 $ip2))) { - fail 'Not an IPv4' + fail 'Not an IPv4 '$ip1' → '$ip2 } - -cmp [(splits . $ip1)] [(splits . $ip2)] + -cmp [(str:split . $ip1)] [(str:split . $ip2)] } fn cmp6 [ip1 ip2]{ if (or (not (is-ipv6 $ip1)) (not (is-ipv6 $ip2))) { - fail 'Not an IPv6' + fail 'Not an IPv6: '$ip1' → '$ip2 + } + -cmp [(str:split : (middle6 $ip1))] [(str:split : (middle6 $ip2))] +} + +fn cmp [ip1 ip2]{ + if (is-ipv4 $ip1) { + if (is-ipv4 $ip2) { + cmp4 $ip1 $ip2 + } else { + put -1 + } + } elif (is-ipv6 $ip1) { + if (is-ipv4 $ip2) { + put 1 + } elif (is-ipv6 $ip2) { + cmp6 $ip1 $ip2 + } else { + put -1 + } + } else { + fali 'Not an IP: '$ip1' → '$ip2 } - -cmp [(splits : (middle6 $ip1))] [(splits : (middle6 $ip2))] } diff --git a/lib/moi/util/list.elv b/lib/moi/util/list.elv index 1050802..a3ff1bb 100644 --- a/lib/moi/util/list.elv +++ b/lib/moi/util/list.elv @@ -206,12 +206,12 @@ fn -swap [i j l]{ fail 'Index out of range' } if (eq $i $j) { - explode $l + all $l } else { i j = (condition:mset (< $i $j) [$i $j] [$j $i]) take $i $l put $l[j] - explode $l[(+ $i 1):$j] + all $l[(+ $i 1):$j] put $l[$i] drop (+ $j 1) $l } @@ -270,6 +270,9 @@ fn contains-not [cb @argv]{ contains [v]{ not ($cb $v) } $@argv } fn exists [cb @argv]{ -exists $cb (-p $@argv) } fn exists-not [cb @argv]{ exists [i v]{ $cb $i $v } $@argv } +fn includes [v @argv]{ contains [e]{ is $v $e } $@argv } +fn includes-not [v @argv]{ contains-not [e]{ is $v $e } $@argv } + fn reduce [v cb @argv]{ each [e]{ v = ($cb $v $e) } (to-list $@argv) put $v @@ -281,29 +284,28 @@ fn premove-duplicate [@argv]{ -premove-duplicate (-p $@argv) } fn swap [i j @argv]{ -swap $i $j (-p $@argv) } fn -s1 [cb l e]{ - c = (count $l) - i = (first-index [v]{ $cb $e $v } $l) + local:c = (count $l) + local:i = (first-index [v]{ $cb $e $v } $l) if (eq $i 0) { put $e - explode $l + all $l } elif (< $i 0) { - explode $l + all $l put $e } else { - explode $l[:$i] + all $l[:$i] put $e - explode $l[{$i}:] + all $l[{$i}:] } } +fn less [cb]{ + local:l = [a b]{ < ($cb $a $b) 0 } + put $l +} + fn -sort [cb l]{ - c = (count $l) - if (> $c 1) { - l2 = $l[:1] - each [e]{ l2 = [ (-s1 $cb $l2 $e) ] } $l[1:] - l = $l2 - } - explode $l + order &less-than=(less $cb) $l } fn sort [cb @argv]{ -sort $cb (-p $@argv) } diff --git a/lib/moi/util/map.elv b/lib/moi/util/map.elv index 4cbfc47..0536a48 100644 --- a/lib/moi/util/map.elv +++ b/lib/moi/util/map.elv @@ -14,8 +14,8 @@ fn value-of [container k &default=$nil]{ fn unzip [container]{ local:lkeys local:lvalues = [] [] keys $container | each [k]{ - lkeys = [(explode $lkeys) $k] - lvalues = [(explode $lvalues) $container[$k]] + lkeys = [(all $lkeys) $k] + lvalues = [(all $lvalues) $container[$k]] } put $lkeys $lvalues } @@ -69,9 +69,24 @@ fn add [container key @values]{ use ./list local:values = (list:to-list $values) if (has-key $container $key) { - container[$key] = [(explode $container[$key]) $@values] + container[$key] = [(all $container[$key]) $@values] } else { container[$key] = $values } put $container } + +fn join [container container2]{ + keys $container2 | each [k]{ + container[$k] = $container2[$k] + } + put $container +} + +fn sorted-keys [&reverse=$false &less-than=$nil container]{ + keys $container | order &reverse=$reverse &less-than=$less-than +} + +fn sorted-values [&reverse=$false &less-than=$nil container]{ + values $container | order &reverse=$reverse &less-than=$less-than +} diff --git a/lib/moi/util/option.elv b/lib/moi/util/option.elv index b3641cb..f797bbc 100644 --- a/lib/moi/util/option.elv +++ b/lib/moi/util/option.elv @@ -11,9 +11,9 @@ fn -splitm [o]{ each [e]{ put -$e } $o[1:] } fn -splitl [o]{ if (str:contains $o '=') { - local:opt @local:value = (splits '=' $o) + local:opt @local:value = (str:split '=' $o) put $opt - joins '=' $value + str:join '=' $value } else { put $o } @@ -30,7 +30,7 @@ fn split [o]{ } fn -joins [opt]{ - local:o = (each [o]{ put $o[1:] } $opt | joins '') + local:o = (each [o]{ put $o[1:] } $opt | str:join '') if (not-eq $o '') { put -$o } } diff --git a/rc.elv b/rc.elv index 1a7ae98..52d50b3 100644 --- a/rc.elv +++ b/rc.elv @@ -54,7 +54,10 @@ paths = [$@paths ~/bin $E:GOPATH/bin ] -exports- = (aliases:export) -fn dcd [@_args]{ cmd = (aliases:export); $cmd[br~] --only-folders --cmd (joins ' ' $_args)' :cd' } +fn dcd [@_args]{ + use str + cmd = (aliases:export); $cmd[br~] --only-folders --cmd (str:join ' ' $_args)' :cd' +} tache search -d 2w not del