main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "embed"
  4. "errors"
  5. "log"
  6. "net/http"
  7. "os"
  8. "apiote.xyz/p/asgard/eostre"
  9. "apiote.xyz/p/asgard/gersemi"
  10. "apiote.xyz/p/asgard/hermodr"
  11. "apiote.xyz/p/asgard/himinbjorg"
  12. "apiote.xyz/p/asgard/jotunheim"
  13. "apiote.xyz/p/asgard/mimir"
  14. "apiote.xyz/p/asgard/tyr"
  15. "git.sr.ht/~sircmpwn/getopt"
  16. )
  17. //go:embed templates
  18. var templatesFS embed.FS
  19. func main() {
  20. var (
  21. configPath string
  22. dbPath string
  23. )
  24. getopt.StringVar(&configPath, "c", "", "path to config file")
  25. getopt.StringVar(&dbPath, "b", "", "path to database file")
  26. getopt.Parse()
  27. config, err := jotunheim.Read(configPath)
  28. if err != nil {
  29. log.Fatalln(err)
  30. }
  31. db, err := himinbjorg.Migrate(dbPath)
  32. if err != nil {
  33. log.Fatalln(err)
  34. }
  35. defer db.Close()
  36. log.Println("Migrated")
  37. args := getopt.Args()
  38. switch args[0] {
  39. case "hermodr":
  40. fallthrough
  41. case "hermóðr":
  42. hermodr.Hermodr(config)
  43. case "tyr":
  44. fallthrough
  45. case "týr":
  46. if len(args) == 1 {
  47. tyr.Tyr(db, config)
  48. } else {
  49. switch args[1] {
  50. case "list":
  51. tyr.ListLocks(db)
  52. case "offend":
  53. if len(args) == 2 {
  54. log.Fatalln("missing token")
  55. }
  56. tyr.Release(db, config, args[2], "*", config.Tyr.ImapFolderJunk)
  57. case "release":
  58. if len(args) == 2 {
  59. log.Fatalln("missing token (and recipient)")
  60. }
  61. addressTo := ""
  62. if len(args) != 3 {
  63. addressTo = args[3]
  64. }
  65. tyr.Release(db, config, args[2], addressTo, config.Tyr.ImapFolderInbox)
  66. }
  67. }
  68. case "mimir":
  69. fallthrough
  70. case "mímir":
  71. mimir.Mimir(db, config)
  72. case "ēostre":
  73. fallthrough
  74. case "eostre":
  75. n, err := eostre.Eostre(config)
  76. if err != nil {
  77. log.Println(err)
  78. return
  79. }
  80. _, err = os.Stat("diary.epub")
  81. if err != nil && errors.Is(err, os.ErrNotExist) {
  82. if n == 0 {
  83. return
  84. }
  85. err = eostre.DownloadDiary(config)
  86. if err != nil {
  87. log.Println(err)
  88. return
  89. }
  90. }
  91. if n > 0 {
  92. err = eostre.UpdateDiary(config)
  93. if err != nil {
  94. log.Println(err)
  95. return
  96. }
  97. }
  98. err = eostre.SendDiary(config)
  99. if err != nil {
  100. log.Println(err)
  101. return
  102. }
  103. case "gersemi":
  104. log.Println(gersemi.Gersemi(config))
  105. case "serve":
  106. http.HandleFunc("/tyr", tyr.Serve(db, config, templatesFS))
  107. http.HandleFunc("/mimir", mimir.Serve(db, templatesFS))
  108. http.HandleFunc("/mimir/", mimir.Serve(db, templatesFS))
  109. e := http.ListenAndServe(":8081", nil)
  110. if e != nil {
  111. log.Println(e)
  112. }
  113. }
  114. }