mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2025-12-14 13:56:32 +01:00
log updates
This commit is contained in:
parent
075940b0d8
commit
0ad3e40fcc
6 changed files with 221 additions and 226 deletions
11
app.js
11
app.js
|
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
/* eslint unicorn/no-process-exit: 0 */
|
/* eslint unicorn/no-process-exit: 0 */
|
||||||
|
|
||||||
const config = require('./application/config')
|
const config = require('./application/config')
|
||||||
|
|
@ -35,25 +36,25 @@ imapService.on(ImapService.EVENT_DELETED_MAIL, mail =>
|
||||||
)
|
)
|
||||||
|
|
||||||
mailProcessingService.on('error', err => {
|
mailProcessingService.on('error', err => {
|
||||||
console.error('error from mailProcessingService, stopping.', err)
|
console.error('Error from mailProcessingService, stopping.', err)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
imapService.on(ImapService.EVENT_ERROR, error => {
|
imapService.on(ImapService.EVENT_ERROR, error => {
|
||||||
console.error('fatal error from imap service', error)
|
console.error('Fatal error from IMAP service', error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.set('mailProcessingService', mailProcessingService)
|
app.set('mailProcessingService', mailProcessingService)
|
||||||
|
|
||||||
imapService.connectAndLoadMessages().catch(error => {
|
imapService.connectAndLoadMessages().catch(error => {
|
||||||
console.error('fatal error from imap service', error)
|
console.error('Fatal error from IMAP service', error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('error', error => {
|
server.on('error', error => {
|
||||||
if (error.syscall !== 'listen') {
|
if (error.syscall !== 'listen') {
|
||||||
console.error('fatal web server error', error)
|
console.error('Fatal web server error', error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,7 +69,7 @@ server.on('error', error => {
|
||||||
console.error('Port ' + config.http.port + ' is already in use')
|
console.error('Port ' + config.http.port + ' is already in use')
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
default:
|
default:
|
||||||
console.error('fatal web server error', error)
|
console.error('Fatal web server error', error)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -127,21 +127,18 @@ class ImapService extends EventEmitter {
|
||||||
|
|
||||||
this.connection.on('error', err => {
|
this.connection.on('error', err => {
|
||||||
// We assume that the app will be restarted after a crash.
|
// We assume that the app will be restarted after a crash.
|
||||||
console.error(
|
console.error('got fatal error during imap operation, stop app.', err)
|
||||||
'got fatal error during imap operation, stop app.',
|
|
||||||
err
|
|
||||||
)
|
|
||||||
this.emit('error', err)
|
this.emit('error', err)
|
||||||
})
|
})
|
||||||
|
|
||||||
await this.connection.openBox('INBOX')
|
await this.connection.openBox('INBOX')
|
||||||
debug('connected to imap')
|
debug('Connected to imap')
|
||||||
}, {
|
}, {
|
||||||
retries: 5
|
retries: 5
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('can not connect, even with retry, stop app', error)
|
console.error('Cant connect, even after retrying, stopping app', error)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -226,7 +223,7 @@ class ImapService extends EventEmitter {
|
||||||
})
|
})
|
||||||
|
|
||||||
if (uids.length === 0) {
|
if (uids.length === 0) {
|
||||||
debug('no mails to delete.')
|
debug('No mails to delete.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -235,7 +232,7 @@ class ImapService extends EventEmitter {
|
||||||
uids.forEach(uid => {
|
uids.forEach(uid => {
|
||||||
this.emit(ImapService.EVENT_DELETED_MAIL, uid)
|
this.emit(ImapService.EVENT_DELETED_MAIL, uid)
|
||||||
})
|
})
|
||||||
console.log(`deleted ${uids.length} old messages.`)
|
console.log(`Deleted ${uids.length} old messages.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -243,10 +240,10 @@ class ImapService extends EventEmitter {
|
||||||
* @param uid delete specific mail per UID
|
* @param uid delete specific mail per UID
|
||||||
*/
|
*/
|
||||||
async deleteSpecificEmail(uid) {
|
async deleteSpecificEmail(uid) {
|
||||||
debug(`deleting mails ${uid}`)
|
debug(`Deleting mails ${uid}`)
|
||||||
if (!this.config.email.examples.uids.includes(parseInt(uid))) {
|
if (!this.config.email.examples.uids.includes(parseInt(uid))) {
|
||||||
await this.connection.deleteMessage(uid)
|
await this.connection.deleteMessage(uid)
|
||||||
debug(`deleted mail with UID: ${uid}.`)
|
debug(`Deleted mail with UID: ${uid}.`)
|
||||||
this.emit(ImapService.EVENT_DELETED_MAIL, uid)
|
this.emit(ImapService.EVENT_DELETED_MAIL, uid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -297,10 +294,10 @@ class ImapService extends EventEmitter {
|
||||||
async fetchOneFullMail(to, uid, raw = false) {
|
async fetchOneFullMail(to, uid, raw = false) {
|
||||||
if (!this.connection) {
|
if (!this.connection) {
|
||||||
// Here we 'fail fast' instead of waiting for the connection.
|
// Here we 'fail fast' instead of waiting for the connection.
|
||||||
throw new Error('imap connection not ready')
|
throw new Error('IMAP connection not ready')
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(`fetching full message ${uid}`)
|
debug(`Fetching full message ${uid}`)
|
||||||
|
|
||||||
// For security we also filter TO, so it is harder to just enumerate all messages.
|
// For security we also filter TO, so it is harder to just enumerate all messages.
|
||||||
const searchCriteria = [
|
const searchCriteria = [
|
||||||
|
|
@ -344,7 +341,7 @@ class ImapService extends EventEmitter {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
debug('can not fetch', error)
|
debug('Cant fetch', error)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,13 +54,13 @@ class MailProcessingService extends EventEmitter {
|
||||||
|
|
||||||
onInitialLoadDone() {
|
onInitialLoadDone() {
|
||||||
this.initialLoadDone = true
|
this.initialLoadDone = true
|
||||||
console.log(`initial load done, got ${this.mailRepository.mailCount()} mails`)
|
console.log(`Initial load done, got ${this.mailRepository.mailCount()} mails`)
|
||||||
}
|
}
|
||||||
|
|
||||||
onNewMail(mail) {
|
onNewMail(mail) {
|
||||||
if (this.initialLoadDone) {
|
if (this.initialLoadDone) {
|
||||||
// For now, only log messages if they arrive after the initial load
|
// For now, only log messages if they arrive after the initial load
|
||||||
debug('new mail for', mail.to[0])
|
debug('New mail for', mail.to[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
mail.to.forEach(to => {
|
mail.to.forEach(to => {
|
||||||
|
|
@ -70,7 +70,7 @@ class MailProcessingService extends EventEmitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
onMailDeleted(uid) {
|
onMailDeleted(uid) {
|
||||||
debug('mail deleted with uid', uid)
|
debug('Mail deleted with uid', uid)
|
||||||
this.mailRepository.removeUid(uid)
|
this.mailRepository.removeUid(uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,7 +78,7 @@ class MailProcessingService extends EventEmitter {
|
||||||
try {
|
try {
|
||||||
await this.imapService.deleteOldMails(helper.purgeTimeStamp())
|
await this.imapService.deleteOldMails(helper.purgeTimeStamp())
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('can not delete old messages', error)
|
console.log('Cant delete old messages', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,7 +86,7 @@ class MailProcessingService extends EventEmitter {
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
fs.writeFile(filename, JSON.stringify(mails), err => {
|
fs.writeFile(filename, JSON.stringify(mails), err => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('can not save mails to file', err)
|
console.error('Cant save mails to file', err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ class MailRepository {
|
||||||
mails.forEach(mail => {
|
mails.forEach(mail => {
|
||||||
if (mail.to == this.config.email.examples.account && !this.config.email.examples.uids.includes(parseInt(mail.uid))) {
|
if (mail.to == this.config.email.examples.account && !this.config.email.examples.uids.includes(parseInt(mail.uid))) {
|
||||||
mails = mails.filter(m => m.uid != mail.uid)
|
mails = mails.filter(m => m.uid != mail.uid)
|
||||||
debug('prevented non-example email from being shown in example inbox', mail.uid)
|
debug('Prevented non-example email from being shown in example inbox', mail.uid)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return _.orderBy(mails, mail => Date.parse(mail.date), ['desc'])
|
return _.orderBy(mails, mail => Date.parse(mail.date), ['desc'])
|
||||||
|
|
@ -43,7 +43,7 @@ class MailRepository {
|
||||||
.filter(mail => mail.uid === parseInt(uid) && (address ? to == address : true))
|
.filter(mail => mail.uid === parseInt(uid) && (address ? to == address : true))
|
||||||
.forEach(mail => {
|
.forEach(mail => {
|
||||||
this.mailSummaries.remove(to, mail)
|
this.mailSummaries.remove(to, mail)
|
||||||
debug('removed ', mail.date, to, mail.subject)
|
debug('Removed ', mail.date, to, mail.subject)
|
||||||
deleted = true
|
deleted = true
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,7 @@ router.get(
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
res.render(
|
res.render(
|
||||||
'error',
|
'error', {
|
||||||
{
|
|
||||||
purgeTime: purgeTime,
|
purgeTime: purgeTime,
|
||||||
address: req.params.address,
|
address: req.params.address,
|
||||||
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
|
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
|
||||||
|
|
@ -66,7 +65,7 @@ router.get(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('error while fetching one email', error)
|
console.error('Error while fetching email', error)
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +83,7 @@ router.get(
|
||||||
}
|
}
|
||||||
res.redirect(`/inbox/${req.params.address}`)
|
res.redirect(`/inbox/${req.params.address}`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('error while deleting email', error)
|
console.error('Error while deleting email', error)
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -100,7 +99,7 @@ router.get(
|
||||||
await mailProcessingService.deleteSpecificEmail(req.params.address, req.params.uid)
|
await mailProcessingService.deleteSpecificEmail(req.params.address, req.params.uid)
|
||||||
res.redirect(`/inbox/${req.params.address}`)
|
res.redirect(`/inbox/${req.params.address}`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('error while deleting email', error)
|
console.error('Error while deleting email', error)
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -125,13 +124,12 @@ router.get(
|
||||||
res.send(attachment.content);
|
res.send(attachment.content);
|
||||||
return;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('error while fetching attachment', error);
|
console.error('Error while fetching attachment', error);
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
res.render(
|
res.render(
|
||||||
'error',
|
'error', {
|
||||||
{
|
|
||||||
purgeTime: purgeTime,
|
purgeTime: purgeTime,
|
||||||
address: req.params.address,
|
address: req.params.address,
|
||||||
message: 'This attachment could not be found. It either does not exist or has been deleted from our servers!',
|
message: 'This attachment could not be found. It either does not exist or has been deleted from our servers!',
|
||||||
|
|
@ -141,7 +139,7 @@ router.get(
|
||||||
}
|
}
|
||||||
res.redirect(`/inbox/${req.params.address}`)
|
res.redirect(`/inbox/${req.params.address}`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('error while deleting email', error)
|
console.error('Error while deleting email', error)
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -170,8 +168,7 @@ router.get(
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
res.render(
|
res.render(
|
||||||
'error',
|
'error', {
|
||||||
{
|
|
||||||
purgeTime: purgeTime,
|
purgeTime: purgeTime,
|
||||||
address: req.params.address,
|
address: req.params.address,
|
||||||
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
|
message: 'This mail could not be found. It either does not exist or has been deleted from our servers!',
|
||||||
|
|
@ -180,7 +177,7 @@ router.get(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('error while fetching one email', error)
|
console.error('Error while fetching raw email', error)
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ app.use('/inbox', inboxRouter)
|
||||||
|
|
||||||
// Catch 404 and forward to error handler
|
// Catch 404 and forward to error handler
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
next({message: 'page not found', status: 404})
|
next({ message: 'Page not found', status: 404 })
|
||||||
})
|
})
|
||||||
|
|
||||||
// Error handler
|
// Error handler
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue