Refactor connector creation logic

This commit is contained in:
Alex 2020-02-28 10:18:47 +01:00
parent 11963aaf3d
commit 30a5cdc2a3
7 changed files with 120 additions and 115 deletions

View File

@ -9,6 +9,11 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
// Necessary for them to register their protocols
_ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
_ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/mattermost"
_ "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
) )
type Account struct { type Account struct {
@ -45,7 +50,11 @@ func SetAccount(mxid string, name string, protocol string, config map[string]str
go prev_acct.connect() go prev_acct.connect()
} }
} else { } else {
conn := createConnector(protocol) proto, ok := Protocols[protocol]
if !ok {
return fmt.Errorf("Invalid protocol: %s", protocol)
}
conn := proto.NewConnector()
if conn == nil { if conn == nil {
return fmt.Errorf("Could not create connector for protocol %s", protocol) return fmt.Errorf("Could not create connector for protocol %s", protocol)
} }

View File

@ -46,6 +46,11 @@ func (c Configuration) GetBool(k string, deflt ...bool) (bool, error) {
// ---- // ----
type Protocol struct {
NewConnector func() Connector
Schema ConfigSchema
}
type ConfigSchema []*ConfigEntry type ConfigSchema []*ConfigEntry
type ConfigEntry struct { type ConfigEntry struct {
@ -59,8 +64,8 @@ type ConfigEntry struct {
IsBoolean bool IsBoolean bool
} }
var Protocols = map[string]ConfigSchema{} var Protocols = map[string]Protocol{}
func Register(name string, schema ConfigSchema) { func Register(name string, protocol Protocol) {
Protocols[name] = schema Protocols[name] = protocol
} }

View File

@ -5,28 +5,31 @@ import (
) )
func init() { func init() {
Register("irc", ConfigSchema{ Register("irc", Protocol{
&ConfigEntry{ NewConnector: func() Connector { return &IRC{} },
Name: "nick", Schema: ConfigSchema{
Description: "Nickname", &ConfigEntry{
Required: true, Name: "nick",
}, Description: "Nickname",
&ConfigEntry{ Required: true,
Name: "server", },
Description: "Server", &ConfigEntry{
Required: true, Name: "server",
}, Description: "Server",
&ConfigEntry{ Required: true,
Name: "port", },
Description: "Port", &ConfigEntry{
IsNumeric: true, Name: "port",
Default: "6667", Description: "Port",
}, IsNumeric: true,
&ConfigEntry{ Default: "6667",
Name: "ssl", },
Description: "Use SSL", &ConfigEntry{
IsBoolean: true, Name: "ssl",
Default: "false", Description: "Use SSL",
IsBoolean: true,
Default: "false",
},
}, },
}) })
} }

View File

@ -5,48 +5,51 @@ import (
) )
func init() { func init() {
Register("mattermost", ConfigSchema{ Register("mattermost", Protocol{
&ConfigEntry{ NewConnector: func() Connector { return &Mattermost{} },
Name: "server", Schema: ConfigSchema{
Description: "Server", &ConfigEntry{
Required: true, Name: "server",
}, Description: "Server",
&ConfigEntry{ Required: true,
Name: "username", },
Description: "Username", &ConfigEntry{
Required: true, Name: "username",
}, Description: "Username",
&ConfigEntry{ Required: true,
Name: "password", },
Description: "Password", &ConfigEntry{
IsPassword: true, Name: "password",
}, Description: "Password",
&ConfigEntry{ IsPassword: true,
Name: "token", },
Description: "Authentification token (replaces password if set)", &ConfigEntry{
}, Name: "token",
&ConfigEntry{ Description: "Authentification token (replaces password if set)",
Name: "teams", },
Description: "Comma-separated list of teams to follow", &ConfigEntry{
Required: true, Name: "teams",
}, Description: "Comma-separated list of teams to follow",
&ConfigEntry{ Required: true,
Name: "no_tls", },
Description: "Disable SSL/TLS", &ConfigEntry{
IsBoolean: true, Name: "no_tls",
Default: "false", Description: "Disable SSL/TLS",
}, IsBoolean: true,
&ConfigEntry{ Default: "false",
Name: "initial_backlog", },
Description: "Maximum number of messages to load when joining a channel", &ConfigEntry{
IsNumeric: true, Name: "initial_backlog",
Default: "1000", Description: "Maximum number of messages to load when joining a channel",
}, IsNumeric: true,
&ConfigEntry{ Default: "1000",
Name: "initial_members", },
Description: "Maximum number of members to load when joining a channel", &ConfigEntry{
IsNumeric: true, Name: "initial_members",
Default: "100", Description: "Maximum number of members to load when joining a channel",
IsNumeric: true,
Default: "100",
},
}, },
}) })
} }

View File

@ -5,34 +5,37 @@ import (
) )
func init() { func init() {
Register("xmpp", ConfigSchema{ Register("xmpp", Protocol{
&ConfigEntry{ NewConnector: func() Connector { return &XMPP{} },
Name: "jid", Schema: ConfigSchema{
Description: "JID", &ConfigEntry{
Required: true, Name: "jid",
}, Description: "JID",
&ConfigEntry{ Required: true,
Name: "password", },
Description: "Password", &ConfigEntry{
Required: true, Name: "password",
IsPassword: true, Description: "Password",
}, Required: true,
&ConfigEntry{ IsPassword: true,
Name: "nickname", },
Description: "Nickname in MUCs", &ConfigEntry{
Required: true, Name: "nickname",
}, Description: "Nickname in MUCs",
&ConfigEntry{ Required: true,
Name: "port", },
Description: "Port", &ConfigEntry{
IsNumeric: true, Name: "port",
Default: "5222", Description: "Port",
}, IsNumeric: true,
&ConfigEntry{ Default: "5222",
Name: "ssl", },
Description: "Use SSL", &ConfigEntry{
IsBoolean: true, Name: "ssl",
Default: "true", Description: "Use SSL",
IsBoolean: true,
Default: "true",
},
}, },
}) })
} }

18
util.go
View File

@ -11,9 +11,6 @@ import (
"golang.org/x/crypto/nacl/secretbox" "golang.org/x/crypto/nacl/secretbox"
. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/irc"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/mattermost"
"git.deuxfleurs.fr/Deuxfleurs/easybridge/connector/xmpp"
) )
const EASYBRIDGE_SYSTEM_PROTOCOL string = "✯◡✯" const EASYBRIDGE_SYSTEM_PROTOCOL string = "✯◡✯"
@ -101,18 +98,3 @@ func decryptAccountConfig(data string, key *[32]byte) (map[string]string, error)
err = json.Unmarshal(decoded, &config) err = json.Unmarshal(decoded, &config)
return config, err return config, err
} }
// ----
func createConnector(protocol string) Connector {
switch protocol {
case "irc":
return &irc.IRC{}
case "xmpp":
return &xmpp.XMPP{}
case "mattermost":
return &mattermost.Mattermost{}
default:
return nil
}
}

2
web.go
View File

@ -237,7 +237,7 @@ func configForm(w http.ResponseWriter, r *http.Request,
Protocol: protocol, Protocol: protocol,
Config: map[string]string{}, Config: map[string]string{},
Errors: map[string]string{}, Errors: map[string]string{},
Schema: connector.Protocols[protocol], Schema: connector.Protocols[protocol].Schema,
} }
for k, v := range prevConfig { for k, v := range prevConfig {
data.Config[k] = v data.Config[k] = v