Add support for dynamic IconURL (slack). Closes #43

This commit is contained in:
Wim 2016-11-05 01:11:28 +01:00
parent 50a0df4279
commit 2dbe0eb557
9 changed files with 61 additions and 36 deletions

View File

@ -122,3 +122,19 @@ func OverrideCfgFromEnv(cfg *Config, protocol string, account string) {
} }
} }
} }
func GetIconURL(msg *Message, cfg *Protocol) string {
iconURL := cfg.IconURL
iconURL = strings.Replace(iconURL, "{NICK}", msg.Username, -1)
iconURL = strings.Replace(iconURL, "{BRIDGE}", msg.Origin, -1)
iconURL = strings.Replace(iconURL, "{PROTOCOL}", msg.Protocol, -1)
return iconURL
}
func GetNick(msg *Message, cfg *Protocol) string {
nick := cfg.RemoteNickFormat
nick = strings.Replace(nick, "{NICK}", msg.Username, -1)
nick = strings.Replace(nick, "{BRIDGE}", msg.Origin, -1)
nick = strings.Replace(nick, "{PROTOCOL}", msg.Protocol, -1)
return nick
}

View File

@ -103,7 +103,8 @@ func (b *bdiscord) Send(msg config.Message) error {
flog.Errorf("Could not find channelID for %v", msg.Channel) flog.Errorf("Could not find channelID for %v", msg.Channel)
return nil return nil
} }
b.c.ChannelMessageSend(channelID, msg.Username+msg.Text) nick := config.GetNick(&msg, b.Config)
b.c.ChannelMessageSend(channelID, nick+msg.Text)
return nil return nil
} }

View File

@ -105,8 +105,9 @@ func (b *Bgitter) Send(msg config.Message) error {
flog.Errorf("Could not find roomID for %v", msg.Channel) flog.Errorf("Could not find roomID for %v", msg.Channel)
return nil return nil
} }
nick := config.GetNick(&msg, b.Config)
// add ZWSP because gitter echoes our own messages // add ZWSP because gitter echoes our own messages
return b.c.SendMessage(roomID, msg.Username+msg.Text+" ") return b.c.SendMessage(roomID, nick+msg.Text+" ")
} }
func (b *Bgitter) getRoomID(channel string) string { func (b *Bgitter) getRoomID(channel string) string {

View File

@ -123,12 +123,13 @@ func (b *Birc) Send(msg config.Message) error {
b.Command(&msg) b.Command(&msg)
return nil return nil
} }
nick := config.GetNick(&msg, b.Config)
for _, text := range strings.Split(msg.Text, "\n") { for _, text := range strings.Split(msg.Text, "\n") {
if len(b.Local) < b.Config.MessageQueue { if len(b.Local) < b.Config.MessageQueue {
if len(b.Local) == b.Config.MessageQueue-1 { if len(b.Local) == b.Config.MessageQueue-1 {
text = text + " <message clipped>" text = text + " <message clipped>"
} }
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel} b.Local <- config.Message{Text: text, Username: nick, Channel: msg.Channel}
} else { } else {
flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local)) flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
} }

View File

