diff --git a/bridge/api/api.go b/bridge/api/api.go index da1f91e2..eef41a4a 100644 --- a/bridge/api/api.go +++ b/bridge/api/api.go @@ -110,8 +110,8 @@ func (b *Api) handleStream(c echo.Context) error { c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON) c.Response().WriteHeader(http.StatusOK) greet := config.Message{ - Event:config.EVENT_API_CONNECTED, - Timestamp:time.Now(), + Event: config.EVENT_API_CONNECTED, + Timestamp: time.Now(), } if err := json.NewEncoder(c.Response()).Encode(greet); err != nil { return err diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 116bf86d..0549187f 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -91,6 +91,7 @@ func (b *Bdiscord) Connect() error { } // obtaining guild members and initializing nickname mapping b.Lock() + defer b.Unlock() members, err := b.c.GuildMembers(b.guildID, "", 1000) if err != nil { b.Log.Error("Error obtaining guild members", err) @@ -103,7 +104,6 @@ func (b *Bdiscord) Connect() error { b.nickMemberMap[member.Nick] = member } } - b.Unlock() return nil } @@ -488,9 +488,16 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri var err error for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) - files := []*discordgo.File{} - files = append(files, &discordgo.File{fi.Name, "", bytes.NewReader(*fi.Data)}) - _, err = b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{Content: msg.Username + fi.Comment, Files: files}) + file := discordgo.File{ + Name: fi.Name, + ContentType: "", + Reader: bytes.NewReader(*fi.Data), + } + m := discordgo.MessageSend{ + Content: msg.Username + fi.Comment, + Files: []*discordgo.File{&file}, + } + _, err = b.c.ChannelMessageSendComplex(channelID, &m) if err != nil { return "", fmt.Errorf("file upload failed: %#v", err) } diff --git a/bridge/irc/irc.go b/bridge/irc/irc.go index cd37c0b4..c1cbdf60 100644 --- a/bridge/irc/irc.go +++ b/bridge/irc/irc.go @@ -106,7 +106,10 @@ func (b *Birc) Connect() error { }) if b.GetBool("UseSASL") { - i.Config.SASL = &girc.SASLPlain{b.GetString("NickServNick"), b.GetString("NickServPassword")} + i.Config.SASL = &girc.SASLPlain{ + User: b.GetString("NickServNick"), + Pass: b.GetString("NickServPassword"), + } } i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 95ba6b42..ba02d4aa 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -73,8 +73,11 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) { // Make a action /me of the message if msg.Event == config.EVENT_USER_ACTION { - resp, err := b.mc.SendMessageEvent(channel, "m.room.message", - matrix.TextMessage{"m.emote", msg.Username + msg.Text}) + m := matrix.TextMessage{ + MsgType: "m.emote", + Body: msg.Username + msg.Text, + } + resp, err := b.mc.SendMessageEvent(channel, "m.room.message", m) if err != nil { return "", err } @@ -158,7 +161,8 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) { // Text must be a string if rmsg.Text, ok = ev.Content["body"].(string); !ok { - b.Log.Errorf("Content[body] wasn't a %T ?", rmsg.Text) + b.Log.Errorf("Content[body] is not a string: %T\n%#v", + ev.Content["body"], ev.Content) return } diff --git a/bridge/slack/handlers.go b/bridge/slack/handlers.go index 1428cba2..d6894bf6 100644 --- a/bridge/slack/handlers.go +++ b/bridge/slack/handlers.go @@ -166,15 +166,14 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er } rmsg := config.Message{ - Text: ev.Text, - Channel: channelInfo.Name, - Account: b.Account, - ID: "slack " + ev.Timestamp, - Extra: map[string][]interface{}{}, + Text: ev.Text, + Channel: channelInfo.Name, + Account: b.Account, + ID: "slack " + ev.Timestamp, + Extra: map[string][]interface{}{}, ParentID: ev.ThreadTimestamp, } - if b.useChannelID { rmsg.Channel = "ID:" + channelInfo.ID } diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index 4abe79cd..23d5816a 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -4,7 +4,6 @@ import ( "fmt" "regexp" "strings" - "sync" "time" "github.com/nlopes/slack" @@ -61,22 +60,17 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) { const minimumRefreshInterval = 10 * time.Second -var ( - refreshMutex sync.Mutex - refreshInProgress bool - earliestChannelRefresh = time.Now() - earliestUserRefresh = time.Now() -) - func (b *Bslack) populateUsers() { - refreshMutex.Lock() - if time.Now().Before(earliestUserRefresh) || refreshInProgress { - b.Log.Debugf("Not refreshing user list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds())) - refreshMutex.Unlock() + b.refreshMutex.Lock() + if time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress { + b.Log.Debugf("Not refreshing user list as it was done less than %v ago.", + minimumRefreshInterval) + b.refreshMutex.Unlock() + return } - refreshInProgress = true - refreshMutex.Unlock() + b.refreshInProgress = true + b.refreshMutex.Unlock() users, err := b.sc.GetUsers() if err != nil { @@ -95,19 +89,22 @@ func (b *Bslack) populateUsers() { defer b.usersMutex.Unlock() b.users = newUsers - earliestUserRefresh = time.Now().Add(minimumRefreshInterval) - refreshInProgress = false + b.refreshMutex.Lock() + defer b.refreshMutex.Unlock() + b.earliestUserRefresh = time.Now().Add(minimumRefreshInterval) + b.refreshInProgress = false } func (b *Bslack) populateChannels() { - refreshMutex.Lock() - if time.Now().Before(earliestChannelRefresh) || refreshInProgress { - b.Log.Debugf("Not refreshing channel list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds())) - refreshMutex.Unlock() + b.refreshMutex.Lock() + if time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress { + b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.", + minimumRefreshInterval) + b.refreshMutex.Unlock() return } - refreshInProgress = true - refreshMutex.Unlock() + b.refreshInProgress = true + b.refreshMutex.Unlock() newChannelsByID := map[string]*slack.Channel{} newChannelsByName := map[string]*slack.Channel{} @@ -139,8 +136,10 @@ func (b *Bslack) populateChannels() { b.channelsByID = newChannelsByID b.channelsByName = newChannelsByName - earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) - refreshInProgress = false + b.refreshMutex.Lock() + defer b.refreshMutex.Unlock() + b.earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) + b.refreshInProgress = false } var ( diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 924f4131..c1386a46 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" "sync" + "time" "github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge/config" @@ -34,6 +35,11 @@ type Bslack struct { channelsByID map[string]*slack.Channel channelsByName map[string]*slack.Channel channelsMutex sync.RWMutex + + refreshInProgress bool + earliestChannelRefresh time.Time + earliestUserRefresh time.Time + refreshMutex sync.Mutex } const ( @@ -68,12 +74,14 @@ func New(cfg *bridge.Config) bridge.Bridger { cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err) } b := &Bslack{ - Config: cfg, - uuid: xid.New().String(), - cache: newCache, - users: map[string]*slack.User{}, - channelsByID: map[string]*slack.Channel{}, - channelsByName: map[string]*slack.Channel{}, + Config: cfg, + uuid: xid.New().String(), + cache: newCache, + users: map[string]*slack.User{}, + channelsByID: map[string]*slack.Channel{}, + channelsByName: map[string]*slack.Channel{}, + earliestChannelRefresh: time.Now(), + earliestUserRefresh: time.Now(), } return b } diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 378f383d..93cc82f6 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -377,7 +377,10 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string, var c tgbotapi.Chattable for _, f := range msg.Extra["file"] { fi := f.(config.FileInfo) - file := tgbotapi.FileBytes{fi.Name, *fi.Data} + file := tgbotapi.FileBytes{ + Name: fi.Name, + Bytes: *fi.Data, + } re := regexp.MustCompile(".(jpg|png)$") if re.MatchString(fi.Name) { c = tgbotapi.NewPhotoUpload(chatid, file) diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index 4ace2abd..4d6cc3cf 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -75,8 +75,6 @@ func (b *Bxmpp) JoinChannel(channel config.ChannelInfo) error { } func (b *Bxmpp) Send(msg config.Message) (string, error) { - var msgid = "" - var msgreplaceid = "" // ignore delete messages if msg.Event == config.EVENT_MSG_DELETE { return "", nil @@ -93,7 +91,8 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) { } } - msgid = xid.New().String() + var msgreplaceid string + msgid := xid.New().String() if msg.ID != "" { msgid = msg.ID msgreplaceid = msg.ID diff --git a/gateway/gateway.go b/gateway/gateway.go index c1905a9b..fb2cff4f 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -310,9 +310,9 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM msg.Channel = originchannel } - msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel) + msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel) if msg.ParentID == "" { - msg.ParentID = canonicalParentMsgID + msg.ParentID = canonicalParentMsgID } mID, err := dest.Send(msg) diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index f476be06..ae7cd94b 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -365,7 +365,8 @@ func (m *MMClient) parseActionPost(rmsg *Message) { data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string))) // we don't have the user, refresh the userlist if m.GetUser(data.UserId) == nil { - m.log.Infof("User %s is not known, ignoring message %s", data.UserId, data.Message) + m.log.Infof("User '%v' is not known, ignoring message '%#v'", + data.UserId, data) return } rmsg.Username = m.GetUserName(data.UserId) @@ -896,7 +897,7 @@ func (m *MMClient) StatusLoop() { if m.OnWsConnect != nil { m.OnWsConnect() } - m.log.Debugf("StatusLoop: %p", m.OnWsConnect) + m.log.Debug("StatusLoop:", m.OnWsConnect != nil) for { if m.WsQuit { return