Replaced fileExists function with isRegularFile and isDir (not used yet).

This commit is contained in:
urosj 2019-05-16 20:51:48 +02:00
parent 2f7e5bd45a
commit 65c9b407fe

View File

@ -48,7 +48,7 @@ import (
"log/syslog" "log/syslog"
) )
const VERSION = "1.0.0006" const VERSION = "1.0.0008"
const DEBUG = true const DEBUG = true
const RESPONSE_DIR = "/var/spool/autoresponder/responses" const RESPONSE_DIR = "/var/spool/autoresponder/responses"
@ -75,9 +75,19 @@ func DebugSyslogFmt(format string, v ...interface{}) {
} }
// Return true if file exists and is regular file // Return true if file exists and is regular file
func fileExists(name string) bool { func isRegularFile(name string) bool {
st, err := os.Lstat(name) st, err := os.Lstat(name)
if err != nil || (st.Mode() & os.ModeType) != os.FileMode(0) { if err != nil || ! st.Mode().IsRegular() {
return false
}
return true
}
// Return true if dir exists
func isDir(name string) bool {
st, err := os.Lstat(name)
if err != nil || ! st.Mode().IsDir() {
return false return false
} }
@ -109,13 +119,13 @@ func sendMail(from, to string, populateStdin func(io.WriteCloser)) error {
// Set autoresponse using supplied arguments and stdin (email body) // Set autoresponse using supplied arguments and stdin (email body)
func setAutoresponseViaEmail(recipient, sender, saslUser, clientIp string) error { func setAutoresponseViaEmail(recipient, sender, saslUser, clientIp string) error {
senderResponsePath := filepath.Join(RESPONSE_DIR, sender) senderResponsePath := filepath.Join(RESPONSE_DIR, sender)
if fileExists(senderResponsePath) { if isRegularFile(senderResponsePath) {
err := deleteAutoresponse(sender, true) err := deleteAutoresponse(sender, true)
if err != nil { if err != nil {
return err return err
} }
if ! fileExists(senderResponsePath) { if ! isRegularFile(senderResponsePath) {
syslg.Info(fmt.Sprintf("Autoresponse disabled for address %v by SASL authenticated user: %v from: %v", syslg.Info(fmt.Sprintf("Autoresponse disabled for address %v by SASL authenticated user: %v from: %v",
sender, saslUser, clientIp)) sender, saslUser, clientIp))
// Send mail via sendmail // Send mail via sendmail
@ -188,7 +198,7 @@ func setAutoresponseViaEmail(recipient, sender, saslUser, clientIp string) error
} }
} }
if fileExists(senderResponsePath) { if isRegularFile(senderResponsePath) {
syslg.Info(fmt.Sprintf("Autoresponse enabled for address %v by SASL authenticated user: %v from: %v", syslg.Info(fmt.Sprintf("Autoresponse enabled for address %v by SASL authenticated user: %v from: %v",
sender, saslUser, clientIp)) sender, saslUser, clientIp))
// Send mail via sendmail // Send mail via sendmail
@ -212,10 +222,10 @@ func forwardEmailAndAutoresponse(recipient, sender, saslUser, clientIp string, r
recipientRateLog := filepath.Join(RATE_LOG_DIR, recipient) recipientRateLog := filepath.Join(RATE_LOG_DIR, recipient)
recipientSenderRateLog := filepath.Join(RATE_LOG_DIR, recipient, sender) recipientSenderRateLog := filepath.Join(RATE_LOG_DIR, recipient, sender)
if fileExists(recipientResponsePath) { if isRegularFile(recipientResponsePath) {
// Check rate log // Check rate log
sendResponse := true sendResponse := true
if fileExists(recipientSenderRateLog) { if isRegularFile(recipientSenderRateLog) {
curTime := time.Now() curTime := time.Now()
st, err := os.Stat(recipientSenderRateLog) st, err := os.Stat(recipientSenderRateLog)
if err != nil { if err != nil {
@ -324,13 +334,13 @@ func enableAutoresponse(email string) error {
editFilePath := emailResponsePath editFilePath := emailResponsePath
// If editFilePath does not exist, also try to enable previosly disabled autoresponse // If editFilePath does not exist, also try to enable previosly disabled autoresponse
if ! fileExists(editFilePath) { if ! isRegularFile(editFilePath) {
enableExAutoresponse(email, true) enableExAutoresponse(email, true)
} }
// If file does not exist yet, create template file as tmp file // If file does not exist yet, create template file as tmp file
var oldModTime, newModTime time.Time var oldModTime, newModTime time.Time
if ! fileExists(editFilePath) { if ! isRegularFile(editFilePath) {
editFile, err := ioutil.TempFile("", "autoresponder") editFile, err := ioutil.TempFile("", "autoresponder")
if err != nil { if err != nil {
return err return err
@ -408,7 +418,7 @@ mail body`, email))
func disableAutoresponse(email string) error { func disableAutoresponse(email string) error {
emailResponsePath := filepath.Join(RESPONSE_DIR, email) emailResponsePath := filepath.Join(RESPONSE_DIR, email)
if fileExists(emailResponsePath) { if isRegularFile(emailResponsePath) {
disableEmailResponsePath := emailResponsePath + "_DISABLED" disableEmailResponsePath := emailResponsePath + "_DISABLED"
os.Remove(disableEmailResponsePath) os.Remove(disableEmailResponsePath)
err := os.Rename(emailResponsePath, disableEmailResponsePath) err := os.Rename(emailResponsePath, disableEmailResponsePath)
@ -432,7 +442,7 @@ func enableExAutoresponse(email string, nostdout bool) error {
emailResponsePath := filepath.Join(RESPONSE_DIR, email) emailResponsePath := filepath.Join(RESPONSE_DIR, email)
disableEmailResponsePath := emailResponsePath + "_DISABLED" disableEmailResponsePath := emailResponsePath + "_DISABLED"
if fileExists(disableEmailResponsePath) { if isRegularFile(disableEmailResponsePath) {
os.Remove(emailResponsePath) os.Remove(emailResponsePath)
err := os.Rename(disableEmailResponsePath, emailResponsePath) err := os.Rename(disableEmailResponsePath, emailResponsePath)
if err != nil { if err != nil {
@ -458,7 +468,7 @@ func deleteAutoresponse(email string, nostdout bool) error {
disabledDeleteResponsePath := deleteResponsePath + "_DISABLED" disabledDeleteResponsePath := deleteResponsePath + "_DISABLED"
recipientRateLog := filepath.Join(RATE_LOG_DIR, email) recipientRateLog := filepath.Join(RATE_LOG_DIR, email)
if fileExists(deleteResponsePath) { if isRegularFile(deleteResponsePath) {
os.Remove(disabledDeleteResponsePath) os.Remove(disabledDeleteResponsePath)
os.RemoveAll(recipientRateLog) os.RemoveAll(recipientRateLog)
err := os.Remove(deleteResponsePath) err := os.Remove(deleteResponsePath)
@ -510,7 +520,7 @@ func main() {
flag.Parse() flag.Parse()
if *showVersion { if *showVersion {
fmt.Printf("autoresponder %v, written by Uros Juvan <asmpro@gmail.com> 2017\n", VERSION) fmt.Printf("autoresponder %v, written by Uros Juvan <asmpro@gmail.com> 2017-2019\n", VERSION)
os.Exit(0) os.Exit(0)
} }