Skip to content

Commit

Permalink
🎨 Format code according to ESLint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
cfpwastaken committed Feb 20, 2024
1 parent e2d44ac commit 08e49e4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 35 deletions.
2 changes: 2 additions & 0 deletions src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const ESC = "\u001b"

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const RESET = ESC + "[m"
const GRAY = ESC + "[37m"
const DARK = ESC + "[90m"
Expand Down
2 changes: 1 addition & 1 deletion src/models/Mail.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes } from "sequelize"
import { AllowNull, BelongsTo, Column, ForeignKey, Model, PrimaryKey, Table, Unique } from "sequelize-typescript"
import { DataTypes } from "sequelize"
import User from "./User.js"

@Table({
Expand Down
2 changes: 1 addition & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTypes } from "sequelize"
import { AllowNull, Column, HasMany, Model, PrimaryKey, Table, Unique } from "sequelize-typescript"
import { DataTypes } from "sequelize"
import Mail from "./Mail.js"
import { createHash } from "node:crypto"

Expand Down
28 changes: 16 additions & 12 deletions src/pop3/POP3Server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import net from "net"
import tls from "tls"
import Logger from "../Logger.js"
import Mail from "../models/Mail.js"
import User from "../models/User.js"
import { createHash } from "node:crypto"
import Mail from "../models/Mail.js"
import Logger from "../Logger.js"
import getConfig from "../config.js"
import net from "net"
import { readFile } from "fs/promises"
import tls from "tls"

const logger = new Logger("POP3", "YELLOW")

Expand Down Expand Up @@ -53,16 +53,16 @@ export default class POP3Server {
username = username.substring(0, username.lastIndexOf("@"))
}
if (username.startsWith("\"") && username.endsWith("\"")) username = username.substring(1, username.length-1)
else if (username.includes("@")) {
else if (username.includes("@")) {
// this is not allowed
return void sock.write("-ERR Invalid username or password\r\n")
}

const _user = await User.findOne({ where: { username } })
const dbuser = await User.findOne({ where: { username } })

if (!_user) return void sock.write("-ERR Invalid username or password\r\n")
if (!dbuser) return void sock.write("-ERR Invalid username or password\r\n")

user = _user
user = dbuser
sock.write("+OK\r\n")
} else if (msg.startsWith("PASS")) { // client gives password
if (args.length < 1) return void sock.write("-ERR Invalid username or password\r\n")
Expand Down Expand Up @@ -92,7 +92,7 @@ export default class POP3Server {
} else if (msg.startsWith("RETR")) {
if (args.length < 1) return void sock.write("-ERR No message specified\r\n")

const mail = await user.getMail(parseInt(args[0].trim()))
const mail = await user.getMail(parseInt(args[0].trim(), 10))

if (!mail) return void sock.write("-ERR No such message\r\n")

Expand All @@ -102,7 +102,7 @@ export default class POP3Server {
} else if (msg.startsWith("TOP")) {
if (args.length < 2) return void sock.write("-ERR No message specified\r\n")

const mail = await user.getMail(parseInt(args[0].trim()))
const mail = await user.getMail(parseInt(args[0].trim(), 10))

if (!mail) return void sock.write("-ERR No such message\r\n")

Expand All @@ -120,7 +120,7 @@ export default class POP3Server {
} else if (msg.startsWith("DELE")) {
if (args.length < 1) return void sock.write("-ERR No message specified\r\n")

const mail = await user.getMail(parseInt(args[0].trim()))
const mail = await user.getMail(parseInt(args[0].trim(), 10))

if (!mail) return void sock.write("-ERR No such message\r\n")

Expand All @@ -129,7 +129,11 @@ export default class POP3Server {
} else if (msg.startsWith("NOOP")) { // this is used to keep the connection alive
sock.write("+OK\r\n")
} else if (msg.startsWith("RSET")) {
for (const mail of markedForDeletion) await mail.restore()
const restores = []

for (const mail of markedForDeletion) restores.push(mail.restore())

await Promise.all(restores)

sock.write("+OK\r\n")
}
Expand Down
9 changes: 4 additions & 5 deletions src/smtp/SMTP.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { mkdir, writeFile } from "fs/promises"
import getConfig from "../config.js"
import Logger from "../Logger.js"
import User from "../models/User.js"
import crypto from "node:crypto"
import getConfig from "../config.js"

const logger = new Logger("SMTP", "GREEN")

Expand Down Expand Up @@ -32,18 +32,17 @@ export default class SMTP {
logger.log("All recipients are on this server.")
} else if (!info.to.every(email => email.endsWith(`@${serverName}`))) logger.warn("Not all recipients are from this server. Will NOT forward mail to other servers.")


const recipients = info.to.filter(email => email.endsWith(`@${serverName}`))

for (const rec of recipients) {
recipients.forEach(async rec => {
logger.log(`Forwarding mail to ${rec}`)
const user = await User.findOne({ where: { username: rec.substring(0, rec.lastIndexOf("@")) } })

if (!user) {
logger.error(`User ${rec} does not exist.`)

// Since we verify the recipients at the RCPT TO command, we should never get here, but you never know
continue
return
}

await user.$create("mail", {
Expand All @@ -53,7 +52,7 @@ export default class SMTP {
})

logger.log(`Forwarded mail to ${rec}`)
}
})
}

}
8 changes: 4 additions & 4 deletions src/smtp/SMTPClient.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import Logger from "../Logger.js"
import getConfig from "../config.js"
import net from "net"
import tls from "tls"
import getConfig from "../config.js"
import Logger from "../Logger.js"

const logger = new Logger("SMTPClient", "TEAL")

export default class SMTPClient {

static async sendMessage(host: string, port: number, from: string, to: string, content: string, useTLS: boolean) {
static sendMessage(host: string, port: number, from: string, to: string, content: string, useTLS: boolean) {
// const sock = net.createConnection(port, host)
const sock = useTLS ? tls.connect(port, host) : net.createConnection(port, host)

sock.on("data", (data: Buffer) => {
logger.log(`Received data: ${data.toString()}`)
})
sock.on("connect", async () => {
sock.on("connect", () => {
logger.log("Connected to server")
sock.write(`EHLO ${getConfig("host", "localhost")}\r\n`)
sock.write(`MAIL FROM:<${from}>\r\n`)
Expand Down
22 changes: 10 additions & 12 deletions src/smtp/SMTPServer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import net from "net"
import tls from "tls"
import { mkdirSync, writeFileSync } from "fs"
import getConfig from "../config.js"
import Mail from "../models/Mail.js"
import User from "../models/User.js"
import crypto from "node:crypto"
import sendStatus from "./status.js"
import Logger from "../Logger.js"
import SMTP from "./SMTP.js"
import User from "../models/User.js"
import getConfig from "../config.js"
import net from "net"
import sendStatus from "./status.js"
import tls from "tls"

const logger = new Logger("SMTPServer", "GREEN")

Expand Down Expand Up @@ -90,11 +87,11 @@ export default class SMTPServer {
}

const email = msg.split(":")[1].split(">")[0].replace("<", "")
const username = email.split("@")[0]
const domain = email.split("@")[1]
const [username, domain] = email.split("@")

if (domain != getConfig("host")) {
// The spec says we MAY forward the message ourselves, but simply returning 550 is fine, and the client should handle it
// The spec says we MAY forward the message ourselves,
// but simply returning 550 is fine, and the client should handle it
status(550)

return
Expand Down Expand Up @@ -127,7 +124,8 @@ export default class SMTPServer {
status(221, "2.0.0")
sock.end()
} else if (msg.startsWith("VRFY")) {
// This command is used to verify if a user exists, but that can be a security risk + it is also done with RCPT TO anyway
// This command is used to verify if a user exists,
// but that can be a security risk + it is also done with RCPT TO anyway
status(502)
} else if (msg.startsWith("EXPN")) status(502)
else status(502)
Expand Down

0 comments on commit 08e49e4

Please sign in to comment.