Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New config services #68

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
263 changes: 263 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
package config

import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
text "text/template"

"gopkg.in/yaml.v3"
)

var (
basepath string
bot Config
mutex sync.RWMutex
servers []ServerConfig
)

type reaction struct {
name string `yaml:"name"`
id string `yaml:"id,omitempty"`
}

type Response struct {
Message string `yaml:"message,omitempty"`
Reaction *reaction `yaml:"reaction,omitempty"`
tmpl *text.Template `yaml:"-"`
}

func (r *Response) MessageTemplate() *text.Template {
return r.tmpl
}

func (r *Response) ReactionString() (string, bool) {
if r.Reaction != nil {
if r.Reaction.id != "" {
return r.Reaction.name + ":" + r.Reaction.id, true
}
return r.Reaction.name, true
}

return "", false
}

type Command struct {
Response `yaml:",inline"`
Name string `yaml:"name"`
allowedChannels []string `yaml:"allowed_channels,omitempty"`
allowedRoles []string `yaml:"allowed_roles,omitempty"`
}

func (c *Command) HasChannel(id string) bool {
if len(c.allowedChannels) == 0 {
return true
}

for _, ch := range c.allowedChannels {
if ch == id {
return true
}
}

return false
}

func (c *Command) HasRole(id string) bool {
if len(c.allowedRoles) == 0 {
return true
}

for _, ch := range c.allowedRoles {
if ch == id {
return true
}
}

return false
}

type DMCommand struct {
Response `yaml:",inline"`
Name string `yaml:"name"`
}

type Trigger struct {
Response `yaml:",inline"`
Pattern string `yaml:"pattern"`
Delete bool `yaml:"delete,omitempty"`
}

type ParseConfig struct {
FileTypes []string `yaml:"allowed_filetypes"`
Sites []string `yaml:"allowed_sites"`
Max int `yaml:"max"`
OnParse Response `yaml:"on_parse,omitempty"`
OnMaxed Response `yaml:"on_maxed,omitempty"`
}

func (p *ParseConfig) HasFileType(t string) bool {
for _, f := range p.FileTypes {
if f == t {
return true
}
}

return false
}

func (p *ParseConfig) HasSite(s string) bool {
for _, u := range p.Sites {
if u == s {
return true
}
}

return false
}

type ServerConfig struct {
ID string `yaml:"server_id"`
AllowedChannels []int64 `yaml:"allowed_channels"`
AllowedRoles []int64 `yaml:"allowed_roles"`

OnMention Response `yaml:"on_mention"`
Commands []*Command `yaml:"commands"`
Triggers []*Trigger `yaml:"triggers"`
Parsing ParseConfig `yaml:"parsing"`
}

type IRCConfig struct {
Host string `yaml:"host"`
Port string `yaml:"port"`
Ident string `yaml:"ident"`
Password string `yaml:"password"`
RealName string `yaml:"real_name"`
Nickname string `yaml:"nickname"`
}

func (i *IRCConfig) Address() string {
return i.Host + ":" + i.Port
}

type Config struct {
DiscordToken string `yaml:"discord_token"`
IRC *IRCConfig `yaml:"irc,omitempty"`
Prefix string `yaml:"prefix"`
Status string `yaml:"status"`
DeleteInvocation bool `yaml:"delete_invocation,omitempty"`
DMCommands []DMCommand `yaml:"dm_commands,omitempty"`
}

func init() {
if basepath == "" {
p := os.Getenv("PTRON_CONFIGS")
if p == "" {
cwd, _ := os.Getwd()
p = filepath.Join(cwd, "configs")
}
basepath = p
}
}

func LoadBot() (*Config, error) {
path := filepath.Join(basepath, "bot.yml")
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
path = filepath.Join(basepath, "bot.yaml")
_, err = os.Stat(path)
}

if err != nil {
return nil, err
}
}

buf, err := os.ReadFile(path)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(buf, &bot)

return &bot, err
}

func LoadServers() (int, error) {
path := filepath.Join(basepath, "discord")
dir, err := os.ReadDir(path)
if err != nil {
return 0, err
}

for _, e := range dir {
if e.IsDir() {
continue
}

if strings.HasSuffix(e.Name(), ".yml") || strings.HasSuffix(e.Name(), ".yaml") {
buf, err := os.ReadFile(filepath.Join(path, e.Name()))
if err != nil {
return 0, err
}

var srv ServerConfig
if err := yaml.Unmarshal(buf, &srv); err != nil {
return 0, err
}

if srv.OnMention.Message != "" {
tmpl, err := text.New(srv.ID).Parse(srv.OnMention.Message)
if err == nil {
srv.OnMention.tmpl = tmpl
}
}

for _, c := range srv.Commands {
if c.Message != "" {
tmpl, err := text.New(c.Name).Parse(c.Message)
if err != nil {
continue
}
c.tmpl = tmpl
}
}

for i, t := range srv.Triggers {
if t.Message != "" {
n := srv.ID + ".t." + strconv.Itoa(i)
tmpl, err := text.New(n).Parse(t.Message)
if err != nil {
continue
}
t.tmpl = tmpl
}
}

servers = append(servers, srv)
}
}

return len(servers), nil
}

func GetServers() []ServerConfig {
mutex.RLock()
defer mutex.RUnlock()

return servers
}

func GetServer(id string) *ServerConfig {
mutex.RLock()
defer mutex.RUnlock()

for _, s := range servers {
if s.ID == id {
return &s
}
}

return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ require (
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gopkg.in/errgo.v2 v2.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
mvdan.cc/xurls/v2 v2.5.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mvdan.cc/xurls/v2 v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
mvdan.cc/xurls/v2 v2.4.0/go.mod h1:+GEjq9uNjqs8LQfM9nVnM8rff0OQ5Iash5rzX+N1CSg=
mvdan.cc/xurls/v2 v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
Expand Down
Loading