-
Notifications
You must be signed in to change notification settings - Fork 33
/
session.go
138 lines (120 loc) · 3.46 KB
/
session.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
129
130
131
132
133
134
135
136
137
138
package mail
import (
"context"
"io"
"net/mail"
"strings"
"github.com/emersion/go-smtp"
"github.com/ncarlier/readflow/pkg/constant"
"github.com/ncarlier/readflow/pkg/model"
"github.com/ncarlier/readflow/pkg/service"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
ErrActionAborted = &smtp.SMTPError{
Code: 451,
EnhancedCode: smtp.EnhancedCode{4, 5, 1},
Message: "Action aborted: Local error in processing",
}
ErrMailboxUnavailable = &smtp.SMTPError{
Code: 550,
EnhancedCode: smtp.EnhancedCode{5, 5, 0},
Message: "Requested action not taken: mailbox unavailable",
}
ErrBadSequence = &smtp.SMTPError{
Code: 503,
EnhancedCode: smtp.EnhancedCode{5, 0, 3},
Message: "Bad sequence of commands",
}
)
type Session struct {
from string
ctx context.Context
logger zerolog.Logger
}
func (s *Session) AuthPlain(username, password string) error {
return smtp.ErrAuthUnsupported
}
func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
// TODO limit body size
s.from = from
return nil
}
func (s *Session) Rcpt(to string, opts *smtp.RcptOptions) error {
// decode recipient
dest := strings.Split(to, "@")[0]
parts := strings.Split(dest, "-")
hash := parts[len(parts)-1]
alias := strings.TrimSuffix(dest, "-"+hash)
// retrieve the corresponding user
user, err := service.Lookup().GetUserByHashID(s.ctx, hash)
if err != nil {
return ErrMailboxUnavailable
}
s.ctx = context.WithValue(s.ctx, constant.ContextUser, *user)
s.ctx = context.WithValue(s.ctx, constant.ContextUserID, *user.ID)
s.ctx = context.WithValue(s.ctx, constant.ContextIsAdmin, false)
s.logger = s.logger.With().Uint("uid", *user.ID).Logger()
// retrieve the corresponding incoming webhook
// TODO escape alias? so find another way to retrieve the webhook (full scan ?)
incomingWebhook, err := service.Lookup().GetIncomingWebhookByAlias(s.ctx, alias)
if err != nil || incomingWebhook == nil {
return ErrMailboxUnavailable
}
s.ctx = context.WithValue(s.ctx, constant.ContextIncomingWebhook, incomingWebhook)
s.logger = s.logger.With().Str("webhook", incomingWebhook.Alias).Logger()
return nil
}
func (s *Session) Data(r io.Reader) error {
if s.ctx.Value(constant.ContextIncomingWebhook) == nil {
return ErrBadSequence
}
s.logger.Debug().Msg("receiving mail using incoming webhook...")
// read message
msg, err := mail.ReadMessage(r)
if err != nil {
s.logger.Error().Err(err).Msg("unable to read message")
return ErrActionAborted
}
// extract content
html, text, err := extractMailContent(msg)
if err != nil {
s.logger.Error().Err(err).Msg("unable to read message content")
return ErrActionAborted
}
// extract headers
from, subject := extractMailHeader(msg.Header)
// build article
builder := model.NewArticleCreateFormBuilder()
builder.Origin(from)
builder.Title(subject)
if html != "" {
builder.HTML(html)
}
if text != "" {
builder.Text(text)
}
// create article
_, err = service.Lookup().CreateArticle(s.ctx, *builder.Build(), service.ArticleCreationOptions{IgnoreHydrateError: true})
if err != nil {
s.logger.Error().Err(err).Msg("unable to create article")
return ErrActionAborted
}
return nil
}
func (s *Session) Reset() {
s.ctx = context.Background()
}
func (s *Session) Logout() error {
s.ctx = nil
return nil
}
func NewSession() *Session {
logger := log.With().Str("component", "mail").Logger()
return &Session{
from: "",
ctx: context.Background(),
logger: logger,
}
}