main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "apiote.xyz/p/go-dirty"
  7. )
  8. type TyrConfig struct {
  9. ImapAddress string
  10. ImapUsername string
  11. ImapPassword string
  12. ImapFolderInbox string
  13. ImapFolderJunk string
  14. ImapFolderArchive string
  15. ImapFolderTrash string
  16. ImapFolderDrafts string
  17. ImapFolderQuarantine string
  18. ImapFolderSent string
  19. }
  20. type HermodrConfig struct {
  21. ImapAddress string
  22. ImapUsername string
  23. ImapPassword string
  24. ImapFolderInbox string
  25. ImapFolderRedirected string
  26. Recipient string
  27. SmtpServer string
  28. SmtpUsername string
  29. PublicKey string
  30. }
  31. type MimirConfig struct {
  32. ImapAddress string
  33. ImapUsername string
  34. ImapPassword string
  35. ImapInbox string
  36. RecipientTemplate string
  37. Categories []string
  38. ForwardAddress string
  39. PersonalAddress string
  40. SmtpAddress string
  41. SmtpSender string
  42. Companion string
  43. }
  44. type Config struct {
  45. Tyr TyrConfig
  46. Hermodr HermodrConfig
  47. Mimir MimirConfig
  48. }
  49. func readConfig() (Config, error) {
  50. file, err := os.Open("config.dirty")
  51. if err != nil {
  52. if os.IsNotExist(err) {
  53. return Config{}, err
  54. } else {
  55. log.Printf("error opening configuration %v\n", err)
  56. return Config{}, err
  57. }
  58. }
  59. defer file.Close()
  60. config := Config{}
  61. err = dirty.LoadStruct(file, &config)
  62. return config, err
  63. }
  64. func main() {
  65. config, err := readConfig()
  66. if err != nil {
  67. log.Fatalln(err)
  68. }
  69. //log.Printf("running with conifig\n%+v\n", config)
  70. db, err := migrate()
  71. if err != nil {
  72. log.Fatalln(err)
  73. }
  74. defer db.Close()
  75. log.Println("Migrated")
  76. switch os.Args[1] {
  77. case "hermodr":
  78. fallthrough
  79. case "hermóðr":
  80. hermodr(config)
  81. case "tyr":
  82. fallthrough
  83. case "týr":
  84. if len(os.Args) == 2 {
  85. tyr(db, config)
  86. } else {
  87. switch os.Args[2] {
  88. case "list":
  89. tyr_lists_locks(db)
  90. case "offend":
  91. if len(os.Args) == 3 {
  92. log.Fatalln("missing token")
  93. }
  94. tyr_release(db, config, os.Args[3], "*", config.Tyr.ImapFolderJunk)
  95. case "release":
  96. if len(os.Args) == 3 {
  97. log.Fatalln("missing token (and recipient)")
  98. }
  99. addressTo := ""
  100. if len(os.Args) == 4 {
  101. addressTo = "*"
  102. } else {
  103. addressTo = os.Args[4]
  104. }
  105. tyr_release(db, config, os.Args[3], addressTo, config.Tyr.ImapFolderInbox)
  106. }
  107. }
  108. case "mimir":
  109. fallthrough
  110. case "mímir":
  111. mimir(db, config)
  112. case "serve":
  113. http.HandleFunc("/tyr", tyr_serve)
  114. http.HandleFunc("/mimir", mimir_serve(db))
  115. http.HandleFunc("/mimir/", mimir_serve(db))
  116. e := http.ListenAndServe(":8081", nil)
  117. if e != nil {
  118. log.Println(e)
  119. }
  120. }
  121. }