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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.io.InputStream
import java.io.OutputStream
import java.net.InetAddress
import java.net.Socket
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
class Invalidatable {
var isInvalid: Boolean = false
fun checkValid() {
if (isInvalid)
throw IllegalStateException("Accessed invalid object")
}
fun invalidate() {
isInvalid = true
}
}
abstract class Protocol {
interface IO {
fun isOpen(): Boolean
suspend fun pushBack(data: ByteArray)
suspend fun readBytes(into: ByteArray): Int
suspend fun send(bytes: ByteArray)
suspend fun close()
class FromSocket(val socket: Socket) : FromStreams(socket.getInputStream(), socket.getOutputStream()) {
override suspend fun close() {
super.close()
with(Dispatchers.IO) {
socket.close()
}
}
}
open class FromStreams(val inputStream: InputStream, val outputStream: OutputStream) : IO {
private val i = Invalidatable()
override fun isOpen(): Boolean =
!i.isInvalid
val readBuffer = mutableListOf<ByteArray>()
override suspend fun pushBack(data: ByteArray) {
i.checkValid()
if (data.isEmpty()) return
readBuffer.add(0, data)
}
override suspend fun send(bytes: ByteArray) {
i.checkValid()
with(Dispatchers.IO) {
outputStream.write(bytes)
outputStream.flush()
}
}
override suspend fun close() {
i.checkValid()
i.invalidate()
with(Dispatchers.IO) {
inputStream.close()
outputStream.close()
}
}
override suspend fun readBytes(into: ByteArray): Int {
i.checkValid()
val rb = readBuffer.removeFirstOrNull()
if (rb != null) {
val w = minOf(rb.size, into.size)
rb.copyInto(into, 0, 0, w)
return w
}
return with(Dispatchers.IO) {
inputStream.read(into)
}
}
}
}
protected abstract suspend fun IO.execute()
fun executeAsync(scope: CoroutineScope, io: Protocol.IO): Job {
return scope.launch {
io.execute()
}
}
}
suspend fun Protocol.IO.send(string: String) = send(string.encodeToByteArray())
suspend fun Protocol.IO.readLine(): String {
val y = mutableListOf<String>()
while (true) {
val buffer = ByteArray(4096)
val read = readBytes(buffer)
val i = buffer.findCRLF()
if (i in 0 until read) {
y.add(buffer.copyOfRange(0, i).decodeToString())
pushBack(buffer.copyOfRange(i + 2, read))
break
} else {
y.add(buffer.copyOfRange(0, read).decodeToString())
}
}
return y.joinToString("")
}
private fun ByteArray.findCRLF(): Int {
return this.asSequence().zipWithNext().withIndex().firstOrNull { (idx, v) ->
(v.first == '\r'.code.toByte()) and (v.second == '\n'.code.toByte())
}?.index ?: -1
}
suspend fun Protocol.IO.pushBack(string: String) = pushBack(string.encodeToByteArray())
suspend fun Protocol.IO.lookahead(string: String): Boolean = lookahead(string.encodeToByteArray())
suspend fun Protocol.IO.lookahead(bytes: ByteArray): Boolean {
val buffer = ByteArray(bytes.size)
val read = readBytes(buffer)
if (read != bytes.size || !buffer.contentEquals(bytes)) {
pushBack(buffer.copyOf(read))
return false
}
return true
}
@OptIn(ExperimentalContracts::class)
class SMTPReceiveProtocol(val localHost: String, val inetAddress: InetAddress) : Protocol() {
class Commands(val line: String, private val io: IO) : IO by io {
var matched = false
suspend inline fun command(vararg name: String, block: IO.(String) -> Unit) {
contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) }
for (n in name) commandOnce(n, block)
}
suspend inline fun commandOnce(name: String, block: IO.(String) -> Unit) {
contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) }
if (matched) return
if (!line.startsWith(name)) return
matched = true
block(line.substring(name.length).trimStart())
}
suspend inline fun otherwise(block: IO.(String) -> Unit) {
contract { callsInPlace(block, InvocationKind.AT_MOST_ONCE) }
if (matched) return
matched = true
block(line)
}
}
suspend inline fun IO.commands(line: String, block: Commands.() -> Unit) {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
Commands(line, this).block()
}
data class Mail(val sender: String, val recipient: List<String>, val text: String)
class MailTransaction(
var isHelod: Boolean = false,
var isEhlod: Boolean = false,
var recipients: MutableList<String> = mutableListOf(),
var sender: String? = null,
) {
fun reset() {
recipients = mutableListOf()
sender = null
}
}
override suspend fun IO.execute() {
send("220 $localHost\r\n")
val messages = mutableListOf<Mail>()
val trans = MailTransaction()
while (isOpen()) {
commands(readLine()) {
println(line)
command("EHLO") {
send("250 hello advanced $it\r\n")
trans.isHelod = true
trans.isEhlod = true
}
command("HELO") {
send("250 Hello $it, how are you on this fine day?\r\n")
trans.isHelod = true
}
command("MAIL FROM:") {
send("250 Sender ok\r\n")
trans.sender = it
}
command("RCPT TO:") {
send("250 Receipient ok\r\n")
trans.recipients.add(it)
}
command("DATA") {
send("354 Enter mail, end with \".\" on a line by itself\r\n")
var text = ""
while (true) {
val tmp = readLine()
if (tmp == ".") break
text += tmp + "\n"
}
messages.add(Mail(trans.sender!!, trans.recipients.toList(), text))
trans.reset()
send("250 Message accepted for delivery\r\n")
}
command("QUIT") {
send("221 $localHost closing connection\r\n")
close()
}
otherwise {
send("500 ERROR UNKNOWN CODE $it\r\n")
}
}
}
println("Got ${messages.size} messages")
messages.forEach {
println("Message:")
println("MAIL FROM: ${it.sender}")
println("RCPTS TO: ${it.recipient}")
println("CONTENT: ${it.text}")
}
}
}
|