-
Notifications
You must be signed in to change notification settings - Fork 0
/
chkdelivery.go
207 lines (193 loc) · 5.67 KB
/
chkdelivery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
"regexp"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"flag"
"os"
"log/syslog"
"github.com/coreos/go-systemd/daemon"
"time"
)
type pOperation func([]string)
type regaction struct {
reg *regexp.Regexp
name string
op pOperation
}
type msgData struct {
id string
from string
to string
subject string
status string
stline string
}
var (
xlog *syslog.Writer
xdebug bool
rAct []regaction
msgMap map[string]*msgData
db *sql.DB
)
func manyPing(pdb **sql.DB) bool {
var err error
err=nil
db=*pdb
for i:=0; i<5 && err==nil; i++ { err=db.Ping() }
if err!=nil {
fmt.Printf("Database lost, trying to reconnect...\n")
db.Close()
db,err=sql.Open("mysql",fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?autocommit=true",cfg["dbuser"],cfg["dbpass"],cfg["dbhost"],cfg["dbport"],cfg["dbname"]))
if err!=nil {
fmt.Printf("Reconnection error: '%s'\n",err)
return false
}
db.SetConnMaxLifetime(time.Minute*3)
fmt.Printf("Reconnected\n")
err=db.Ping()
if err!=nil { fmt.Printf("Ping after reopening Error: '%s'\n",err) }
*pdb=db
}
var dummy int
err=db.QueryRow("SELECT 169").Scan(&dummy)
if err!=nil {
fmt.Printf("Banal query failed: '%s'\n",err)
return false
}
if dummy!=169 {
fmt.Printf("Banal query wrong result: '%d'\n",dummy)
return false
}
return true
}
func fConnection(parms []string) {
msgid:=parms[4]
if xdebug {
xlog.Debug(fmt.Sprintf("new connection: id %s",msgid))
fmt.Printf("new connection: id %s\n",msgid)
}
msgMap[msgid]=&msgData{msgid,"","","","",""}
}
func fNewMessage(parms []string) {
msgid:=parms[4]
if xdebug {
xlog.Debug(fmt.Sprintf("new message %s <%s>",msgid,parms[5]))
fmt.Printf(fmt.Sprintf("new message %s <%s>\n",msgid,parms[5]))
}
if _,ok:=msgMap[msgid]; !ok {
// local message, no smtp connection
msgMap[msgid]=&msgData{msgid,"","","","",""}
}
}
func fQueueIn(parms []string) {
msgid:=parms[4]
msgfrom:=parms[5]
if xdebug {
xlog.Debug(fmt.Sprintf("queue: Message id %s from %s",msgid,msgfrom))
fmt.Printf("queue: Message id %s from %s\n",msgid,msgfrom)
}
if _,ok:=msgMap[msgid]; !ok {
// bounce, not present in msgMap
msgMap[msgid]=&msgData{msgid,"<BOUNCE>","","","",""}
} else {
msgMap[msgid].from=msgfrom
}
}
func fQueueOut(parms []string) {
msgid:=parms[4]
if _,ok:=msgMap[msgid]; !ok {
xlog.Err(fmt.Sprintf("queue: ERROR! Message %s not found",msgid))
fmt.Printf("queue: ERROR! Message %s not found\n",msgid)
return
}
if xdebug {
xlog.Debug(fmt.Sprintf("queue: Message id %s REMOVED",msgid))
fmt.Printf("queue: Message id %s REMOVED\n",msgid)
}
delete(msgMap,msgid)
}
func fSmtp(parms []string) {
msgid:=parms[4]
msgto:=parms[5]
msgstatus:=parms[6]
msgextra:=parms[7]
if xdebug {
xlog.Debug(fmt.Sprintf("smtpout: Message id %s to %s status %s [%s]",msgid,msgto,msgstatus,msgextra))
fmt.Printf("smtpout: Message id %s to %s status %s [%s]\n",msgid,msgto,msgstatus,msgextra)
}
if _,ok:=msgMap[msgid]; !ok {
// not present in msgMap
msgMap[msgid]=&msgData{msgid,"<???>","","","",""}
}
msgMap[msgid].to=msgto
msgMap[msgid].status=msgstatus
msgMap[msgid].stline=msgextra
if manyPing(&db) {
_,err:=db.Exec("INSERT INTO "+cfg["dbtable"]+" (qid, tstamp, sender, recipient, status, msg, subject) VALUES (?, NOW(), ?, ?, ?, ?, ?)",
msgid, msgMap[msgid].from, msgMap[msgid].to, msgMap[msgid].status, msgMap[msgid].stline, msgMap[msgid].subject)
if err!=nil {
xlog.Err(fmt.Sprintf("%s",err))
fmt.Printf("ERROR: %s\n",err)
} else {
if xdebug {
xlog.Debug("smtpout: inserted")
fmt.Printf("smtpout: inserted\n")
}
}
} else {
xlog.Err("Unable to use MySQL")
fmt.Printf("Unable to use MySQL")
}
}
func fRspamd(parms []string) {
msgid:=parms[4]
subject:=parms[5]
if _,ok:=msgMap[msgid]; !ok {
xlog.Debug(fmt.Sprintf("Message %s not found",msgid))
fmt.Printf("Message %s not found\n",msgid)
return
}
msgMap[msgid].subject=subject
}
func init() {
xlog, _ = syslog.New(syslog.LOG_INFO|syslog.LOG_DAEMON, "chkdelivery")
cfgfile:=flag.String("cfg","/usr/local/etc/chkdelivery.cf","Configuration file name")
flag.BoolVar(&xdebug,"debug",false,"Enable debug messages")
flag.Parse()
InitCfg(*cfgfile)
rAct=[]regaction{
regaction {regexp.MustCompile(`^(... .. \d\d:\d\d:\d\d) (\S+) (postfix/smtpd)\[\d+\]: (\S+): (.*)`),"connection",fConnection},
regaction {regexp.MustCompile(`^(... .. \d\d:\d\d:\d\d) (\S+) (postfix/cleanup)\[\d+\]: (\S+): message-id=<(\S*)>`),"newmessage",fNewMessage},
regaction {regexp.MustCompile(`^(... .. \d\d:\d\d:\d\d) (\S+) (postfix/qmgr)\[\d+\]: (\S+): from=<(\S*)>, size=(\d+), (.*)`), "queuemanager",fQueueIn},
regaction {regexp.MustCompile(`^(... .. \d\d:\d\d:\d\d) (\S+) (postfix/smtp)\[\d+\]: (\S+): to=<(\S*)>, .* status=(\S+) (.*)`), "smtpout",fSmtp},
regaction {regexp.MustCompile(`^(... .. \d\d:\d\d:\d\d) (\S+) (postfix/qmgr)\[\d+\]: (\S+): (removed)`), "queuemanager",fQueueOut},
regaction {regexp.MustCompile(`^(... .. \d\d:\d\d:\d\d) (\S+) (rspamd)\[\d+\]: .*, qid: <(\S+)>, .*, subject: "(.*)"`), "rspamd", fRspamd},
}
msgMap=make(map[string]*msgData)
var err error
db,err=sql.Open("mysql",fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?autocommit=true",cfg["dbuser"],cfg["dbpass"],cfg["dbhost"],cfg["dbport"],cfg["dbname"]))
if (err!=nil) {
fmt.Println("ERROR CONNECTING MYSQL")
xlog.Crit("ERROR CONNECTING MYSQL")
os.Exit(1)
}
}
func main() {
ff:=follower{}
ff.init(cfg["logfile"])
daemon.SdNotify(false, "READY=1")
if xdebug {
xlog.Debug(fmt.Sprintf("chkdelivery: starting"))
fmt.Println("chkdelivery: starting")
}
for {
s:=ff.tail()
for _,v:=range rAct {
f:=v.reg.FindStringSubmatch(s)
if f==nil { continue }
v.op(f)
}
}
}