asgard.git

ref: master

./main.go


  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main

import (
	"embed"
	"errors"
	"log"
	"net/http"
	"os"

	"apiote.xyz/p/asgard/eostre"
	"apiote.xyz/p/asgard/gersemi"
	"apiote.xyz/p/asgard/hermodr"
	"apiote.xyz/p/asgard/himinbjorg"
	"apiote.xyz/p/asgard/jotunheim"
	"apiote.xyz/p/asgard/mimir"
	"apiote.xyz/p/asgard/tyr"

	"git.sr.ht/~sircmpwn/getopt"
)

//go:embed templates
var templatesFS embed.FS

func main() {
	var (
		configPath string
		dbPath     string
	)
	getopt.StringVar(&configPath, "c", "", "path to config file")
	getopt.StringVar(&dbPath, "b", "", "path to database file")
	getopt.Parse()

	config, err := jotunheim.Read(configPath)
	if err != nil {
		log.Fatalln(err)
	}

	db, err := himinbjorg.Migrate(dbPath)
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()
	log.Println("Migrated")

	args := getopt.Args()
	switch args[0] {
	case "hermodr":
		fallthrough
	case "hermóðr":
		hermodr.Hermodr(config)

	case "tyr":
		fallthrough
	case "týr":
		if len(args) == 1 {
			tyr.Tyr(db, config)
		} else {
			switch args[1] {
			case "list":
				tyr.ListLocks(db)
			case "offend":
				if len(args) == 2 {
					log.Fatalln("missing token")
				}
				tyr.Release(db, config, args[2], "*", config.Tyr.ImapFolderJunk)
			case "release":
				if len(args) == 2 {
					log.Fatalln("missing token (and recipient)")
				}
				addressTo := ""
				if len(args) != 3 {
					addressTo = args[3]
				}
				tyr.Release(db, config, args[2], addressTo, config.Tyr.ImapFolderInbox)
			}
		}

	case "mimir":
		fallthrough
	case "mímir":
		mimir.Mimir(db, config)

	case "ēostre":
		fallthrough
	case "eostre":
		n, err := eostre.Eostre(config)
		if err != nil {
			log.Println(err)
			return
		}
		_, err = os.Stat("diary.epub")
		if err != nil && errors.Is(err, os.ErrNotExist) {
			if n == 0 {
				return
			}
			err = eostre.DownloadDiary(config)
			if err != nil {
				log.Println(err)
				return
			}
		}
		if n > 0 {
			err = eostre.UpdateDiary(config)
			if err != nil {
				log.Println(err)
				return
			}
		}
		err = eostre.SendDiary(config)
		if err != nil {
			log.Println(err)
			return
		}

	case "gersemi":
		log.Println(gersemi.Gersemi(config))

	case "serve":
		http.HandleFunc("/tyr", tyr.Serve(db, config, templatesFS))
		http.HandleFunc("/mimir", mimir.Serve(db, templatesFS))
		http.HandleFunc("/mimir/", mimir.Serve(db, templatesFS))
		e := http.ListenAndServe(":8081", nil)
		if e != nil {
			log.Println(e)
		}
	}

}