mailbox.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package idavollr
  2. import (
  3. "errors"
  4. "log"
  5. "apiote.xyz/p/asgard/jotunheim"
  6. "github.com/emersion/go-imap"
  7. "github.com/emersion/go-imap/client"
  8. )
  9. type AbstractMailbox interface {
  10. MailboxName() string
  11. ImapAddress() string
  12. ImapUsername() string
  13. ImapPassword() string
  14. SetClient(*client.Client)
  15. Client() *client.Client
  16. Config() jotunheim.Config
  17. SetMailbox(*imap.MailboxStatus)
  18. GetMailbox() *imap.MailboxStatus
  19. SetSection(*imap.BodySectionName)
  20. Section() *imap.BodySectionName
  21. Messages() chan *imap.Message
  22. Done() chan error
  23. SetupChannels()
  24. }
  25. type Mailbox struct {
  26. MboxName string
  27. ImapAdr string
  28. ImapUser string
  29. ImapPass string
  30. Conf jotunheim.Config
  31. cli *client.Client
  32. mbox *imap.MailboxStatus
  33. sect *imap.BodySectionName
  34. msgs chan *imap.Message
  35. dn chan error
  36. }
  37. func (m Mailbox) MailboxName() string {
  38. return m.MboxName
  39. }
  40. func (m Mailbox) ImapAddress() string {
  41. return m.ImapAdr
  42. }
  43. func (m Mailbox) ImapUsername() string {
  44. return m.ImapUser
  45. }
  46. func (m Mailbox) ImapPassword() string {
  47. return m.ImapPass
  48. }
  49. func (m *Mailbox) SetClient(c *client.Client) {
  50. m.cli = c
  51. }
  52. func (m Mailbox) Client() *client.Client {
  53. return m.cli
  54. }
  55. func (m Mailbox) Config() jotunheim.Config {
  56. return m.Conf
  57. }
  58. func (m *Mailbox) SetMailbox(mbox *imap.MailboxStatus) {
  59. m.mbox = mbox
  60. }
  61. func (m Mailbox) GetMailbox() *imap.MailboxStatus {
  62. return m.mbox
  63. }
  64. func (m *Mailbox) SetSection(sect *imap.BodySectionName) {
  65. m.sect = sect
  66. }
  67. func (m Mailbox) Section() *imap.BodySectionName {
  68. return m.sect
  69. }
  70. func (m Mailbox) Messages() chan *imap.Message {
  71. return m.msgs
  72. }
  73. func (m Mailbox) Done() chan error {
  74. return m.dn
  75. }
  76. func (m *Mailbox) SetupChannels() {
  77. m.msgs = make(chan *imap.Message, 10)
  78. m.dn = make(chan error, 1)
  79. }
  80. func Connect(m AbstractMailbox) (AbstractMailbox, error) {
  81. c, err := client.DialTLS(m.ImapAddress(), nil)
  82. m.SetClient(c)
  83. return m, err
  84. }
  85. func ConnectInsecure(m AbstractMailbox) (AbstractMailbox, error) {
  86. c, err := client.Dial(m.ImapAddress())
  87. m.SetClient(c)
  88. return m, err
  89. }
  90. func Login(m AbstractMailbox) error {
  91. return m.Client().Login(m.ImapUsername(), m.ImapPassword())
  92. }
  93. func SelectInbox(m AbstractMailbox) (AbstractMailbox, error) {
  94. mbox, err := m.Client().Select(m.MailboxName(), false)
  95. m.SetMailbox(mbox)
  96. return m, err
  97. }
  98. func CheckEmptyBox(m AbstractMailbox) error {
  99. if m.GetMailbox().Messages == 0 {
  100. return EmptyBoxError{}
  101. }
  102. return nil
  103. }
  104. func FetchMessages(m AbstractMailbox) AbstractMailbox {
  105. from := uint32(1)
  106. to := m.GetMailbox().Messages
  107. seqset := new(imap.SeqSet)
  108. seqset.AddRange(from, to)
  109. m.SetSection(&imap.BodySectionName{})
  110. items := []imap.FetchItem{imap.FetchEnvelope, m.Section().FetchItem(), imap.FetchUid}
  111. go func() {
  112. m.Done() <- m.Client().Fetch(seqset, items, m.Messages())
  113. }()
  114. return m
  115. }
  116. func CheckFetchError(m AbstractMailbox) error {
  117. return <-m.Done()
  118. }
  119. func Expunge(m AbstractMailbox) error {
  120. return m.Client().Expunge(nil)
  121. }
  122. func IgnoreEmptyBox(m AbstractMailbox, e error) (AbstractMailbox, error) {
  123. var emptyBoxError EmptyBoxError
  124. if errors.As(e, &emptyBoxError) {
  125. log.Println("Mailbox is empty")
  126. return m, nil
  127. }
  128. return m, e
  129. }
  130. func Disconnect(m AbstractMailbox, e error) (AbstractMailbox, error) {
  131. if m.Client() != nil {
  132. m.Client().Logout()
  133. m.Client().Close()
  134. }
  135. return m, e
  136. }