-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
154 lines (134 loc) · 5.93 KB
/
config.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// This file is part of goimapnotify
// Copyright (C) 2017-2024 Jorge Javier Araya Navarro
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
type EventType int
const (
NEWMAIL EventType = iota + 1
DELETEDMAIL
)
func (e EventType) String() string {
switch e {
case NEWMAIL:
return "New Email"
case DELETEDMAIL:
return "Deleted Email"
default:
return "Unknown Event"
}
}
type Configuration struct {
Configurations []NotifyConfig `json:"configurations" yaml:"configurations"`
}
// ConfigurationLegacy holds the old configuration format
type ConfigurationLegacy struct {
Host string `yaml:"host" json:"host"`
HostCMD string `yaml:"hostCMD" json:"hostCMD"`
Port int `yaml:"port" json:"port"`
TLS bool `yaml:"tls" json:"tls"`
TLSOptions TLSOptionsStruct `yaml:"tlsOptions" json:"tlsOptions"`
Username string `yaml:"username" json:"username"`
UsernameCMD string `yaml:"usernameCMD" json:"usernameCMD"`
Password string `yaml:"password" json:"password"`
PasswordCMD string `yaml:"passwordCMD" json:"passwordCMD"`
XOAuth2 bool `yaml:"xoAuth2" json:"xoAuth2"`
OnNewMail string `yaml:"onNewMail" json:"onNewMail"`
OnNewMailPost string `yaml:"onNewMailPost" json:"onNewMailPost"`
OnDeletedMail string `yaml:"onDeletedMail" json:"onDeletedMail"`
OnDeletedMailPost string `yaml:"onDeletedMailPost" json:"onDeletedMailPost"`
Debug bool `yaml:"-" json:"-"`
Boxes []string `yaml:"boxes" json:"boxes"`
}
// NotifyConfig holds the configuration
type NotifyConfig struct {
Host string `yaml:"host" json:"host"`
HostCMD string `yaml:"hostCMD" json:"hostCMD"`
Port int `yaml:"port" json:"port"`
TLS bool `yaml:"tls" json:"tls"`
TLSOptions TLSOptionsStruct `yaml:"tlsOptions" json:"tlsOptions"`
Username string `yaml:"username" json:"username"`
UsernameCMD string `yaml:"usernameCMD" json:"usernameCMD"`
Alias string `yaml:"alias" json:"alias"`
Password string `yaml:"password" json:"password"`
PasswordCMD string `yaml:"passwordCMD" json:"passwordCMD"`
XOAuth2 bool `yaml:"xoAuth2" json:"xoAuth2"`
OnNewMail string `yaml:"onNewMail" json:"onNewMail"`
OnNewMailPost string `yaml:"onNewMailPost" json:"onNewMailPost"`
OnDeletedMail string `yaml:"onDeletedMail" json:"onDeletedMail"`
OnDeletedMailPost string `yaml:"onDeletedMailPost" json:"onDeletedMailPost"`
Debug bool `yaml:"-" json:"-"`
Boxes []Box `yaml:"boxes" json:"boxes"`
}
type TLSOptionsStruct struct {
RejectUnauthorized bool `yaml:"rejectUnauthorized" json:"rejectUnauthorized"`
STARTTLS bool `yaml:"starttls" json:"starttls"`
}
/*
Box stores all the necessary info needed to be passed in an
IDLEEvent handler routine, in order to schedule commands and
print informative messages
*/
type Box struct {
Alias string `json:"-" yaml:"-"`
Mailbox string `yaml:"mailbox" json:"mailbox"`
Reason EventType `json:"-" yaml:"-"`
OnNewMail string `yaml:"onNewMail" json:"onNewMail"`
OnNewMailPost string `yaml:"onNewMailPost" json:"onNewMailPost"`
OnDeletedMail string `yaml:"onDeletedMail" json:"onDeletedMail"`
OnDeletedMailPost string `yaml:"onDeletedMailPost" json:"onDeletedMailPost"`
ExistingEmail uint32 `json:"-" yaml:"-"`
}
func legacyConverter(conf ConfigurationLegacy) []NotifyConfig {
var r []NotifyConfig
var c NotifyConfig
c.Host = conf.Host
c.HostCMD = conf.HostCMD
c.Port = conf.Port
c.TLS = conf.TLS
c.TLSOptions = conf.TLSOptions
c.Username = conf.Username
c.UsernameCMD = conf.UsernameCMD
c.Password = conf.Password
c.PasswordCMD = conf.PasswordCMD
c.XOAuth2 = conf.XOAuth2
c.OnNewMail = conf.OnNewMail
c.OnNewMailPost = conf.OnNewMailPost
c.OnDeletedMail = conf.OnDeletedMail
c.OnDeletedMailPost = conf.OnDeletedMailPost
for _, mailbox := range conf.Boxes {
c.Boxes = append(c.Boxes, Box{Mailbox: mailbox})
}
return append(r, c)
}
func loadConfiguration(path string) (Configuration, error) {
var topConfiguration Configuration
if err := viper.Unmarshal(&topConfiguration); err != nil {
return Configuration{}, fmt.Errorf("Can't parse the configuration: %s, error: %v", path, err)
}
if topConfiguration.Configurations == nil {
var legacy ConfigurationLegacy
if err := viper.UnmarshalExact(&legacy); err != nil {
return Configuration{}, fmt.Errorf("Can't parse the configuration in 'legacy' format: %s, error: %v", path, err)
}
logrus.Info("legacy format configuration detected")
topConfiguration.Configurations = legacyConverter(legacy)
}
if len(topConfiguration.Configurations) > 0 && (topConfiguration.Configurations[0].Host == "" && topConfiguration.Configurations[0].HostCMD == "") {
return Configuration{}, fmt.Errorf("configuration file '%s' is empty or have invalid configuration format", path)
}
return topConfiguration, nil
}