summaryrefslogtreecommitdiffstats
path: root/yamn/cmd/yamn-encode/main.go
blob: 70f6494752938fd25755cd3a469224cb3bd1ea92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"encoding/json"
	"fmt"
	"io"
	"os"

	"git.virebent.art/virebent/yamnweb/yamn/encoder"
)

type request struct {
	Kind          encoder.Kind `json:"kind"`
	PublicKeyring string       `json:"public_keyring"`
	Entry         string       `json:"entry"`
	Chain         []string     `json:"chain"`
	From          string       `json:"from"`
	ReplyTo       string       `json:"reply_to"`
	To            string       `json:"to"`
	Subject       string       `json:"subject"`
	Newsgroup     string       `json:"newsgroup"`
	Body          string       `json:"body"`
	References    string       `json:"references"`
}

type response struct {
	Success      bool   `json:"success"`
	EntryAddress string `json:"entry_address,omitempty"`
	Envelope     string `json:"envelope,omitempty"`
	Error        string `json:"error,omitempty"`
}

func main() {
	var input request
	data, err := io.ReadAll(io.LimitReader(os.Stdin, 256*1024))
	if err == nil {
		err = json.Unmarshal(data, &input)
	}
	if err == nil {
		result, encodeErr := encoder.Encode(encoder.Request{
			Kind: input.Kind, PublicKeyring: input.PublicKeyring, Entry: input.Entry,
			Chain: input.Chain, From: input.From, ReplyTo: input.ReplyTo, To: input.To,
			Subject: input.Subject, Newsgroup: input.Newsgroup, Body: input.Body,
			References: input.References,
		})
		if encodeErr == nil {
			_ = json.NewEncoder(os.Stdout).Encode(response{Success: true, EntryAddress: result.EntryAddress, Envelope: string(result.Envelope)})
			return
		}
		err = encodeErr
	}
	_ = json.NewEncoder(os.Stdout).Encode(response{Success: false, Error: "YAMN encoding failed"})
	fmt.Fprintln(os.Stderr, "yamn-encode:", err)
	os.Exit(1)
}