@ -106,10 +106,10 @@ func (b *Bmattermost) Protocol() string {
func (b *Bmattermost) Send(msg config.Message) error { func (b *Bmattermost) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
return b.SendType(msg.Username, msg.Text, msg.Channel, "") nick := config.GetNick(&msg, b.Config)
} message := msg.Text
channel := msg.Channel
func (b *Bmattermost) SendType(nick string, message string, channel string, mtype string) error {
if b.Config.PrefixMessagesWithNick { if b.Config.PrefixMessagesWithNick {
/*if IsMarkup(message) { /*if IsMarkup(message) {
message = nick + "\n\n" + message message = nick + "\n\n" + message
@ -122,7 +122,7 @@ func (b *Bmattermost) SendType(nick string, message string, channel string, mtyp
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
matterMessage.Type = mtype matterMessage.Type = ""
matterMessage.Text = message matterMessage.Text = message
err := b.mh.Send(matterMessage) err := b.mh.Send(matterMessage)
if err != nil { if err != nil {

View File

@ -94,13 +94,12 @@ func (b *Bslack) Origin() string {
func (b *Bslack) Send(msg config.Message) error { func (b *Bslack) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
if msg.FullOrigin != b.FullOrigin() { if msg.FullOrigin == b.FullOrigin() {
return b.SendType(msg.Username, msg.Text, msg.Channel, "")
}
return nil return nil
} }
nick := config.GetNick(&msg, b.Config)
func (b *Bslack) SendType(nick string, message string, channel string, mtype string) error { message := msg.Text
channel := msg.Channel
if b.Config.PrefixMessagesWithNick { if b.Config.PrefixMessagesWithNick {
message = nick + " " + message message = nick + " " + message
} }
@ -108,7 +107,7 @@ func (b *Bslack) SendType(nick string, message string, channel string, mtype str
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
matterMessage.Type = mtype matterMessage.Type = ""
matterMessage.Text = message matterMessage.Text = message
err := b.mh.Send(matterMessage) err := b.mh.Send(matterMessage)
if err != nil { if err != nil {
@ -121,8 +120,19 @@ func (b *Bslack) SendType(nick string, message string, channel string, mtype str
if err != nil { if err != nil {
return err return err
} }
np := slack.NewPostMessageParameters()
if b.Config.PrefixMessagesWithNick == true {
np.AsUser = true
}
np.Username = nick
np.IconURL = config.GetIconURL(&msg, b.Config)
b.sc.PostMessage(schannel.ID, message, np)
/*
newmsg := b.rtm.NewOutgoingMessage(message, schannel.ID) newmsg := b.rtm.NewOutgoingMessage(message, schannel.ID)
b.rtm.SendMessage(newmsg) b.rtm.SendMessage(newmsg)
*/
return nil return nil
} }

View File

@ -71,7 +71,8 @@ func (b *Bxmpp) Origin() string {
func (b *Bxmpp) Send(msg config.Message) error { func (b *Bxmpp) Send(msg config.Message) error {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text}) nick := config.GetNick(&msg, b.Config)
b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: nick + msg.Text})
return nil return nil
} }

View File

@ -117,7 +117,6 @@ func (gw *Gateway) handleMessage(msg config.Message, dest bridge.Bridge) {
log.Debug("empty channel") log.Debug("empty channel")
return return
} }
gw.modifyMessage(&msg, dest)
log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.FullOrigin, originchannel, dest.FullOrigin(), channel) log.Debugf("Sending %#v from %s (%s) to %s (%s)", msg, msg.FullOrigin, originchannel, dest.FullOrigin(), channel)
err := dest.Send(msg) err := dest.Send(msg)
if err != nil { if err != nil {
@ -144,16 +143,9 @@ func (gw *Gateway) modifyMessage(msg *config.Message, dest bridge.Bridge) {
if strings.ToLower(typeField.Name) == dest.Protocol() { if strings.ToLower(typeField.Name) == dest.Protocol() {
// get the Protocol struct from the map // get the Protocol struct from the map
protoCfg := val.Field(i).MapIndex(reflect.ValueOf(dest.Origin())) protoCfg := val.Field(i).MapIndex(reflect.ValueOf(dest.Origin()))
setNickFormat(msg, protoCfg.Interface().(config.Protocol)) //config.SetNickFormat(msg, protoCfg.Interface().(config.Protocol))
val.Field(i).SetMapIndex(reflect.ValueOf(dest.Origin()), protoCfg) val.Field(i).SetMapIndex(reflect.ValueOf(dest.Origin()), protoCfg)
break break
} }
} }
} }
func setNickFormat(msg *config.Message, cfg config.Protocol) {
format := cfg.RemoteNickFormat
msg.Username = strings.Replace(format, "{NICK}", msg.Username, -1)
msg.Username = strings.Replace(msg.Username, "{BRIDGE}", msg.Origin, -1)
msg.Username = strings.Replace(msg.Username, "{PROTOCOL}", msg.Protocol, -1)
}

