1package imapserver
2
3import (
4 "errors"
5 "io"
6 "os"
7 "time"
8
9 "github.com/mjl-/bstore"
10
11 "github.com/mjl-/mox/message"
12 "github.com/mjl-/mox/mlog"
13 "github.com/mjl-/mox/store"
14)
15
16// Replace relaces a message for another, atomically, possibly in another mailbox,
17// without needing a sequence of: append message, store \deleted flag, expunge.
18//
19// State: Selected
20func (c *conn) cmdxReplace(isUID bool, tag, cmd string, p *parser) {
21 // Command: ../rfc/8508:158 ../rfc/8508:198
22
23 // Request syntax: ../rfc/8508:471
24 p.xspace()
25 star := p.take("*")
26 var num uint32
27 if !star {
28 num = p.xnznumber()
29 }
30 p.xspace()
31 name := p.xmailbox()
32
33 // ../rfc/4466:473
34 p.xspace()
35 var storeFlags store.Flags
36 var keywords []string
37 if p.hasPrefix("(") {
38 // Error must be a syntax error, to properly abort the connection due to literal.
39 var err error
40 storeFlags, keywords, err = store.ParseFlagsKeywords(p.xflagList())
41 if err != nil {
42 xsyntaxErrorf("parsing flags: %v", err)
43 }
44 p.xspace()
45 }
46
47 var tm time.Time
48 if p.hasPrefix(`"`) {
49 tm = p.xdateTime()
50 p.xspace()
51 } else {
52 tm = time.Now()
53 }
54
55 // todo: only with utf8 should we we accept message headers with utf-8. we currently always accept them.
56 // todo: this is only relevant if we also support the CATENATE extension?
57 // ../rfc/6855:204
58 utf8 := p.take("UTF8 (")
59 if utf8 {
60 p.xtake("~")
61 }
62 // Always allow literal8, for binary extension. ../rfc/4466:486
63 // For utf8, we already consumed the required ~ above.
64 size, synclit := p.xliteralSize(!utf8, false)
65
66 // Check the request, including old message in database, whether the message fits
67 // in quota. If a non-nil func is returned, an error was found. Calling the
68 // function aborts handling this command.
69 var uidOld store.UID
70 checkMessage := func(tx *bstore.Tx) func() {
71 if c.readonly {
72 return func() { xuserErrorf("mailbox open in read-only mode") }
73 }
74
75 mb, err := c.account.MailboxFind(tx, name)
76 if err != nil {
77 return func() { xserverErrorf("finding mailbox: %v", err) }
78 }
79 if mb == nil {
80 return func() { xusercodeErrorf("TRYCREATE", "%w", store.ErrUnknownMailbox) }
81 }
82
83 // Resolve "*" for UID or message sequence.
84 if star {
85 if len(c.uids) == 0 {
86 return func() { xuserErrorf("cannot use * on empty mailbox") }
87 }
88 if isUID {
89 num = uint32(c.uids[len(c.uids)-1])
90 } else {
91 num = uint32(len(c.uids))
92 }
93 star = false
94 }
95
96 // Find or verify UID of message to replace.
97 var seq msgseq
98 if isUID {
99 seq = c.sequence(store.UID(num))
100 if seq <= 0 {
101 return func() { xuserErrorf("unknown uid %d", num) }
102 }
103 } else if num > uint32(len(c.uids)) {
104 return func() { xuserErrorf("invalid msgseq") }
105 } else {
106 seq = msgseq(num)
107 }
108
109 uidOld = c.uids[int(seq)-1]
110
111 // Check the message still exists in the database. If it doesn't, it may have been
112 // deleted just now and we won't check the quota. We'll raise an error later on,
113 // when we are not possibly reading a sync literal and can respond with unsolicited
114 // expunges.
115 q := bstore.QueryTx[store.Message](tx)
116 q.FilterNonzero(store.Message{MailboxID: c.mailboxID, UID: uidOld})
117 q.FilterEqual("Expunged", false)
118 _, err = q.Get()
119 if err == bstore.ErrAbsent {
120 return nil
121 }
122 if err != nil {
123 return func() { xserverErrorf("get message to replace: %v", err) }
124 }
125
126 // Check if we can add size bytes. We can't necessarily remove the current message yet.
127 ok, maxSize, err := c.account.CanAddMessageSize(tx, size)
128 if err != nil {
129 return func() { xserverErrorf("check quota: %v", err) }
130 }
131 if !ok {
132 // ../rfc/9208:472
133 return func() { xusercodeErrorf("OVERQUOTA", "account over maximum total message size %d", maxSize) }
134 }
135 return nil
136 }
137
138 var errfn func()
139 if synclit {
140 // Check request, if it cannot succeed, fail it now before client is sending the data.
141
142 name = xcheckmailboxname(name, true)
143
144 c.account.WithRLock(func() {
145 c.xdbread(func(tx *bstore.Tx) {
146 errfn = checkMessage(tx)
147 if errfn != nil {
148 errfn()
149 }
150 })
151 })
152
153 c.writelinef("+ ")
154 } else {
155 var err error
156 name, _, err = store.CheckMailboxName(name, true)
157 if err != nil {
158 errfn = func() { xusercodeErrorf("CANNOT", "%s", err) }
159 } else {
160 c.account.WithRLock(func() {
161 c.xdbread(func(tx *bstore.Tx) {
162 errfn = checkMessage(tx)
163 })
164 })
165 }
166 }
167
168 var file *os.File
169 var newID int64 // Delivered message ID, file removed on error.
170 var f io.Writer
171 var commit bool
172
173 if errfn != nil {
174 // We got a non-sync literal, we will consume some data, but abort if there's too
175 // much. We draw the line at 1mb. Client should have used synchronizing literal.
176 if size > 1000*1000 {
177 // ../rfc/9051:357 ../rfc/3501:347
178 err := errors.New("error condition and non-synchronizing literal too big")
179 bye := "* BYE [ALERT] " + err.Error()
180 panic(syntaxError{bye, "TOOBIG", err.Error(), err})
181 }
182 // Message will not be accepted.
183 f = io.Discard
184 } else {
185 // Read the message into a temporary file.
186 var err error
187 file, err = store.CreateMessageTemp(c.log, "imap-replace")
188 xcheckf(err, "creating temp file for message")
189 defer store.CloseRemoveTempFile(c.log, file, "temporary message file")
190 f = file
191
192 defer func() {
193 if !commit && newID != 0 {
194 p := c.account.MessagePath(newID)
195 err := os.Remove(p)
196 c.xsanity(err, "remove message file for replace after error")
197 }
198 }()
199 }
200
201 // Read the message data.
202 defer c.xtrace(mlog.LevelTracedata)()
203 mw := message.NewWriter(f)
204 msize, err := io.Copy(mw, io.LimitReader(c.br, size))
205 c.xtrace(mlog.LevelTrace) // Restore.
206 if err != nil {
207 // Cannot use xcheckf due to %w handling of errIO.
208 c.xbrokenf("reading literal message: %s (%w)", err, errIO)
209 }
210 if msize != size {
211 c.xbrokenf("read %d bytes for message, expected %d (%w)", msize, size, errIO)
212 }
213
214 // Finish reading the command.
215 line := c.readline(false)
216 p = newParser(line, c)
217 if utf8 {
218 p.xtake(")")
219 }
220 p.xempty()
221
222 // If an error was found earlier, abort the command now that we've read the message.
223 if errfn != nil {
224 errfn()
225 }
226
227 var oldMsgExpunged bool
228
229 var om, nm store.Message
230 var mbSrc, mbDst store.Mailbox // Src and dst mailboxes can be different. ../rfc/8508:263
231 var pendingChanges []store.Change
232
233 c.account.WithWLock(func() {
234 var changes []store.Change
235
236 c.xdbwrite(func(tx *bstore.Tx) {
237 mbSrc = c.xmailboxID(tx, c.mailboxID)
238
239 // Get old message. If it has been expunged, we should have a pending change for
240 // it. We'll send untagged responses and fail the command.
241 var err error
242 qom := bstore.QueryTx[store.Message](tx)
243 qom.FilterNonzero(store.Message{MailboxID: mbSrc.ID, UID: uidOld})
244 om, err = qom.Get()
245 xcheckf(err, "get old message to replace from database")
246 if om.Expunged {
247 oldMsgExpunged = true
248 return
249 }
250
251 // Check quota for addition of new message. We can't necessarily yet remove the old message.
252 ok, maxSize, err := c.account.CanAddMessageSize(tx, mw.Size)
253 xcheckf(err, "checking quota")
254 if !ok {
255 // ../rfc/9208:472
256 xusercodeErrorf("OVERQUOTA", "account over maximum total message size %d", maxSize)
257 }
258
259 modseq, err := c.account.NextModSeq(tx)
260 xcheckf(err, "get next mod seq")
261
262 chremuids, _, err := c.account.MessageRemove(c.log, tx, modseq, &mbSrc, store.RemoveOpts{}, om)
263 xcheckf(err, "expunge old message")
264 changes = append(changes, chremuids)
265 // Note: we only add a mbSrc counts change later on, if it is not equal to mbDst.
266
267 err = tx.Update(&mbSrc)
268 xcheckf(err, "updating source mailbox counts")
269
270 mbDst = c.xmailbox(tx, name, "TRYCREATE")
271 mbDst.ModSeq = modseq
272
273 nkeywords := len(mbDst.Keywords)
274
275 // Make new message to deliver.
276 nm = store.Message{
277 MailboxID: mbDst.ID,
278 MailboxOrigID: mbDst.ID,
279 Received: tm,
280 Flags: storeFlags,
281 Keywords: keywords,
282 Size: mw.Size,
283 ModSeq: modseq,
284 CreateSeq: modseq,
285 }
286
287 err = c.account.MessageAdd(c.log, tx, &mbDst, &nm, file, store.AddOpts{})
288 xcheckf(err, "delivering message")
289 newID = nm.ID
290
291 changes = append(changes, nm.ChangeAddUID(), mbDst.ChangeCounts())
292 if nkeywords != len(mbDst.Keywords) {
293 changes = append(changes, mbDst.ChangeKeywords())
294 }
295
296 err = tx.Update(&mbDst)
297 xcheckf(err, "updating destination mailbox")
298 })
299
300 // Fetch pending changes, possibly with new UIDs, so we can apply them before adding our own new UID.
301 pendingChanges = c.comm.Get()
302
303 if oldMsgExpunged {
304 return
305 }
306
307 // Success, make sure messages aren't cleaned up anymore.
308 commit = true
309
310 // Broadcast the change to other connections.
311 if mbSrc.ID != mbDst.ID {
312 changes = append(changes, mbSrc.ChangeCounts())
313 }
314 c.broadcast(changes)
315 })
316
317 // Must update our msgseq/uids tracking with latest pending changes.
318 c.applyChanges(pendingChanges, false)
319
320 // If we couldn't find the message, send a NO response. We've just applied pending
321 // changes, which should have expunged the absent message.
322 if oldMsgExpunged {
323 xuserErrorf("message to be replaced has been expunged")
324 }
325
326 // If the destination mailbox is our currently selected mailbox, we register and
327 // announce the new message.
328 if mbDst.ID == c.mailboxID {
329 c.uidAppend(nm.UID)
330 // We send an untagged OK with APPENDUID, for sane bookkeeping in clients. ../rfc/8508:401
331 c.bwritelinef("* OK [APPENDUID %d %d] ", mbDst.UIDValidity, nm.UID)
332 c.bwritelinef("* %d EXISTS", len(c.uids))
333 }
334
335 // We must return vanished instead of expunge, and also highestmodseq, when qresync
336 // was enabled. ../rfc/8508:422 ../rfc/7162:1883
337 qresync := c.enabled[capQresync]
338
339 // Now that we are in sync with msgseq, we can find our old msgseq and say it is
340 // expunged or vanished. ../rfc/7162:1900
341 omsgseq := c.xsequence(om.UID)
342 c.sequenceRemove(omsgseq, om.UID)
343 if qresync {
344 c.bwritelinef("* VANISHED %d", om.UID)
345 // ../rfc/7162:1916
346 } else {
347 c.bwritelinef("* %d EXPUNGE", omsgseq)
348 }
349 c.writeresultf("%s OK [HIGHESTMODSEQ %d] replaced", tag, nm.ModSeq.Client())
350}
351