1
2local vtc_port1 = 0
3local mailsreceived = 0
4local mailconnectionsmade = 0
5local healthcheckcounter = 0
6
7core.register_action("bug", { "http-res" }, function(txn)
8	data = txn:get_priv()
9	if not data then
10		data = 0
11	end
12	data = data + 1
13	print(string.format("set to %d", data))
14	txn.http:res_set_status(200 + data)
15	txn:set_priv(data)
16end)
17
18core.register_service("luahttpservice", "http", function(applet)
19	local response = "?"
20	local responsestatus = 200
21       if applet.path == "/setport" then
22		vtc_port1 = applet.headers["vtcport1"][0]
23		response = "OK"
24	end
25	if applet.path == "/svr_healthcheck" then
26		healthcheckcounter = healthcheckcounter + 1
27		if healthcheckcounter < 2 or healthcheckcounter > 6 then
28			responsestatus = 403
29		end
30	end
31
32	applet:set_status(responsestatus)
33       if applet.path == "/checkMailCounters" then
34		response = "MailCounters"
35		applet:add_header("mailsreceived", mailsreceived)
36		applet:add_header("mailconnectionsmade", mailconnectionsmade)
37	end
38	applet:start_response()
39	applet:send(response)
40end)
41
42core.register_service("fakeserv", "http", function(applet)
43	applet:set_status(200)
44	applet:start_response()
45end)
46
47function RecieveAndCheck(applet, expect)
48	data = applet:getline()
49	if data:sub(1,expect:len()) ~= expect then
50		core.Info("Expected: "..expect.." but got:"..data:sub(1,expect:len()))
51		applet:send("Expected: "..expect.." but got:"..data.."\r\n")
52		return false
53	end
54	return true
55end
56
57core.register_service("mailservice", "tcp", function(applet)
58	core.Info("############# Mailservice Called #############")
59	mailconnectionsmade = mailconnectionsmade + 1
60	applet:send("220 Welcome\r\n")
61	local data
62
63	if RecieveAndCheck(applet, "EHLO") == false then
64		return
65	end
66	applet:send("250 OK\r\n")
67	if RecieveAndCheck(applet, "MAIL FROM:") == false then
68		return
69	end
70	applet:send("250 OK\r\n")
71	if RecieveAndCheck(applet, "RCPT TO:") == false then
72		return
73	end
74	applet:send("250 OK\r\n")
75	if RecieveAndCheck(applet, "DATA") == false then
76		return
77	end
78	applet:send("354 OK\r\n")
79	core.Info("#### Send your mailbody")
80	local endofmail = false
81	local subject = ""
82	while endofmail ~= true do
83		data = applet:getline() -- BODY CONTENT
84		--core.Info(data)
85		if data:sub(1, 9) == "Subject: " then
86			subject = data
87		end
88		if (data == "\r\n") then
89			data = applet:getline() -- BODY CONTENT
90			core.Info(data)
91			if (data == ".\r\n") then
92				endofmail = true
93			end
94		end
95	end
96	core.Info("#### Body recieved OK")
97	applet:send("250 OK\r\n")
98
99	if RecieveAndCheck(applet, "QUIT") == false then
100		return
101	end
102	applet:send("221 Mail queued for delivery to /dev/null \r\n")
103	core.Info("Mail queued for delivery to /dev/null subject: "..subject)
104	mailsreceived = mailsreceived + 1
105end)
106