1function setup(c_ip, helo, hn)
2  if not c_ip then c_ip = "127.0.0.1" end
3  if not helo then helo = "it.is.i" end
4  if not hn then hn = "localhost" end
5  conn = mt.connect("inet:" .. port .. "@" .. host)
6  if conn == nil then
7    error "mt.connect() failed"
8  end
9  if mt.conninfo(conn, hn, c_ip) then
10    error "mt.conninfo() failed"
11  end
12  if mt.getreply(conn) ~= SMFIR_CONTINUE then
13    error "mt.conninfo() unexpected reply"
14  end
15  if mt.helo(conn, helo) then
16    error "mt.helo() failed"
17  end
18  if mt.getreply(conn) ~= SMFIR_CONTINUE then
19    error "mt.helo() unexpected reply"
20  end
21end
22
23function teardown()
24  if conn then
25    mt.disconnect(conn)
26  end
27  conn = nil
28end
29
30function send_message(body, hdrs, id, sender, rcpts)
31  mt.macro(conn, SMFIC_MAIL, "i", id or "test-id")
32  if mt.mailfrom(conn, sender or "sender@example.com") then
33    error "mt.mailfrom() failed"
34  end
35  if mt.getreply(conn) ~= SMFIR_CONTINUE then
36    error "mt.mailfrom() unexpected reply"
37  end
38  if not rcpts then
39    rcpts = {"rcpt@example.com"}
40  end
41  for _, r in ipairs(rcpts) do
42    mt.rcptto(conn, r)
43  end
44  if not hdrs then
45    hdrs = default_hdrs
46  end
47  if not hdrs['From'] then
48    hdrs['From'] = sender or "sender@example.com"
49  end
50  for k, v in pairs(hdrs) do
51    if mt.header(conn, k, v) then
52      error (string.format("mt.header(%s) failed", k))
53    end
54  end
55  if mt.eoh(conn) then
56    error "mt.eoh() failed"
57  end
58  if mt.getreply(conn) ~= SMFIR_CONTINUE then
59    error "mt.eoh() unexpected reply"
60  end
61  if mt.bodystring(conn, body .. "\r\n") then
62    error "mt.bodystring() failed"
63  end
64  if mt.getreply(conn) ~= SMFIR_CONTINUE then
65    error "mt.bodystring() unexpected reply"
66  end
67  if mt.eom(conn) then
68    error "mt.eom() failed"
69  end
70end
71
72function check_accept()
73  local rc = mt.getreply(conn)
74  if rc ~= SMFIR_ACCEPT then
75    error (string.format("mt.eom() unexpected reply: %s", rc))
76  end
77end
78
79function check_gtube(code, ecode, msg)
80  if not mt.eom_check(conn, MT_SMTPREPLY, code or '554', ecode or '5.7.1', msg or 'Gtube pattern') then
81    error "mt.eom_check() failed"
82  end
83  local rc = mt.getreply(conn)
84  if rc ~= SMFIR_REPLYCODE then
85    error (string.format("mt.eom() unexpected reply: %s", rc))
86  end
87end
88
89function check_defer(code, ecode, msg)
90  if not mt.eom_check(conn, MT_SMTPREPLY, code or '451', ecode or '4.7.1', msg or 'Try much later') then
91    error "mt.eom_check() failed"
92  end
93  local rc = mt.getreply(conn)
94  if rc ~= SMFIR_REPLYCODE then
95    error (string.format("mt.eom() unexpected reply: %s", rc))
96  end
97end
98
99function check_subject_rw(subj, tmpl)
100  if not subj then
101    subj = default_hdrs['Subject']
102  end
103  if not tmpl then
104    tmpl = "*** SPAM *** %s"
105  end
106  local new_subj = string.format(tmpl, subj)
107  if not mt.eom_check(conn, MT_HDRCHANGE, "Subject", new_subj) then
108    error "subject not rewritten"
109  end
110end
111
112function check_headers(count)
113  for i=0, count-1 do
114    local hdr = mt.getheader(conn, "DKIM-Signature", i)
115    if not hdr then
116      error (string.format("Signature %s not added", i))
117    end
118  end
119end
120