

[TO_PRI_2]
exten => 6815876,1,Gosub(Sub_Fax_Reception,start,1(Jack White,${GET_Email_FAX_Destinataire(${EXTEN})},Sales_Departement))

[Sub_Fax_Reception]

exten => start,1,NoOp()
same => n,Answer()
same  => n,Set(CDR(garneau_dst)=${ARG1})
same  => n,Set(CDR(garneau_userfield)=${CALLERID(all)})
same => n,Set(Bad_CID=${READ_Blacklist(${CALLERID(num)})})
same => n,GotoIf($["${Bad_CID}"  >= "1"]?SPAM_Number:Valid_Number)

same => n(Valid_Number),Set(Destinataire=${ARG1})
same => n,Set(Email_Destinataire=${ARG2})
same => n,Set(Departement_Destinataire=${ARG3})
same => n,Set(FAXDEST=/var/spool/asterisk/fax)
same => n,Set(tempfax=${STRFTIME(,,%C%y%m%d%H%M)}-${Departement_Destinataire})

;Hangup handler
same => n,Set(CHANNEL(hangup_handler_push)=hdlr_Process_Fax,s,1(args))
same => n,ReceiveFAX(${FAXDEST}/${tempfax}.tif,fd)
same => n,Hangup()

;Blacklisted
same  => n(SPAM_Number),NoOp()
same  => n,Zapateller(answer)
same  => n,Hangup()

[hdlr_Process_Fax]

exten => s,1,NoOp(Email du FAX en cours)
same => n,Set(WRITE_FAX-ENTRANT()='${CALLERID(All)}','${Email_Destinataire}','${FAXBITRATE}','${FAXPAGES}','${FAXERROR}','${FAXSTATUS}')
same => n,GotoIf($["${FAXSTATUS}"  = "SUCCESS"]?FAX_SUCCESS:FAX_FAILED)
same => n(FAX_SUCCESS),NoOp()
same => n,System(/usr/bin/tiff2pdf /var/spool/asterisk/fax/${tempfax}.tif  >  /var/spool/asterisk/fax/pdf/${Departement_Destinataire}/${tempfax}.pdf)
same => n,System(/var/lib/asterisk/agi-bin/sendfax.pl ${tempfax} "${Destinataire}" ${Email_Destinataire} "${CALLERID(num)}" ${Departement_Destinataire})
same => n(FAX_FAILED),NoOp()
same => n,Return()
