; TENORA FAX APPLICATION - working with FreePBX
; v.0.1 - Apr 2007 - Ioan (Nini) Indreias - Asterisk 1.2 + spandsp + app_fax
; v.0.2 - May 2009 - Ioan (Nini) Indreias - Asterisk 1.4 + Digium FFA
; www.modulo.ro
;

[macro-tenora-sendmail]
exten => s,1,NoOp(*** Sending a mail ***)
exten => s,n,Set(MAIL_TO=${ARG1})
exten => s,n,Set(MAIL_SUBJ=${ARG2})
exten => s,n,Set(MAIL_BODY=${ARG3})
exten => s,n,Set(MAIL_EXTRA=${ARG4})
exten => s,n,System(mime-construct --to ${MAIL_TO} --subject "${MAIL_SUBJ}" --file "${MAIL_BODY}" ${MAIL_EXTRA})

[macro-tenora-faxreceive]
exten => s,1,NoOp(*** FAX receiving application ***)
exten => s,n,Set(FAXOPT(filename)=${FAXFILE}.tif)
exten => s,n,Set(FAXOPT(ecm)=yes)
exten => s,n,Set(FAXOPT(localstationid)=${TENORA_FAX_LOCALID})
exten => s,n,Set(FAXOPT(maxrate)=14400)
exten => s,n,Set(FAXOPT(minrate)=2400)
exten => s,n,receiveFAX(${FAXFILE}.tif,d)

