#! /bin/bash # # saptdvhf : send page and media to dokuwiki via html form # Copyright (C) 2012 2013 2015 2022 Schplurtz le déboulonné # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # #File debut IFS=' ' ########## # This whole script was produced by | Ce script tout entier est produit par # a script compiler that removed | un compilateur de script, qui a retir'e # all comments, and (most probably) | les commentaires et a vraissemblablement # added library files. Do not modify| ajout'e des fichiers de biblioth`eque. Ne # it ; find the source and modify it| modifiez pas ce script, mais sa source. ########## #End of File debut #Begin File saptdvhf #! /bin/bash unset IFS IFS=' ' version=2022-04-27.1 smallhelp() { cat <<-'!' saptdvhf [-h|-?|--help] saptdvhf [-v|--version] saptdvhf -C|--copyright saptdvhf -c|--connect cookie-file saptdvhf create-ident-file filename to get a page : saptdvhf [-q|--quiet] [-l|--log logfile] [-b|--brownie-file cookie-file] [-i|--ident-file user:password-file] [-I|--interactive-login] [-a|--web-user-agent user_agent_string] -g|--get-a-page [output-file] somedokuwiki-url-to-a-page to send a test page : saptdvhf [-q|--quiet] [-l|--log logfile] -t|--test-page [-s|--summary 'some summary'] [-b|--brownie-file cookie-file] [-i|--ident-file user:password-file] [-I|--interactive-login] [-a|--web-user-agent user_agent_string] [-d|--do-not-get-page-after-sending] somedokuwiki-url-to-a-test-page to send a page file : saptdvhf [-q|--quiet] [-l|--log logfile] -p|--page-file file [-s|--summary 'some summary'] [-b|--brownie-file cookie-file] [-i|--ident-file user:password-file] [-I|--interactive-login] [-a|--web-user-agent user_agent_string] [-d|--do-not-get-page-after-sending] somedokuwiki-url-to-a-page to send a media file : saptdvhf [-q|--quiet] [-l|--log logfile] -m|--media mediaid mediafile [-i|--ident-file user:password-file] [-I|--interactive-login] [-a|--web-user-agent user_agent_string] [-b|--brownie-file file] somedokuwiki-url-to-any-page ! } show_copyright= pageurl= pagefile=/etc/issue summary="$(date +'Automatically edited on %FT%T%z by an ugly script of schplurtz'\'' !')" user_agent="SchplurtzDWPageSubmitter-$version" curl=curl ident_file= pwfile= interactivepw=1 asklogin= get_page_after_sending=1 logfile= quiet= sendtestpage= output_file=- args=() mediaid= mediafile= jsphpfile= medialoginoption= cookiefile= connectfile= oldlogexist= log=() sommet= command=page help() { smallhelp cat <<-! This script tries to send a page by filling the dokuwiki editform. It can log into dokuwiki. The file that it sends should be a valid dokuwiki markup file. If there is a cap- tcha, this script can handle it automatically, provided it is of the standard dokuwiki javascript type. This script is very ugly in any way. Using this script is a waste of your system resources, network resources, and system resources at the doku site. It is intended to be a last resort solution only. All option or file contents etc are expected to be UTF-8 encoded. Arguments for long option may use an = sign or not. For example, the 2 following lines are valid. --summary "ran spell checker" '--summary=ran spell checker' Arguments for short options are separated from option by a space. -s 'ran spell checker' short options may be grouped "à la" tar : saptdvhf -qlmi /tmp/log :playground:foo.png \\ file.png mydwpwfile http://doku.me/foo This is the same as : saptdvhg -q -l /tmp/log -= :playground:foo.png \\ file.png -i mydwpwfile http://doku.me/foo If it is necessary to log into dokuwiki, the user and password must be in a one line text file, in user:password format. For example, the file could contain this. alice:S0meP@ssw0rd The file must be mode 600. You can easily and safely create such a file using this incantation : saptdvhf create-ident-file filename example: saptdvh create-identfile mydokuloginpwfile saptdvhf \\ --summary 'automatic update' \\ --page-file /some/file.txt \\ http://some-doku.example.org/start saptdvhf \\ -i mydokuloginpwfile \\ --media :toto:tata.png /some/file.png \\ http://some-doku.example.org/start If you are going to update many pages this way and don't want to "log into" the doku-site for each page, it is possible to log once, then run multiple saptdvhf commands. the -c and -b options are needed for this : First, log in your doku with : saptdvhf \\ -c cookie-file -I \\ http://some-doku.example.org/start or saptdvhf \\ -c cookie-file -i mydokuloginpwfile \\ http://some-doky.example.org/start Then, use the -b option to run multiple saptdvhf commands : saptdvhf -b cookie-file \\ -p /some/file.txt http://some-doku.example.org/start saptdvhf -b cookie-file \\ -p /some/other-file.txt http://some-doku.example.org/another-page saptdvhf -b cookie-file \\ -p /some/file3.txt http://some-doku.example.org/third-page saptdvhf -b cookie-file ... All options are, well, hum, optionnal. Here are the defaults : summary -------- : $summary page-file ------ : $pagefile web-user-agent - : $user_agent if ran without argument then a short help is printed. ! } #Begin Library liblocal ##VERB # local.sh. This file is part of libsh # http://schplurtz.free.fr/schplurtziel/bibsh/ # # libsh. a portable bourn shell library. # Copyright (C) 2004 Christophe Martin # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # I can be reached by email at : Schplurtz@free.fr # altough I don't read this mail box very often. # #---------------------------------------------------------------------------- # # local.sh. test if sh supports ``typeset'' or ``local'' ##EOVERB detectlocal() { typeset=typeset exec 4>&2 exec 2>/dev/null typeset foo || typeset=local exec 2>&4 exec 4>&- } detectlocal setv() { eval "$1=\$2" } setp() { eval "$1=\${$2}" } printp() { eval "printf '%s' \"\${$1}\"" } printpn() { eval "printf '%s\\n' \"\${$1}\"" } getp() { eval "printf '%s' \"\${$1}\"" } LOpxxx="$0" LOpxxx=${LOpxxx##*/} tmpfilename() { if test '' = "$1" then $typeset mask mask=`umask` umask 077 LOtmpdir="${TEMP:-/tmp}/${LOpxxx}.$$.$RANDOM" mkdir "$LOtmpdir" >/dev/null || { return 1 } umask $mask echo 0 >"$LOtmpdir"/.id return fi $typeset LOtempfilename_id LOtempfilename_id=$(<"$LOtmpdir"/.id) LOtempfilename_id=$(( LOtempfilename_id + 1 )) echo $LOtempfilename_id >"$LOtmpdir"/.id setv "$1" "$LOtmpdir/${2:-tmp}-$LOtempfilename_id" } NL=' ' DIFS=" $NL" LOifss= unset LOifss LOifss[0]= newifs() { LOifss[${#LOifss[@]}]=$IFS case "$1" in NL) set -- "$NL" ;; DEF) set -- "$DIFS" ;; TAB) set -- ' ' ;; SP) set -- ' ' ;; PIPE) set -- '|' ;; esac IFS=$1 } oldifs() { IFS=${LOifss[${#LOifss[@]} - 1]} unset LOifss[${#LOifss[@]}-1] } #End Library liblocal #Begin Library libkwubst ##VERB # kwubst.sh. This file is part of libsh # http://schplurtz.free.fr/schplurtziel/bibsh/ # # libsh. a portable bourn shell library. # Copyright (C) 2005 Christophe Martin # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # I can be reached by email at : Schplurtz@free.fr # altough I don't read this mail box very often. # #---------------------------------------------------------------------------- # # kwubst.sh. global replace in strings and string quoting. ##EOVERB function backslashslash { gsubst "$1" / '\/' } function gsubst { $typeset ret='' suff="$1" pref repl="$2" nouv="$3" while : do pref="${suff%%$repl*}" if test ".$pref" = ".$suff" then ret="${ret}$suff" printf '%s' "$ret" return fi suff=${suff#*$repl} ret="$ret$pref$nouv" done } vargsubst() { $typeset GSret='' GSsuff GSpref eval "GSsuff=\${$1}" while : do GSpref="${GSsuff%%$2*}" if test ".$GSpref" = ".$GSsuff" then GSret="${GSret}$GSsuff" eval "$1=\$GSret" return fi GSsuff=${GSsuff#*$2} GSret="$GSret$GSpref$3" done } KWtst=1 KWtst=$(printf %q a 2>/dev/null || : ) if test a = "$KWtst" -o "'a'" = "$KWtst" then function kwote { test $# = 0 && return test '' = "$1" && printf "''" || printf %q "$1" } else function kwote { test $# = 0 && return $typeset suff="$1" vargsubst suff \' \'\\\'\' printf "'%s'" "$suff" } fi unset KWtst function kwotenokwote { test $# = 0 && return $typeset ret ret="$1" vargsubst ret \' \'\\\'\' printf %s "$ret" } varkwote() { test $# = 0 && return vargsubst "$1" \' \'\\\'\' eval "$1=\\'\${$1}\\'" } varkwotenokwote() { test $# = 0 && return vargsubst "$1" \' \'\\\'\' } varbackslashslash() { test $# = 0 && return vargsubst "$1" / '\/' } #End Library libkwubst #Begin Library libmaniptab ##VERB # maniptab.sh. This file is part of libsh. # http://schplurtz.free.fr/schplurtziel/bibsh/ # # libsh. a portable bourn shell library. # Copyright (C) 2005 2018 Christophe Martin # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # I can be reached by email at : Schplurtz@free.fr # altough I don't read this mail box very often. # #---------------------------------------------------------------------------- # # maniptab.sh array manipulation ##EOVERB TAcmlev=0 if ( eval 'TAtoto=( 1 2 )' >/dev/null 2>&1 ) then unset TAtoto function tableau { $typeset TAcma="$1" shift eval "$TAcma=( \"\$@\" )" } else function tableau { $typeset TAcma="$1" shift eval "set -A $TAcma -- \"\$@\"" } fi function tabcopie { eval "tableau $1 \"\${${2}[@]}\"" } function tabajoute { $typeset TAcma="$1" TAcmi TAcml shift eval " TAcml=\${#${TAcma}[@]} for TAcmi do ${TAcma}[\$TAcml]=\"\$TAcmi\" TAcml=\$((TAcml + 1)) done " } function tabdepile { $typeset TAcma="$1" TAcml eval " TAcml=\${#${TAcma}[@]} if test 1 -gt \$TAcml then return 1 fi $2=\${${TAcma}[TAcml-1]} unset ${TAcma}[TAcml-1] " } function tabsommet { $typeset TAcma="$1" TAcml eval " TAcml=\${#${TAcma}[@]} if test \$# = 2 then if test 1 -gt \$TAcml then $2= return 1 fi $2=\${${TAcma}[TAcml-1]} return else test 1 -gt \$TAcml && return 1 printf '%s' \"\${${TAcma}[TAcml-1]}\" fi " } function tabdefile { $typeset ret; eval "${2}=\${${1}[0]} ret=\$(( \${#${1}[@]} == 0 ))" tabdetruit $1 0 1 return $ret } function tabconcatene { $typeset TAcma="$1" TAcmn TAcmt TAcmN TAcmi shift eval "TAcmn=\${#${TAcma}[@]}" for TAcmt do eval " TAcmN=\${#${TAcmt}[@]} TAcmi=0 while test \$TAcmi -lt \$TAcmN do ${TAcma}[\$TAcmn]=\"\${${TAcmt}[\$TAcmi]}\" TAcmi=\$((TAcmi+1)) TAcmn=\$((TAcmn + 1)) done " done } function tabindicede { $typeset TAcmn=0 TAcmi TAindice= eval " for TAcmi in \"\${${1}[@]}\" do if test \".\$TAcmi\" = .\"\$2\" then TAindice=\$TAcmn break fi TAcmn=\$((TAcmn + 1)) done " } function tabcontient { $typeset TAcmn=0 TAcmi TAindice= eval " for TAcmi in \"\${${1}[@]}\" do if test \".\$TAcmi\" = .\"\$2\" then TAindice=\$TAcmn return 0 fi TAcmn=\$((TAcmn + 1)) done return 1 " } function tabtousindicede { $typeset TAcmn=0 TAcmi TAindice= eval " for TAcmi in \"\${${1}[@]}\" do if test \".\$TAcmi\" = .\"\$2\" then TAindice=\"\$TAindice \$TAcmn\" fi TAcmn=\$((TAcmn + 1)) done " test "$TAindice" } function tabaffiche { $typeset TAcmn TAcmi=0 TAcmf eval "TAcmn=\${#${1}[@]}" if test $TAcmn -gt 1000 then TAcmf=%4d elif test $TAcmn -gt 100 then TAcmf=%3d elif test $TAcmn -gt 10 then TAcmf=%2d else TAcmf=%1d fi eval " while test \$TAcmi -lt \$TAcmn do printf \"${1}[$TAcmf] = '%s'\\n\" \$TAcmi \"\${${1}[\$TAcmi]}\" TAcmi=\$((TAcmi + 1)) done " } function tabplace { $typeset TAcma="$1" TAcmn="$2" TAcmi shift 2 2>/dev/null || return eval " if test $TAcmn -gt \${#${TAcma}[@]} then return 1 fi for TAcmi do ${TAcma}[\$TAcmn]=\"\$TAcmi\" TAcmn=\$(( TAcmn + 1)) done " } function tabdetruit { $typeset TAcma="$1" TAcmI="$2" TAcmi TAcmj=0 TAcmn if test tout = "$2" then eval "unset $TAcma" return fi eval "TAcmn=\${#${TAcma}[@]}" TAcmi=$TAcmI eval " while test \$TAcmj -lt $3 -a \$TAcmi -lt $TAcmn do unset ${TAcma}[\$TAcmi] TAcmi=\$((TAcmi + 1)) TAcmj=\$((TAcmj+1)) done while test \$TAcmi -lt $TAcmn do ${TAcma}[\$TAcmI]=\"\${${TAcma}[\$TAcmi]}\" ; unset ${TAcma}[\$TAcmi] TAcmi=\$((TAcmi + 1)) TAcmI=\$((TAcmI + 1)) done " } function tabinsere { $typeset TAcma="$1" shift $typeset TAcmi TAcmn=0 eval "for TAcmi in \"\${${TAcma}[@]}\" do ${TAcma}[$# + TAcmn]=\$TAcmi TAcmn=\$(( TAcmn + 1 )) done " tabplace $TAcma 0 "$@" } function tabintercale { $typeset TAcma="$1" TAcmI="$2" TAcmn TAcmi TAcme shift 2 || return eval " TAcmn=\${#${TAcma}[@]} TAcmi=\$((TAcmn - 1)) if test \$TAcmI = \$TAcmn then tabajoute ${TAcma} \"\$@\" return elif test \$TAcmI -gt \$TAcmn then return 1 fi TAcmi=\$((TAcmn - 1 + $#)) TAcme=\$((TAcmn - TAcmI)) while test \$TAcme -gt 0 do ${TAcma}[\$TAcmi]=\"\${${TAcma}[\$((TAcmi-$#))]}\" TAcmi=\$((TAcmi - 1)) TAcme=\$((TAcme - 1)) done for TAcme do ${TAcma}[\$TAcmI]=\"\$TAcme\" TAcmI=\$((TAcmI + 1)) done " } function tabmix { $typeset TAcmf=$1 $typeset TAcmI=0 TAcmi $typeset TAcmD TAcmF TAcmn shift for TAcmi do TAcmD="$TAcmD ${TAcmf}[\$TAcmI]=\"\${${TAcmi}[\$TAcmi]}\" TAcmI=\$((TAcmI + 1))" done eval 'TAcmi=0 while test $TAcmi -lt ${#'"$1"'[@]} do '"$TAcmD"' TAcmi=$((TAcmi + 1)) done TAcmn=$((${#'$TAcmf'[@]} -1 )) while test $TAcmn -ge $TAcmI do unset '$TAcmf'[$TAcmn] TAcmn=$((TAcmn - 1)) done ' } function tabsep { $typeset TAcmf=$1 $typeset TAcmI=0 TAcmi=0 TAcmn TAcmD TAcmF shift for TAcmi do TAcmD="$TAcmD ${TAcmi}[\$TAcmI]=\"\${${TAcmf}[\$TAcmi]}\" TAcmi=\$((TAcmi + 1))" TAcmF="$TAcmF tabdetruit ${TAcmi} \$TAcmI \${#${TAcmi}[@]}" done eval 'TAcmi=0 while test $TAcmi -lt ${#'${TAcmf}'[@]} do '"$TAcmD"' TAcmI=$((TAcmI + 1)) done '"$TAcmF" } function tabforeach { TAcmlev=$(( TAcmlev + 1 )) $typeset TAcmi$TAcmlev TAcmn$TAcmlev TAcmincr$TAcmlev $typeset TAcmi TAcmn TAcmincr if test $# = 2 then TAcmincr=1 TAcmi=-$TAcmincr TAcmn="$(gsubst "$2" TAB "$1")" elif test $# = 3 then TAcmincr=1 TAcmi=$(( $2 - $TAcmincr )) TAcmn="$(gsubst "$3" TAB "$1")" else TAcmincr=$3 TAcmi=$(( $2 - $TAcmincr )) TAcmn="$(gsubst "$4" TAB "$1")" fi eval " TAcmincr$TAcmlev=$TAcmincr TAcmi$TAcmlev=$TAcmi TAcmn$TAcmlev=\$(( \${#${1}[@]} - 1 )) while test \$TAcmi$TAcmlev -lt \$TAcmn$TAcmlev do TAcmi$TAcmlev=\$(( TAcmi$TAcmlev + TAcmincr$TAcmlev )) set zapme \$TAcmi$TAcmlev \"\${$1[TAcmi$TAcmlev]}\" shift $TAcmn done " TAcmlev=$(( TAcmlev - 1 )) } tabboucle() { $typeset TAcmmax eval TAcmmax=\${#${1}[@]} case $# in 3) TAcmmax=$(( TAcmmax - 1 )) eval "$2=-1 ; while test \$$2 -lt $TAcmmax ; do $2=\$(( $2 + 1)) ; $3 done" ;; 4) TAcmmax=$(( TAcmmax - 1 )) eval "$2=\$(( $3 - 1 )); while test \$$2 -lt $TAcmmax ; do $2=\$(( $2 + 1)) ; $4 done" ;; 5) TAcmmax=$(( TAcmmax - $4 )) eval "$2=\$(( $3 - $4 )) ; while test \$$2 -lt $TAcmmax ; do $2=\$(( $2 + $4 )) ; $5 done" ;; esac } function tabpourn { $typeset TAcmi TAcmn TAcmb TAcmmin="$2" TAcmmax="$3" TAcmt="$1" TAcmstep if test 4 = $# then TAcmstep=1 TAcmc=$4 else TAcmstep=$4 TAcmc=$5 fi TAcmi=0 TAcmc="$(gsubst "$TAcmc" TAB "$1")" [ .- = ."$TAcmmin" ] && TAcmmin=0 [ .- = ."$TAcmmax" ] && eval "TAcmmax=\$(( \${#${1}[@]} - 1))" TAcmmax=$(( TAcmmax - TAcmstep )) TAcmn=$(( TAcmmin - TAcmstep)) eval " while test \$TAcmn -le $TAcmmax do TAcmn=\$(( TAcmn + TAcmstep )) set zapme \$TAcmn \"\${${TAcmt}[TAcmn]}\" shift $TAcmc done " } function tabsauve { $typeset TAcma TAcma=\\' ' echo "tableau ${2:-$1} \\" tabforeach "$1" 'printf " %s%s" "$(kwote "$2")" "$TAcma"' echo } function tablong { eval "echo \${#${1}[@]}" } #End Library libmaniptab #Begin Library liboption ##VERB # option.sh. This file is part of libsh # http://schplurtz.free.fr/schplurtziel/bibsh/ # # libsh. a portable bourn shell library. # Copyright (C) 2004 2018 Christophe Martin # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # I can be reached by email at : Schplurtz@free.fr # altough I don't read this mail box very often. # #---------------------------------------------------------------------------- # # option.sh. a shell only way of getting short, long, multiparametered options ##EOVERB OClevel=1 function OCchange_option { if test $(( ( $# / 3 ) * 3 )) != $# then printf "change_option needs a number of args multiple of 3\n" >&2 exit fi tabdetruit OCcourt tout || : tabdetruit OClong tout || : tabdetruit OCbesoin tout || : OCanum= $typeset t1 t2 while test $# -gt 0 do if test num = "$1" then OCanum=${#OCcourt[@]} tabajoute OCbesoin 1 else case "$3" in '+'*) tabajoute OCbesoin "${3#?}" ;; '?') tabajoute OCbesoin '0 1' ;; *-*) t1=${3%%-*} t2=${3##*-} tabajoute OCbesoin "$t1 $t2" ;; [0-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) tabajoute OCbesoin "$3" ;; esac fi tabajoute OCcourt "$1" tabajoute OClong "$2" shift 3 done } function OCaccepte_option { OCchange_option "$@" OCraz } function OCaccepte_option_t { eval "OCaccepte_option \"\${${1}[@]}\"" } function OCraz { OCsuff= OCcurs=1 OCargsused=0 OCmsg_err= } function OCgetopt { $typeset OCvaropt="$1" shift setv $OCvaropt '' tabdetruit OCarg tout if test '' = "$OCsuff" then OCargsused=$(( OCcurs - 1 )) if test $# -lt $OCcurs then return 1 fi eval "OCsuff=\"\${${OCcurs}}\"" case "$OCsuff" in --) OCargsused=$OCcurs OCcurs=$(( OCcurs + 1 )) return 1 ;; -) OCargsused=$(( OCcurs - 1 )) return 1 ;; --*) OCsuff= OCoption_longue "$@" return $? ;; -*) OCcurs=$(( OCcurs + 1 )) OCsuff=${OCsuff#-} ;; *) OCargsused=$(( OCcurs - 1 )) return 1 ;; esac fi OCoption_courte "$@" } function OCoption_courte { $typeset OClopt OClpreml OCltrouve OClbesoin OClbesoinmax OClopt=${OCsuff%${OCsuff#?}} OCsuff=${OCsuff#?} if test $OCanum then case "$OClopt" in [0-9]) OCltrouve=$OCanum OClpreml=${OCsuff%${OCsuff#?}} while : ; do case "$OClpreml" in [0-9]) OClopt=$OClopt$OClpreml OCsuff=${OCsuff#?} OClpreml=${OCsuff%${OCsuff#?}} ;; *) OCarg=$OClopt setv $OCvaropt num return 0 ;; esac done ;; esac fi if ! test $OCltrouve then tabforeach OCcourt \ 'eval "case \"\$OClopt\" in $2) OCltrouve=$1 ; break ;; esac"' if ! test $OCltrouve then OCmsg_err="$OClopt : no such option" return 0 fi fi OClbesoin=${OCbesoin[$OCltrouve]} case "$OClbesoin" in 0) : ;; [0-9]*' '[0-9]*) OClbesoinmax=${OClbesoin##* } OClbesoin=${OClbesoin%% *} OCmange_n_arg "$@" || return 0 ;; [1-9]*) if test $(( OCcurs + OClbesoin - 1 )) -gt $# then OCmsg_err="option $OClopt needs $OClbesoin parameters" return 0 fi while test "$OClbesoin" -gt 0 do OClbesoin=$(( OClbesoin - 1 )) eval "tabajoute OCarg \"\${$OCcurs}\"" OCcurs=$(( OCcurs + 1 )) done ;; *) OCmangearg "$@" || return 0 ;; esac setv $OCvaropt "$OClopt" } function OCmangearg { while : do if test $OCcurs -gt $# then OCmsg_err="Delimitator --|$OClbesoin|-- for option $OClopt not found" tabdetruit OCarg tout return 1 fi eval " if test \".\${$OCcurs}\" = \".\$OClbesoin\" then OCcurs=\$(( OCcurs + 1 )) break fi eval \"tabajoute OCarg \\\"\\\${\${OCcurs}}\\\"\" OCcurs=\$(( OCcurs + 1 )) " done } function OCmange_n_arg { $typeset minn maxn i minn=${OClbesoin} maxn=${OClbesoinmax} if test $(( OCcurs - 1 + minn )) -gt $# then OCmsg_err="Option $OClopt needs at least $minn arg, got only $(( $# - OCcurs + 1))" tabdetruit OCarg tout return 1 fi i=0 while test $i -lt $minn do eval "tabajoute OCarg \"\${$OCcurs}\"" OCcurs=$(( OCcurs + 1 )) i=$(( i + 1 )) done while test $OCcurs -le $# do i=$(( i + 1 )) if test $i -gt $maxn then return 0 fi eval " case \"\${$OCcurs}\" in -) tabajoute OCarg \"\${$OCcurs}\" OCcurs=\$(( OCcurs + 1 )) ;; -*) break ;; *) tabajoute OCarg \"\${$OCcurs}\" OCcurs=\$(( OCcurs + 1 )) ;; esac " done } function OCoption_longue { $typeset OCloptorg OClopt OClnpremval OClbesoin OClaifs OClpossible OClexact OCli OClbesoinmax OClnpremval=0 eval "OClopt=\"\${$OCcurs}\"" OCcurs=$(( OCcurs + 1 )) OCloptorg=$OClopt OClopt=${OClopt#--} case "$OClopt" in *=*) OClnpremval=1 tableau OCarg "${OClopt#*=}" OClopt=${OClopt%%=*} ;; esac case "$OClopt" in ?) OCmsg_err="\"--$OClopt\" : option longue abregee a une seule lettre" return ;; esac tabforeach OClong ' OClaifs=$IFS IFS=\| for OCli in $2 do IFS=$OClaifs eval " case \"\$OCli\" in \"$OClopt\"*) OClpossible=\"\$OClpossible\${OClpossible:+ }\$1\" break ;; esac " done IFS=$OClaifs ' case "$OClpossible" in '') OCmsg_err="no such option as ${OCloptorg%%=*}" return 0 ;; *\ *) tabforeach OClong ' OClaifs=$IFS IFS=\| for OCli in $2 do IFS=$OClaifs if test ."$OCli" = ."$OClopt" then OClexact=$1 break fi done IFS=$OClaifs ' if test $OClexact then OClpossible=$OClexact else OCmsg_err="${OCloptorg%%=*} : ambiguite entre les options" for OCli in $OClpossible do OCmsg_err="$OCmsg_err --${OClong[$OCli]}" done return 0 fi ;; esac OClbesoin=${OCbesoin[$OClpossible]} case "$OClbesoin" in 0) : ;; [0-9]*' '[0-9]*) OClbesoinmax=${OClbesoin##* } OClbesoin=${OClbesoin%% *} OClbesoin=$(( OClbesoin - OClnpremval )) OClbesoinmax=$(( OClbesoinmax - OClnpremval )) OCmange_n_arg "$@" || return 0 ;; [1-9]*) OClbesoin=$(( OClbesoin - OClnpremval )) if test $(( OCcurs + OClbesoin - 1 )) -gt $# then OCmsg_err="option ${OCloptorg%%=*} needs $OClbesoin parameters" return 0 fi while test "$OClbesoin" -gt 0 do OClbesoin=$(( OClbesoin - 1 )) eval "tabajoute OCarg \"\${$OCcurs}\"" OCcurs=$(( OCcurs + 1 )) done ;; *) OCmangearg "$@" || return 0 ;; esac OCli=${OCcourt[$OClpossible]} if test num = "$OCli" then OClexact=$OCarg OCli=${OClexact%${OClexact#?}} while : ; do case "$OCli" in [0123456789]) OClexact=${OClexact#?} OCli=${OClexact%${OClexact#?}} ;; '') setv $OCvaropt num break ;; *) OCmsg_err="option ${OCloptorg%%=*} requires an integer argument. $OCarg found" break ;; esac done return fi while test ${#OCli} -gt 1 do OCli=${OCli#?} done setv $OCvaropt "$OCli" } #End Library liboption #Begin Library libsystem ##VERB # system.sh. This file is part of libsh # http://schplurtz.free.fr/schplurtziel/bibsh/ # # libsh. a portable bourn shell library. # Copyright (C) 2004 2018 Christophe Martin # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # # I can be reached by email at : Schplurtz@free.fr # altough I don't read this mail box very often. # #---------------------------------------------------------------------------- # # system.sh various sh system library function ##EOVERB function filemode { $typeset rights urights grights orights svrights $typeset uvrights gvrights ovrights eval "set zapme $(ls -ld "$1")" rights=${2#?} rights=${rights%${rights#?????????}} urights=${rights%??????} orights=${rights#??????} grights=${rights%???} grights=${grights#???} svrights=0 uvrights=0 gvrights=0 ovrights=0 case $urights in r*) uvrights=4;; esac case $urights in *w*) uvrights=$(( uvrights + 2 ));; esac case $urights in *x) uvrights=$(( uvrights + 1 ));; *s) uvrights=$(( uvrights + 1 )) svrights=4 ;; *S) svrights=4 ;; esac case $grights in r*) gvrights=4;; esac case $grights in *w*) gvrights=$(( gvrights + 2 ));; esac case $grights in *x) gvrights=$(( gvrights + 1 )) ;; *s) gvrights=$(( gvrights + 1 )) svrights=$(( svrights + 2 )) ;; *S) svrights=$(( svrights + 2 )) ;; *l|*L) svrights=$(( svrights + 2 )) ;; esac case $orights in r*) ovrights=4;; esac case $orights in *w*) ovrights=$(( ovrights + 2 ));; esac case $orights in *x) ovrights=$(( ovrights + 1 )) ;; *t) ovrights=$(( ovrights + 1 )) svrights=$(( svrights + 1 )) ;; *T) svrights=$(( svrights + 1 )) ;; esac echo ${svrights}${uvrights}${gvrights}${ovrights} } function fileowner { eval "set -- $(ls -ld "$1")" echo "${3}:$4" } function get_directory_dev { ( cd "$1" >/dev/null 2>&1 || : basta ) $typeset df aifs="$IFS" dev df="$(df -k "$1" 2>/dev/null)" IFS=' ' set -- $df IFS="$aifs" dev="${2%% *}" if test '' = "$dev" then return 1 fi echo "$dev" } function get_directory_mountpoint { $typeset df er aifs="$IFS" df="$(df -k "$1" 2>/dev/null)" er=$? test $er = 0 || return $er IFS=' ' set -- $df IFS="$aifs" eval 'df="$'"$#"'"' df=${df#*\%} while : do case "$df" in " "*) df="${df#* }" ;; *) break esac done printf "%s" "$df" } #End Library libsystem warning() { dolog "$@" ; echo "$@" >&2; } fatal() { warning "$@"; exit 1; } __DATA__() { exit 0; } data() { sed -e 1,/^__DATA__$/d "$0" ; } versionshort() { echo "saptdvhf-version $version" echo "Copyright (C) 2012 2013 2015 2022 Schplurtz le déboulonné " } version() { versionshort echo "This is free software; run 'saptdvhf -C' for exact licence terms. There is NO" echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." } formatlog() { echo -- "$@" } dolog() { test "$logfile" || { oldlogexist=1 tabajoute log "$(formatlog "$@")" return 0 } { _flushlog formatlog "$@" } >>"$logfile" } _flushlog() { test $oldlogexist || return 0 oldlogexist= printf '%s\n' "${log[@]}" } logallargs() { local i j s dolog "received ${#args[@]} args" j=1 for i in "${args[@]}" ; do s="$(printf '%4d ==>%s<==\n' $j "$i")" dolog "$s" j=$(( $j + 1 )) done } flattenreponse() { awk -v d="$1.$$" '{ gsub( />/, ">\n" ) ; print >d }' "$1" && cat "$1.$$" >"$1" || : rm -f "$1.$$" } makeawkscript() { awkscript='/form.*'"$1"'/ { ok=1 next } ok && /<\/form/ { ok=0 exit next } ! (ok && /u close(u) print substr( $0, pos+1, length()-pos) >p close(p) } ' "$1" set -- "$(<$_dwl_u)" "$(<$_dwl_p)" rm -f "$_dwl_u" "$_dwl_p" unset _dwl_u _dwl_p fi case "$1" in (@*) set -- "$(<${_dwl_u#@})" "$(<${_dwl_p#@})" ;; esac local _dwl_u _dwl_p _dwl_reponse tmpfilename _dwl_u tmpfilename _dwl_p tmpfilename _dwl_reponse printf %s "$1" >"$_dwl_u" printf %s "$2" >"$_dwl_p" dolog dwlogin user and password are in secret files dolog dwlogin sending login request "$_dwl_url" Curl -i -L -j -d do=login -o "$_dwl_reponse" "$_dwl_url" || fatal "dwlogin curl failed" test -r "$_dwl_reponse" || fatal "No response from web server" dolog "dwlogin got answer from server" grep -q dw__login "$_dwl_reponse" 2>/dev/null || { fatal "Can't log into DokuWiki. page does not contain « dw__login » form" } test "$_dwl_responsevar" && { tmpfilename _dwl_responsefile cp "$_dwl_reponse" "$_dwl_responsefile" setv "$_dwl_responsevar" "$_dwl_responsefile" } dolog dwlogin server answered with user:password form flattenreponse "$_dwl_reponse" local _dwl_options=$( awk -v addmoinsd=1 "$_dwl_awkscr" "$_dwl_reponse" ) dolog dwlogin sending user and password to "$_dwl_url" >"$_dwl_reponse" Curl -i -o "$_dwl_reponse" $_dwl_options --data-urlencode "u@$_dwl_u" --data-urlencode "p@$_dwl_p" "$_dwl_url" rm -f "$_dwl_u" "$_dwl_p" grep -q ^Location: "$_dwl_reponse" local _dwl_retval=$? dolog "dwlogin got 'Location:' header (0 is ok) ? = $_dwl_retval" rm -f "$_dwl_reponse" umask $_dwl_umask return $_dwl_retval } create_ident_file() { dolog 'create_ident_file Creating identification file' local f u i l p set -e f=$1 test "$f" || { help exit } test -e $f && fatal "file $f already exists. exiting" u=$(umask) umask 077 case $(umask) in (077|0077) : ;; (*) fatal "unable to set umask to 077. Exiting" ;; esac > "$f" || fatal "Cannot write to $f" tty >/dev/null 2>&1 printf '%s ' dokuwiki login read l printf '%s ' dokuwiki password stty -echo read p printf '%s:%s\n' "$l" "$p" >"$f" || { stty echo fatal "Cannot overwrite empty file $f" } p= l= umask $u stty echo echo dolog 'create_ident_file done' } Curl() { dolog Curl "$@" dolog Curl calling « Curl_base -s -S "$@" » Curl_base -s -S "$@" } Curl_base() { dolog Curl_base "$@" dolog Curl_base running « $curl -A "$user_agent" -b "$cookiefile" -c "$cookiefile" "$@" » $curl -A "$user_agent" -b "$cookiefile" -c "$cookiefile" "$@" } do_page() { dolog do_page "$@" dolog requesting edit of page "$pageurl" Curl -d rev= -d do=edit "${pageurl}" >$reponse || fatal "Cannot request edit for page $pageurl. curl failed" dolog got response from server flattenreponse $reponse set -- $( awk ' // { ok=1 next } ok { x=$0 sub( /<\/span>.*/, "", x ) printf "--data-urlencode plug" "in__cap" "tcha=%s\n", x ok=0 } ' "$reponse" ) dolog "cap""tcha : $@" grep -q dw__editform "$reponse" || fatal "Can't edit page, there is no « dw__editform » form." makeawkscript 'id="dw__editform"' set -- $( awk -v addmoinsd=1 "$awkscript" $reponse ) "$@" -d 'do%5Bsave%5D=Zorglub' --data-urlencode "summary=$summary" --data-urlencode "wikitext@$pagefile" dolog "curl command built : $@" rm -f "$reponse" Curl_base "$@" "${pageurl}" test 0 = $? -a "$get_page_after_sending" && { dolog "getting the page after edit" Curl "${pageurl}" >/dev/null || warn "Cannot re-get page $pageurl after successful edition. curl failed" dolog "done getting the page after edit" } } do_data() { local ns id id=${mediaid##*:} ns=${mediaid%:$id} flattenreponse "$jsphpfile" local mediamanagerurl= baseurl= totalurl= mediareponse= mediamanagerurl=$(sed -ne '\@