2021-03-04 17:29:32 +00:00
|
|
|
#! /bin/bash
|
|
|
|
|
2021-04-07 16:23:29 +00:00
|
|
|
function usage {
|
2021-03-04 18:44:27 +00:00
|
|
|
echo "Usage:
|
|
|
|
$ ./fax.sh <letter.pdf> <recipient>
|
2021-04-07 16:23:29 +00:00
|
|
|
where <recipient> has to be a german faxline number
|
|
|
|
$ ./fax.sh <letter.pdf> -handle <handle>
|
|
|
|
Asks Letterhead's address database for <handle>'s fax number.
|
|
|
|
$ ./fax.sh -help
|
|
|
|
Print this Message and exit"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
function abort {
|
|
|
|
echo "Letterhead API not available"
|
|
|
|
exit 1
|
|
|
|
}
|
2021-03-04 19:17:30 +00:00
|
|
|
BASEPATH=$(dirname $(realpath $0))
|
2021-04-07 16:23:29 +00:00
|
|
|
. $BASEPATH/.env
|
|
|
|
|
|
|
|
#Parsing Parameters:
|
|
|
|
unset $GETOPT_COMPATIBLE
|
|
|
|
ARGV=$(getopt -n "$0" -a -o "" -l "help,handle:" -- "$@")
|
|
|
|
if [ $? -ne 0 ]
|
|
|
|
then
|
|
|
|
usage
|
|
|
|
fi
|
|
|
|
eval set -- "$ARGV"
|
|
|
|
REC0=
|
|
|
|
#Now we have $@ clean and tidy and begin parsing
|
|
|
|
while :
|
|
|
|
do
|
|
|
|
case "$1" in
|
|
|
|
"--help") usage ;;
|
|
|
|
"--handle")
|
|
|
|
$LETTERHEAD_PATH/Adressen/address.sh -l > /dev/null || abort
|
2021-04-20 20:12:58 +00:00
|
|
|
HAND=$2
|
2021-04-23 11:03:37 +00:00
|
|
|
if [[ -e DECKBLATT.csv && $HAND = "+" ]]
|
2021-04-20 20:12:58 +00:00
|
|
|
then
|
|
|
|
HAND=$(grep + DECKBLATT.csv | cut -d, -f2)
|
|
|
|
fi
|
|
|
|
REC0=$($LETTERHEAD_PATH/Adressen/address.sh -f $HAND 2>/dev/null | sed 's: ::g')
|
2021-04-07 16:23:29 +00:00
|
|
|
shift
|
|
|
|
shift ;;
|
|
|
|
"--")
|
|
|
|
shift
|
|
|
|
break ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
#Check if you have enough parameters
|
|
|
|
if [ -z $REC0 ]
|
|
|
|
then
|
|
|
|
REC0=$2
|
|
|
|
fi
|
|
|
|
if [[ -z $REC0 || -z $1 ]]
|
|
|
|
then
|
|
|
|
usage
|
|
|
|
fi
|
2022-03-22 09:30:29 +00:00
|
|
|
#Check if account works - not necessarily needed anymore, since sipgate changed their auth processes
|
2021-03-04 19:17:30 +00:00
|
|
|
. $BASEPATH/authorization.sh
|
2021-03-04 17:29:32 +00:00
|
|
|
#Prepare fax credentials:
|
|
|
|
FILE=$1
|
2021-03-04 18:44:27 +00:00
|
|
|
echo -n "Sending file ${FILE} "
|
2021-04-07 16:23:29 +00:00
|
|
|
REC=$(echo "$REC0" | sed -e 's:[^+0-9]::g' -e 's:^0\([1-9]\):+49\1:')
|
2021-03-04 18:44:27 +00:00
|
|
|
echo "to $REC"
|
2021-03-17 12:40:13 +00:00
|
|
|
#Checking for file size:
|
2024-09-16 07:41:46 +00:00
|
|
|
if [ ! -e "$FILE" ]
|
2021-04-19 11:55:36 +00:00
|
|
|
then
|
|
|
|
echo "File not found"
|
|
|
|
exit 2
|
|
|
|
fi
|
2021-04-07 16:31:15 +00:00
|
|
|
SIZE=$(stat -c%s "$FILE")
|
2021-03-17 12:40:13 +00:00
|
|
|
if [ $SIZE -gt 10485760 ]
|
|
|
|
then
|
|
|
|
echo "File too big. Cannot be more than 10MB."
|
|
|
|
exit 2
|
|
|
|
fi
|
2021-04-19 10:52:47 +00:00
|
|
|
if hash gs 2>/dev/null
|
|
|
|
then
|
2021-04-19 11:55:36 +00:00
|
|
|
PAGES=$(gs -q -dNODISPLAY -dNOSAFER -c "($FILE) (r) file runpdfbegin pdfpagecount = quit" 2>&1)
|
2021-04-19 11:13:53 +00:00
|
|
|
if [ $PAGES -gt 30 ]
|
2021-04-19 10:52:47 +00:00
|
|
|
then
|
|
|
|
echo "Sipgate won't send your file, because it has more than 30 pages"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "ghostscript not installed, cannot check for page numbers. Sipgate accepts up to 30 pages."
|
|
|
|
fi
|
2021-03-04 17:29:32 +00:00
|
|
|
#Needs to be base64 encoded:
|
2021-04-07 17:09:47 +00:00
|
|
|
CONTENT=$(base64 "$FILE" -w 0)
|
2021-03-04 17:29:32 +00:00
|
|
|
if [ -z "$CONTENT" ];
|
|
|
|
then
|
|
|
|
echo "Encoding failed"
|
2021-03-04 19:41:00 +00:00
|
|
|
exit 2
|
2021-03-04 17:29:32 +00:00
|
|
|
fi
|
2021-03-10 16:11:38 +00:00
|
|
|
#save payload to tempfile to prevent curl from throwing 'argument list too long' error.
|
2021-03-11 09:45:59 +00:00
|
|
|
echo '{"faxlineId":"f0", "recipient":"'${REC}'", "filename":"fax.pdf", "base64Content":"'${CONTENT}'"}' > /tmp/sipgatedata.txt
|
2021-03-04 17:29:32 +00:00
|
|
|
#Send fax
|
2022-03-22 09:30:29 +00:00
|
|
|
REPLY=$(curl --request POST --header 'Content-Type: application/json' --header 'Accept: application/json' --user $SIPUSER:$SIPPWD --data @/tmp/sipgatedata.txt "https://api.sipgate.com/v2/sessions/fax" -s)
|
2021-03-17 12:40:13 +00:00
|
|
|
FAXID=$(echo $REPLY | jq '.sessionId' | sed -e 's:"::g' )
|
|
|
|
echo $REPLY > /tmp/sipgatereply.txt
|
2021-03-10 16:11:38 +00:00
|
|
|
#remove tempfile
|
2021-03-11 09:45:59 +00:00
|
|
|
rm /tmp/sipgatedata.txt
|
2021-03-04 18:44:27 +00:00
|
|
|
if [ -z $FAXID ]
|
|
|
|
then
|
|
|
|
echo "Something went wrong, I didn't receive some fax id back."
|
2021-03-10 16:09:13 +00:00
|
|
|
exit 2
|
2021-03-04 18:44:27 +00:00
|
|
|
else
|
|
|
|
echo "Sipgate took your fax as sessionID $FAXID"
|
2021-03-04 19:17:30 +00:00
|
|
|
echo "$FAXID" >> $BASEPATH/.fax_history
|
2021-03-04 18:44:27 +00:00
|
|
|
fi
|
2021-03-04 17:29:32 +00:00
|
|
|
#Check if fax request was accepted by sipgate
|
2022-03-22 09:30:29 +00:00
|
|
|
curl --request GET --header 'Content-Type: application/json' --header 'Accept: application/json' --user $SIPUSER:$SIPPWD "https://api.sipgate.com/v2/history/$FAXID" -s | jq '.faxStatusType'
|
2021-03-04 17:29:32 +00:00
|
|
|
#And show the account balance
|
2021-03-04 19:17:30 +00:00
|
|
|
. $BASEPATH/getbalance.sh
|