Skip to content

Commit

Permalink
feat: Rewrite client and server to be more compatible with libmilter/…
Browse files Browse the repository at this point in the history
…sendmail/postfix

BREAKING CHANGE: Signature changes in Client and Milter. Enhanced state enforcement and error detection can break existing (wrong) code.
  • Loading branch information
d--j committed Feb 26, 2023
1 parent b759f67 commit f9e0337
Show file tree
Hide file tree
Showing 40 changed files with 7,237 additions and 1,059 deletions.
19 changes: 0 additions & 19 deletions .build.yml

This file was deleted.

18 changes: 9 additions & 9 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build
run: go build -v ./...
- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
- name: Test
run: go test -v ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof

/.idea
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
BSD 2-Clause License

Copyright (c) 2017 Bozhin Zafirov
Copyright (c) 2019 Simon Ser
Copyright (c) 2023 Daniel Jagszent
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,90 @@
# go-milter

[![GoDoc](https://godoc.org/github.com/emersion/go-milter?status.svg)](https://godoc.org/github.com/emersion/go-milter)
[![builds.sr.ht status](https://builds.sr.ht/~emersion/go-milter/commits.svg)](https://builds.sr.ht/~emersion/go-milter/commits?)
[![GoDoc](https://godoc.org/github.com/d--j/go-milter?status.svg)](https://godoc.org/github.com/d--j/go-milter)
![Build status](https://github.com/d--j/go-milter/actions/workflows/go.yml/badge.svg?branch=main)

A Go library to write mail filters.

With this library you can write both the client (MTA/SMTP-Server) and server (milter filter)
in pure Go without sendmail's libmilter.

## Features

* Client & Server support milter protocol version 6 with all features. E.g.:
* all milter events including DATA, UNKNOWN, ABORT and QUIT NEW CONNECTION
* milter can skip e.g. body chunks when it does not need all chunks
* milter can send progress notifications when response can take some time
* milter can automatically instruct the MTA which macros it needs.
* UTF-8 support

## Usage

```go
package main

import (
"log"
"net"
"sync"

"github.com/d--j/go-milter"
)

type ExampleBackend struct {
milter.NoOpMilter
}

func (b *ExampleBackend) RcptTo(rcptTo string, esmtpArgs string, m *milter.Modifier) (*milter.Response, error) {
// reject the mail when it goes to [email protected] and is a local delivery
if rcptTo == "[email protected]" && m.Macros.Get(milter.MacroRcptMailer) == "local" {
return milter.RejectWithCodeAndReason(550, "We do not like you\r\nvery much, please go away")
}
return milter.RespContinue, nil
}

func main() {
// create socket to listen on
socket, err := net.Listen("tcp4", "127.0.0.1:6785")
if err != nil {
log.Fatal(err)
}
defer socket.Close()

// define the backend, required actions, protocol options and macros we want
server := milter.NewServer(
milter.WithMilter(func() milter.Milter {
return &ExampleBackend{}
}),
milter.WithProtocol(milter.OptNoConnect|milter.OptNoHelo|milter.OptNoMailFrom|milter.OptNoBody|milter.OptNoHeaders|milter.OptNoEOH|milter.OptNoUnknown|milter.OptNoData),
milter.WithAction(milter.OptChangeFrom|milter.OptAddRcpt|milter.OptRemoveRcpt),
milter.WithMaroRequest(milter.StageRcpt, []milter.MacroName{milter.MacroRcptMailer}),
)
defer server.Close()

// start the milter
var wgDone sync.WaitGroup
wgDone.Add(1)
go func(socket net.Listener) {
if err := server.Serve(socket); err != nil {
log.Fatal(err)
}
wgDone.Done()
}(socket)

log.Printf("Started milter on %s:%s", socket.Addr().Network(), socket.Addr().String())

// quit when milter quits
wgDone.Wait()
}
```

See [![GoDoc](https://godoc.org/github.com/d--j/go-milter?status.svg)](https://godoc.org/github.com/d--j/go-milter) for more documentation and an example for a milter client.

## License

BSD 2-Clause

## Credits

Based on https://github.com/emersion/go-milter by [Simon Ser](https://github.com/emersion) which is based on https://github.com/phalaaxx/milter by
[Bozhin Zafirov](https://github.com/phalaaxx). [Max Mazurov](https://github.com/foxcpp) made major contributions to this code as well.
Loading

0 comments on commit f9e0337

Please sign in to comment.