Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor gofaxsend into a package #58

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gofaxsend/main.go → gofaxsend/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/gonicus/gofaxip/gofaxlib"
"github.com/gonicus/gofaxip/gofaxlib/logger"
"github.com/gonicus/gofaxip/gofaxsend"
)

const (
Expand Down Expand Up @@ -73,18 +74,17 @@ func main() {

qfilename := flag.Arg(0)
if qfilename == "" {
logger.Logger.Println("No qfile provided on command line")
os.Exit(sendFailed)
logger.Logger.Fatalln("No qfile provided on command line")
}

gofaxlib.LoadConfig(*configFile)
devicefifo := filepath.Join(gofaxlib.Config.Hylafax.Spooldir, fifoPrefix+*deviceID)
gofaxlib.SendFIFO(devicefifo, "SB")

returned, err := SendQfile(qfilename)
returned, err := gofaxsend.SendQfileFromDisk(qfilename, *deviceID)
if err != nil {
logger.Logger.Printf("Error processing qfile %v: %v", qfilename, err)
returned = sendFailed
returned = gofaxsend.SendFailed
}

gofaxlib.SendFIFO(devicefifo, "SR")
Expand All @@ -94,5 +94,5 @@ func main() {
}

logger.Logger.Print("Exiting with status ", returned)
os.Exit(returned)
os.Exit(int(returned))
}
2 changes: 1 addition & 1 deletion gofaxsend/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package main
package gofaxsend

// FaxError is a Error including information if a fax shoud be retried
type FaxError interface {
Expand Down
2 changes: 1 addition & 1 deletion gofaxsend/faxfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package main
package gofaxsend

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion gofaxsend/faxjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package main
package gofaxsend

import (
"github.com/google/uuid"
Expand Down
2 changes: 1 addition & 1 deletion gofaxsend/qfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package main
package gofaxsend

import (
"bufio"
Expand Down
2 changes: 1 addition & 1 deletion gofaxsend/qfile_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gofaxsend

import (
"testing"
Expand Down
64 changes: 35 additions & 29 deletions gofaxsend/sendqfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package main
package gofaxsend

import (
"fmt"
Expand All @@ -30,28 +30,34 @@ import (

const (
// Return codes for Hylafax.
sendRetry = iota
sendFailed
sendDone
sendReformat
sendV34fail
sendV17fail
sendBatchfail
sendNobatch
SendRetry SendResult = iota
SendFailed
SendDone
SendReformat
SendV34fail
SendV17fail
SendBatchfail
SendNobatch
)

// SendQfile immediately tries to send the given qfile using FreeSWITCH
func SendQfile(qfilename string) (returned int, err error) {
returned = sendFailed
type SendResult int

// SendQfileFromDisk reads the qfile from disk and then immediately tries to send the given qfile using FreeSWITCH
func SendQfileFromDisk(filename, deviceID string) (SendResult, error) {
// Open qfile
qf, err := OpenQfile(qfilename)
qf, err := OpenQfile(filename)
if err != nil {
err = fmt.Errorf("Cannot open qfile %v: %v", qfilename, err)
return
return SendFailed, fmt.Errorf("cannot open qfile %v: %w", filename, err)
}
defer qf.Close()

return SendQfile(qf, deviceID)
}

// SendQfile immediately tries to send the given qfile using FreeSWITCH
func SendQfile(qf *Qfile, deviceID string) (returned SendResult, err error) {
returned = SendFailed

var jobid uint
if jobidstr := qf.GetString("jobid"); jobidstr != "" {
if i, err := strconv.Atoi(jobidstr); err == nil {
Expand Down Expand Up @@ -114,29 +120,29 @@ func SendQfile(qfilename string) (returned int, err error) {
// Query DynamicConfig
if dcCmd := gofaxlib.Config.Gofaxsend.DynamicConfig; dcCmd != "" {
sessionlog.Log("Calling DynamicConfig script", dcCmd)
dc, err := gofaxlib.DynamicConfig(dcCmd, *deviceID, qf.GetString("owner"), qf.GetString("number"), fmt.Sprint(jobid))
dc, err := gofaxlib.DynamicConfig(dcCmd, deviceID, qf.GetString("owner"), qf.GetString("number"), fmt.Sprint(jobid))
if err != nil {
errmsg := fmt.Sprintln("Error calling DynamicConfig:", err)
sessionlog.Log(errmsg)
qf.Set("returned", strconv.Itoa(sendRetry))
qf.Set("returned", strconv.Itoa(int(SendRetry)))
qf.Set("status", errmsg)
if err = qf.Write(); err != nil {
sessionlog.Logf("Error updating qfile:", err)
}
// Retry, as this is an internal error executing the DynamicConfig script which could recover later
return sendRetry, nil
return SendRetry, nil
}

// Check if call should be rejected
if gofaxlib.DynamicConfigBool(dc.GetString("RejectCall")) {
errmsg := "Transmission rejected by DynamicConfig"
sessionlog.Log(errmsg)
qf.Set("returned", strconv.Itoa(sendFailed))
qf.Set("returned", strconv.Itoa(int(SendFailed)))
qf.Set("status", errmsg)
if err = qf.Write(); err != nil {
sessionlog.Logf("Error updating qfile:", err)
}
return sendFailed, nil
return SendFailed, nil
}

// Check if a custom identifier should be set
Expand Down Expand Up @@ -204,10 +210,10 @@ func SendQfile(qfilename string) (returned int, err error) {
qf.Set("totdials", strconv.Itoa(totdials))
if err = qf.Write(); err != nil {
sessionlog.Log("Error updating qfile:", err)
return sendFailed, nil
return SendFailed, nil
}
// Default: Retry when transmission fails
returned = sendRetry
returned = SendRetry

// Start transmission goroutine
transmitTs := time.Now()
Expand Down Expand Up @@ -235,7 +241,7 @@ StatusLoop:
// Fax Finished
status = result.ResultText
if result.Success {
returned = sendDone
returned = SendDone
}
break StatusLoop
}
Expand All @@ -260,23 +266,23 @@ StatusLoop:
qf.Set("ndials", strconv.Itoa(ndials))
status = faxerr.Error()
if faxerr.Retry() {
returned = sendRetry
returned = SendRetry
} else {
returned = sendFailed
returned = SendFailed
}
break StatusLoop
}
}

qf.Set("status", status)
qf.Set("returned", strconv.Itoa(returned))
qf.Set("returned", strconv.Itoa(int(returned)))
if err = qf.Write(); err != nil {
sessionlog.Log("Error updating qfile:", err)
}

xfl := &gofaxlib.XFRecord{}
xfl.Commid = sessionlog.CommID()
xfl.Modem = *deviceID
xfl.Modem = deviceID
xfl.Jobid = uint(jobid)
xfl.Jobtag = qf.GetString("jobtag")
xfl.Sender = qf.GetString("mailaddr")
Expand All @@ -287,11 +293,11 @@ StatusLoop:
if result.Success {
sessionlog.Logf("Fax sent successfully. Hangup Cause: %v. Result: %v", result.Hangupcause, status)
} else {
sessionlog.Logf("Fax failed. Retry: %v. Hangup Cause: %v. Result: %v", returned == sendRetry, result.Hangupcause, status)
sessionlog.Logf("Fax failed. Retry: %v. Hangup Cause: %v. Result: %v", returned == SendRetry, result.Hangupcause, status)
}
xfl.SetResult(result)
} else {
sessionlog.Logf("Call failed. Retry: %v. Result: %v", returned == sendRetry, status)
sessionlog.Logf("Call failed. Retry: %v. Result: %v", returned == SendRetry, status)
xfl.Reason = status
xfl.Ts = transmitTs
xfl.Jobtime = time.Now().Sub(transmitTs)
Expand Down
2 changes: 1 addition & 1 deletion gofaxsend/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

package main
package gofaxsend

import (
"bytes"
Expand Down