Skip to content

Instantly share code, notes, and snippets.

@jirutka
Last active February 15, 2016 21:01
Show Gist options
  • Save jirutka/9e4921f447a254aa9631 to your computer and use it in GitHub Desktop.
Save jirutka/9e4921f447a254aa9631 to your computer and use it in GitHub Desktop.
Some experimental filters for OpenSMTPD.

Warning: Filter API is not stable yet.

Filters are not triggered when sending e-mail directly using the sendmail command.

# coding=utf-8
#
# Dummy filter from Gilles Chehade (poolpOrg) written in Python.
#
import filter
def on_connect(id, local, remote, hostname):
print("on_connect: ", hex(id), local, remote, hostname)
return filter.accept(id)
def on_helo(id, helo):
print("on_helo: ", hex(id), helo)
return filter.accept(id)
def on_mail(id, mail):
print("on_mail: ", hex(id), mail)
return filter.accept(id)
def on_rcpt(id, rcpt):
print("on_rcpt: ", hex(id), rcpt)
return filter.accept(id)
def on_data(id):
print("on_data: ", hex(id))
return filter.accept(id)
def on_dataline(id, line):
print("on_dataline: ", hex(id), line)
return filter.writeln(id, line)
def on_eom(id, size):
print("on_eom: ", hex(id), size)
return filter.accept(id)
def on_commit(id):
print("on_commit: ", hex(id))
return filter.accept(id)
def on_rollback(id):
print("on_rollback: ", hex(id))
return filter.accept(id)
def on_disconnect(id):
print("on_disconnect: ", hex(id))
#!/usr/bin/perl
# Copyright (c) 2014 Sunil Nimmagadda <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use 5.20.1;
use warnings;
use experimental 'signatures';
use HTTP::Tiny;
my $hdrs_ref = {};
sub on_data ($id) { $hdrs_ref->{$id} = {}; }
sub on_dataline ($id, $line) {
smtpd::filter_api_writeln($id, $line);
my $h = $hdrs_ref->{$id};
$h->{is_done} = 1 if $line =~ /^$/; # end of headers
return if $h->{is_done};
$h->{From} = $line if $line =~ /^From:/;
$h->{To} = $line if $line =~ /^To:/;
$h->{Cc} = $line if $line =~ /^Cc:/;
$h->{Subject} = $line if $line =~ /^Subject:/;
}
sub on_commit ($id) {
my $mailaddr = '[email protected]';
my $h = $hdrs_ref->{$id};
my $subject = $h->{Subject};
my $from = $h->{From};
pushover( $from, $subject ) if grep { /$mailaddr/ } ( $h->{To}, $h->{Cc} );
delete $hdrs_ref->{$id};
}
sub on_disconnect ($id) { delete $hdrs_ref->{$id}; }
sub on_reset ($id) { delete $hdrs_ref->{$id}; }
sub on_rollback ($id) { delete $hdrs_ref->{$id}; }
sub pushover ($from, $subject) {
my $token = 'xxxxxx';
my $user = 'yyyyyy';
my $url = 'https://api.pushover.net/1/messages.json';
my $pid = fork;
if ( !defined $pid ) {
say 'Pushover: fork failed';
return;
}
return if $pid; # parent exits
my $res = HTTP::Tiny->new->post_form(
$url,
{
token => $token,
user => $user,
message => $subject,
title => $from
}
);
say "Pushover: ", $res->{status}, " ", $res->{reason};
}
filter dummy python "/etc/opensmtpd/dummy_filter.py"
listen on lo filter dummy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment