summaryrefslogtreecommitdiffstats
path: root/reg-tests/mailers/healthcheckmail.lua
blob: 4cb0e9d58276a3ccd850572aece4e2cd723a2d8f (plain)
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
local vtc_port1 = 0
local mailsreceived = 0
local mailconnectionsmade = 0
local healthcheckcounter = 0

function RecieveAndCheck(applet, expect)
	data = applet:getline()
	if data:sub(1,expect:len()) ~= expect then
		core.Info("Expected: "..expect.." but got:"..data:sub(1,expect:len()))
		applet:send("Expected: "..expect.." but got:"..data.."\r\n")
		return false
	end
	return true
end

core.register_service("mailservice", "tcp", function(applet)
	core.Info("############# Mailservice Called #############")
	mailconnectionsmade = mailconnectionsmade + 1
	applet:send("220 Welcome\r\n")
	local data

	if RecieveAndCheck(applet, "HELO") == false then
	   applet:set_var("txn.result", "ERROR (step: HELO)")
	   return
	end
	applet:send("250 OK\r\n")
	if RecieveAndCheck(applet, "MAIL FROM:") == false then
	   applet:set_var("txn.result", "ERROR (step: MAIL FROM)")
	   return
	end
	applet:send("250 OK\r\n")
	if RecieveAndCheck(applet, "RCPT TO:") == false then
	   applet:set_var("txn.result", "ERROR (step: RCPT TO)")
	   return
	end
	applet:send("250 OK\r\n")
	if RecieveAndCheck(applet, "DATA") == false then
	   applet:set_var("txn.result", "ERROR (step: DATA)")
	   return
	end
	applet:send("354 OK\r\n")
	core.Info("#### Send your mailbody")
	local endofmail = false
	local subject = ""
	while endofmail ~= true do
		data = applet:getline() -- BODY CONTENT
		--core.Info(data)
		if data:sub(1, 9) == "Subject: " then
		   subject = data
		end
		if (data == "\r\n") then
			data = applet:getline() -- BODY CONTENT
			core.Info(data)
			if (data == ".\r\n") then
				endofmail = true
			end
		end
	end
	core.Info("#### Body received OK")
	applet:send("250 OK\r\n")

	if RecieveAndCheck(applet, "QUIT") == false then
	   applet:set_var("txn.result", "ERROR (step: QUIT)")
	   return
	end
	applet:send("221 Mail queued for delivery to /dev/null \r\n")
	core.Info("Mail queued for delivery to /dev/null subject: "..subject)
	applet:set_var("txn.result", "SUCCESS")
end)