View File

@ -41,7 +41,7 @@ NickServPassword="secret"
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge #The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge #The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge
#OPTIONAL (default {BRIDGE}-{NICK}) #OPTIONAL (default empty)
RemoteNickFormat="[{PROTOCOL}] <{NICK}> " RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
#Nicks you want to ignore. #Nicks you want to ignore.
@ -166,7 +166,7 @@ PrefixMessagesWithNick=false
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge #The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge #The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge
#OPTIONAL (default {BRIDGE}-{NICK}) #OPTIONAL (default empty)
RemoteNickFormat="[{PROTOCOL}] <{NICK}> " RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
#how to format the list of IRC nicks when displayed in mattermost. #how to format the list of IRC nicks when displayed in mattermost.
@ -205,7 +205,7 @@ IgnoreNicks="spammer1 spammer2"
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge #The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge #The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge
#OPTIONAL (default {BRIDGE}-{NICK}) #OPTIONAL (default empty)
RemoteNickFormat="[{PROTOCOL}] <{NICK}> " RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
################################################################### ###################################################################
@ -232,10 +232,6 @@ URL="https://hooks.slack.com/services/yourhook"
#REQUIRED (unless useAPI=true) #REQUIRED (unless useAPI=true)
BindAddress="0.0.0.0:9999" BindAddress="0.0.0.0:9999"
#Icon that will be showed in slack
#OPTIONAL
IconURL="http://youricon.png"
#### Settings for using slack API #### Settings for using slack API
#OPTIONAL #OPTIONAL
useAPI=false useAPI=false
@ -246,7 +242,14 @@ Token="yourslacktoken"
#### Shared settings for webhooks and API #### Shared settings for webhooks and API
#Whether to prefix messages from other bridges to mattermost with the sender's nick. #Icon that will be showed in slack
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge
#OPTIONAL
IconURL="https://robohash.org/{NICK}.png?size=48x48"
#Whether to prefix messages from other bridges to mattermost with RemoteNickFormat
#Useful if username overrides for incoming webhooks isn't enabled on the #Useful if username overrides for incoming webhooks isn't enabled on the
#slack server. If you set PrefixMessagesWithNick to true, each message #slack server. If you set PrefixMessagesWithNick to true, each message
#from bridge to Slack will by default be prefixed by "bridge-" + nick. You can, #from bridge to Slack will by default be prefixed by "bridge-" + nick. You can,
@ -257,8 +260,8 @@ PrefixMessagesWithNick=false
#RemoteNickFormat defines how remote users appear on this bridge #RemoteNickFormat defines how remote users appear on this bridge
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge #The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
#OPTIONAL (default {BRIDGE}-{NICK})
#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge #The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge
#OPTIONAL (default empty)
RemoteNickFormat="[{PROTOCOL}] <{NICK}> " RemoteNickFormat="[{PROTOCOL}] <{NICK}> "
#how to format the list of IRC nicks when displayed in slack #how to format the list of IRC nicks when displayed in slack
@ -300,7 +303,7 @@ IgnoreNicks="spammer1 spammer2"
#The string "{NICK}" (case sensitive) will be replaced by the actual nick / username. #The string "{NICK}" (case sensitive) will be replaced by the actual nick / username.
#The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge #The string "{BRIDGE}" (case sensitive) will be replaced by the sending bridge
#The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge #The string "{PROTOCOL}" (case sensitive) will be replaced by the protocol used by the bridge
#OPTIONAL (default {BRIDGE}-{NICK}) #OPTIONAL (default empty)
RemoteNickFormat="[{PROTOCOL}] <{NICK}> " RemoteNickFormat="[{PROTOCOL}] <{NICK}> "