aboutsummaryrefslogtreecommitdiffstats
path: root/giphyproxy/main.go
blob: 3bf162092f64566c5451c9337de5a0ad84a147cd (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// maunium-stickerpicker - A fast and simple Matrix sticker picker widget.
// Copyright (C) 2024 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"regexp"
	"slices"
	"time"

	"go.mau.fi/util/exerrors"
	"gopkg.in/yaml.v3"
	"maunium.net/go/mautrix/federation"
	"maunium.net/go/mautrix/mediaproxy"
)

type Config struct {
	mediaproxy.BasicConfig  `yaml:",inline"`
	mediaproxy.ServerConfig `yaml:",inline"`
	Destination             string `yaml:"destination"`
	SplitIdx                []int  `yaml:"split_idx"`
}

var configPath = flag.String("config", "config.yaml", "config file path")
var generateServerKey = flag.Bool("generate-key", false, "generate a new server key and exit")

var giphyIDRegex = regexp.MustCompile(`^[a-zA-Z0-9-_]+$`)
var destination = "https://i.giphy.com/%s.webp"
var splitIdx []int

func main() {
	flag.Parse()
	if *generateServerKey {
		fmt.Println(federation.GenerateSigningKey().SynapseString())
	} else {
		cfgFile := exerrors.Must(os.ReadFile(*configPath))
		var cfg Config
		exerrors.PanicIfNotNil(yaml.Unmarshal(cfgFile, &cfg))
		mp := exerrors.Must(mediaproxy.NewFromConfig(cfg.BasicConfig, getMedia))
		mp.KeyServer.Version.Name = "mautrix-go + maunium-stickerpicker giphy proxy"
		if cfg.Destination != "" {
			destination = cfg.Destination
			splitIdx = cfg.SplitIdx
			slices.Sort(splitIdx)
			slices.Reverse(splitIdx)
		}
		exerrors.PanicIfNotNil(mp.Listen(cfg.ServerConfig))
	}
}

func getMedia(_ context.Context, id string, _ map[string]string) (response mediaproxy.GetMediaResponse, err error) {
	// This is not related to giphy, but random cats are always fun
	if id == "cat" {
		return &mediaproxy.GetMediaResponseURL{
			URL:       "https://cataas.com/cat",
			ExpiresAt: time.Now(),
		}, nil
	}
	if !giphyIDRegex.MatchString(id) {
		return nil, mediaproxy.ErrInvalidMediaIDSyntax
	}
	for _, idx := range splitIdx {
		if idx >= len(id) {
			return nil, mediaproxy.ErrInvalidMediaIDSyntax
		}
		id = id[:idx] + "/" + id[idx:]
	}
	return &mediaproxy.GetMediaResponseURL{
		URL: fmt.Sprintf(destination, id),
	}, nil
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage