-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.go
105 lines (96 loc) · 2.24 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
package main
import (
"fmt"
"github.com/eternal-flame-AD/gotify-broadcast/rules"
)
// ChannelDef is the definition of a channel in the configuration
type ChannelDef struct {
Name string `yaml:"name"`
Public bool `yaml:"public"`
}
// Config is user plugin configuration
type Config struct {
Channels []ChannelDef `yaml:"channels"`
SenderFilter rules.RuleChain `yaml:"sender_filter"`
ReceiverFilter rules.RuleChain `yaml:"receiver_filter"`
}
// DefaultConfig implements plugin.Configurer
func (c *Plugin) DefaultConfig() interface{} {
return &Config{
Channels: []ChannelDef{
ChannelDef{
Name: "example",
Public: false,
},
},
SenderFilter: rules.RuleChain{
rules.Rule{
Match: rules.MatchSet{
rules.Match{
Mode: rules.ModeUserName,
UserName: "my_server",
},
rules.Match{
Mode: rules.ModeMessageText,
Regex: true,
MessageText: "^\\[(INFO|DEBUG)\\]",
},
},
Action: rules.Reject,
},
rules.Rule{
Match: rules.MatchSet{
rules.Match{
Mode: rules.ModeUserName,
UserName: "some_one_i_dont_want_to_see_broadcast_from",
},
},
Action: rules.Reject,
},
rules.Rule{
Match: rules.MatchSet{
rules.Match{
Mode: rules.ModeAny,
},
},
Action: rules.Accept,
},
},
ReceiverFilter: rules.RuleChain{
rules.Rule{
Match: rules.MatchSet{
rules.Match{
Mode: rules.ModeUserName,
UserName: "some_one_i_dont_want_to_send_broadcast_to",
},
},
Action: rules.Reject,
},
rules.Rule{
Match: rules.MatchSet{
rules.Match{
Mode: rules.ModeAny,
},
},
Action: rules.Accept,
},
},
}
}
// ValidateAndSetConfig implements plugin.Configurer
func (c *Plugin) ValidateAndSetConfig(config interface{}) error {
newConfig := config.(*Config)
if err := newConfig.ReceiverFilter.Check(); err != nil {
return err
}
channels := make(map[string]struct{})
for _, ch := range newConfig.Channels {
if _, ok := channels[ch.Name]; ok {
return fmt.Errorf("channel name %s is duplicated", ch.Name)
}
channels[ch.Name] = struct{}{}
}
publicChannels.UpdateChannelsForUser(c.UserCtx, newConfig.Channels)
c.config = newConfig
return nil
}