Cleanup mattermost bridge debug/info messages

This commit is contained in:
Wim 2016-09-19 23:58:57 +02:00
parent 18504ec08d
commit 002c5fd0d1
2 changed files with 17 additions and 34 deletions

View File

@ -28,23 +28,17 @@ type Bmattermost struct {
MMhook MMhook
MMapi MMapi
Config *config.Protocol Config *config.Protocol
Plus bool
Remote chan config.Message Remote chan config.Message
name string name string
origin string origin string
protocol string protocol string
} }
type FancyLog struct { var flog *log.Entry
mm *log.Entry var protocol = "mattermost"
}
var flog FancyLog
const Legacy = "legacy"
func init() { func init() {
flog.mm = log.WithFields(log.Fields{"module": "mattermost"}) flog = log.WithFields(log.Fields{"module": protocol})
} }
func New(cfg config.Protocol, origin string, c chan config.Message) *Bmattermost { func New(cfg config.Protocol, origin string, c chan config.Message) *Bmattermost {
@ -54,7 +48,6 @@ func New(cfg config.Protocol, origin string, c chan config.Message) *Bmattermost
b.Remote = c b.Remote = c
b.protocol = "mattermost" b.protocol = "mattermost"
b.name = cfg.Name b.name = cfg.Name
b.Plus = cfg.UseAPI
b.mmMap = make(map[string]string) b.mmMap = make(map[string]string)
return b return b
} }
@ -64,7 +57,8 @@ func (b *Bmattermost) Command(cmd string) string {
} }
func (b *Bmattermost) Connect() error { func (b *Bmattermost) Connect() error {
if !b.Plus { if !b.Config.UseAPI {
flog.Info("Connecting webhooks")
b.mh = matterhook.New(b.Config.URL, b.mh = matterhook.New(b.Config.URL,
matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify, matterhook.Config{InsecureSkipVerify: b.Config.SkipTLSVerify,
BindAddress: b.Config.BindAddress}) BindAddress: b.Config.BindAddress})
@ -73,18 +67,12 @@ func (b *Bmattermost) Connect() error {
b.Config.Team, b.Config.Server) b.Config.Team, b.Config.Server)
b.mc.SkipTLSVerify = b.Config.SkipTLSVerify b.mc.SkipTLSVerify = b.Config.SkipTLSVerify
b.mc.NoTLS = b.Config.NoTLS b.mc.NoTLS = b.Config.NoTLS
flog.mm.Infof("Trying login %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server) flog.Infof("Connecting %s (team: %s) on %s", b.Config.Login, b.Config.Team, b.Config.Server)
err := b.mc.Login() err := b.mc.Login()
if err != nil { if err != nil {
return err return err
} }
flog.mm.Info("Login ok") flog.Info("Connection succeeded")
/*
b.mc.JoinChannel(b.Config.Channel)
for _, val := range b.Config.Channel {
b.mc.JoinChannel(val.Mattermost)
}
*/
go b.mc.WsReceiver() go b.mc.WsReceiver()
} }
go b.handleMatter() go b.handleMatter()
@ -112,8 +100,8 @@ func (b *Bmattermost) Protocol() string {
} }
func (b *Bmattermost) Send(msg config.Message) error { func (b *Bmattermost) Send(msg config.Message) error {
flog.mm.Infof("mattermost send %#v", msg) flog.Debugf("Receiving %#v", msg)
if msg.Origin != b.origin { if msg.FullOrigin != b.FullOrigin() {
return b.SendType(msg.Username, msg.Text, msg.Channel, "") return b.SendType(msg.Username, msg.Text, msg.Channel, "")
} }
return nil return nil
@ -128,7 +116,7 @@ func (b *Bmattermost) SendType(nick string, message string, channel string, mtyp
message = nick + " " + message message = nick + " " + message
//} //}
} }
if !b.Plus { if !b.Config.UseAPI {
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
matterMessage.Channel = channel matterMessage.Channel = channel
matterMessage.UserName = nick matterMessage.UserName = nick
@ -136,30 +124,27 @@ func (b *Bmattermost) SendType(nick string, message string, channel string, mtyp
matterMessage.Text = message matterMessage.Text = message
err := b.mh.Send(matterMessage) err := b.mh.Send(matterMessage)
if err != nil { if err != nil {
flog.mm.Info(err) flog.Info(err)
return err return err
} }
flog.mm.Debug("->mattermost channel: ", channel, " ", message)
return nil return nil
} }
flog.mm.Debug("->mattermost channel plus: ", channel, " ", message)
b.mc.PostMessage(b.mc.GetChannelId(channel, ""), message) b.mc.PostMessage(b.mc.GetChannelId(channel, ""), message)
return nil return nil
} }
func (b *Bmattermost) handleMatter() { func (b *Bmattermost) handleMatter() {
flog.mm.Infof("Choosing API based Mattermost connection: %t", b.Plus) flog.Debugf("Choosing API based Mattermost connection: %t", b.Config.UseAPI)
mchan := make(chan *MMMessage) mchan := make(chan *MMMessage)
if b.Plus { if b.Config.UseAPI {
go b.handleMatterClient(mchan) go b.handleMatterClient(mchan)
} else { } else {
go b.handleMatterHook(mchan) go b.handleMatterHook(mchan)
} }
flog.mm.Info("Start listening for Mattermost messages")
for message := range mchan { for message := range mchan {
texts := strings.Split(message.Text, "\n") texts := strings.Split(message.Text, "\n")
for _, text := range texts { for _, text := range texts {
flog.mm.Debug("Sending message from " + message.Username + " to " + message.Channel) flog.Debugf("Sending message from %s on %s to gateway", message.Username, b.FullOrigin())
b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()} b.Remote <- config.Message{Text: text, Username: message.Username, Channel: message.Channel, Origin: b.origin, Protocol: b.protocol, FullOrigin: b.FullOrigin()}
} }
} }
@ -169,8 +154,7 @@ func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) {
for message := range b.mc.MessageChan { for message := range b.mc.MessageChan {
// do not post our own messages back to irc // do not post our own messages back to irc
if message.Raw.Event == "posted" && b.mc.User.Username != message.Username { if message.Raw.Event == "posted" && b.mc.User.Username != message.Username {
flog.mm.Debugf("receiving from matterclient %#v", message) flog.Debugf("Receiving from matterclient %#v", message)
flog.mm.Debugf("receiving from matterclient %#v", message.Raw)
m := &MMMessage{} m := &MMMessage{}
m.Username = message.Username m.Username = message.Username
m.Channel = message.Channel m.Channel = message.Channel
@ -183,7 +167,7 @@ func (b *Bmattermost) handleMatterClient(mchan chan *MMMessage) {
func (b *Bmattermost) handleMatterHook(mchan chan *MMMessage) { func (b *Bmattermost) handleMatterHook(mchan chan *MMMessage) {
for { for {
message := b.mh.Receive() message := b.mh.Receive()
flog.mm.Debugf("receiving from matterhook %#v", message) flog.Debugf("Receiving from matterhook %#v", message)
m := &MMMessage{} m := &MMMessage{}
m.Username = message.UserName m.Username = message.UserName
m.Text = message.Text m.Text = message.Text

View File

@ -601,7 +601,6 @@ func (m *MMClient) StatusLoop() {
func (m *MMClient) initUser() error { func (m *MMClient) initUser() error {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
m.log.Debug("initUser()")
initLoad, err := m.Client.GetInitialLoad() initLoad, err := m.Client.GetInitialLoad()
if err != nil { if err != nil {
return err return err
@ -610,7 +609,7 @@ func (m *MMClient) initUser() error {
m.User = initData.User m.User = initData.User
// we only load all team data on initial login. // we only load all team data on initial login.
// all other updates are for channels from our (primary) team only. // all other updates are for channels from our (primary) team only.
m.log.Debug("initUser(): loading all team data") //m.log.Debug("initUser(): loading all team data")
for _, v := range initData.Teams { for _, v := range initData.Teams {
m.Client.SetTeamId(v.Id) m.Client.SetTeamId(v.Id)
mmusers, _ := m.Client.GetProfiles(v.Id, "") mmusers, _ := m.Client.GetProfiles(v.Id, "")