mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 12:50:27 +00:00
Use viper (github.com/spf13/viper) for configuration
This commit is contained in:
parent
25a72113b1
commit
f2f1d874e1
@ -15,7 +15,7 @@ import (
|
|||||||
type Api struct {
|
type Api struct {
|
||||||
Messages ring.Ring
|
Messages ring.Ring
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiMessage struct {
|
type ApiMessage struct {
|
||||||
@ -26,27 +26,27 @@ type ApiMessage struct {
|
|||||||
Gateway string `json:"gateway"`
|
Gateway string `json:"gateway"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Api{BridgeConfig: cfg}
|
b := &Api{Config: cfg}
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.HideBanner = true
|
e.HideBanner = true
|
||||||
e.HidePort = true
|
e.HidePort = true
|
||||||
b.Messages = ring.Ring{}
|
b.Messages = ring.Ring{}
|
||||||
b.Messages.SetCapacity(b.Config.Buffer)
|
b.Messages.SetCapacity(b.GetInt("Buffer"))
|
||||||
if b.Config.Token != "" {
|
if b.GetString("Token") != "" {
|
||||||
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
||||||
return key == b.Config.Token, nil
|
return key == b.GetString("Token"), nil
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
e.GET("/api/messages", b.handleMessages)
|
e.GET("/api/messages", b.handleMessages)
|
||||||
e.GET("/api/stream", b.handleStream)
|
e.GET("/api/stream", b.handleStream)
|
||||||
e.POST("/api/message", b.handlePostMessage)
|
e.POST("/api/message", b.handlePostMessage)
|
||||||
go func() {
|
go func() {
|
||||||
if b.Config.BindAddress == "" {
|
if b.GetString("BindAddress") == "" {
|
||||||
b.Log.Fatalf("No BindAddress configured.")
|
b.Log.Fatalf("No BindAddress configured.")
|
||||||
}
|
}
|
||||||
b.Log.Infof("Listening on %s", b.Config.BindAddress)
|
b.Log.Infof("Listening on %s", b.GetString("BindAddress"))
|
||||||
b.Log.Fatal(e.Start(b.Config.BindAddress))
|
b.Log.Fatal(e.Start(b.GetString("BindAddress")))
|
||||||
}()
|
}()
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ type Bridger interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Bridge struct {
|
type Bridge struct {
|
||||||
Config config.Protocol
|
|
||||||
Bridger
|
Bridger
|
||||||
Name string
|
Name string
|
||||||
Account string
|
Account string
|
||||||
@ -23,10 +22,19 @@ type Bridge struct {
|
|||||||
Channels map[string]config.ChannelInfo
|
Channels map[string]config.ChannelInfo
|
||||||
Joined map[string]bool
|
Joined map[string]bool
|
||||||
Log *log.Entry
|
Log *log.Entry
|
||||||
|
Config *config.Config
|
||||||
|
General *config.Protocol
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
// General *config.Protocol
|
||||||
|
Remote chan config.Message
|
||||||
|
Log *log.Entry
|
||||||
|
*Bridge
|
||||||
}
|
}
|
||||||
|
|
||||||
// Factory is the factory function to create a bridge
|
// Factory is the factory function to create a bridge
|
||||||
type Factory func(*config.BridgeConfig) Bridger
|
type Factory func(*Config) Bridger
|
||||||
|
|
||||||
func New(bridge *config.Bridge) *Bridge {
|
func New(bridge *config.Bridge) *Bridge {
|
||||||
b := new(Bridge)
|
b := new(Bridge)
|
||||||
@ -59,3 +67,42 @@ func (b *Bridge) joinChannels(channels map[string]config.ChannelInfo, exists map
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) ReloadConfig() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) GetBool(key string) bool {
|
||||||
|
if b.Config.GetBool(b.Account+"."+key) != false {
|
||||||
|
return b.Config.GetBool(b.Account + "." + key)
|
||||||
|
}
|
||||||
|
return b.Config.GetBool("general." + key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) GetInt(key string) int {
|
||||||
|
if b.Config.GetInt(b.Account+"."+key) != 0 {
|
||||||
|
return b.Config.GetInt(b.Account + "." + key)
|
||||||
|
}
|
||||||
|
return b.Config.GetInt("general." + key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) GetString(key string) string {
|
||||||
|
if b.Config.GetString(b.Account+"."+key) != "" {
|
||||||
|
return b.Config.GetString(b.Account + "." + key)
|
||||||
|
}
|
||||||
|
return b.Config.GetString("general." + key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) GetStringSlice(key string) []string {
|
||||||
|
if len(b.Config.GetStringSlice(b.Account+"."+key)) != 0 {
|
||||||
|
return b.Config.GetStringSlice(b.Account + "." + key)
|
||||||
|
}
|
||||||
|
return b.Config.GetStringSlice("general." + key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) GetStringSlice2D(key string) [][]string {
|
||||||
|
if len(b.Config.GetStringSlice2D(b.Account+"."+key)) != 0 {
|
||||||
|
return b.Config.GetStringSlice2D(b.Account + "." + key)
|
||||||
|
}
|
||||||
|
return b.Config.GetStringSlice2D("general." + key)
|
||||||
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -141,7 +141,7 @@ type SameChannelGateway struct {
|
|||||||
Accounts []string
|
Accounts []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type ConfigValues struct {
|
||||||
Api map[string]Protocol
|
Api map[string]Protocol
|
||||||
Irc map[string]Protocol
|
Irc map[string]Protocol
|
||||||
Mattermost map[string]Protocol
|
Mattermost map[string]Protocol
|
||||||
@ -159,88 +159,78 @@ type Config struct {
|
|||||||
SameChannelGateway []SameChannelGateway
|
SameChannelGateway []SameChannelGateway
|
||||||
}
|
}
|
||||||
|
|
||||||
type BridgeConfig struct {
|
type Config struct {
|
||||||
Config Protocol
|
v *viper.Viper
|
||||||
General *Protocol
|
*ConfigValues
|
||||||
Account string
|
sync.RWMutex
|
||||||
Remote chan Message
|
|
||||||
Log *log.Entry
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(cfgfile string) *Config {
|
func NewConfig(cfgfile string) *Config {
|
||||||
var cfg Config
|
var cfg ConfigValues
|
||||||
if _, err := toml.DecodeFile(cfgfile, &cfg); err != nil {
|
viper.SetConfigType("toml")
|
||||||
|
viper.SetEnvPrefix("matterbridge")
|
||||||
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
f, err := os.Open(cfgfile)
|
||||||
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fail := false
|
err = viper.ReadConfig(f)
|
||||||
for k, v := range cfg.Mattermost {
|
if err != nil {
|
||||||
res := Deprecated(v, "mattermost."+k)
|
log.Fatal(err)
|
||||||
if res {
|
|
||||||
fail = res
|
|
||||||
}
|
}
|
||||||
|
err = viper.Unmarshal(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("blah", err)
|
||||||
}
|
}
|
||||||
for k, v := range cfg.Slack {
|
mycfg := new(Config)
|
||||||
res := Deprecated(v, "slack."+k)
|
mycfg.v = viper.GetViper()
|
||||||
if res {
|
|
||||||
fail = res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for k, v := range cfg.Rocketchat {
|
|
||||||
res := Deprecated(v, "rocketchat."+k)
|
|
||||||
if res {
|
|
||||||
fail = res
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if fail {
|
|
||||||
log.Fatalf("Fix your config. Please see changelog for more information")
|
|
||||||
}
|
|
||||||
if cfg.General.MediaDownloadSize == 0 {
|
if cfg.General.MediaDownloadSize == 0 {
|
||||||
cfg.General.MediaDownloadSize = 1000000
|
cfg.General.MediaDownloadSize = 1000000
|
||||||
}
|
}
|
||||||
return &cfg
|
mycfg.ConfigValues = &cfg
|
||||||
|
return mycfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func OverrideCfgFromEnv(cfg *Config, protocol string, account string) {
|
func (c *Config) GetBool(key string) bool {
|
||||||
var protoCfg Protocol
|
c.RLock()
|
||||||
val := reflect.ValueOf(cfg).Elem()
|
defer c.RUnlock()
|
||||||
// loop over the Config struct
|
// log.Debugf("getting bool %s = %#v", key, c.v.GetBool(key))
|
||||||
for i := 0; i < val.NumField(); i++ {
|
return c.v.GetBool(key)
|
||||||
typeField := val.Type().Field(i)
|
|
||||||
// look for the protocol map (both lowercase)
|
|
||||||
if strings.ToLower(typeField.Name) == protocol {
|
|
||||||
// get the Protocol struct from the map
|
|
||||||
data := val.Field(i).MapIndex(reflect.ValueOf(account))
|
|
||||||
protoCfg = data.Interface().(Protocol)
|
|
||||||
protoStruct := reflect.ValueOf(&protoCfg).Elem()
|
|
||||||
// loop over the found protocol struct
|
|
||||||
for i := 0; i < protoStruct.NumField(); i++ {
|
|
||||||
typeField := protoStruct.Type().Field(i)
|
|
||||||
// build our environment key (eg MATTERBRIDGE_MATTERMOST_WORK_LOGIN)
|
|
||||||
key := "matterbridge_" + protocol + "_" + account + "_" + typeField.Name
|
|
||||||
key = strings.ToUpper(key)
|
|
||||||
// search the environment
|
|
||||||
res := os.Getenv(key)
|
|
||||||
// if it exists and the current field is a string
|
|
||||||
// then update the current field
|
|
||||||
if res != "" {
|
|
||||||
fieldVal := protoStruct.Field(i)
|
|
||||||
if fieldVal.Kind() == reflect.String {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"prefix": "config",
|
|
||||||
}).Infof("overriding %s from env with %s\n", key, res)
|
|
||||||
fieldVal.Set(reflect.ValueOf(res))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// update the map with the modified Protocol (cfg.Protocol[account] = Protocol)
|
|
||||||
val.Field(i).SetMapIndex(reflect.ValueOf(account), reflect.ValueOf(protoCfg))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIconURL(msg *Message, cfg *Protocol) string {
|
func (c *Config) GetInt(key string) int {
|
||||||
iconURL := cfg.IconURL
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
// log.Debugf("getting int %s = %d", key, c.v.GetInt(key))
|
||||||
|
return c.v.GetInt(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) GetString(key string) string {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
// log.Debugf("getting String %s = %s", key, c.v.GetString(key))
|
||||||
|
return c.v.GetString(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) GetStringSlice(key string) []string {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
// log.Debugf("getting StringSlice %s = %#v", key, c.v.GetStringSlice(key))
|
||||||
|
return c.v.GetStringSlice(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) GetStringSlice2D(key string) [][]string {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
if res, ok := c.v.Get(key).([][]string); ok {
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
// log.Debugf("getting StringSlice2D %s = %#v", key, c.v.Get(key))
|
||||||
|
return [][]string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetIconURL(msg *Message, iconURL string) string {
|
||||||
info := strings.Split(msg.Account, ".")
|
info := strings.Split(msg.Account, ".")
|
||||||
protocol := info[0]
|
protocol := info[0]
|
||||||
name := info[1]
|
name := info[1]
|
||||||
@ -249,17 +239,3 @@ func GetIconURL(msg *Message, cfg *Protocol) string {
|
|||||||
iconURL = strings.Replace(iconURL, "{PROTOCOL}", protocol, -1)
|
iconURL = strings.Replace(iconURL, "{PROTOCOL}", protocol, -1)
|
||||||
return iconURL
|
return iconURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func Deprecated(cfg Protocol, account string) bool {
|
|
||||||
if cfg.BindAddress != "" {
|
|
||||||
log.Printf("ERROR: %s BindAddress is deprecated, you need to change it to WebhookBindAddress.", account)
|
|
||||||
} else if cfg.URL != "" {
|
|
||||||
log.Printf("ERROR: %s URL is deprecated, you need to change it to WebhookURL.", account)
|
|
||||||
} else if cfg.UseAPI {
|
|
||||||
log.Printf("ERROR: %s UseAPI is deprecated, it's enabled by default, please remove it from your config file.", account)
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
//log.Fatalf("ERROR: Fix your config: %s", account)
|
|
||||||
}
|
|
||||||
|
@ -23,32 +23,33 @@ type Bdiscord struct {
|
|||||||
webhookToken string
|
webhookToken string
|
||||||
channelInfoMap map[string]*config.ChannelInfo
|
channelInfoMap map[string]*config.ChannelInfo
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Bdiscord{BridgeConfig: cfg}
|
b := &Bdiscord{Config: cfg}
|
||||||
b.userMemberMap = make(map[string]*discordgo.Member)
|
b.userMemberMap = make(map[string]*discordgo.Member)
|
||||||
b.channelInfoMap = make(map[string]*config.ChannelInfo)
|
b.channelInfoMap = make(map[string]*config.ChannelInfo)
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
b.Log.Debug("Configuring Discord Incoming Webhook")
|
b.Log.Debug("Configuring Discord Incoming Webhook")
|
||||||
b.webhookID, b.webhookToken = b.splitURL(b.Config.WebhookURL)
|
b.webhookID, b.webhookToken = b.splitURL(b.GetString("WebhookURL"))
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bdiscord) Connect() error {
|
func (b *Bdiscord) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
|
var token string
|
||||||
b.Log.Info("Connecting")
|
b.Log.Info("Connecting")
|
||||||
if b.Config.WebhookURL == "" {
|
if b.GetString("WebhookURL") == "" {
|
||||||
b.Log.Info("Connecting using token")
|
b.Log.Info("Connecting using token")
|
||||||
} else {
|
} else {
|
||||||
b.Log.Info("Connecting using webhookurl (for posting) and token")
|
b.Log.Info("Connecting using webhookurl (for posting) and token")
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(b.Config.Token, "Bot ") {
|
if !strings.HasPrefix(b.GetString("Token"), "Bot ") {
|
||||||
b.Config.Token = "Bot " + b.Config.Token
|
token = "Bot " + b.GetString("Token")
|
||||||
}
|
}
|
||||||
b.c, err = discordgo.New(b.Config.Token)
|
b.c, err = discordgo.New(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -71,7 +72,7 @@ func (b *Bdiscord) Connect() error {
|
|||||||
}
|
}
|
||||||
b.Nick = userinfo.Username
|
b.Nick = userinfo.Username
|
||||||
for _, guild := range guilds {
|
for _, guild := range guilds {
|
||||||
if guild.Name == b.Config.Server {
|
if guild.Name == b.GetString("Server") {
|
||||||
b.Channels, err = b.c.GuildChannels(guild.ID)
|
b.Channels, err = b.c.GuildChannels(guild.ID)
|
||||||
b.guildID = guild.ID
|
b.guildID = guild.ID
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -83,7 +84,7 @@ func (b *Bdiscord) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bdiscord) Disconnect() error {
|
func (b *Bdiscord) Disconnect() error {
|
||||||
return nil
|
return b.c.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {
|
func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {
|
||||||
@ -186,13 +187,13 @@ func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelet
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
|
func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
|
||||||
if b.Config.EditDisable {
|
if b.GetBool("EditDisable") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// only when message is actually edited
|
// only when message is actually edited
|
||||||
if m.Message.EditedTimestamp != "" {
|
if m.Message.EditedTimestamp != "" {
|
||||||
b.Log.Debugf("Sending edit message")
|
b.Log.Debugf("Sending edit message")
|
||||||
m.Content = m.Content + b.Config.EditSuffix
|
m.Content = m.Content + b.GetString("EditSuffix")
|
||||||
b.messageCreate(s, (*discordgo.MessageCreate)(m))
|
b.messageCreate(s, (*discordgo.MessageCreate)(m))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,14 +237,14 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set username
|
// set username
|
||||||
if !b.Config.UseUserName {
|
if !b.GetBool("UseUserName") {
|
||||||
rmsg.Username = b.getNick(m.Author)
|
rmsg.Username = b.getNick(m.Author)
|
||||||
} else {
|
} else {
|
||||||
rmsg.Username = m.Author.Username
|
rmsg.Username = m.Author.Username
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we have embedded content add it to text
|
// if we have embedded content add it to text
|
||||||
if b.Config.ShowEmbeds && m.Message.Embeds != nil {
|
if b.GetBool("ShowEmbeds") && m.Message.Embeds != nil {
|
||||||
for _, embed := range m.Message.Embeds {
|
for _, embed := range m.Message.Embeds {
|
||||||
rmsg.Text = rmsg.Text + "embed: " + embed.Title + " - " + embed.Description + " - " + embed.URL + "\n"
|
rmsg.Text = rmsg.Text + "embed: " + embed.Title + " - " + embed.Description + " - " + embed.URL + "\n"
|
||||||
}
|
}
|
||||||
@ -367,7 +368,7 @@ func (b *Bdiscord) splitURL(url string) (string, string) {
|
|||||||
|
|
||||||
// useWebhook returns true if we have a webhook defined somewhere
|
// useWebhook returns true if we have a webhook defined somewhere
|
||||||
func (b *Bdiscord) useWebhook() bool {
|
func (b *Bdiscord) useWebhook() bool {
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for _, channel := range b.channelInfoMap {
|
for _, channel := range b.channelInfoMap {
|
||||||
@ -380,8 +381,8 @@ func (b *Bdiscord) useWebhook() bool {
|
|||||||
|
|
||||||
// isWebhookID returns true if the specified id is used in a defined webhook
|
// isWebhookID returns true if the specified id is used in a defined webhook
|
||||||
func (b *Bdiscord) isWebhookID(id string) bool {
|
func (b *Bdiscord) isWebhookID(id string) bool {
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
wID, _ := b.splitURL(b.Config.WebhookURL)
|
wID, _ := b.splitURL(b.GetString("WebhookURL"))
|
||||||
if wID == id {
|
if wID == id {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -14,17 +14,17 @@ type Bgitter struct {
|
|||||||
User *gitter.User
|
User *gitter.User
|
||||||
Users []gitter.User
|
Users []gitter.User
|
||||||
Rooms []gitter.Room
|
Rooms []gitter.Room
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Bgitter{BridgeConfig: cfg}
|
return &Bgitter{Config: cfg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bgitter) Connect() error {
|
func (b *Bgitter) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
b.Log.Info("Connecting")
|
b.Log.Info("Connecting")
|
||||||
b.c = gitter.New(b.Config.Token)
|
b.c = gitter.New(b.GetString("Token"))
|
||||||
b.User, err = b.c.GetUser()
|
b.User, err = b.c.GetUser()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -29,24 +29,25 @@ type Birc struct {
|
|||||||
connected chan struct{}
|
connected chan struct{}
|
||||||
Local chan config.Message // local queue for flood control
|
Local chan config.Message // local queue for flood control
|
||||||
FirstConnection bool
|
FirstConnection bool
|
||||||
|
MessageDelay, MessageQueue, MessageLength int
|
||||||
|
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Birc{}
|
b := &Birc{}
|
||||||
b.BridgeConfig = cfg
|
b.Config = cfg
|
||||||
b.Nick = b.Config.Nick
|
b.Nick = b.GetString("Nick")
|
||||||
b.names = make(map[string][]string)
|
b.names = make(map[string][]string)
|
||||||
b.connected = make(chan struct{})
|
b.connected = make(chan struct{})
|
||||||
if b.Config.MessageDelay == 0 {
|
if b.GetInt("MessageDelay") == 0 {
|
||||||
b.Config.MessageDelay = 1300
|
b.MessageDelay = 1300
|
||||||
}
|
}
|
||||||
if b.Config.MessageQueue == 0 {
|
if b.GetInt("MessageQueue") == 0 {
|
||||||
b.Config.MessageQueue = 30
|
b.MessageQueue = 30
|
||||||
}
|
}
|
||||||
if b.Config.MessageLength == 0 {
|
if b.GetInt("MessageLength") == 0 {
|
||||||
b.Config.MessageLength = 400
|
b.MessageLength = 400
|
||||||
}
|
}
|
||||||
b.FirstConnection = true
|
b.FirstConnection = true
|
||||||
return b
|
return b
|
||||||
@ -63,9 +64,9 @@ func (b *Birc) Command(msg *config.Message) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Birc) Connect() error {
|
func (b *Birc) Connect() error {
|
||||||
b.Local = make(chan config.Message, b.Config.MessageQueue+10)
|
b.Local = make(chan config.Message, b.GetInt("MessageQueue")+10)
|
||||||
b.Log.Infof("Connecting %s", b.Config.Server)
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
||||||
server, portstr, err := net.SplitHostPort(b.Config.Server)
|
server, portstr, err := net.SplitHostPort(b.GetString("Server"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -74,7 +75,7 @@ func (b *Birc) Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// fix strict user handling of girc
|
// fix strict user handling of girc
|
||||||
user := b.Config.Nick
|
user := b.GetString("Nick")
|
||||||
for !girc.IsValidUser(user) {
|
for !girc.IsValidUser(user) {
|
||||||
if len(user) == 1 {
|
if len(user) == 1 {
|
||||||
user = "matterbridge"
|
user = "matterbridge"
|
||||||
@ -85,18 +86,18 @@ func (b *Birc) Connect() error {
|
|||||||
|
|
||||||
i := girc.New(girc.Config{
|
i := girc.New(girc.Config{
|
||||||
Server: server,
|
Server: server,
|
||||||
ServerPass: b.Config.Password,
|
ServerPass: b.GetString("Password"),
|
||||||
Port: port,
|
Port: port,
|
||||||
Nick: b.Config.Nick,
|
Nick: b.GetString("Nick"),
|
||||||
User: user,
|
User: user,
|
||||||
Name: b.Config.Nick,
|
Name: b.GetString("Nick"),
|
||||||
SSL: b.Config.UseTLS,
|
SSL: b.GetBool("UseTLS"),
|
||||||
TLSConfig: &tls.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, ServerName: server},
|
TLSConfig: &tls.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"), ServerName: server},
|
||||||
PingDelay: time.Minute,
|
PingDelay: time.Minute,
|
||||||
})
|
})
|
||||||
|
|
||||||
if b.Config.UseSASL {
|
if b.GetBool("UseSASL") {
|
||||||
i.Config.SASL = &girc.SASLPlain{b.Config.NickServNick, b.Config.NickServPassword}
|
i.Config.SASL = &girc.SASLPlain{b.GetString("NickServNick"), b.GetString("NickServPassword")}
|
||||||
}
|
}
|
||||||
|
|
||||||
i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection)
|
i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection)
|
||||||
@ -127,7 +128,7 @@ func (b *Birc) Connect() error {
|
|||||||
return fmt.Errorf("connection timed out")
|
return fmt.Errorf("connection timed out")
|
||||||
}
|
}
|
||||||
//i.Debug = false
|
//i.Debug = false
|
||||||
if b.Config.DebugLevel == 0 {
|
if b.GetInt("DebugLevel") == 0 {
|
||||||
i.Handlers.Clear(girc.ALL_EVENTS)
|
i.Handlers.Clear(girc.ALL_EVENTS)
|
||||||
}
|
}
|
||||||
go b.doSend()
|
go b.doSend()
|
||||||
@ -135,7 +136,7 @@ func (b *Birc) Connect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Birc) Disconnect() error {
|
func (b *Birc) Disconnect() error {
|
||||||
//b.i.Disconnect()
|
b.i.Close()
|
||||||
close(b.Local)
|
close(b.Local)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -164,9 +165,9 @@ func (b *Birc) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// convert to specified charset
|
// convert to specified charset
|
||||||
if b.Config.Charset != "" {
|
if b.GetString("Charset") != "" {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
w, err := charset.NewWriter(b.Config.Charset, buf)
|
w, err := charset.NewWriter(b.GetString("Charset"), buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.Errorf("charset from utf-8 conversion failed: %s", err)
|
b.Log.Errorf("charset from utf-8 conversion failed: %s", err)
|
||||||
return "", err
|
return "", err
|
||||||
@ -197,19 +198,19 @@ func (b *Birc) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// split long messages on messageLength, to avoid clipped messages #281
|
// split long messages on messageLength, to avoid clipped messages #281
|
||||||
if b.Config.MessageSplit {
|
if b.GetBool("MessageSplit") {
|
||||||
msg.Text = helper.SplitStringLength(msg.Text, b.Config.MessageLength)
|
msg.Text = helper.SplitStringLength(msg.Text, b.GetInt("MessageLength"))
|
||||||
}
|
}
|
||||||
for _, text := range strings.Split(msg.Text, "\n") {
|
for _, text := range strings.Split(msg.Text, "\n") {
|
||||||
if len(text) > b.Config.MessageLength {
|
if len(text) > b.MessageLength {
|
||||||
text = text[:b.Config.MessageLength-len(" <message clipped>")]
|
text = text[:b.MessageLength-len(" <message clipped>")]
|
||||||
if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError {
|
if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError {
|
||||||
text = text[:len(text)-size]
|
text = text[:len(text)-size]
|
||||||
}
|
}
|
||||||
text += " <message clipped>"
|
text += " <message clipped>"
|
||||||
}
|
}
|
||||||
if len(b.Local) < b.Config.MessageQueue {
|
if len(b.Local) < b.GetInt("MessageQueue") {
|
||||||
if len(b.Local) == b.Config.MessageQueue-1 {
|
if len(b.Local) == b.GetInt("MessageQueue")-1 {
|
||||||
text = text + " <message clipped>"
|
text = text + " <message clipped>"
|
||||||
}
|
}
|
||||||
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
|
||||||
@ -221,13 +222,14 @@ func (b *Birc) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Birc) doSend() {
|
func (b *Birc) doSend() {
|
||||||
rate := time.Millisecond * time.Duration(b.Config.MessageDelay)
|
rate := time.Millisecond * time.Duration(b.MessageDelay)
|
||||||
throttle := time.NewTicker(rate)
|
throttle := time.NewTicker(rate)
|
||||||
for msg := range b.Local {
|
for msg := range b.Local {
|
||||||
<-throttle.C
|
<-throttle.C
|
||||||
if msg.Event == config.EVENT_USER_ACTION {
|
if msg.Event == config.EVENT_USER_ACTION {
|
||||||
b.i.Cmd.Action(msg.Channel, msg.Username+msg.Text)
|
b.i.Cmd.Action(msg.Channel, msg.Username+msg.Text)
|
||||||
} else {
|
} else {
|
||||||
|
b.Log.Debugf("Sending to channel %s", msg.Channel)
|
||||||
b.i.Cmd.Message(msg.Channel, msg.Username+msg.Text)
|
b.i.Cmd.Message(msg.Channel, msg.Username+msg.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -277,7 +279,7 @@ func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
|
|||||||
channel := strings.ToLower(event.Params[0])
|
channel := strings.ToLower(event.Params[0])
|
||||||
if event.Command == "KICK" {
|
if event.Command == "KICK" {
|
||||||
b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
|
b.Log.Infof("Got kicked from %s by %s", channel, event.Source.Name)
|
||||||
time.Sleep(time.Duration(b.Config.RejoinDelay) * time.Second)
|
time.Sleep(time.Duration(b.GetInt("RejoinDelay")) * time.Second)
|
||||||
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
|
b.Remote <- config.Message{Username: "system", Text: "rejoin", Channel: channel, Account: b.Account, Event: config.EVENT_REJOIN_CHANNELS}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -299,15 +301,15 @@ func (b *Birc) handleJoinPart(client *girc.Client, event girc.Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Birc) handleNotice(client *girc.Client, event girc.Event) {
|
func (b *Birc) handleNotice(client *girc.Client, event girc.Event) {
|
||||||
if strings.Contains(event.String(), "This nickname is registered") && event.Source.Name == b.Config.NickServNick {
|
if strings.Contains(event.String(), "This nickname is registered") && event.Source.Name == b.GetString("NickServNick") {
|
||||||
b.i.Cmd.Message(b.Config.NickServNick, "IDENTIFY "+b.Config.NickServPassword)
|
b.i.Cmd.Message(b.GetString("NickServNick"), "IDENTIFY "+b.GetString("NickServPassword"))
|
||||||
} else {
|
} else {
|
||||||
b.handlePrivMsg(client, event)
|
b.handlePrivMsg(client, event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
|
func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
|
||||||
if b.Config.DebugLevel == 1 {
|
if b.GetInt("DebugLevel") == 1 {
|
||||||
if event.Command != "CLIENT_STATE_UPDATED" &&
|
if event.Command != "CLIENT_STATE_UPDATED" &&
|
||||||
event.Command != "CLIENT_GENERAL_UPDATED" {
|
event.Command != "CLIENT_GENERAL_UPDATED" {
|
||||||
b.Log.Debugf("%#v", event.String())
|
b.Log.Debugf("%#v", event.String())
|
||||||
@ -322,9 +324,9 @@ func (b *Birc) handleOther(client *girc.Client, event girc.Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Birc) handleOtherAuth(client *girc.Client, event girc.Event) {
|
func (b *Birc) handleOtherAuth(client *girc.Client, event girc.Event) {
|
||||||
if strings.EqualFold(b.Config.NickServNick, "Q@CServe.quakenet.org") {
|
if strings.EqualFold(b.GetString("NickServNick"), "Q@CServe.quakenet.org") {
|
||||||
b.Log.Debugf("Authenticating %s against %s", b.Config.NickServUsername, b.Config.NickServNick)
|
b.Log.Debugf("Authenticating %s against %s", b.GetString("NickServUsername"), b.GetString("NickServNick"))
|
||||||
b.i.Cmd.Message(b.Config.NickServNick, "AUTH "+b.Config.NickServUsername+" "+b.Config.NickServPassword)
|
b.i.Cmd.Message(b.GetString("NickServNick"), "AUTH "+b.GetString("NickServUsername")+" "+b.GetString("NickServPassword"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,7 +371,7 @@ func (b *Birc) handlePrivMsg(client *girc.Client, event girc.Event) {
|
|||||||
// start detecting the charset
|
// start detecting the charset
|
||||||
var r io.Reader
|
var r io.Reader
|
||||||
var err error
|
var err error
|
||||||
mycharset := b.Config.Charset
|
mycharset := b.GetString("Charset")
|
||||||
if mycharset == "" {
|
if mycharset == "" {
|
||||||
// detect what were sending so that we convert it to utf-8
|
// detect what were sending so that we convert it to utf-8
|
||||||
detector := chardet.NewTextDetector()
|
detector := chardet.NewTextDetector()
|
||||||
|
@ -18,26 +18,26 @@ type Bmatrix struct {
|
|||||||
UserID string
|
UserID string
|
||||||
RoomMap map[string]string
|
RoomMap map[string]string
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Bmatrix{BridgeConfig: cfg}
|
b := &Bmatrix{Config: cfg}
|
||||||
b.RoomMap = make(map[string]string)
|
b.RoomMap = make(map[string]string)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmatrix) Connect() error {
|
func (b *Bmatrix) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
b.Log.Infof("Connecting %s", b.Config.Server)
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
||||||
b.mc, err = matrix.NewClient(b.Config.Server, "", "")
|
b.mc, err = matrix.NewClient(b.GetString("Server"), "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
resp, err := b.mc.Login(&matrix.ReqLogin{
|
resp, err := b.mc.Login(&matrix.ReqLogin{
|
||||||
Type: "m.login.password",
|
Type: "m.login.password",
|
||||||
User: b.Config.Login,
|
User: b.GetString("Login"),
|
||||||
Password: b.Config.Password,
|
Password: b.GetString("Password"),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -162,7 +162,7 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove homeserver suffix if configured
|
// Remove homeserver suffix if configured
|
||||||
if b.Config.NoHomeServerSuffix {
|
if b.GetBool("NoHomeServerSuffix") {
|
||||||
re := regexp.MustCompile("(.*?):.*")
|
re := regexp.MustCompile("(.*?):.*")
|
||||||
rmsg.Username = re.ReplaceAllString(rmsg.Username, `$1`)
|
rmsg.Username = re.ReplaceAllString(rmsg.Username, `$1`)
|
||||||
}
|
}
|
||||||
@ -207,7 +207,7 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in
|
|||||||
if url, ok = content["url"].(string); !ok {
|
if url, ok = content["url"].(string); !ok {
|
||||||
return fmt.Errorf("url isn't a %T", url)
|
return fmt.Errorf("url isn't a %T", url)
|
||||||
}
|
}
|
||||||
url = strings.Replace(url, "mxc://", b.Config.Server+"/_matrix/media/v1/download/", -1)
|
url = strings.Replace(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/", -1)
|
||||||
|
|
||||||
if info, ok = content["info"].(map[string]interface{}); !ok {
|
if info, ok = content["info"].(map[string]interface{}); !ok {
|
||||||
return fmt.Errorf("info isn't a %T", info)
|
return fmt.Errorf("info isn't a %T", info)
|
||||||
|
@ -15,12 +15,13 @@ type Bmattermost struct {
|
|||||||
mh *matterhook.Client
|
mh *matterhook.Client
|
||||||
mc *matterclient.MMClient
|
mc *matterclient.MMClient
|
||||||
TeamID string
|
TeamID string
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
avatarMap map[string]string
|
avatarMap map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Bmattermost{BridgeConfig: cfg, avatarMap: make(map[string]string)}
|
b := &Bmattermost{Config: cfg, avatarMap: make(map[string]string)}
|
||||||
|
b.Log.Debugf("MATTERMOST %#v", b.General)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,19 +30,19 @@ func (b *Bmattermost) Command(cmd string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmattermost) Connect() error {
|
func (b *Bmattermost) Connect() error {
|
||||||
if b.Config.WebhookBindAddress != "" {
|
if b.GetString("WebhookBindAddress") != "" {
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
BindAddress: b.Config.WebhookBindAddress})
|
BindAddress: b.GetString("WebhookBindAddress")})
|
||||||
} else if b.Config.Token != "" {
|
} else if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (sending)")
|
b.Log.Info("Connecting using token (sending)")
|
||||||
err := b.apiLogin()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if b.Config.Login != "" {
|
} else if b.GetString("Login") != "" {
|
||||||
b.Log.Info("Connecting using login/password (sending)")
|
b.Log.Info("Connecting using login/password (sending)")
|
||||||
err := b.apiLogin()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -49,26 +50,26 @@ func (b *Bmattermost) Connect() error {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
BindAddress: b.Config.WebhookBindAddress})
|
BindAddress: b.GetString("WebhookBindAddress")})
|
||||||
}
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
b.Log.Info("Connecting using webhookurl (sending)")
|
b.Log.Info("Connecting using webhookurl (sending)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
DisableServer: true})
|
DisableServer: true})
|
||||||
if b.Config.Token != "" {
|
if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (receiving)")
|
b.Log.Info("Connecting using token (receiving)")
|
||||||
err := b.apiLogin()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
} else if b.Config.Login != "" {
|
} else if b.GetString("Login") != "" {
|
||||||
b.Log.Info("Connecting using login/password (receiving)")
|
b.Log.Info("Connecting using login/password (receiving)")
|
||||||
err := b.apiLogin()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -77,14 +78,14 @@ func (b *Bmattermost) Connect() error {
|
|||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
} else if b.Config.Token != "" {
|
} else if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (sending and receiving)")
|
b.Log.Info("Connecting using token (sending and receiving)")
|
||||||
err := b.apiLogin()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
} else if b.Config.Login != "" {
|
} else if b.GetString("Login") != "" {
|
||||||
b.Log.Info("Connecting using login/password (sending and receiving)")
|
b.Log.Info("Connecting using login/password (sending and receiving)")
|
||||||
err := b.apiLogin()
|
err := b.apiLogin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -92,7 +93,7 @@ func (b *Bmattermost) Connect() error {
|
|||||||
}
|
}
|
||||||
go b.handleMatter()
|
go b.handleMatter()
|
||||||
}
|
}
|
||||||
if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Login == "" && b.Config.Token == "" {
|
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" && b.GetString("Login") == "" && b.GetString("Token") == "" {
|
||||||
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token/Login/Password/Server/Team configured")
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token/Login/Password/Server/Team configured")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -104,7 +105,7 @@ func (b *Bmattermost) Disconnect() error {
|
|||||||
|
|
||||||
func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
|
func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
|
||||||
// we can only join channels using the API
|
// we can only join channels using the API
|
||||||
if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" {
|
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" {
|
||||||
id := b.mc.GetChannelId(channel.Name, "")
|
id := b.mc.GetChannelId(channel.Name, "")
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return fmt.Errorf("Could not find channel ID for channel %s", channel.Name)
|
return fmt.Errorf("Could not find channel ID for channel %s", channel.Name)
|
||||||
@ -128,7 +129,7 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use webhook to send the message
|
// Use webhook to send the message
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
return b.sendWebhook(msg)
|
return b.sendWebhook(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,7 +152,7 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepend nick if configured
|
// Prepend nick if configured
|
||||||
if b.Config.PrefixMessagesWithNick {
|
if b.GetBool("PrefixMessagesWithNick") {
|
||||||
msg.Text = msg.Username + msg.Text
|
msg.Text = msg.Username + msg.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,11 +167,11 @@ func (b *Bmattermost) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
func (b *Bmattermost) handleMatter() {
|
func (b *Bmattermost) handleMatter() {
|
||||||
messages := make(chan *config.Message)
|
messages := make(chan *config.Message)
|
||||||
if b.Config.WebhookBindAddress != "" {
|
if b.GetString("WebhookBindAddress") != "" {
|
||||||
b.Log.Debugf("Choosing webhooks based receiving")
|
b.Log.Debugf("Choosing webhooks based receiving")
|
||||||
go b.handleMatterHook(messages)
|
go b.handleMatterHook(messages)
|
||||||
} else {
|
} else {
|
||||||
if b.Config.Token != "" {
|
if b.GetString("Token") != "" {
|
||||||
b.Log.Debugf("Choosing token based receiving")
|
b.Log.Debugf("Choosing token based receiving")
|
||||||
} else {
|
} else {
|
||||||
b.Log.Debugf("Choosing login/password based receiving")
|
b.Log.Debugf("Choosing login/password based receiving")
|
||||||
@ -221,8 +222,8 @@ func (b *Bmattermost) handleMatterClient(messages chan *config.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create a text for bridges that don't support native editing
|
// create a text for bridges that don't support native editing
|
||||||
if message.Raw.Event == "post_edited" && !b.Config.EditDisable {
|
if message.Raw.Event == "post_edited" && !b.GetBool("EditDisable") {
|
||||||
rmsg.Text = message.Text + b.Config.EditSuffix
|
rmsg.Text = message.Text + b.GetString("EditSuffix")
|
||||||
}
|
}
|
||||||
|
|
||||||
if message.Raw.Event == "post_deleted" {
|
if message.Raw.Event == "post_deleted" {
|
||||||
@ -250,18 +251,18 @@ func (b *Bmattermost) handleMatterHook(messages chan *config.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bmattermost) apiLogin() error {
|
func (b *Bmattermost) apiLogin() error {
|
||||||
password := b.Config.Password
|
password := b.GetString("Password")
|
||||||
if b.Config.Token != "" {
|
if b.GetString("Token") != "" {
|
||||||
password = "MMAUTHTOKEN=" + b.Config.Token
|
password = "MMAUTHTOKEN=" + b.GetString("Token")
|
||||||
}
|
}
|
||||||
|
|
||||||
b.mc = matterclient.New(b.Config.Login, password, b.Config.Team, b.Config.Server)
|
b.mc = matterclient.New(b.GetString("Login"), password, b.GetString("Team"), b.GetString("Server"))
|
||||||
if b.General.Debug {
|
if b.GetBool("debug") {
|
||||||
b.mc.SetLogLevel("debug")
|
b.mc.SetLogLevel("debug")
|
||||||
}
|
}
|
||||||
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify
|
b.mc.SkipTLSVerify = b.GetBool("SkipTLSVerify")
|
||||||
b.mc.NoTLS = b.Config.NoTLS
|
b.mc.NoTLS = b.GetBool("NoTLS")
|
||||||
b.Log.Infof("Connecting %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server)
|
b.Log.Infof("Connecting %s (team: %s) on %s", b.GetString("Login"), b.GetString("Team"), b.GetString("Server"))
|
||||||
err := b.mc.Login()
|
err := b.mc.Login()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -344,7 +345,7 @@ func (b *Bmattermost) handleUploadFile(msg *config.Message) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
msg.Text = fi.Comment
|
msg.Text = fi.Comment
|
||||||
if b.Config.PrefixMessagesWithNick {
|
if b.GetBool("PrefixMessagesWithNick") {
|
||||||
msg.Text = msg.Username + msg.Text
|
msg.Text = msg.Username + msg.Text
|
||||||
}
|
}
|
||||||
res, err = b.mc.PostMessageWithFiles(channelID, msg.Text, []string{id})
|
res, err = b.mc.PostMessageWithFiles(channelID, msg.Text, []string{id})
|
||||||
@ -359,13 +360,13 @@ func (b *Bmattermost) sendWebhook(msg config.Message) (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Config.PrefixMessagesWithNick {
|
if b.GetBool("PrefixMessagesWithNick") {
|
||||||
msg.Text = msg.Username + msg.Text
|
msg.Text = msg.Username + msg.Text
|
||||||
}
|
}
|
||||||
if msg.Extra != nil {
|
if msg.Extra != nil {
|
||||||
// this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE
|
// this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: rmsg.Channel, UserName: rmsg.Username, Text: rmsg.Text, Props: make(map[string]interface{})}
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL"), Channel: rmsg.Channel, UserName: rmsg.Username, Text: rmsg.Text, Props: make(map[string]interface{})}
|
||||||
matterMessage.Props["matterbridge"] = true
|
matterMessage.Props["matterbridge"] = true
|
||||||
b.mh.Send(matterMessage)
|
b.mh.Send(matterMessage)
|
||||||
}
|
}
|
||||||
@ -381,7 +382,7 @@ func (b *Bmattermost) sendWebhook(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: msg.Channel, UserName: msg.Username, Text: msg.Text, Props: make(map[string]interface{})}
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL"), Channel: msg.Channel, UserName: msg.Username, Text: msg.Text, Props: make(map[string]interface{})}
|
||||||
if msg.Avatar != "" {
|
if msg.Avatar != "" {
|
||||||
matterMessage.IconURL = msg.Avatar
|
matterMessage.IconURL = msg.Avatar
|
||||||
}
|
}
|
||||||
@ -406,7 +407,7 @@ func (b *Bmattermost) skipMessage(message *matterclient.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle edited messages
|
// Handle edited messages
|
||||||
if (message.Raw.Event == "post_edited") && b.Config.EditDisable {
|
if (message.Raw.Event == "post_edited") && b.GetBool("EditDisable") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,11 +15,11 @@ type MMhook struct {
|
|||||||
|
|
||||||
type Brocketchat struct {
|
type Brocketchat struct {
|
||||||
MMhook
|
MMhook
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Brocketchat{BridgeConfig: cfg}
|
return &Brocketchat{Config: cfg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Brocketchat) Command(cmd string) string {
|
func (b *Brocketchat) Command(cmd string) string {
|
||||||
@ -28,10 +28,10 @@ func (b *Brocketchat) Command(cmd string) string {
|
|||||||
|
|
||||||
func (b *Brocketchat) Connect() error {
|
func (b *Brocketchat) Connect() error {
|
||||||
b.Log.Info("Connecting webhooks")
|
b.Log.Info("Connecting webhooks")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
DisableServer: true})
|
DisableServer: true})
|
||||||
b.rh = rockethook.New(b.Config.WebhookURL, rockethook.Config{BindAddress: b.Config.WebhookBindAddress})
|
b.rh = rockethook.New(b.GetString("WebhookURL"), rockethook.Config{BindAddress: b.GetString("WebhookBindAddress")})
|
||||||
go b.handleRocketHook()
|
go b.handleRocketHook()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ func (b *Brocketchat) Send(msg config.Message) (string, error) {
|
|||||||
b.Log.Debugf("=> Receiving %#v", msg)
|
b.Log.Debugf("=> Receiving %#v", msg)
|
||||||
if msg.Extra != nil {
|
if msg.Extra != nil {
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: rmsg.Channel, UserName: rmsg.Username,
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL"), Channel: rmsg.Channel, UserName: rmsg.Username,
|
||||||
Text: rmsg.Text}
|
Text: rmsg.Text}
|
||||||
b.mh.Send(matterMessage)
|
b.mh.Send(matterMessage)
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ func (b *Brocketchat) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL")}
|
||||||
matterMessage.Channel = msg.Channel
|
matterMessage.Channel = msg.Channel
|
||||||
matterMessage.UserName = msg.Username
|
matterMessage.UserName = msg.Username
|
||||||
matterMessage.Type = ""
|
matterMessage.Type = ""
|
||||||
@ -85,7 +85,7 @@ func (b *Brocketchat) handleRocketHook() {
|
|||||||
message := b.rh.Receive()
|
message := b.rh.Receive()
|
||||||
b.Log.Debugf("Receiving from rockethook %#v", message)
|
b.Log.Debugf("Receiving from rockethook %#v", message)
|
||||||
// do not loop
|
// do not loop
|
||||||
if message.UserName == b.Config.Nick {
|
if message.UserName == b.GetString("Nick") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.UserName, b.Account)
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", message.UserName, b.Account)
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
"html"
|
"html"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,13 +24,14 @@ type Bslack struct {
|
|||||||
Usergroups []slack.UserGroup
|
Usergroups []slack.UserGroup
|
||||||
si *slack.Info
|
si *slack.Info
|
||||||
channels []slack.Channel
|
channels []slack.Channel
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageDeleted = "message_deleted"
|
const messageDeleted = "message_deleted"
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Bslack{BridgeConfig: cfg}
|
return &Bslack{Config: cfg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bslack) Command(cmd string) string {
|
func (b *Bslack) Command(cmd string) string {
|
||||||
@ -37,64 +39,65 @@ func (b *Bslack) Command(cmd string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bslack) Connect() error {
|
func (b *Bslack) Connect() error {
|
||||||
if b.Config.WebhookBindAddress != "" {
|
b.RLock()
|
||||||
if b.Config.WebhookURL != "" {
|
defer b.RUnlock()
|
||||||
|
if b.GetString("WebhookBindAddress") != "" {
|
||||||
|
if b.GetString("WebhookURL") != "" {
|
||||||
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookurl (sending) and webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
BindAddress: b.Config.WebhookBindAddress})
|
BindAddress: b.GetString("WebhookBindAddress")})
|
||||||
} else if b.Config.Token != "" {
|
} else if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (sending)")
|
b.Log.Info("Connecting using token (sending)")
|
||||||
b.sc = slack.New(b.Config.Token)
|
b.sc = slack.New(b.GetString("Token"))
|
||||||
b.rtm = b.sc.NewRTM()
|
b.rtm = b.sc.NewRTM()
|
||||||
go b.rtm.ManageConnection()
|
go b.rtm.ManageConnection()
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
BindAddress: b.Config.WebhookBindAddress})
|
BindAddress: b.GetString("WebhookBindAddress")})
|
||||||
} else {
|
} else {
|
||||||
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
b.Log.Info("Connecting using webhookbindaddress (receiving)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
BindAddress: b.Config.WebhookBindAddress})
|
BindAddress: b.GetString("WebhookBindAddress")})
|
||||||
}
|
}
|
||||||
go b.handleSlack()
|
go b.handleSlack()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
b.Log.Info("Connecting using webhookurl (sending)")
|
b.Log.Info("Connecting using webhookurl (sending)")
|
||||||
b.mh = matterhook.New(b.Config.WebhookURL,
|
b.mh = matterhook.New(b.GetString("WebhookURL"),
|
||||||
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
|
matterhook.Config{InsecureSkipVerify: b.GetBool("SkipTLSVerify"),
|
||||||
DisableServer: true})
|
DisableServer: true})
|
||||||
if b.Config.Token != "" {
|
if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (receiving)")
|
b.Log.Info("Connecting using token (receiving)")
|
||||||
b.sc = slack.New(b.Config.Token)
|
b.sc = slack.New(b.GetString("Token"))
|
||||||
b.rtm = b.sc.NewRTM()
|
b.rtm = b.sc.NewRTM()
|
||||||
go b.rtm.ManageConnection()
|
go b.rtm.ManageConnection()
|
||||||
go b.handleSlack()
|
go b.handleSlack()
|
||||||
}
|
}
|
||||||
} else if b.Config.Token != "" {
|
} else if b.GetString("Token") != "" {
|
||||||
b.Log.Info("Connecting using token (sending and receiving)")
|
b.Log.Info("Connecting using token (sending and receiving)")
|
||||||
b.sc = slack.New(b.Config.Token)
|
b.sc = slack.New(b.GetString("Token"))
|
||||||
b.rtm = b.sc.NewRTM()
|
b.rtm = b.sc.NewRTM()
|
||||||
go b.rtm.ManageConnection()
|
go b.rtm.ManageConnection()
|
||||||
go b.handleSlack()
|
go b.handleSlack()
|
||||||
}
|
}
|
||||||
if b.Config.WebhookBindAddress == "" && b.Config.WebhookURL == "" && b.Config.Token == "" {
|
if b.GetString("WebhookBindAddress") == "" && b.GetString("WebhookURL") == "" && b.GetString("Token") == "" {
|
||||||
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
return errors.New("no connection method found. See that you have WebhookBindAddress, WebhookURL or Token configured")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bslack) Disconnect() error {
|
func (b *Bslack) Disconnect() error {
|
||||||
return nil
|
return b.rtm.Disconnect()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
|
func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
|
||||||
// we can only join channels using the API
|
// we can only join channels using the API
|
||||||
if b.sc != nil {
|
if b.sc != nil {
|
||||||
if strings.HasPrefix(b.Config.Token, "xoxb") {
|
if strings.HasPrefix(b.GetString("Token"), "xoxb") {
|
||||||
// TODO check if bot has already joined channel
|
// TODO check if bot has already joined channel
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -117,7 +120,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use webhook to send the message
|
// Use webhook to send the message
|
||||||
if b.Config.WebhookURL != "" {
|
if b.GetString("WebhookURL") != "" {
|
||||||
return b.sendWebhook(msg)
|
return b.sendWebhook(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +146,7 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepend nick if configured
|
// Prepend nick if configured
|
||||||
if b.Config.PrefixMessagesWithNick {
|
if b.GetBool("PrefixMessagesWithNick") {
|
||||||
msg.Text = msg.Username + msg.Text
|
msg.Text = msg.Username + msg.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,12 +162,12 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
// create slack new post parameters
|
// create slack new post parameters
|
||||||
np := slack.NewPostMessageParameters()
|
np := slack.NewPostMessageParameters()
|
||||||
if b.Config.PrefixMessagesWithNick {
|
if b.GetBool("PrefixMessagesWithNick") {
|
||||||
np.AsUser = true
|
np.AsUser = true
|
||||||
}
|
}
|
||||||
np.Username = msg.Username
|
np.Username = msg.Username
|
||||||
np.LinkNames = 1 // replace mentions
|
np.LinkNames = 1 // replace mentions
|
||||||
np.IconURL = config.GetIconURL(&msg, &b.Config)
|
np.IconURL = config.GetIconURL(&msg, b.GetString("iconurl"))
|
||||||
if msg.Avatar != "" {
|
if msg.Avatar != "" {
|
||||||
np.IconURL = msg.Avatar
|
np.IconURL = msg.Avatar
|
||||||
}
|
}
|
||||||
@ -198,6 +201,10 @@ func (b *Bslack) Send(msg config.Message) (string, error) {
|
|||||||
return "slack " + id, nil
|
return "slack " + id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bslack) Reload(cfg *bridge.Config) (string, error) {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bslack) getAvatar(user string) string {
|
func (b *Bslack) getAvatar(user string) string {
|
||||||
var avatar string
|
var avatar string
|
||||||
if b.Users != nil {
|
if b.Users != nil {
|
||||||
@ -236,7 +243,7 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
|
|||||||
|
|
||||||
func (b *Bslack) handleSlack() {
|
func (b *Bslack) handleSlack() {
|
||||||
messages := make(chan *config.Message)
|
messages := make(chan *config.Message)
|
||||||
if b.Config.WebhookBindAddress != "" {
|
if b.GetString("WebhookBindAddress") != "" {
|
||||||
b.Log.Debugf("Choosing webhooks based receiving")
|
b.Log.Debugf("Choosing webhooks based receiving")
|
||||||
go b.handleMatterHook(messages)
|
go b.handleMatterHook(messages)
|
||||||
} else {
|
} else {
|
||||||
@ -419,7 +426,7 @@ func (b *Bslack) handleDownloadFile(rmsg *config.Message, file *slack.File) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// actually download the file
|
// actually download the file
|
||||||
data, err := helper.DownloadFileAuth(file.URLPrivateDownload, "Bearer "+b.Config.Token)
|
data, err := helper.DownloadFileAuth(file.URLPrivateDownload, "Bearer "+b.GetString("Token"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err)
|
return fmt.Errorf("download %s failed %#v", file.URLPrivateDownload, err)
|
||||||
}
|
}
|
||||||
@ -454,10 +461,10 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Edit message
|
// Edit message
|
||||||
if !b.Config.EditDisable && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
if !b.GetBool("EditDisable") && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
||||||
b.Log.Debugf("SubMessage %#v", ev.SubMessage)
|
b.Log.Debugf("SubMessage %#v", ev.SubMessage)
|
||||||
ev.User = ev.SubMessage.User
|
ev.User = ev.SubMessage.User
|
||||||
ev.Text = ev.SubMessage.Text + b.Config.EditSuffix
|
ev.Text = ev.SubMessage.Text + b.GetString("EditSuffix")
|
||||||
}
|
}
|
||||||
|
|
||||||
// use our own func because rtm.GetChannelInfo doesn't work for private channels
|
// use our own func because rtm.GetChannelInfo doesn't work for private channels
|
||||||
@ -493,7 +500,7 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// when using webhookURL we can't check if it's our webhook or not for now
|
// when using webhookURL we can't check if it's our webhook or not for now
|
||||||
if ev.BotID != "" && b.Config.WebhookURL == "" {
|
if ev.BotID != "" && b.GetString("WebhookURL") == "" {
|
||||||
bot, err := b.rtm.GetBotInfo(ev.BotID)
|
bot, err := b.rtm.GetBotInfo(ev.BotID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -568,14 +575,14 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Config.PrefixMessagesWithNick {
|
if b.GetBool("PrefixMessagesWithNick") {
|
||||||
msg.Text = msg.Username + msg.Text
|
msg.Text = msg.Username + msg.Text
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Extra != nil {
|
if msg.Extra != nil {
|
||||||
// this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE
|
// this sends a message only if we received a config.EVENT_FILE_FAILURE_SIZE
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Channel: msg.Channel, UserName: rmsg.Username, Text: rmsg.Text}
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL"), Channel: msg.Channel, UserName: rmsg.Username, Text: rmsg.Text}
|
||||||
b.mh.Send(matterMessage)
|
b.mh.Send(matterMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -598,7 +605,7 @@ func (b *Bslack) sendWebhook(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL, Attachments: attachs, Channel: msg.Channel, UserName: msg.Username, Text: msg.Text}
|
matterMessage := matterhook.OMessage{IconURL: b.GetString("IconURL"), Attachments: attachs, Channel: msg.Channel, UserName: msg.Username, Text: msg.Text}
|
||||||
if msg.Avatar != "" {
|
if msg.Avatar != "" {
|
||||||
matterMessage.IconURL = msg.Avatar
|
matterMessage.IconURL = msg.Avatar
|
||||||
}
|
}
|
||||||
@ -618,7 +625,7 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// do not send messages from ourself
|
// do not send messages from ourself
|
||||||
if b.Config.WebhookURL == "" && b.Config.WebhookBindAddress == "" && ev.Username == b.si.User.Name {
|
if b.GetString("WebhookURL") == "" && b.GetString("WebhookBindAddress") == "" && ev.Username == b.si.User.Name {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -629,7 +636,7 @@ func (b *Bslack) skipMessageEvent(ev *slack.MessageEvent) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !b.Config.EditDisable && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
if !b.GetBool("EditDisable") && ev.SubMessage != nil && ev.SubMessage.ThreadTimestamp != ev.SubMessage.Timestamp {
|
||||||
// it seems ev.SubMessage.Edited == nil when slack unfurls
|
// it seems ev.SubMessage.Edited == nil when slack unfurls
|
||||||
// do not forward these messages #266
|
// do not forward these messages #266
|
||||||
if ev.SubMessage.Edited == nil {
|
if ev.SubMessage.Edited == nil {
|
||||||
|
@ -14,18 +14,18 @@ import (
|
|||||||
type Bsshchat struct {
|
type Bsshchat struct {
|
||||||
r *bufio.Scanner
|
r *bufio.Scanner
|
||||||
w io.WriteCloser
|
w io.WriteCloser
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Bsshchat{BridgeConfig: cfg}
|
return &Bsshchat{Config: cfg}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bsshchat) Connect() error {
|
func (b *Bsshchat) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
b.Log.Infof("Connecting %s", b.Config.Server)
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
||||||
go func() {
|
go func() {
|
||||||
err = sshd.ConnectShell(b.Config.Server, b.Config.Nick, func(r io.Reader, w io.WriteCloser) error {
|
err = sshd.ConnectShell(b.GetString("Server"), b.GetString("Nick"), func(r io.Reader, w io.WriteCloser) error {
|
||||||
b.r = bufio.NewScanner(r)
|
b.r = bufio.NewScanner(r)
|
||||||
b.w = w
|
b.w = w
|
||||||
b.r.Scan()
|
b.r.Scan()
|
||||||
|
@ -18,11 +18,11 @@ type Bsteam struct {
|
|||||||
connected chan struct{}
|
connected chan struct{}
|
||||||
userMap map[steamid.SteamId]string
|
userMap map[steamid.SteamId]string
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Bsteam{BridgeConfig: cfg}
|
b := &Bsteam{Config: cfg}
|
||||||
b.userMap = make(map[steamid.SteamId]string)
|
b.userMap = make(map[steamid.SteamId]string)
|
||||||
b.connected = make(chan struct{})
|
b.connected = make(chan struct{})
|
||||||
return b
|
return b
|
||||||
@ -81,9 +81,9 @@ func (b *Bsteam) getNick(id steamid.SteamId) string {
|
|||||||
|
|
||||||
func (b *Bsteam) handleEvents() {
|
func (b *Bsteam) handleEvents() {
|
||||||
myLoginInfo := new(steam.LogOnDetails)
|
myLoginInfo := new(steam.LogOnDetails)
|
||||||
myLoginInfo.Username = b.Config.Login
|
myLoginInfo.Username = b.GetString("Login")
|
||||||
myLoginInfo.Password = b.Config.Password
|
myLoginInfo.Password = b.GetString("Password")
|
||||||
myLoginInfo.AuthCode = b.Config.AuthCode
|
myLoginInfo.AuthCode = b.GetString("AuthCode")
|
||||||
// Attempt to read existing auth hash to avoid steam guard.
|
// Attempt to read existing auth hash to avoid steam guard.
|
||||||
// Maybe works
|
// Maybe works
|
||||||
//myLoginInfo.SentryFileHash, _ = ioutil.ReadFile("sentry")
|
//myLoginInfo.SentryFileHash, _ = ioutil.ReadFile("sentry")
|
||||||
|
@ -14,18 +14,18 @@ import (
|
|||||||
|
|
||||||
type Btelegram struct {
|
type Btelegram struct {
|
||||||
c *tgbotapi.BotAPI
|
c *tgbotapi.BotAPI
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
avatarMap map[string]string // keep cache of userid and avatar sha
|
avatarMap map[string]string // keep cache of userid and avatar sha
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
return &Btelegram{BridgeConfig: cfg, avatarMap: make(map[string]string)}
|
return &Btelegram{Config: cfg, avatarMap: make(map[string]string)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Btelegram) Connect() error {
|
func (b *Btelegram) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
b.Log.Info("Connecting")
|
b.Log.Info("Connecting")
|
||||||
b.c, err = tgbotapi.NewBotAPI(b.Config.Token)
|
b.c, err = tgbotapi.NewBotAPI(b.GetString("Token"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.Debugf("%#v", err)
|
b.Log.Debugf("%#v", err)
|
||||||
return err
|
return err
|
||||||
@ -64,7 +64,7 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
return b.cacheAvatar(&msg)
|
return b.cacheAvatar(&msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.Config.MessageFormat == "HTML" {
|
if b.GetString("MessageFormat") == "HTML" {
|
||||||
msg.Text = makeHTML(msg.Text)
|
msg.Text = makeHTML(msg.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,11 +99,11 @@ func (b *Btelegram) Send(msg config.Message) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
m := tgbotapi.NewEditMessageText(chatid, msgid, msg.Username+msg.Text)
|
||||||
if b.Config.MessageFormat == "HTML" {
|
if b.GetString("MessageFormat") == "HTML" {
|
||||||
b.Log.Debug("Using mode HTML")
|
b.Log.Debug("Using mode HTML")
|
||||||
m.ParseMode = tgbotapi.ModeHTML
|
m.ParseMode = tgbotapi.ModeHTML
|
||||||
}
|
}
|
||||||
if b.Config.MessageFormat == "Markdown" {
|
if b.GetString("MessageFormat") == "Markdown" {
|
||||||
b.Log.Debug("Using mode markdown")
|
b.Log.Debug("Using mode markdown")
|
||||||
m.ParseMode = tgbotapi.ModeMarkdown
|
m.ParseMode = tgbotapi.ModeMarkdown
|
||||||
}
|
}
|
||||||
@ -137,9 +137,9 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// edited channel message
|
// edited channel message
|
||||||
if update.EditedChannelPost != nil && !b.Config.EditDisable {
|
if update.EditedChannelPost != nil && !b.GetBool("EditDisable") {
|
||||||
message = update.EditedChannelPost
|
message = update.EditedChannelPost
|
||||||
rmsg.Text = rmsg.Text + message.Text + b.Config.EditSuffix
|
rmsg.Text = rmsg.Text + message.Text + b.GetString("EditSuffix")
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle groups
|
// handle groups
|
||||||
@ -148,9 +148,9 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// edited group message
|
// edited group message
|
||||||
if update.EditedMessage != nil && !b.Config.EditDisable {
|
if update.EditedMessage != nil && !b.GetBool("EditDisable") {
|
||||||
message = update.EditedMessage
|
message = update.EditedMessage
|
||||||
rmsg.Text = rmsg.Text + message.Text + b.Config.EditSuffix
|
rmsg.Text = rmsg.Text + message.Text + b.GetString("EditSuffix")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the ID's from the channel or group message
|
// set the ID's from the channel or group message
|
||||||
@ -160,7 +160,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
|
|
||||||
// handle username
|
// handle username
|
||||||
if message.From != nil {
|
if message.From != nil {
|
||||||
if b.Config.UseFirstName {
|
if b.GetBool("UseFirstName") {
|
||||||
rmsg.Username = message.From.FirstName
|
rmsg.Username = message.From.FirstName
|
||||||
}
|
}
|
||||||
if rmsg.Username == "" {
|
if rmsg.Username == "" {
|
||||||
@ -189,7 +189,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
// handle forwarded messages
|
// handle forwarded messages
|
||||||
if message.ForwardFrom != nil {
|
if message.ForwardFrom != nil {
|
||||||
usernameForward := ""
|
usernameForward := ""
|
||||||
if b.Config.UseFirstName {
|
if b.GetBool("UseFirstName") {
|
||||||
usernameForward = message.ForwardFrom.FirstName
|
usernameForward = message.ForwardFrom.FirstName
|
||||||
}
|
}
|
||||||
if usernameForward == "" {
|
if usernameForward == "" {
|
||||||
@ -208,7 +208,7 @@ func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {
|
|||||||
if message.ReplyToMessage != nil {
|
if message.ReplyToMessage != nil {
|
||||||
usernameReply := ""
|
usernameReply := ""
|
||||||
if message.ReplyToMessage.From != nil {
|
if message.ReplyToMessage.From != nil {
|
||||||
if b.Config.UseFirstName {
|
if b.GetBool("UseFirstName") {
|
||||||
usernameReply = message.ReplyToMessage.From.FirstName
|
usernameReply = message.ReplyToMessage.From.FirstName
|
||||||
}
|
}
|
||||||
if usernameReply == "" {
|
if usernameReply == "" {
|
||||||
@ -338,7 +338,7 @@ func (b *Btelegram) handleDownload(message *tgbotapi.Message, rmsg *config.Messa
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// use the URL instead of native upload
|
// use the URL instead of native upload
|
||||||
if b.Config.UseInsecureURL {
|
if b.GetBool("UseInsecureURL") {
|
||||||
b.Log.Debugf("Setting message text to :%s", text)
|
b.Log.Debugf("Setting message text to :%s", text)
|
||||||
rmsg.Text = rmsg.Text + text
|
rmsg.Text = rmsg.Text + text
|
||||||
return nil
|
return nil
|
||||||
@ -382,13 +382,13 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string,
|
|||||||
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
func (b *Btelegram) sendMessage(chatid int64, username, text string) (string, error) {
|
||||||
m := tgbotapi.NewMessage(chatid, "")
|
m := tgbotapi.NewMessage(chatid, "")
|
||||||
m.Text = username + text
|
m.Text = username + text
|
||||||
if b.Config.MessageFormat == "HTML" {
|
if b.GetString("MessageFormat") == "HTML" {
|
||||||
b.Log.Debug("Using mode HTML")
|
b.Log.Debug("Using mode HTML")
|
||||||
username = html.EscapeString(username)
|
username = html.EscapeString(username)
|
||||||
m.Text = username + text
|
m.Text = username + text
|
||||||
m.ParseMode = tgbotapi.ModeHTML
|
m.ParseMode = tgbotapi.ModeHTML
|
||||||
}
|
}
|
||||||
if b.Config.MessageFormat == "Markdown" {
|
if b.GetString("MessageFormat") == "Markdown" {
|
||||||
b.Log.Debug("Using mode markdown")
|
b.Log.Debug("Using mode markdown")
|
||||||
m.ParseMode = tgbotapi.ModeMarkdown
|
m.ParseMode = tgbotapi.ModeMarkdown
|
||||||
}
|
}
|
||||||
|
@ -14,18 +14,18 @@ import (
|
|||||||
type Bxmpp struct {
|
type Bxmpp struct {
|
||||||
xc *xmpp.Client
|
xc *xmpp.Client
|
||||||
xmppMap map[string]string
|
xmppMap map[string]string
|
||||||
*config.BridgeConfig
|
*bridge.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *config.BridgeConfig) bridge.Bridger {
|
func New(cfg *bridge.Config) bridge.Bridger {
|
||||||
b := &Bxmpp{BridgeConfig: cfg}
|
b := &Bxmpp{Config: cfg}
|
||||||
b.xmppMap = make(map[string]string)
|
b.xmppMap = make(map[string]string)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bxmpp) Connect() error {
|
func (b *Bxmpp) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
b.Log.Infof("Connecting %s", b.Config.Server)
|
b.Log.Infof("Connecting %s", b.GetString("Server"))
|
||||||
b.xc, err = b.createXMPP()
|
b.xc, err = b.createXMPP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Log.Debugf("%#v", err)
|
b.Log.Debugf("%#v", err)
|
||||||
@ -63,7 +63,7 @@ func (b *Bxmpp) Disconnect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bxmpp) JoinChannel(channel config.ChannelInfo) error {
|
func (b *Bxmpp) JoinChannel(channel config.ChannelInfo) error {
|
||||||
b.xc.JoinMUCNoHistory(channel.Name+"@"+b.Config.Muc, b.Config.Nick)
|
b.xc.JoinMUCNoHistory(channel.Name+"@"+b.GetString("Muc"), b.GetString("Nick"))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
|
|||||||
// Upload a file (in xmpp case send the upload URL because xmpp has no native upload support)
|
// Upload a file (in xmpp case send the upload URL because xmpp has no native upload support)
|
||||||
if msg.Extra != nil {
|
if msg.Extra != nil {
|
||||||
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
||||||
b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: rmsg.Channel + "@" + b.Config.Muc, Text: rmsg.Username + rmsg.Text})
|
b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: rmsg.Channel + "@" + b.GetString("Muc"), Text: rmsg.Username + rmsg.Text})
|
||||||
}
|
}
|
||||||
if len(msg.Extra["file"]) > 0 {
|
if len(msg.Extra["file"]) > 0 {
|
||||||
return b.handleUploadFile(&msg)
|
return b.handleUploadFile(&msg)
|
||||||
@ -85,7 +85,7 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Post normal message
|
// Post normal message
|
||||||
_, err := b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text})
|
_, err := b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.GetString("Muc"), Text: msg.Username + msg.Text})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -94,17 +94,16 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
|
|||||||
|
|
||||||
func (b *Bxmpp) createXMPP() (*xmpp.Client, error) {
|
func (b *Bxmpp) createXMPP() (*xmpp.Client, error) {
|
||||||
tc := new(tls.Config)
|
tc := new(tls.Config)
|
||||||
tc.InsecureSkipVerify = b.Config.SkipTLSVerify
|
tc.InsecureSkipVerify = b.GetBool("SkipTLSVerify")
|
||||||
tc.ServerName = strings.Split(b.Config.Server, ":")[0]
|
tc.ServerName = strings.Split(b.GetString("Server"), ":")[0]
|
||||||
options := xmpp.Options{
|
options := xmpp.Options{
|
||||||
Host: b.Config.Server,
|
Host: b.GetString("Server"),
|
||||||
User: b.Config.Jid,
|
User: b.GetString("Jid"),
|
||||||
Password: b.Config.Password,
|
Password: b.GetString("Password"),
|
||||||
NoTLS: true,
|
NoTLS: true,
|
||||||
StartTLS: true,
|
StartTLS: true,
|
||||||
TLSConfig: tc,
|
TLSConfig: tc,
|
||||||
|
Debug: b.GetBool("debug"),
|
||||||
Debug: b.General.Debug,
|
|
||||||
Logger: b.Log.Writer(),
|
Logger: b.Log.Writer(),
|
||||||
Session: true,
|
Session: true,
|
||||||
Status: "",
|
Status: "",
|
||||||
@ -150,6 +149,7 @@ func (b *Bxmpp) handleXMPP() error {
|
|||||||
switch v := m.(type) {
|
switch v := m.(type) {
|
||||||
case xmpp.Chat:
|
case xmpp.Chat:
|
||||||
if v.Type == "groupchat" {
|
if v.Type == "groupchat" {
|
||||||
|
b.Log.Debugf("== Receiving %#v", v)
|
||||||
// skip invalid messages
|
// skip invalid messages
|
||||||
if b.skipMessage(v) {
|
if b.skipMessage(v) {
|
||||||
continue
|
continue
|
||||||
@ -188,7 +188,7 @@ func (b *Bxmpp) handleUploadFile(msg *config.Message) (string, error) {
|
|||||||
if fi.URL != "" {
|
if fi.URL != "" {
|
||||||
msg.Text += fi.URL
|
msg.Text += fi.URL
|
||||||
}
|
}
|
||||||
_, err := b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text})
|
_, err := b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.GetString("Muc"), Text: msg.Username + msg.Text})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -218,7 +218,7 @@ func (b *Bxmpp) parseChannel(remote string) string {
|
|||||||
// skipMessage skips messages that need to be skipped
|
// skipMessage skips messages that need to be skipped
|
||||||
func (b *Bxmpp) skipMessage(message xmpp.Chat) bool {
|
func (b *Bxmpp) skipMessage(message xmpp.Chat) bool {
|
||||||
// skip messages from ourselves
|
// skip messages from ourselves
|
||||||
if b.parseNick(message.Remote) == b.Config.Nick {
|
if b.parseNick(message.Remote) == b.GetString("Nick") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,17 +50,17 @@ type BrMsgID struct {
|
|||||||
var flog *log.Entry
|
var flog *log.Entry
|
||||||
|
|
||||||
var bridgeMap = map[string]bridge.Factory{
|
var bridgeMap = map[string]bridge.Factory{
|
||||||
"mattermost": bmattermost.New,
|
|
||||||
"irc": birc.New,
|
|
||||||
"gitter": bgitter.New,
|
|
||||||
"matrix": bmatrix.New,
|
|
||||||
"slack": bslack.New,
|
|
||||||
"api": api.New,
|
"api": api.New,
|
||||||
"telegram": btelegram.New,
|
|
||||||
"discord": bdiscord.New,
|
"discord": bdiscord.New,
|
||||||
"steam": bsteam.New,
|
"gitter": bgitter.New,
|
||||||
"sshchat": bsshchat.New,
|
"irc": birc.New,
|
||||||
|
"mattermost": bmattermost.New,
|
||||||
|
"matrix": bmatrix.New,
|
||||||
"rocketchat": brocketchat.New,
|
"rocketchat": brocketchat.New,
|
||||||
|
"slack": bslack.New,
|
||||||
|
"sshchat": bsshchat.New,
|
||||||
|
"steam": bsteam.New,
|
||||||
|
"telegram": btelegram.New,
|
||||||
"xmpp": bxmpp.New,
|
"xmpp": bxmpp.New,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,16 +81,10 @@ func (gw *Gateway) AddBridge(cfg *config.Bridge) error {
|
|||||||
br := gw.Router.getBridge(cfg.Account)
|
br := gw.Router.getBridge(cfg.Account)
|
||||||
if br == nil {
|
if br == nil {
|
||||||
br = bridge.New(cfg)
|
br = bridge.New(cfg)
|
||||||
// override config from environment
|
br.Config = gw.Router.Config
|
||||||
config.OverrideCfgFromEnv(gw.Config, br.Protocol, br.Name)
|
|
||||||
// set logging
|
// set logging
|
||||||
br.Log = log.WithFields(log.Fields{"prefix": "bridge"})
|
br.Log = log.WithFields(log.Fields{"prefix": "bridge"})
|
||||||
// get the protocol configuration (eg irc)
|
brconfig := &bridge.Config{Remote: gw.Message, Log: log.WithFields(log.Fields{"prefix": br.Protocol}), Bridge: br}
|
||||||
pcfg := getField(gw.Config, strings.Title(br.Protocol))
|
|
||||||
// get the config for this name (eg freenode, from irc.freenode)
|
|
||||||
br.Config = pcfg[br.Name]
|
|
||||||
// create the bridge config
|
|
||||||
brconfig := &config.BridgeConfig{General: &gw.Config.General, Account: br.Account, Remote: gw.Message, Config: br.Config, Log: log.WithFields(log.Fields{"prefix": br.Protocol})}
|
|
||||||
// add the actual bridger for this protocol to this bridge using the bridgeMap
|
// add the actual bridger for this protocol to this bridge using the bridgeMap
|
||||||
br.Bridger = bridgeMap[br.Protocol](brconfig)
|
br.Bridger = bridgeMap[br.Protocol](brconfig)
|
||||||
}
|
}
|
||||||
@ -226,12 +220,12 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
|
|||||||
}
|
}
|
||||||
|
|
||||||
// only relay join/part when configured
|
// only relay join/part when configured
|
||||||
if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.ShowJoinPart {
|
if msg.Event == config.EVENT_JOIN_LEAVE && !gw.Bridges[dest.Account].Config.GetBool("ShowJoinPart") {
|
||||||
return brMsgIDs
|
return brMsgIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
// only relay topic change when configured
|
// only relay topic change when configured
|
||||||
if msg.Event == config.EVENT_TOPIC_CHANGE && !gw.Bridges[dest.Account].Config.ShowTopicChange {
|
if msg.Event == config.EVENT_TOPIC_CHANGE && !gw.Bridges[dest.Account].Config.GetBool("ShowTopicChange") {
|
||||||
return brMsgIDs
|
return brMsgIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,7 +302,7 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// is the username in IgnoreNicks field
|
// is the username in IgnoreNicks field
|
||||||
for _, entry := range strings.Fields(gw.Bridges[msg.Account].Config.IgnoreNicks) {
|
for _, entry := range strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreNicks")) {
|
||||||
if msg.Username == entry {
|
if msg.Username == entry {
|
||||||
flog.Debugf("ignoring %s from %s", msg.Username, msg.Account)
|
flog.Debugf("ignoring %s from %s", msg.Username, msg.Account)
|
||||||
return true
|
return true
|
||||||
@ -317,7 +311,7 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
|
|||||||
|
|
||||||
// does the message match regex in IgnoreMessages field
|
// does the message match regex in IgnoreMessages field
|
||||||
// TODO do not compile regexps everytime
|
// TODO do not compile regexps everytime
|
||||||
for _, entry := range strings.Fields(gw.Bridges[msg.Account].Config.IgnoreMessages) {
|
for _, entry := range strings.Fields(gw.Bridges[msg.Account].GetString("IgnoreMessages")) {
|
||||||
if entry != "" {
|
if entry != "" {
|
||||||
re, err := regexp.Compile(entry)
|
re, err := regexp.Compile(entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -336,17 +330,17 @@ func (gw *Gateway) ignoreMessage(msg *config.Message) bool {
|
|||||||
func (gw *Gateway) modifyUsername(msg config.Message, dest *bridge.Bridge) string {
|
func (gw *Gateway) modifyUsername(msg config.Message, dest *bridge.Bridge) string {
|
||||||
br := gw.Bridges[msg.Account]
|
br := gw.Bridges[msg.Account]
|
||||||
msg.Protocol = br.Protocol
|
msg.Protocol = br.Protocol
|
||||||
if gw.Config.General.StripNick || dest.Config.StripNick {
|
if gw.Config.General.StripNick || dest.Config.GetBool("StripNick") {
|
||||||
re := regexp.MustCompile("[^a-zA-Z0-9]+")
|
re := regexp.MustCompile("[^a-zA-Z0-9]+")
|
||||||
msg.Username = re.ReplaceAllString(msg.Username, "")
|
msg.Username = re.ReplaceAllString(msg.Username, "")
|
||||||
}
|
}
|
||||||
nick := dest.Config.RemoteNickFormat
|
nick := dest.GetString("RemoteNickFormat")
|
||||||
if nick == "" {
|
if nick == "" {
|
||||||
nick = gw.Config.General.RemoteNickFormat
|
nick = gw.Config.General.RemoteNickFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop to replace nicks
|
// loop to replace nicks
|
||||||
for _, outer := range br.Config.ReplaceNicks {
|
for _, outer := range br.GetStringSlice2D("ReplaceNicks") {
|
||||||
search := outer[0]
|
search := outer[0]
|
||||||
replace := outer[1]
|
replace := outer[1]
|
||||||
// TODO move compile to bridge init somewhere
|
// TODO move compile to bridge init somewhere
|
||||||
@ -373,7 +367,7 @@ func (gw *Gateway) modifyUsername(msg config.Message, dest *bridge.Bridge) strin
|
|||||||
|
|
||||||
nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1)
|
nick = strings.Replace(nick, "{BRIDGE}", br.Name, -1)
|
||||||
nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
|
nick = strings.Replace(nick, "{PROTOCOL}", br.Protocol, -1)
|
||||||
nick = strings.Replace(nick, "{LABEL}", br.Config.Label, -1)
|
nick = strings.Replace(nick, "{LABEL}", br.GetString("Label"), -1)
|
||||||
nick = strings.Replace(nick, "{NICK}", msg.Username, -1)
|
nick = strings.Replace(nick, "{NICK}", msg.Username, -1)
|
||||||
return nick
|
return nick
|
||||||
}
|
}
|
||||||
@ -381,7 +375,7 @@ func (gw *Gateway) modifyUsername(msg config.Message, dest *bridge.Bridge) strin
|
|||||||
func (gw *Gateway) modifyAvatar(msg config.Message, dest *bridge.Bridge) string {
|
func (gw *Gateway) modifyAvatar(msg config.Message, dest *bridge.Bridge) string {
|
||||||
iconurl := gw.Config.General.IconURL
|
iconurl := gw.Config.General.IconURL
|
||||||
if iconurl == "" {
|
if iconurl == "" {
|
||||||
iconurl = dest.Config.IconURL
|
iconurl = dest.GetString("IconURL")
|
||||||
}
|
}
|
||||||
iconurl = strings.Replace(iconurl, "{NICK}", msg.Username, -1)
|
iconurl = strings.Replace(iconurl, "{NICK}", msg.Username, -1)
|
||||||
if msg.Avatar == "" {
|
if msg.Avatar == "" {
|
||||||
@ -396,7 +390,7 @@ func (gw *Gateway) modifyMessage(msg *config.Message) {
|
|||||||
|
|
||||||
br := gw.Bridges[msg.Account]
|
br := gw.Bridges[msg.Account]
|
||||||
// loop to replace messages
|
// loop to replace messages
|
||||||
for _, outer := range br.Config.ReplaceMessages {
|
for _, outer := range br.GetStringSlice2D("ReplaceMessages") {
|
||||||
search := outer[0]
|
search := outer[0]
|
||||||
replace := outer[1]
|
replace := outer[1]
|
||||||
// TODO move compile to bridge init somewhere
|
// TODO move compile to bridge init somewhere
|
||||||
|
Loading…
Reference in New Issue
Block a user