diff options
author | Wim <wim@42.be> | 2016-06-23 20:28:05 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2016-06-23 20:28:05 +0200 |
commit | 6ec77e06eaecc9b0567e1c67e9255da448fa4b2f (patch) | |
tree | 9520a787e4b8bbc582fc1fddbf8bedd810f8bd6c /vendor/github.com/42wim/matterbridge-plus | |
parent | e48db67649118bb2c77acfedc9320b45db9fd4ea (diff) | |
download | matterbridge-msglm-6ec77e06eaecc9b0567e1c67e9255da448fa4b2f.tar.gz matterbridge-msglm-6ec77e06eaecc9b0567e1c67e9255da448fa4b2f.tar.bz2 matterbridge-msglm-6ec77e06eaecc9b0567e1c67e9255da448fa4b2f.zip |
Sync with mattermost 3.1.0
Diffstat (limited to 'vendor/github.com/42wim/matterbridge-plus')
3 files changed, 224 insertions, 115 deletions
diff --git a/vendor/github.com/42wim/matterbridge-plus/bridge/bridge.go b/vendor/github.com/42wim/matterbridge-plus/bridge/bridge.go index ebd5456d..ee27a65d 100644 --- a/vendor/github.com/42wim/matterbridge-plus/bridge/bridge.go +++ b/vendor/github.com/42wim/matterbridge-plus/bridge/bridge.go @@ -21,15 +21,17 @@ type MMhook struct { } type MMapi struct { - mc *matterclient.MMClient - mmMap map[string]string + mc *matterclient.MMClient + mmMap map[string]string + mmIgnoreNicks []string } type MMirc struct { - i *irc.Connection - ircNick string - ircMap map[string]string - names map[string][]string + i *irc.Connection + ircNick string + ircMap map[string]string + names map[string][]string + ircIgnoreNicks []string } type MMMessage struct { @@ -53,6 +55,8 @@ type FancyLog struct { var flog FancyLog +const Legacy = "legacy" + func initFLog() { flog.irc = log.WithFields(log.Fields{"module": "irc"}) flog.mm = log.WithFields(log.Fields{"module": "mattermost"}) @@ -66,7 +70,9 @@ func NewBridge(name string, config *Config, kind string) *Bridge { b.ircNick = b.Config.IRC.Nick b.ircMap = make(map[string]string) b.MMirc.names = make(map[string][]string) - if kind == "legacy" { + b.ircIgnoreNicks = strings.Fields(b.Config.IRC.IgnoreNicks) + b.mmIgnoreNicks = strings.Fields(b.Config.Mattermost.IgnoreNicks) + if kind == Legacy { if len(b.Config.Token) > 0 { for _, val := range b.Config.Token { b.ircMap[val.IRCChannel] = val.MMChannel @@ -87,10 +93,14 @@ func NewBridge(name string, config *Config, kind string) *Bridge { } b.mc = matterclient.New(b.Config.Mattermost.Login, b.Config.Mattermost.Password, b.Config.Mattermost.Team, b.Config.Mattermost.Server) + b.mc.SkipTLSVerify = b.Config.Mattermost.SkipTLSVerify + b.mc.NoTLS = b.Config.Mattermost.NoTLS + flog.mm.Infof("Trying login %s (team: %s) on %s", b.Config.Mattermost.Login, b.Config.Mattermost.Team, b.Config.Mattermost.Server) err := b.mc.Login() if err != nil { - flog.mm.Fatal("can not connect", err) + flog.mm.Fatal("Can not connect", err) } + flog.mm.Info("Login ok") b.mc.JoinChannel(b.Config.Mattermost.Channel) if len(b.Config.Channel) > 0 { for _, val := range b.Config.Channel { @@ -99,7 +109,9 @@ func NewBridge(name string, config *Config, kind string) *Bridge { } go b.mc.WsReceiver() } + flog.irc.Info("Trying IRC connection") b.i = b.createIRC(name) + flog.irc.Info("Connection succeeded") go b.handleMatter() return b } @@ -111,37 +123,51 @@ func (b *Bridge) createIRC(name string) *irc.Connection { if b.Config.IRC.Password != "" { i.Password = b.Config.IRC.Password } - i.AddCallback("*", b.handleOther) + i.AddCallback(ircm.RPL_WELCOME, b.handleNewConnection) i.Connect(b.Config.IRC.Server + ":" + strconv.Itoa(b.Config.IRC.Port)) return i } func (b *Bridge) handleNewConnection(event *irc.Event) { + flog.irc.Info("Registering callbacks") + i := b.i b.ircNick = event.Arguments[0] + i.AddCallback("PRIVMSG", b.handlePrivMsg) + i.AddCallback("CTCP_ACTION", b.handlePrivMsg) + i.AddCallback(ircm.RPL_ENDOFNAMES, b.endNames) + i.AddCallback(ircm.RPL_NAMREPLY, b.storeNames) + i.AddCallback(ircm.RPL_TOPICWHOTIME, b.handleTopicWhoTime) + i.AddCallback(ircm.NOTICE, b.handleNotice) + i.AddCallback(ircm.RPL_MYINFO, func(e *irc.Event) { flog.irc.Infof("%s: %s", e.Code, strings.Join(e.Arguments[1:], " ")) }) + i.AddCallback("PING", func(e *irc.Event) { + i.SendRaw("PONG :" + e.Message()) + flog.irc.Debugf("PING/PONG") + }) + if b.Config.Mattermost.ShowJoinPart { + i.AddCallback("JOIN", b.handleJoinPart) + i.AddCallback("PART", b.handleJoinPart) + } + i.AddCallback("*", b.handleOther) b.setupChannels() } func (b *Bridge) setupChannels() { i := b.i - flog.irc.Info("Joining ", b.Config.IRC.Channel, " as ", b.ircNick) - i.Join(b.Config.IRC.Channel) - if b.kind == "legacy" { + if b.Config.IRC.Channel != "" { + flog.irc.Infof("Joining %s as %s", b.Config.IRC.Channel, b.ircNick) + i.Join(b.Config.IRC.Channel) + } + if b.kind == Legacy { for _, val := range b.Config.Token { - flog.irc.Info("Joining ", val.IRCChannel, " as ", b.ircNick) + flog.irc.Infof("Joining %s as %s", val.IRCChannel, b.ircNick) i.Join(val.IRCChannel) } } else { for _, val := range b.Config.Channel { - flog.irc.Info("Joining ", val.IRC, " as ", b.ircNick) + flog.irc.Infof("Joining %s as %s", val.IRC, b.ircNick) i.Join(val.IRC) } } - i.AddCallback("PRIVMSG", b.handlePrivMsg) - i.AddCallback("CTCP_ACTION", b.handlePrivMsg) - if b.Config.Mattermost.ShowJoinPart { - i.AddCallback("JOIN", b.handleJoinPart) - i.AddCallback("PART", b.handleJoinPart) - } } func (b *Bridge) handleIrcBotCommand(event *irc.Event) bool { @@ -177,6 +203,9 @@ func (b *Bridge) ircNickFormat(nick string) string { } func (b *Bridge) handlePrivMsg(event *irc.Event) { + if b.ignoreMessage(event.Nick, event.Message(), "irc") { + return + } if b.handleIrcBotCommand(event) { return } @@ -238,81 +267,21 @@ func (b *Bridge) endNames(event *irc.Event) { b.MMirc.names[channel] = nil } -func (b *Bridge) handleTopicWhoTime(event *irc.Event) bool { +func (b *Bridge) handleTopicWhoTime(event *irc.Event) { parts := strings.Split(event.Arguments[2], "!") - t_i, err := strconv.ParseInt(event.Arguments[3], 10, 64) + t, err := strconv.ParseInt(event.Arguments[3], 10, 64) if err != nil { flog.irc.Errorf("Invalid time stamp: %s", event.Arguments[3]) - return false } user := parts[0] if len(parts) > 1 { user += " [" + parts[1] + "]" } - flog.irc.Infof("%s: Topic set by %s [%s]", event.Code, user, time.Unix(t_i, 0)) - return true + flog.irc.Infof("%s: Topic set by %s [%s]", event.Code, user, time.Unix(t, 0)) } func (b *Bridge) handleOther(event *irc.Event) { flog.irc.Debugf("%#v", event) - switch event.Code { - case ircm.RPL_WELCOME: - b.handleNewConnection(event) - case ircm.RPL_ENDOFNAMES: - b.endNames(event) - case ircm.RPL_NAMREPLY: - b.storeNames(event) - case ircm.RPL_ISUPPORT: - fallthrough - case ircm.RPL_LUSEROP: - fallthrough - case ircm.RPL_LUSERUNKNOWN: - fallthrough - case ircm.RPL_LUSERCHANNELS: - fallthrough - case ircm.RPL_MYINFO: - flog.irc.Infof("%s: %s", event.Code, strings.Join(event.Arguments[1:], " ")) - case ircm.RPL_YOURHOST: - fallthrough - case ircm.RPL_CREATED: - fallthrough - case ircm.RPL_STATSDLINE: - fallthrough - case ircm.RPL_LUSERCLIENT: - fallthrough - case ircm.RPL_LUSERME: - fallthrough - case ircm.RPL_LOCALUSERS: - fallthrough - case ircm.RPL_GLOBALUSERS: - fallthrough - case ircm.RPL_MOTD: - flog.irc.Infof("%s: %s", event.Code, event.Message()) - // flog.irc.Info(event.Message()) - case ircm.RPL_TOPIC: - flog.irc.Infof("%s: Topic for %s: %s", event.Code, event.Arguments[1], event.Message()) - case ircm.RPL_TOPICWHOTIME: - if !b.handleTopicWhoTime(event) { - break - } - case ircm.MODE: - flog.irc.Infof("%s: %s %s", event.Code, event.Arguments[1], event.Arguments[0]) - case ircm.JOIN: - fallthrough - case ircm.PING: - fallthrough - case ircm.PONG: - flog.irc.Infof("%s: %s", event.Code, event.Message()) - case ircm.RPL_ENDOFMOTD: - case ircm.RPL_MOTDSTART: - case ircm.ERR_NICKNAMEINUSE: - flog.irc.Warn(event.Message()) - case ircm.NOTICE: - b.handleNotice(event) - default: - flog.irc.Infof("UNKNOWN EVENT: %#v", event) - return - } } func (b *Bridge) Send(nick string, message string, channel string) error { @@ -327,7 +296,7 @@ func (b *Bridge) SendType(nick string, message string, channel string, mtype str message = nick + " " + message } } - if b.kind == "legacy" { + if b.kind == Legacy { matterMessage := matterhook.OMessage{IconURL: b.Config.Mattermost.IconURL} matterMessage.Channel = channel matterMessage.UserName = nick @@ -371,24 +340,34 @@ func (b *Bridge) handleMatterClient(mchan chan *MMMessage) { } func (b *Bridge) handleMatter() { + flog.mm.Infof("Choosing Mattermost connection type %s", b.kind) mchan := make(chan *MMMessage) - if b.kind == "legacy" { + if b.kind == Legacy { go b.handleMatterHook(mchan) } else { go b.handleMatterClient(mchan) } + flog.mm.Info("Start listening for Mattermost messages") for message := range mchan { var username string + if b.ignoreMessage(message.Username, message.Text, "mattermost") { + continue + } username = message.Username + ": " if b.Config.IRC.RemoteNickFormat != "" { username = strings.Replace(b.Config.IRC.RemoteNickFormat, "{NICK}", message.Username, -1) } else if b.Config.IRC.UseSlackCircumfix { username = "<" + message.Username + "> " } - cmd := strings.Fields(message.Text)[0] + cmds := strings.Fields(message.Text) + // empty message + if len(cmds) == 0 { + continue + } + cmd := cmds[0] switch cmd { case "!users": - flog.mm.Info("received !users from ", message.Username) + flog.mm.Info("Received !users from ", message.Username) b.i.SendRaw("NAMES " + b.getIRCChannel(message.Channel)) continue case "!gif": @@ -425,7 +404,7 @@ func (b *Bridge) getMMChannel(ircChannel string) string { } func (b *Bridge) getIRCChannel(channel string) string { - if b.kind == "legacy" { + if b.kind == Legacy { ircchannel := b.Config.IRC.Channel _, ok := b.Config.Token[channel] if ok { @@ -439,3 +418,17 @@ func (b *Bridge) getIRCChannel(channel string) string { } return ircchannel } + +func (b *Bridge) ignoreMessage(nick string, message string, protocol string) bool { + var ignoreNicks = b.mmIgnoreNicks + if protocol == "irc" { + ignoreNicks = b.ircIgnoreNicks + } + // should we discard messages ? + for _, entry := range ignoreNicks { + if nick == entry { + return true + } + } + return false +} diff --git a/vendor/github.com/42wim/matterbridge-plus/bridge/config.go b/vendor/github.com/42wim/matterbridge-plus/bridge/config.go index ca4eb225..f31db98d 100644 --- a/vendor/github.com/42wim/matterbridge-plus/bridge/config.go +++ b/vendor/github.com/42wim/matterbridge-plus/bridge/config.go @@ -19,6 +19,7 @@ type Config struct { NickServNick string NickServPassword string RemoteNickFormat string + IgnoreNicks string } Mattermost struct { URL string @@ -37,6 +38,8 @@ type Config struct { Login string Password string RemoteNickFormat *string + IgnoreNicks string + NoTLS bool } Token map[string]*struct { IRCChannel string diff --git a/vendor/github.com/42wim/matterbridge-plus/matterclient/matterclient.go b/vendor/github.com/42wim/matterbridge-plus/matterclient/matterclient.go index 13faf383..93128220 100644 --- a/vendor/github.com/42wim/matterbridge-plus/matterclient/matterclient.go +++ b/vendor/github.com/42wim/matterbridge-plus/matterclient/matterclient.go @@ -1,9 +1,12 @@ package matterclient import ( + "crypto/tls" "errors" log "github.com/Sirupsen/logrus" "net/http" + "net/http/cookiejar" + "net/url" "strings" "time" @@ -13,11 +16,12 @@ import ( ) type Credentials struct { - Login string - Team string - Pass string - Server string - NoTLS bool + Login string + Team string + Pass string + Server string + NoTLS bool + SkipTLSVerify bool } type Message struct { @@ -33,6 +37,8 @@ type MMClient struct { *Credentials Client *model.Client WsClient *websocket.Conn + WsQuit bool + WsAway bool Channels *model.ChannelList MoreChannels *model.ChannelList User *model.User @@ -60,6 +66,9 @@ func (m *MMClient) SetLogLevel(level string) { } func (m *MMClient) Login() error { + if m.WsQuit { + return nil + } b := &backoff.Backoff{ Min: time.Second, Max: 5 * time.Minute, @@ -73,12 +82,25 @@ func (m *MMClient) Login() error { } // login to mattermost m.Client = model.NewClient(uriScheme + m.Credentials.Server) + m.Client.HttpClient.Transport = &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}} var myinfo *model.Result var appErr *model.AppError var logmsg = "trying login" for { - m.log.Debugf(logmsg+" %s %s %s", m.Credentials.Team, m.Credentials.Login, m.Credentials.Server) - myinfo, appErr = m.Client.Login(m.Credentials.Login, m.Credentials.Pass) + m.log.Debugf("%s %s %s %s", logmsg, m.Credentials.Team, m.Credentials.Login, m.Credentials.Server) + if strings.Contains(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN) { + m.log.Debugf(logmsg+" with ", model.SESSION_COOKIE_TOKEN) + token := strings.Split(m.Credentials.Pass, model.SESSION_COOKIE_TOKEN+"=") + m.Client.HttpClient.Jar = m.createCookieJar(token[1]) + m.Client.MockSession(token[1]) + myinfo, appErr = m.Client.GetMe("") + if myinfo.Data.(*model.User) == nil { + m.log.Errorf("LOGIN TOKEN: %s is invalid", m.Credentials.Pass) + return errors.New("invalid " + model.SESSION_COOKIE_TOKEN) + } + } else { + myinfo, appErr = m.Client.Login(m.Credentials.Login, m.Credentials.Pass) + } if appErr != nil { d := b.Duration() m.log.Debug(appErr.DetailedError) @@ -89,7 +111,7 @@ func (m *MMClient) Login() error { } return errors.New(appErr.Message) } - m.log.Debug("LOGIN: %s, reconnecting in %s", appErr, d) + m.log.Debugf("LOGIN: %s, reconnecting in %s", appErr, d) time.Sleep(d) logmsg = "retrying login" continue @@ -98,15 +120,16 @@ func (m *MMClient) Login() error { } // reset timer b.Reset() - m.User = myinfo.Data.(*model.User) - teamdata, _ := m.Client.GetAllTeamListings() - teams := teamdata.Data.(map[string]*model.Team) - for k, v := range teams { + initLoad, _ := m.Client.GetInitialLoad() + initData := initLoad.Data.(*model.InitialLoad) + m.User = initData.User + for _, v := range initData.Teams { + m.log.Debugf("trying %s (id: %s)", v.Name, v.Id) if v.Name == m.Credentials.Team { - m.Client.SetTeamId(k) + m.Client.SetTeamId(v.Id) m.Team = v - m.log.Debug("GetallTeamListings: found id ", k) + m.log.Debugf("GetallTeamListings: found id %s for team %s", v.Id, v.Name) break } } @@ -119,13 +142,14 @@ func (m *MMClient) Login() error { header := http.Header{} header.Set(model.HEADER_AUTH, "BEARER "+m.Client.AuthToken) - var WsClient *websocket.Conn + m.log.Debug("WsClient: making connection") var err error for { - WsClient, _, err = websocket.DefaultDialer.Dial(wsurl, header) + wsDialer := &websocket.Dialer{Proxy: http.ProxyFromEnvironment, TLSClientConfig: &tls.Config{InsecureSkipVerify: m.SkipTLSVerify}} + m.WsClient, _, err = wsDialer.Dial(wsurl, header) if err != nil { d := b.Duration() - log.Printf("WSS: %s, reconnecting in %s", err, d) + m.log.Debugf("WSS: %s, reconnecting in %s", err, d) time.Sleep(d) continue } @@ -133,8 +157,6 @@ func (m *MMClient) Login() error { } b.Reset() - m.WsClient = WsClient - // populating users m.UpdateUsers() @@ -147,12 +169,19 @@ func (m *MMClient) Login() error { func (m *MMClient) WsReceiver() { var rmsg model.Message for { + if m.WsQuit { + m.log.Debug("exiting WsReceiver") + return + } if err := m.WsClient.ReadJSON(&rmsg); err != nil { - log.Println("error:", err) + m.log.Error("error:", err) // reconnect m.Login() } - //log.Printf("WsReceiver: %#v", rmsg) + if rmsg.Action == "ping" { + m.handleWsPing() + continue + } msg := &Message{Raw: &rmsg, Team: m.Credentials.Team} m.parseMessage(msg) m.MessageChan <- msg @@ -160,6 +189,14 @@ func (m *MMClient) WsReceiver() { } +func (m *MMClient) handleWsPing() { + m.log.Debug("Ws PING") + if !m.WsQuit && !m.WsAway { + m.log.Debug("Ws PONG") + m.WsClient.WriteMessage(websocket.PongMessage, []byte{}) + } +} + func (m *MMClient) parseMessage(rmsg *Message) { switch rmsg.Raw.Action { case model.ACTION_POSTED: @@ -198,7 +235,7 @@ func (m *MMClient) parseActionPost(rmsg *Message) { } func (m *MMClient) UpdateUsers() error { - mmusers, _ := m.Client.GetProfiles(m.Client.GetTeamId(), "") + mmusers, _ := m.Client.GetProfilesForDirectMessageList(m.Team.Id) m.Users = mmusers.Data.(map[string]*model.User) return nil } @@ -294,29 +331,49 @@ func (m *MMClient) GetPosts(channelId string, limit int) *model.PostList { return res.Data.(*model.PostList) } +func (m *MMClient) GetPublicLink(filename string) string { + res, err := m.Client.GetPublicLink(filename) + if err != nil { + return "" + } + return res.Data.(string) +} + +func (m *MMClient) GetPublicLinks(filenames []string) []string { + var output []string + for _, f := range filenames { + res, err := m.Client.GetPublicLink(f) + if err != nil { + continue + } + output = append(output, res.Data.(string)) + } + return output +} + func (m *MMClient) UpdateChannelHeader(channelId string, header string) { data := make(map[string]string) data["channel_id"] = channelId data["channel_header"] = header - log.Printf("updating channelheader %#v, %#v", channelId, header) + m.log.Debugf("updating channelheader %#v, %#v", channelId, header) _, err := m.Client.UpdateChannelHeader(data) if err != nil { - log.Print(err) + log.Error(err) } } func (m *MMClient) UpdateLastViewed(channelId string) { - log.Printf("posting lastview %#v", channelId) + m.log.Debugf("posting lastview %#v", channelId) _, err := m.Client.UpdateLastViewedAt(channelId) if err != nil { - log.Print(err) + m.log.Error(err) } } func (m *MMClient) UsernamesInChannel(channelName string) []string { ceiRes, err := m.Client.GetChannelExtraInfo(m.GetChannelId(channelName), 5000, "") if err != nil { - log.Errorf("UsernamesInChannel(%s) failed: %s", channelName, err) + m.log.Errorf("UsernamesInChannel(%s) failed: %s", channelName, err) return []string{} } extra := ceiRes.Data.(*model.ChannelExtra) @@ -326,3 +383,59 @@ func (m *MMClient) UsernamesInChannel(channelName string) []string { } return result } + +func (m *MMClient) createCookieJar(token string) *cookiejar.Jar { + var cookies []*http.Cookie + jar, _ := cookiejar.New(nil) + firstCookie := &http.Cookie{ + Name: "MMAUTHTOKEN", + Value: token, + Path: "/", + Domain: m.Credentials.Server, + } + cookies = append(cookies, firstCookie) + cookieURL, _ := url.Parse("https://" + m.Credentials.Server) + jar.SetCookies(cookieURL, cookies) + return jar +} + +func (m *MMClient) GetOtherUserDM(channel string) *model.User { + m.UpdateUsers() + var rcvuser *model.User + if strings.Contains(channel, "__") { + rcvusers := strings.Split(channel, "__") + if rcvusers[0] != m.User.Id { + rcvuser = m.Users[rcvusers[0]] + } else { + rcvuser = m.Users[rcvusers[1]] + } + } + return rcvuser +} + +func (m *MMClient) SendDirectMessage(toUserId string, msg string) { + m.log.Debugf("SendDirectMessage to %s, msg %s", toUserId, msg) + var channel string + // We don't have a DM with this user yet. + if m.GetChannelId(toUserId+"__"+m.User.Id) == "" && m.GetChannelId(m.User.Id+"__"+toUserId) == "" { + // create DM channel + _, err := m.Client.CreateDirectChannel(toUserId) + if err != nil { + m.log.Debugf("SendDirectMessage to %#v failed: %s", toUserId, err) + } + // update our channels + mmchannels, _ := m.Client.GetChannels("") + m.Channels = mmchannels.Data.(*model.ChannelList) + } + + // build the channel name + if toUserId > m.User.Id { + channel = m.User.Id + "__" + toUserId + } else { + channel = toUserId + "__" + m.User.Id + } + // build & send the message + msg = strings.Replace(msg, "\r", "", -1) + post := &model.Post{ChannelId: m.GetChannelId(channel), Message: msg} + m.Client.CreatePost(post) +} |