/
cmdlist.go
79 lines (61 loc) · 1.44 KB
/
cmdlist.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
package session
import (
"fmt"
log "github.com/sirupsen/logrus"
)
type cmdlist struct {
ses *Session
tokens []string
pe processEntry
}
func newCmdList(ses *Session, tokens []string, pe processEntry) *cmdlist {
return &cmdlist{
ses: ses,
tokens: tokens,
pe: pe,
}
}
func (cmd *cmdlist) requireAuth() *cmdlist {
if cmd.pe == nil {
return cmd
}
log.WithFields(log.Fields{"cmd": cmd}).Debug("session::cmdList::requireAuth called")
if !cmd.ses.id.Authenticated() {
cmd.ses.sendStatement("530 Please login with USER and PASS.")
cmd.pe = nil
return cmd
}
return cmd
}
func (cmd *cmdlist) requirePASV() *cmdlist {
if cmd.pe == nil {
return cmd
}
log.WithFields(log.Fields{"cmd": cmd}).Debug("session::cmdList::requirePASV called")
if cmd.ses.lastDataChanneler == nil || cmd.ses.lastDataChanneler.IsClosed() {
cmd.ses.sendStatement("425 Use PASV or EPSV first")
cmd.pe = nil
return cmd
}
return cmd
}
func (cmd *cmdlist) resetREST() *cmdlist {
cmd.ses.lastREST = 0
return cmd
}
func (cmd *cmdlist) resetUSER() *cmdlist {
if !cmd.ses.id.Authenticated() {
cmd.ses.id.SetUsername("")
}
return cmd
}
func (cmd *cmdlist) Execute() bool {
log.WithFields(log.Fields{"cmd": cmd}).Debug("session::cmdList::Execute called")
if cmd.pe == nil {
return false
}
return cmd.pe(cmd.tokens)
}
func (cmd *cmdlist) String() string {
return fmt.Sprintf("{ses:%v, tokens:%v, pe:%v}", cmd.ses, cmd.tokens, cmd.pe)
}