aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-28 10:18:47 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-28 10:18:47 +0100
commit30a5cdc2a3088995a6ab1521d6b97715ec0a36f5 (patch)
treefb6fe41d315ff46a9f764afc3524314cd2c0c6a9
parent11963aaf3d7f681b27cd1e48f596cb1d5ca9d349 (diff)
downloadeasybridge-30a5cdc2a3088995a6ab1521d6b97715ec0a36f5.tar.gz
easybridge-30a5cdc2a3088995a6ab1521d6b97715ec0a36f5.zip
Refactor connector creation logic
-rw-r--r--account.go11
-rw-r--r--connector/config.go11
-rw-r--r--connector/irc/config.go47
-rw-r--r--connector/mattermost/config.go87
-rw-r--r--connector/xmpp/config.go59
-rw-r--r--util.go18
-rw-r--r--web.go2
7 files changed, 120 insertions, 115 deletions
diff --git a/account.go b/account.go
index c7f30f0..c975c6e 100644
--- a/account.go
+++ b/account.go
@@ -9,6 +9,11 @@ import (
log "github.com/sirupsen/logrus"
. "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 {
@@ -45,7 +50,11 @@ func SetAccount(mxid string, name string, protocol string, config map[string]str
go prev_acct.connect()
}
} else {
- conn := createConnector(protocol)
+ proto, ok := Protocols[protocol]
+ if !ok {
+ return fmt.Errorf("Invalid protocol: %s", protocol)
+ }
+ conn := proto.NewConnector()
if conn == nil {
return fmt.Errorf("Could not create connector for protocol %s", protocol)
}
diff --git a/connector/config.go b/connector/config.go
index 97e4556..0bf3614 100644
--- a/connector/config.go
+++ b/connector/config.go
@@ -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 ConfigEntry struct {
@@ -59,8 +64,8 @@ type ConfigEntry struct {
IsBoolean bool
}
-var Protocols = map[string]ConfigSchema{}
+var Protocols = map[string]Protocol{}
-func Register(name string, schema ConfigSchema) {
- Protocols[name] = schema
+func Register(name string, protocol Protocol) {
+ Protocols[name] = protocol
}
diff --git a/connector/irc/config.go b/connector/irc/config.go
index 26d9a63..33469ed 100644
--- a/connector/irc/config.go
+++ b/connector/irc/config.go
@@ -5,28 +5,31 @@ import (
)
func init() {
- Register("irc", ConfigSchema{
- &ConfigEntry{
- Name: "nick",
- Description: "Nickname",
- Required: true,
- },
- &ConfigEntry{
- Name: "server",
- Description: "Server",
- Required: true,
- },
- &ConfigEntry{
- Name: "port",
- Description: "Port",
- IsNumeric: true,
- Default: "6667",
- },
- &ConfigEntry{
- Name: "ssl",
- Description: "Use SSL",
- IsBoolean: true,
- Default: "false",
+ Register("irc", Protocol{
+ NewConnector: func() Connector { return &IRC{} },
+ Schema: ConfigSchema{
+ &ConfigEntry{
+ Name: "nick",
+ Description: "Nickname",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "server",
+ Description: "Server",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "port",
+ Description: "Port",
+ IsNumeric: true,
+ Default: "6667",
+ },
+ &ConfigEntry{
+ Name: "ssl",
+ Description: "Use SSL",
+ IsBoolean: true,
+ Default: "false",
+ },
},
})
}
diff --git a/connector/mattermost/config.go b/connector/mattermost/config.go
index b7c4ba8..dd3bbbb 100644
--- a/connector/mattermost/config.go
+++ b/connector/mattermost/config.go
@@ -5,48 +5,51 @@ import (
)
func init() {
- Register("mattermost", ConfigSchema{
- &ConfigEntry{
- Name: "server",
- Description: "Server",
- Required: true,
- },
- &ConfigEntry{
- Name: "username",
- Description: "Username",
- Required: true,
- },
- &ConfigEntry{
- Name: "password",
- Description: "Password",
- IsPassword: true,
- },
- &ConfigEntry{
- Name: "token",
- Description: "Authentification token (replaces password if set)",
- },
- &ConfigEntry{
- Name: "teams",
- Description: "Comma-separated list of teams to follow",
- Required: true,
- },
- &ConfigEntry{
- Name: "no_tls",
- Description: "Disable SSL/TLS",
- IsBoolean: true,
- Default: "false",
- },
- &ConfigEntry{
- Name: "initial_backlog",
- Description: "Maximum number of messages to load when joining a channel",
- IsNumeric: true,
- Default: "1000",
- },
- &ConfigEntry{
- Name: "initial_members",
- Description: "Maximum number of members to load when joining a channel",
- IsNumeric: true,
- Default: "100",
+ Register("mattermost", Protocol{
+ NewConnector: func() Connector { return &Mattermost{} },
+ Schema: ConfigSchema{
+ &ConfigEntry{
+ Name: "server",
+ Description: "Server",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "username",
+ Description: "Username",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "password",
+ Description: "Password",
+ IsPassword: true,
+ },
+ &ConfigEntry{
+ Name: "token",
+ Description: "Authentification token (replaces password if set)",
+ },
+ &ConfigEntry{
+ Name: "teams",
+ Description: "Comma-separated list of teams to follow",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "no_tls",
+ Description: "Disable SSL/TLS",
+ IsBoolean: true,
+ Default: "false",
+ },
+ &ConfigEntry{
+ Name: "initial_backlog",
+ Description: "Maximum number of messages to load when joining a channel",
+ IsNumeric: true,
+ Default: "1000",
+ },
+ &ConfigEntry{
+ Name: "initial_members",
+ Description: "Maximum number of members to load when joining a channel",
+ IsNumeric: true,
+ Default: "100",
+ },
},
})
}
diff --git a/connector/xmpp/config.go b/connector/xmpp/config.go
index a5306b4..a6abfac 100644
--- a/connector/xmpp/config.go
+++ b/connector/xmpp/config.go
@@ -5,34 +5,37 @@ import (
)
func init() {
- Register("xmpp", ConfigSchema{
- &ConfigEntry{
- Name: "jid",
- Description: "JID",
- Required: true,
- },
- &ConfigEntry{
- Name: "password",
- Description: "Password",
- Required: true,
- IsPassword: true,
- },
- &ConfigEntry{
- Name: "nickname",
- Description: "Nickname in MUCs",
- Required: true,
- },
- &ConfigEntry{
- Name: "port",
- Description: "Port",
- IsNumeric: true,
- Default: "5222",
- },
- &ConfigEntry{
- Name: "ssl",
- Description: "Use SSL",
- IsBoolean: true,
- Default: "true",
+ Register("xmpp", Protocol{
+ NewConnector: func() Connector { return &XMPP{} },
+ Schema: ConfigSchema{
+ &ConfigEntry{
+ Name: "jid",
+ Description: "JID",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "password",
+ Description: "Password",
+ Required: true,
+ IsPassword: true,
+ },
+ &ConfigEntry{
+ Name: "nickname",
+ Description: "Nickname in MUCs",
+ Required: true,
+ },
+ &ConfigEntry{
+ Name: "port",
+ Description: "Port",
+ IsNumeric: true,
+ Default: "5222",
+ },
+ &ConfigEntry{
+ Name: "ssl",
+ Description: "Use SSL",
+ IsBoolean: true,
+ Default: "true",
+ },
},
})
}
diff --git a/util.go b/util.go
index 6193d4d..323f99d 100644
--- a/util.go
+++ b/util.go
@@ -11,9 +11,6 @@ import (
"golang.org/x/crypto/nacl/secretbox"
. "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 = "✯◡✯"
@@ -101,18 +98,3 @@ func decryptAccountConfig(data string, key *[32]byte) (map[string]string, error)
err = json.Unmarshal(decoded, &config)
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
- }
-}
diff --git a/web.go b/web.go
index 8f64043..ce7fd0e 100644
--- a/web.go
+++ b/web.go
@@ -237,7 +237,7 @@ func configForm(w http.ResponseWriter, r *http.Request,
Protocol: protocol,
Config: map[string]string{},
Errors: map[string]string{},
- Schema: connector.Protocols[protocol],
+ Schema: connector.Protocols[protocol].Schema,
}
for k, v := range prevConfig {
data.Config[k] = v