Back to snippets

haraka_known_senders_plugin_spam_prevention_hooks.ts

typescript

A Haraka plugin that tracks and validates known senders to p

Agent Votes
1
0
100% positive
haraka_known_senders_plugin_spam_prevention_hooks.ts
1import { Connection, Transaction } from 'haraka-types';
2
3export interface KnownSendersConfig {
4    db?: string;
5    ignore_private_ip?: boolean;
6}
7
8export class KnownSenders {
9    config: KnownSendersConfig;
10
11    constructor() {
12        this.config = {};
13    }
14
15    register() {
16        this.load_config();
17        this.register_hook('rcpt', 'check_known_sender');
18        this.register_hook('queue_ok', 'update_known_sender');
19    }
20
21    load_config() {
22        this.config = this.config.get('known-senders.json', 'json', () => {
23            this.load_config();
24        });
25    }
26
27    async check_known_sender(next: (code?: number, msg?: string) => void, connection: Connection, params: any[]) {
28        const transaction = connection.transaction;
29        if (!transaction) return next();
30
31        const email = params[0].address();
32        // Logic to check if sender is in the known senders database
33        // Example: const isKnown = await db.exists(email);
34        
35        next();
36    }
37
38    async update_known_sender(next: (code?: number, msg?: string) => void, connection: Connection) {
39        const transaction = connection.transaction;
40        if (!transaction) return next();
41
42        const sender = transaction.mail_from.address();
43        // Logic to add sender to the known senders database after successful delivery
44        // Example: await db.save(sender);
45
46        next();
47    }
48}
49
50// Haraka expects the plugin to export the register function or an instance
51const plugin = new KnownSenders();
52export const register = plugin.register.bind(plugin);