Skip to content

Commit

Permalink
wip: Provide hooks to manage IMAP connection #277
Browse files Browse the repository at this point in the history
  • Loading branch information
cnouguier committed Jun 13, 2024
1 parent 2453b0b commit f6912d1
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 12 deletions.
11 changes: 5 additions & 6 deletions lib/hooks/hooks.ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const exec = util.promisify(_exec)
const debug = makeDebug('krawler:hooks:ftp')

// Helper function to wrap lftp cli
async function runFtpCommand (client, commands, settings = {}) {
async function lftpCommand (client, commands, settings = {}) {
let user = client.user
let pass = client.pass
let port = client.port
Expand Down Expand Up @@ -45,7 +45,7 @@ export function listFTP (options = {}) {

// Run the command
const commands = [`cd ${remoteDir}`, 'cls -1']
const { stdout, stderr } = await runFtpCommand(client, commands, options.settings)
const { stdout, stderr } = await lftpCommand(client, commands, options.settings)
if (stderr) {
console.error(stderr.toString())
}
Expand All @@ -54,7 +54,6 @@ export function listFTP (options = {}) {
}
return hook
}

return callOnHookItems(options)(list)
}

Expand All @@ -69,7 +68,7 @@ export function globFTP (options = {}) {

// Run the command
const commands = [`cd ${remoteDir}`, `glob -f echo ${pattern}`]
const { stdout, stderr } = await runFtpCommand(client, commands, options.settings)
const { stdout, stderr } = await lftpCommand(client, commands, options.settings)
if (stderr) {
console.error(stderr.toString())
}
Expand Down Expand Up @@ -110,7 +109,7 @@ export function getFTP (options = {}) {

// Run the command
const commands = [`get ${remoteFile} -o ${localFilePath}`]
const { stdout, stderr } = await runFtpCommand(client, commands, options.settings)
const { stdout, stderr } = await lftpCommand(client, commands, options.settings)
if (options.stderr && stderr) {
console.error(stderr.toString())
}
Expand All @@ -137,7 +136,7 @@ export function putFTP (options = {}) {

// Run the command
const commands = [`put ${localFile} -O ${remoteFile}`]
const { stdout, stderr } = await runFtpCommand(client, commands, options.settings)
const { stdout, stderr } = await lftpCommand(client, commands, options.settings)
if (stderr) {
console.error(stderr.toString())
}
Expand Down
85 changes: 85 additions & 0 deletions lib/hooks/hooks.imap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import util from 'util'
import _ from 'lodash'
import { ImapFlow } from 'imapflow'
import makeDebug from 'debug'
import { callOnHookItems } from '../utils.js'

const debug = makeDebug('krawler:hooks:imap')

// Connect to the IMAP server
export function connectIMAP (options = {}) {
return async function (hook) {
if (hook.type !== 'before') {
throw new Error('The \'connectIMAP\' hook should only be used as a \'before\' hook.')
}
let client = _.get(hook.data, options.clientPath || 'client')
if (client) {
debug(`[IMAP] ${hook.data.id} is already connected`)
return hook
}
debug(`[IMAP] connecting ${hook.data.id}`)
client = new ImapFlow(options)
await client.connect()
_.set(hook.data, options.clientPath || 'client', client)
debug(`[IMAP] ${hook.data.id} is now connected`)
return hook
}
}

// Disconnect from IMAP server
export function disconnectIMAP (options = {}) {
return async function (hook) {
if ((hook.type !== 'after') && (hook.type !== 'error')) {
throw new Error('The \'disconnectIMAP\' hook should only be used as a \'after/error\' hook.')
}
const client = _.get(hook.data, options.clientPath || 'client')
if (_.isNil(client)) {
debug(`[IMAP] ${hook.data.id} is alredy disconnected`)
return hook
}
debug(`[IMAP] disconnecting ${hook.data.id}`)
await client.logout()
_.unset(hook.data, options.clientPath || 'client')
debug(`[IMAP] ${hook.data.id} is now disconnected`)
return hook
}
}

// List mailboxes
export function listIMAPMailboxes (options = {}) {
async function listMailboxes (item, hook) {
const client = _.get(hook.data, options.clientPath || 'client')
if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'listIMAP\' hook')

debug(`[IMAP] listing mailboxes for ${hook.data.id}`)
const mailboxes = await client.list(options)
console.log(mailboxes)
_.set(hook, options.dataPath || 'result.data', mailboxes)
return hook
}
return callOnHookItems(options)(listMailboxes)
}

// List mailboxes
export function fetchIMAPMessages (options = {}) {
async function fetchMessage (item, hook) {
const client = _.get(hook.data, options.clientPath || 'client')
if (_.isNil(client)) throw new Error('You must provide client parameters to use the \'listIMAP\' hook')

debug(`[IMAP] fetching messages for ${hook.data.id}`)
let lock = await client.getMailboxLock(options.mailbox)
try {
let messages = []
for await (const message of client.fetch(options.range, options.query, _.omit(options, ['mailbox', 'range', 'query', 'clientPath']))) {
messages.push(message)
}
console.log(messages)
_.set(hook, options.dataPath || 'result.data', messages)
} finally {
// Make sure lock is released, otherwise next `getMailboxLock()` never returns
lock.release();
}
return hook
}
return callOnHookItems(options)(fetchMessage)
}
1 change: 1 addition & 0 deletions lib/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './hooks.docker.js'
export * from './hooks.feathers.js'
export * from './hooks.ftp.js'
export * from './hooks.grid.js'
export * from './hooks.imap.js'
export * from './hooks.json.js'
export * from './hooks.geojson.js'
export * from './hooks.kml.js'
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
},
"c8": {
"all": true,
"reporter": [ "lcov" ]
"reporter": [
"lcov"
]
},
"pkg": {
"scripts": [
Expand Down Expand Up @@ -81,6 +83,7 @@
"fs-blob-store": "^5.2.1",
"fs-extra": "^8.1.0",
"helmet": "^3.5.0",
"imapflow": "^1.0.162",
"js-yaml": "^3.13.1",
"kue": "^0.11.6",
"lodash": "^4.17.15",
Expand Down
Loading

0 comments on commit f6912d1

Please sign in to comment.