2024-10-04 23:32:21 +02:00
|
|
|
const express = require('express')
|
2024-10-05 00:09:54 +02:00
|
|
|
const router = new express.Router()
|
2024-09-09 07:10:30 +02:00
|
|
|
const {param} = require('express-validator')
|
2024-10-04 23:32:21 +02:00
|
|
|
|
2023-11-01 13:04:44 +01:00
|
|
|
const config = require('../../../application/config')
|
2024-10-03 01:22:10 +02:00
|
|
|
const Helper = require('../../../application/helper')
|
|
|
|
const helper = new(Helper)
|
|
|
|
|
2024-10-04 23:32:21 +02:00
|
|
|
const purgeTime = helper.purgeTimeElemetBuilder()
|
2024-10-03 01:22:10 +02:00
|
|
|
|
2024-09-09 07:10:30 +02:00
|
|
|
const sanitizeAddress = param('address').customSanitizer(
|
2023-11-01 11:48:19 +01:00
|
|
|
(value, {req}) => {
|
|
|
|
return req.params.address
|
|
|
|
.replace(/[^A-Za-z0-9_.+@-]/g, '') // Remove special characters
|
|
|
|
.toLowerCase()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
router.get('^/:address([^@/]+@[^@/]+)', sanitizeAddress, (req, res, _next) => {
|
|
|
|
const mailProcessingService = req.app.get('mailProcessingService')
|
|
|
|
res.render('inbox', {
|
2024-09-17 20:44:42 +02:00
|
|
|
title: `${config.http.branding[0]} | ` + req.params.address,
|
2024-10-03 01:22:10 +02:00
|
|
|
purgeTime: purgeTime,
|
2023-11-01 11:48:19 +01:00
|
|
|
address: req.params.address,
|
2023-11-01 13:21:01 +01:00
|
|
|
mailSummaries: mailProcessingService.getMailSummaries(req.params.address),
|
2024-10-02 14:50:40 +02:00
|
|
|
branding: config.http.branding,
|
2023-11-01 11:48:19 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get(
|
2024-10-02 08:27:10 +02:00
|
|
|
'^/:address/:uid([0-9]+)',
|
2023-11-01 11:48:19 +01:00
|
|
|
sanitizeAddress,
|
|
|
|
async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const mailProcessingService = req.app.get('mailProcessingService')
|
|
|
|
const mail = await mailProcessingService.getOneFullMail(
|
|
|
|
req.params.address,
|
|
|
|
req.params.uid
|
|
|
|
)
|
2024-09-28 05:21:51 +02:00
|
|
|
if (mail) {
|
2024-09-29 10:20:20 +02:00
|
|
|
// Set a default subject if none is present
|
|
|
|
if (!mail.subject) {
|
|
|
|
mail.subject = 'No Subject'
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:48:19 +01:00
|
|
|
// Emails are immutable, cache if found
|
|
|
|
res.set('Cache-Control', 'private, max-age=600')
|
|
|
|
res.render('mail', {
|
2024-09-28 01:49:06 +02:00
|
|
|
title: mail.subject + " | " + req.params.address,
|
2024-10-03 01:22:10 +02:00
|
|
|
purgeTime: purgeTime,
|
2023-11-01 11:48:19 +01:00
|
|
|
address: req.params.address,
|
2023-11-01 13:21:37 +01:00
|
|
|
mail,
|
2023-11-02 06:25:22 +01:00
|
|
|
uid: req.params.uid,
|
2024-10-02 14:50:40 +02:00
|
|
|
branding: config.http.branding,
|
2023-11-01 11:48:19 +01:00
|
|
|
})
|
|
|
|
} else {
|
2024-01-26 02:41:50 +01:00
|
|
|
res.render(
|
|
|
|
'error',
|
|
|
|
{
|
2024-10-03 01:22:10 +02:00
|
|
|
purgeTime: purgeTime,
|
2024-01-26 02:41:50 +01:00
|
|
|
address: req.params.address,
|
|
|
|
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
|
2024-10-02 14:50:40 +02:00
|
|
|
branding: config.http.branding
|
|
|
|
|
2024-01-26 02:41:50 +01:00
|
|
|
}
|
|
|
|
)
|
2023-11-01 11:48:19 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('error while fetching one email', error)
|
|
|
|
next(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-09-28 02:16:58 +02:00
|
|
|
router.get(
|
|
|
|
'^/:address/delete-all',
|
|
|
|
sanitizeAddress,
|
|
|
|
async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const mailProcessingService = req.app.get('mailProcessingService')
|
|
|
|
const mailSummaries = await mailProcessingService.getMailSummaries(req.params.address)
|
|
|
|
for (mail in mailSummaries) {
|
|
|
|
await mailProcessingService.deleteSpecificEmail(req.params.address, mailSummaries[mail].uid)
|
|
|
|
}
|
|
|
|
res.redirect(`/inbox/${req.params.address}`)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('error while deleting email', error)
|
|
|
|
next(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-02 06:25:22 +01:00
|
|
|
router.get(
|
2024-09-28 01:49:06 +02:00
|
|
|
'^/:address/:uid/delete',
|
2023-11-02 06:25:22 +01:00
|
|
|
sanitizeAddress,
|
|
|
|
async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const mailProcessingService = req.app.get('mailProcessingService')
|
2023-11-02 06:27:28 +01:00
|
|
|
await mailProcessingService.deleteSpecificEmail(req.params.address, req.params.uid)
|
2023-11-03 06:11:17 +01:00
|
|
|
res.redirect(`/inbox/${req.params.address}`)
|
2023-11-02 06:25:22 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error('error while deleting email', error)
|
|
|
|
next(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-09-28 09:04:05 +02:00
|
|
|
router.get(
|
2024-10-02 08:27:10 +02:00
|
|
|
'^/:address/:uid/:checksum([a-f0-9]+)',
|
2024-09-28 09:04:05 +02:00
|
|
|
sanitizeAddress,
|
|
|
|
async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const mailProcessingService = req.app.get('mailProcessingService')
|
|
|
|
const mail = await mailProcessingService.getOneFullMail(
|
|
|
|
req.params.address,
|
|
|
|
req.params.uid
|
|
|
|
)
|
|
|
|
var index = mail.attachments.findIndex(attachment => attachment.checksum === req.params.checksum);
|
|
|
|
const attachment = mail.attachments[index];
|
|
|
|
if (attachment) {
|
|
|
|
try {
|
2024-09-29 09:56:35 +02:00
|
|
|
res.set('Content-Disposition', `attachment; filename=${attachment.filename}`);
|
|
|
|
res.set('Content-Type', attachment.contentType);
|
|
|
|
res.send(attachment.content);
|
|
|
|
return;
|
2024-09-28 09:04:05 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error('error while fetching attachment', error);
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.render(
|
|
|
|
'error',
|
|
|
|
{
|
2024-10-03 01:22:10 +02:00
|
|
|
purgeTime: purgeTime,
|
2024-09-28 09:04:05 +02:00
|
|
|
address: req.params.address,
|
|
|
|
message: 'This attachment could not be found. It either does not exist or has been deleted from our servers!',
|
2024-10-02 14:50:40 +02:00
|
|
|
branding: config.http.branding,
|
2024-09-28 09:04:05 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
res.redirect(`/inbox/${req.params.address}`)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('error while deleting email', error)
|
|
|
|
next(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-09-28 01:49:06 +02:00
|
|
|
router.get(
|
|
|
|
'^/:address/:uid/raw',
|
|
|
|
sanitizeAddress,
|
|
|
|
async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const mailProcessingService = req.app.get('mailProcessingService')
|
2024-09-28 04:44:13 +02:00
|
|
|
mail = await mailProcessingService.getOneFullMail(
|
2024-09-28 01:49:06 +02:00
|
|
|
req.params.address,
|
2024-09-28 04:44:13 +02:00
|
|
|
req.params.uid,
|
|
|
|
true
|
2024-09-28 01:49:06 +02:00
|
|
|
)
|
2024-09-28 05:21:51 +02:00
|
|
|
if (mail) {
|
2024-10-02 08:27:10 +02:00
|
|
|
mail = mail.replace(/(?:\r\n|\r|\n)/g, '<br>')
|
2024-09-28 01:49:06 +02:00
|
|
|
// Emails are immutable, cache if found
|
|
|
|
res.set('Cache-Control', 'private, max-age=600')
|
|
|
|
res.render('raw', {
|
2024-09-28 04:44:13 +02:00
|
|
|
title: req.params.uid + " | raw | " + req.params.address,
|
2024-09-28 01:49:06 +02:00
|
|
|
mail
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.render(
|
|
|
|
'error',
|
|
|
|
{
|
2024-10-03 01:22:10 +02:00
|
|
|
purgeTime: purgeTime,
|
2024-09-28 01:49:06 +02:00
|
|
|
address: req.params.address,
|
|
|
|
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
|
2024-10-02 14:50:40 +02:00
|
|
|
branding: config.http.branding,
|
2024-09-28 01:49:06 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('error while fetching one email', error)
|
|
|
|
next(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-01 11:48:19 +01:00
|
|
|
module.exports = router
|