projects/sievemgr

view cmd/sievemgr/internal/config/parser.go @ 19:854167f55839

Fix SRV record lookup

Do not use a configuration file default value for port in order to detect when
it is not set by the user so that SRV record lookup is triggered.
Depend on version 0.8.1 of go.guido-berhoerster.org/managesieve which contains
an additional necessary fix.
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Wed Feb 03 13:34:44 2021 +0100 (8 months ago)
parents 4dff4c3f0fbb
children
line source
1 // Copyright (C) 2020 Guido Berhoerster <guido+sievemgr@berhoerster.name>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 package config
24 import (
25 "fmt"
26 "strconv"
27 )
29 type parser struct {
30 s *scanner
31 }
33 func (p *parser) scanType(expectedTok token) (string, error) {
34 tok, lit, err := p.s.scan()
35 if err != nil {
36 return lit, NewParserError(err.Error(), p.s.line)
37 } else if tok != expectedTok {
38 return lit, NewParserError(fmt.Sprintf("expected %s, got %s %q", expectedTok, tok, lit), p.s.line)
39 }
40 return lit, nil
41 }
43 func (p *parser) validateAccount(acct *Account) error {
44 if acct.Host == "" {
45 return NewParserError(fmt.Sprintf("no host specified for account %q",
46 acct.Name), 0)
47 } else if acct.User == "" {
48 return NewParserError(fmt.Sprintf("no user specified for account %q",
49 acct.Name), 0)
50 }
51 return nil
52 }
54 func (p *parser) parse(conf *Configuration) error {
55 var acct *Account
56 var isDefault bool
57 var tok token
58 var lit string
59 var err error
60 parsing:
61 for {
62 tok, lit, err = p.s.scan()
63 if err != nil {
64 err = NewParserError(err.Error(), p.s.line)
65 break parsing
66 }
67 if tok == tokenIllegal {
68 err = NewParserError(fmt.Sprintf("illegal token %q", lit), p.s.line)
69 break parsing
70 } else if tok == tokenEOF {
71 break parsing
72 } else if tok != tokenIdent {
73 err = NewParserError(fmt.Sprintf("expected identifier, got %s %q", tok, lit), p.s.line)
74 break parsing
75 }
76 switch lit {
77 case "account":
78 if acct != nil {
79 if err = p.validateAccount(acct); err != nil {
80 break parsing
81 }
82 conf.Accounts = append(conf.Accounts, acct)
83 if isDefault {
84 conf.Default = acct
85 isDefault = false
86 }
87 }
89 // account name
90 if lit, err = p.scanType(tokenString); err != nil {
91 break parsing
92 }
93 acct = &Account{
94 Name: lit,
95 }
96 case "default":
97 isDefault = true
98 case "host":
99 if lit, err = p.scanType(tokenString); err != nil {
100 break parsing
101 }
102 acct.Host = lit
103 case "port":
104 if lit, err = p.scanType(tokenNumber); err != nil {
105 break parsing
106 }
107 var port int
108 if port, err = strconv.Atoi(lit); err != nil {
109 err = NewParserError(fmt.Sprintf("failed to parse port: %s", err), p.s.line)
110 break parsing
111 } else if port < 1 || port > 65535 {
112 err = NewParserError(fmt.Sprintf("invalid port number %d", port), p.s.line)
113 break parsing
114 }
115 acct.Port = lit
116 case "user":
117 if lit, err = p.scanType(tokenString); err != nil {
118 break parsing
119 }
120 acct.User = lit
121 case "pass":
122 if lit, err = p.scanType(tokenString); err != nil {
123 break parsing
124 }
125 acct.Password = lit
126 case "insecure":
127 acct.Insecure = true
128 default:
129 err = NewParserError(fmt.Sprintf("unknown %s: %q", tok, lit), p.s.line)
130 break parsing
131 }
132 }
133 if err != nil {
134 return err
135 }
136 if acct != nil {
137 if err = p.validateAccount(acct); err != nil {
138 return err
139 }
140 conf.Accounts = append(conf.Accounts, acct)
141 if isDefault {
142 conf.Default = acct
143 }
144 }
146 return nil
147 }