xmpp/captcha/captcha.sh

131 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# This script is an example captcha script.
# It takes the text to recognize in the captcha image as a parameter.
# It return the image binary as a result. ejabberd support PNG, JPEG and GIF.
# The whole idea of the captcha script is to let server admins adapt it to
# their own needs. The goal is to be able to make the captcha generation as
# unique as possible, to make the captcha challenge difficult to bypass by
# a bot.
# Server admins are thus supposed to write and use their own captcha generators.
# This script relies on ImageMagick.
# It is NOT compliant with ImageMagick forks like GraphicsMagick.
INPUT=$1
TRANSFORMATIONS=(INTRUS SOMME)
DIGIT=(zéros uns deux trois quatres cinqs six septs huits neufs dix)
if test -n ${BASH_VERSION:-''} ; then
get_random ()
{
R=$RANDOM
}
else
for n in `od -A n -t u2 -N 64 /dev/urandom`; do RL="$RL$n "; done
get_random ()
{
R=${RL%% *}
RL=${RL#* }
}
fi
INTRUS()
{
LENGTH=${#INPUT}
NUMBERS=$(echo $INPUT | grep -o . | tr '\n' ' ')
SORTED_UNIQ_NUM=$(echo "${NUMBERS[@]}" | sort -u | tr '\n' ' ')
RANDOM_DIGITS=$(echo 123456789 | grep -o . | sort -R | tr '\n' ' ')
INTRUS=-1
for i in ${RANDOM_DIGITS[@]}
do
if [[ ! " ${SORTED_UNIQ_NUM[@]} " =~ " ${i} " ]]; then
INTRUS=$i
break
fi
done
# Worst case
if [[ $INTRUS -eq "-1" ]]
then
echo Tapez "$INPUT \n sans changement"
return
fi
for num in ${NUMBERS[@]}
do
get_random
R=$(($R % 100))
if [[ $R -lt 60 ]]; then
NEWINPUT=${NEWINPUT}${num}${INTRUS}
else
NEWINPUT=${NEWINPUT}${num}
fi
done
get_random
R=$(($R % 100))
if [[ $R -lt 50 ]]; then
echo "Tapez $NEWINPUT en\n supprimant les ${DIGIT[$INTRUS]}"
else
echo "Saisissez $NEWINPUT en\n enlevant les ${DIGIT[$INTRUS]}"
fi
}
SOMME()
{
get_random
RA=$(($R % 100))
if [[ $(($INPUT % 2)) -eq 0 ]]; then
A=$(($INPUT - $RA))
B=$RA
else
B=$(($INPUT - $RA))
A=$RA
fi
get_random
R=$(($R % 100))
if [[ $R -lt 25 ]]; then
echo "Tapez le résultat\n de $A + $B"
elif [[ $R -lt 50 ]]; then
echo "Sommez\n $A et $B"
elif [[ $R -lt 75 ]]; then
echo "Additionnez\n $A et $B"
else
echo "Saisissez le résultat\n de $A + $B"
fi
}
get_random
RAND_ITALIC=$(($R % 25))
get_random
RAND_ANGLE=$(($R % 3))
get_random
RAND_INDEX=$(($R % ${#TRANSFORMATIONS[@]}))
convert -size 300x60 xc:none -pointsize 20 \
\( -clone 0 -fill black \
-stroke black -strokewidth 1 \
-annotate "${RAND_ANGLE}x${RAND_ITALIC}+0+0" "\n $(${TRANSFORMATIONS[$RAND_INDEX]})" \
-roll +$ROLL_X+0 \
-wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \
-roll -$ROLL_X+0 \) \
-flatten -crop 300x60 +repage -quality 500 -depth 11 png:"/var/lib/ejabberd/chapril_captchas/$(date "+%Y-%m-%d-%H%M%S")_${INPUT}.png"
convert -size 300x60 xc:none -pointsize 20 \
\( -clone 0 -fill black \
-stroke black -strokewidth 1 \
-annotate "${RAND_ANGLE}x${RAND_ITALIC}+0+0" "\n $(${TRANSFORMATIONS[$RAND_INDEX]})" \
-roll +$ROLL_X+0 \
-wave "$WAVE1_AMPLITUDE"x"$WAVE1_LENGTH" \
-roll -$ROLL_X+0 \) \
-flatten -crop 300x60 +repage -quality 500 -depth 11 png:-