[macro-tenora-faxstatus]
exten => s,1,NoOp(*** Request for FAX status ***)
exten => s,n,Set(FILE=${ARG1})
exten => s,n,System(echo ""                                    >> ${FILE})
exten => s,n,System(echo "               FAX STATUS"          ">> ${FILE})
exten => s,n,System(echo "====================================">> ${FILE})
exten => s,n,System(echo "Ecm:      ${FAXOPT(ecm)}"            >> ${FILE})
exten => s,n,System(echo "File:     ${FAXOPT(filename)}"       >> ${FILE})
exten => s,n,System(echo "Header:   ${FAXOPT(headerinfo)}"     >> ${FILE})
exten => s,n,System(echo "LocalID:  ${FAXOPT(localstationid)}" >> ${FILE})
exten => s,n,System(echo "MaxRate:  ${FAXOPT(maxrate)}"        >> ${FILE})
exten => s,n,System(echo "MinRate:  ${FAXOPT(minrate)}"        >> ${FILE})
exten => s,n,System(echo "Pages:    ${FAXOPT(pages)}"          >> ${FILE})
exten => s,n,System(echo "Rate:     ${FAXOPT(rate)}"           >> ${FILE})
exten => s,n,System(echo "RemoteID: ${FAXOPT(remotestationid)}">> ${FILE})
exten => s,n,System(echo "Res:      ${FAXOPT(resolution)}"     >> ${FILE})
exten => s,n,System(echo "Status:   ${FAXOPT(status)}"         >> ${FILE})
exten => s,n,System(echo "Reason:   ${FAXOPT(statusstr)}"      >> ${FILE})
exten => s,n,System(echo "Error:    ${FAXOPT(error)}"          >> ${FILE})
exten => s,n,System(echo "====================================">> ${FILE})
exten => s,n,System(echo ""                                    >> ${FILE})

[custom-fax]
exten => s,1,NoOp(*** Incoming FAX ***)
exten => s,n,Set(FAXFILE=${TENORA_FAX_INCOMING}/${UNIQUEID})
exten => s,n,Set(fax_option="0")

;Caller ID detection
exten => s,n,Wait(2)
exten => s,n,Macro(user-callerid,SKIPTTL)
exten => s,n,BackTicks(ANAME|${TENORA_TOOLS}/clid2name ${CALLERID(num)} ${CALLERID(name)})

exten => s,n,Answer
exten => s,n,Wait(3)
exten => s,n,Playback(press-1)
exten => s,n,Read(fax_option,to-reach-operator,1,,1,3)
exten => s,n,GotoIf($["${fax_option:0:1}" = "1"]?to_operator)
exten => s,n,Goto(fax,1)
exten => s,n(to_operator),NoOp(INFO: Transfer to operator)
exten => s,n,Goto(from-pstn,o,1)
exten => s,n,Hangup

exten => f,1,Goto(fax,1)

exten => fax,1,NoOp(INFO: Fax attempt from ${ANAME})
;FOP hint start
exten => fax,n,UserEvent(Newexten|Channel: RXFAX/RXFAX-${UNIQUEID}^State: Up^Uniqueid: 1234)
;FOP hint end
exten => fax,n,Macro(tenora-faxreceive)
exten => fax,n,Hangup

exten => t,1,Goto(fax,1)

exten => h,1,NoOp(INFO: Hangup event)
;FOP hint start
exten => h,n,UserEvent(Hangup|Channel: RXFAX/RXFAX-${UNIQUEID}^State: Down^Uniqueid: 1234)
;FOP hint end
exten => h,n,System(tiff2pdf -o ${FAXFILE}.pdf ${FAXFILE}.tif)

exten => h,n,BackTicks(FAXRESULT|${TENORA_TOOLS}/fax_tools --status ${FAXFILE}.pdf)

exten => h,n,NoOp('Fax result is ${FAXRESULT}')
exten => h,n,GotoIf($["${FAXRESULT}" = "OK"]?receivedOK)
exten => h,n,GotoIf($["${ANAME:-3}" = "(-)"]?:err)

exten => h,n,NoOp(INFO: Create a usefull query)
exten => h,n,BackTicks(BODY_QUERY|${TENORA_TOOLS}/clid_query ${CALLERID(num)})
exten => h,n,System(echo ${BODY_QUERY} >> ${FAXFILE}.msg)
exten => h,n,Goto(err)

exten => h,n(last),NoOp(INFO: Last step - cleaning old stuff"
exten => h,n,System(${TENORA_TOOLS}/fax_tools --clean ${TENORA_FAX_INCOMING})
exten => h,n,Hangup

exten => h,n(sendmail),NoOp(INFO: Send status by email)
exten => h,n,Macro(tenora-faxstatus,${FAXFILE}.msg)
exten => h,n,Macro(tenora-sendmail,${TENORA_FAX_EMAIL},${SUBJ},${FAXFILE}.msg,${EXTRA})
exten => h,n,Goto(last)

exten => h,n(receivedOK),NoOp(INFO: Fax received OK)
exten => h,n,Set(SUBJ=Fax from ${ANAME})
exten => h,n,Set(EXTRA=--attachment ${CALLERID(NUM)}.pdf --type application/pdf --file ${FAXFILE}.pdf)
exten => h,n,Goto(sendmail)

exten => h,n(err),NoOp(INFO: Fax error)
exten => h,n,Set(SUBJ=Fax failed from ${ANAME})
exten => h,n,Set(EXTRA=)
exten => h,n,Goto(sendmail)

[custom-outfax]
exten => default,1,NoOp(INFO: Using default Line)
exten => default,n,Goto(fax1,1)

exten => sip,1,NoOp(INFO: Using sip channel)
exten => sip,n,Goto(fax1,2)

exten => local,1,NoOp(INFO: Using local channel)
exten => local,n,Goto(fax1,2)

exten => fax1,1,NoOp(INFO: Fax line 1 configuration)
exten => fax1,n,Set(LOCALSTATIONID=${TENORA_FAX_LOCALID})
exten => fax1,n,Set(LOCALHEADERINFO=${TENORA_FAX_HEADER})
exten => fax1,n,Goto(out_fax,1)

exten => fax2,1,NoOp(INFO: Fax line 2 configuration)
exten => fax2,n,Set(LOCALSTATIONID=+40316204256)
exten => fax2,n,Set(LOCALHEADERINFO=Modulo Consulting - The future is not it was to be)
exten => fax2,n,Goto(out_fax,1)

exten => out_fax,1,NoOp(*** Outgoing FAX ***)

exten => out_fax,n,Answer
exten => out_fax,n,Wait(1)

exten => out_fax,n,Set(FAXFILE=${FAX_DIR}/${FAX_FILE})
exten => out_fax,n,Set(FAX_MSG=${FAX_DIR}/${FAX_FILE}.${UNIQUEID}.msg)
exten => out_fax,n,Set(FAXOPT(filename)=${FAXFILE}.tif)
exten => out_fax,n,Set(FAXOPT(ecm)=yes)
exten => out_fax,n,Set(FAXOPT(headerinfo)=${LOCALHEADERINFO})
exten => out_fax,n,Set(FAXOPT(localstationid)=${LOCALSTATIONID})
exten => out_fax,n,Set(FAXOPT(maxrate)=14400)
exten => out_fax,n,Set(FAXOPT(minrate)=2400)
exten => out_fax,n,SendFAX(${FAXFILE}.tif,d)
exten => out_fax,n,Hangup

exten => failed,1,NoOp(INFO: Outgoing leg failed)
exten => failed,n,BackTicks(CAUSE|${TENORA_TOOLS}/fax_tools --failed ${REASON})
exten => failed,n,Set(FAX_MSG=${FAX_DIR}/${FAX_FILE}.${UNIQUEID}.msg)
exten => failed,n,Set(SKIP_FAXSTATUS="YES")
exten => failed,n,Hangup

exten => h,1,NoOp(INFO: Hangup event)
exten => h,n,Gotoif($["${FAXOPT(status)}" != "SUCCESS"]?err)
exten => h,n,Set(SENT=sent)
exten => h,n,Set(LINE2=Received STATION_ID=${FAXOPT(remotestationid)}
exten => h,n,Goto(sendmail)

exten => h,n(err),NoOp(INFO: Outgoing fax failed)
exten => h,n,Set(SENT=failed to be sent)
exten => h,n,Set(EXTRA=--attachment \'${FAX_FILE}\' --type application/tiff --file \'${FAX_DIR}/${FAX_FILE}\')
exten => h,n,GotoIf($["${CAUSE}" != ""]?err_1)
exten => h,n,Set(LINE2="Cause: Fax communication error.")
exten => h,n,Goto(sendmail)
exten => h,n(err_1),Set(LINE2=${CAUSE})
exten => h,n,Goto(sendmail)

exten => h,n(sendmail),NoOp(INFO: Send status by email)
exten => h,n,System(echo "Fax ${SENT} to ${FAXNUMBER} at ${STRFTIME(${EPOCH},,%d-%b-%Y %H:%M:%S)}\, using line ${CHANNEL}." >> ${FAX_MSG})
exten => h,n,System(echo "${LINE2}" >> ${FAX_MSG})
exten => h,n,GotoIf($["${SKIP_FAXSTATUS}" = "YES"]?extra)
exten => h,n,Macro(tenora-faxstatus,${FAX_MSG})
exten => h,n(extra),Set(EXTRA=${EXTRA} --header \'References: ${MESSAGEID}\' --header \'In-Reply-To: ${MESSAGEID}\')
exten => h,n,Macro(tenora-sendmail,${SENDER},Fax status for ${FAX_FILE},${FAX_MSG},${EXTRA})