diff options
author | Wim <wim@42.be> | 2017-07-01 22:43:53 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-07-01 22:49:06 +0200 |
commit | 8064744d3a5fa4e4152ec2a2bca45e21a8c02fe4 (patch) | |
tree | cd9bbeeeefa417ad5efe20bc3515e8d5b70281bb | |
parent | d261949db2de47578812f852d5c6d5c04d202323 (diff) | |
download | matterbridge-msglm-8064744d3a5fa4e4152ec2a2bca45e21a8c02fe4.tar.gz matterbridge-msglm-8064744d3a5fa4e4152ec2a2bca45e21a8c02fe4.tar.bz2 matterbridge-msglm-8064744d3a5fa4e4152ec2a2bca45e21a8c02fe4.zip |
Fix possible panics. (from @recht matterircd fork)
-rw-r--r-- | matterclient/matterclient.go | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/matterclient/matterclient.go b/matterclient/matterclient.go index 4dcc9ae0..61eccacf 100644 --- a/matterclient/matterclient.go +++ b/matterclient/matterclient.go @@ -263,7 +263,7 @@ func (m *MMClient) WsReceiver() { var event model.WebSocketEvent if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() { - m.log.Debugf("WsReceiver: %#v", event) + m.log.Debugf("WsReceiver event: %#v", event) msg := &Message{Raw: &event, Team: m.Credentials.Team} m.parseMessage(msg) m.MessageChan <- msg @@ -272,7 +272,7 @@ func (m *MMClient) WsReceiver() { var response model.WebSocketResponse if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() { - m.log.Debugf("WsReceiver: %#v", response) + m.log.Debugf("WsReceiver response: %#v", response) m.parseResponse(response) continue } @@ -305,7 +305,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.UpdateUsers() + m.log.Infof("User %s is not known, ignoring message %s", data) + return } rmsg.Username = m.GetUserName(data.UserId) rmsg.Channel = m.GetChannelName(data.ChannelId) @@ -366,9 +367,21 @@ func (m *MMClient) GetChannelName(channelId string) string { m.RLock() defer m.RUnlock() for _, t := range m.OtherTeams { - for _, channel := range append(*t.Channels, *t.MoreChannels...) { - if channel.Id == channelId { - return channel.Name + if t == nil { + continue + } + if t.Channels != nil { + for _, channel := range *t.Channels { + if channel.Id == channelId { + return channel.Name + } + } + } + if t.MoreChannels != nil { + for _, channel := range *t.MoreChannels { + if channel.Id == channelId { + return channel.Name + } } } } @@ -616,7 +629,9 @@ func (m *MMClient) GetTeamFromChannel(channelId string) string { var channels []*model.Channel for _, t := range m.OtherTeams { channels = append(channels, *t.Channels...) - channels = append(channels, *t.MoreChannels...) + if t.MoreChannels != nil { + channels = append(channels, *t.MoreChannels...) + } for _, c := range channels { if c.Id == channelId { return t.Id @@ -648,8 +663,8 @@ func (m *MMClient) GetUsers() map[string]*model.User { } func (m *MMClient) GetUser(userId string) *model.User { - m.RLock() - defer m.RUnlock() + m.Lock() + defer m.Unlock() return m.Users[userId] } |