Skip to content

Commit

Permalink
feat(GODT-2201): IMAP Copy & Move Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
LBeernaertProton committed Feb 14, 2023
1 parent 6aac246 commit ddda34b
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
47 changes: 47 additions & 0 deletions imap/command/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package command

import (
"fmt"
"github.com/ProtonMail/gluon/imap/parser"
)

type CopyCommand struct {
SeqSet []SeqRange
Mailbox string
}

func (l CopyCommand) String() string {
return fmt.Sprintf("COPY %v '%v'", l.SeqSet, l.Mailbox)
}

func (l CopyCommand) SanitizedString() string {
return fmt.Sprintf("COPY %v '%v'", l.SeqSet, sanitizeString(l.Mailbox))
}

type CopyCommandParser struct{}

func (CopyCommandParser) FromParser(p *parser.Parser) (Payload, error) {
// copy = "COPY" SP sequence-set SP mailbox
if err := p.Consume(parser.TokenTypeSP, "expected space after command"); err != nil {
return nil, err
}

seqSet, err := ParseSeqSet(p)
if err != nil {
return nil, err
}

if err := p.Consume(parser.TokenTypeSP, "expected space after seqset"); err != nil {
return nil, err
}

mailbox, err := p.ParseMailbox()
if err != nil {
return nil, err
}

return &CopyCommand{
SeqSet: seqSet,
Mailbox: mailbox,
}, nil
}
25 changes: 25 additions & 0 deletions imap/command/copy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"bytes"
"github.com/ProtonMail/gluon/imap/parser"
"github.com/stretchr/testify/require"
"testing"
)

func TestParser_CopyCommand(t *testing.T) {
input := toIMAPLine(`tag COPY 1:* INBOX`)
s := parser.NewScanner(bytes.NewReader(input))
p := NewParser(s)

expected := Command{Tag: "tag", Payload: &CopyCommand{
Mailbox: "INBOX",
SeqSet: []SeqRange{{Begin: 1, End: SeqNumValueAsterisk}},
}}

cmd, err := p.Parse()
require.NoError(t, err)
require.Equal(t, expected, cmd)
require.Equal(t, "copy", p.LastParsedCommand())
require.Equal(t, "tag", p.LastParsedTag())
}
47 changes: 47 additions & 0 deletions imap/command/move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package command

import (
"fmt"
"github.com/ProtonMail/gluon/imap/parser"
)

type MoveCommand struct {
SeqSet []SeqRange
Mailbox string
}

func (l MoveCommand) String() string {
return fmt.Sprintf("MOVE %v '%v'", l.SeqSet, l.Mailbox)
}

func (l MoveCommand) SanitizedString() string {
return fmt.Sprintf("MOVE %v '%v'", l.SeqSet, sanitizeString(l.Mailbox))
}

type MoveCommandParser struct{}

func (MoveCommandParser) FromParser(p *parser.Parser) (Payload, error) {
// move = "MOVE" SP sequence-set SP mailbox
if err := p.Consume(parser.TokenTypeSP, "expected space after command"); err != nil {
return nil, err
}

seqSet, err := ParseSeqSet(p)
if err != nil {
return nil, err
}

if err := p.Consume(parser.TokenTypeSP, "expected space after seqset"); err != nil {
return nil, err
}

mailbox, err := p.ParseMailbox()
if err != nil {
return nil, err
}

return &MoveCommand{
SeqSet: seqSet,
Mailbox: mailbox,
}, nil
}
25 changes: 25 additions & 0 deletions imap/command/move_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"bytes"
"github.com/ProtonMail/gluon/imap/parser"
"github.com/stretchr/testify/require"
"testing"
)

func TestParser_MoveCommand(t *testing.T) {
input := toIMAPLine(`tag MOVE 1:* INBOX`)
s := parser.NewScanner(bytes.NewReader(input))
p := NewParser(s)

expected := Command{Tag: "tag", Payload: &MoveCommand{
Mailbox: "INBOX",
SeqSet: []SeqRange{{Begin: 1, End: SeqNumValueAsterisk}},
}}

cmd, err := p.Parse()
require.NoError(t, err)
require.Equal(t, expected, cmd)
require.Equal(t, "move", p.LastParsedCommand())
require.Equal(t, "tag", p.LastParsedTag())
}
2 changes: 2 additions & 0 deletions imap/command/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func NewParserWithLiteralContinuationCb(s *parser.Scanner, cb func() error) *Par
"lsub": &LSubCommandParser{},
"login": &LoginCommandParser{},
"store": &StoreCommandParser{},
"copy": &CopyCommandParser{},
"move": &MoveCommandParser{},
},
}
}
Expand Down

0 comments on commit ddda34b

Please sign in to comment.