fixed some issues

This commit is contained in:
Niklas 2019-12-08 14:44:57 +01:00
parent 98d9fb084c
commit 51887828f3
1 changed files with 32 additions and 15 deletions

View File

@ -1,13 +1,14 @@
package main package main
import ( import (
"bufio"
"database/sql" "database/sql"
"flag" "flag"
"fmt" "fmt"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
"io" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -46,24 +47,35 @@ func isRegularFile(name string) bool {
} }
// Send mail from address to address with given mail content being passed as function pointer // Send mail from address to address with given mail content being passed as function pointer
func sendMail(from, to string, populateStdin func(io.WriteCloser)) error { func sendMail(from, to string, msg string) error {
cmd := exec.Command(SENDMAIL_BIN, "-i", "-f", from, to) cmd := exec.Command(SENDMAIL_BIN, "-i", "-f", from, to)
stdin, err := cmd.StdinPipe() stdin, err := cmd.StdinPipe()
if err != nil { if err != nil {
return err return err
} }
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
err = cmd.Start() err = cmd.Start()
if err != nil { if err != nil {
return err return err
} }
go func() {
populateStdin(stdin) stdin.Write([]byte(msg))
}() stdin.Close()
sentBytes, _ := ioutil.ReadAll(stdout)
err = cmd.Wait() err = cmd.Wait()
if err != nil { if err != nil {
return err return err
} }
DebugSyslogFmt("Send Command Output\n")
DebugSyslogFmt(string(sentBytes))
return nil return nil
} }
@ -126,7 +138,7 @@ Subject: %v
X-Version: %v X-Version: %v
X-Service: %v X-Service: %v
%v`, *recipient, config.Section("").Key("query"), config.Section("").Key("version"), config.Section("").Key("service_name"), key.response), nil %v`, *recipient, config.Section("").Key("query").String(), config.Section("").Key("version").String(), config.Section("").Key("service_name").String(), key.response), nil
} }
// Forward email using supplied arguments and stdin (email body) // Forward email using supplied arguments and stdin (email body)
@ -160,11 +172,7 @@ func forwardEmailAndAutoresponse(recipient string, sender string, responseRate u
DebugFmtPrintf(response) DebugFmtPrintf(response)
sendMail(recipient, sender, func(sink io.WriteCloser) { sendMail(recipient, sender, response)
defer sink.Close()
io.Copy(sink, os.Stdin)
})
err = os.MkdirAll(recipientRateLog, 0770) err = os.MkdirAll(recipientRateLog, 0770)
if err != nil { if err != nil {
return err return err
@ -182,12 +190,21 @@ func forwardEmailAndAutoresponse(recipient string, sender string, responseRate u
} }
// Now resend original mail // Now resend original mail
sendMail(sender, recipient, func(sink io.WriteCloser) {
defer sink.Close()
io.Copy(sink, os.Stdin) file := os.Stdin
}) fi, err := file.Stat()
if err != nil {
return fmt.Errorf("file.Stat() error")
}
size := fi.Size()
if size <= 0 {
fmt.Println("Stdin is empty")
return fmt.Errorf("Stdin is empty")
}
reader := bufio.NewReader(file)
text, _ := reader.ReadString('\n')
err = sendMail(sender, recipient, text)
return nil return nil
} }