Modify Send() to return also a message id

This commit is contained in:
Wim 2017-08-27 22:59:37 +02:00
parent cfb8107138
commit 5a8d7b5f6d
13 changed files with 41 additions and 35 deletions

View File

@ -66,11 +66,11 @@ func (b *Api) JoinChannel(channel config.ChannelInfo) error {
} }
func (b *Api) Send(msg config.Message) error { func (b *Api) Send(msg config.Message) (string, error) {
b.Lock() b.Lock()
defer b.Unlock() defer b.Unlock()
b.Messages.Enqueue(&msg) b.Messages.Enqueue(&msg)
return nil return "", nil
} }
func (b *Api) handlePostMessage(c echo.Context) error { func (b *Api) handlePostMessage(c echo.Context) error {

View File

@ -19,7 +19,7 @@ import (
) )
type Bridger interface { type Bridger interface {
Send(msg config.Message) error Send(msg config.Message) (string, error)
Connect() error Connect() error
JoinChannel(channel config.ChannelInfo) error JoinChannel(channel config.ChannelInfo) error
Disconnect() error Disconnect() error

View File

@ -108,12 +108,12 @@ func (b *bdiscord) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *bdiscord) Send(msg config.Message) error { func (b *bdiscord) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
channelID := b.getChannelID(msg.Channel) channelID := b.getChannelID(msg.Channel)
if channelID == "" { if channelID == "" {
flog.Errorf("Could not find channelID for %v", msg.Channel) flog.Errorf("Could not find channelID for %v", msg.Channel)
return nil return "", nil
} }
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
msg.Text = "_" + msg.Text + "_" msg.Text = "_" + msg.Text + "_"
@ -142,7 +142,7 @@ func (b *bdiscord) Send(msg config.Message) error {
AvatarURL: msg.Avatar, AvatarURL: msg.Avatar,
}) })
} }
return nil return "", nil
} }
func (b *bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) { func (b *bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {

View File

@ -97,15 +97,15 @@ func (b *Bgitter) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Bgitter) Send(msg config.Message) error { func (b *Bgitter) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
roomID := b.getRoomID(msg.Channel) roomID := b.getRoomID(msg.Channel)
if roomID == "" { if roomID == "" {
flog.Errorf("Could not find roomID for %v", msg.Channel) flog.Errorf("Could not find roomID for %v", msg.Channel)
return nil return "", nil
} }
// 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, msg.Username+msg.Text+" ")
} }
func (b *Bgitter) getRoomID(channel string) string { func (b *Bgitter) getRoomID(channel string) string {

View File

@ -127,7 +127,7 @@ func (b *Birc) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Birc) Send(msg config.Message) error { func (b *Birc) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
if strings.HasPrefix(msg.Text, "!") { if strings.HasPrefix(msg.Text, "!") {
b.Command(&msg) b.Command(&msg)
@ -145,7 +145,7 @@ func (b *Birc) Send(msg config.Message) error {
flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local)) flog.Debugf("flooding, dropping message (queue at %d)", len(b.Local))
} }
} }
return nil return "", nil
} }
func (b *Birc) doSend() { func (b *Birc) doSend() {

View File

@ -74,17 +74,17 @@ func (b *Bmatrix) JoinChannel(channel config.ChannelInfo) error {
return err return err
} }
func (b *Bmatrix) Send(msg config.Message) error { func (b *Bmatrix) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
channel := b.getRoomID(msg.Channel) channel := b.getRoomID(msg.Channel)
flog.Debugf("Sending to channel %s", channel) flog.Debugf("Sending to channel %s", channel)
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
b.mc.SendMessageEvent(channel, "m.room.message", b.mc.SendMessageEvent(channel, "m.room.message",
matrix.TextMessage{"m.emote", msg.Username + msg.Text}) matrix.TextMessage{"m.emote", msg.Username + msg.Text})
return nil return "", nil
} }
b.mc.SendText(channel, msg.Username+msg.Text) b.mc.SendText(channel, msg.Username+msg.Text)
return nil return "", nil
} }
func (b *Bmatrix) getRoomID(channel string) string { func (b *Bmatrix) getRoomID(channel string) string {

View File

@ -136,7 +136,7 @@ func (b *Bmattermost) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Bmattermost) Send(msg config.Message) error { func (b *Bmattermost) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
msg.Text = "*" + msg.Text + "*" msg.Text = "*" + msg.Text + "*"
@ -158,12 +158,12 @@ func (b *Bmattermost) Send(msg config.Message) error {
err := b.mh.Send(matterMessage) err := b.mh.Send(matterMessage)
if err != nil { if err != nil {
flog.Info(err) flog.Info(err)
return err return "", err
} }
return nil return "", nil
} }
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() {

View File

@ -57,7 +57,7 @@ func (b *Brocketchat) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Brocketchat) Send(msg config.Message) error { func (b *Brocketchat) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL} matterMessage := matterhook.OMessage{IconURL: b.Config.IconURL}
matterMessage.Channel = msg.Channel matterMessage.Channel = msg.Channel
@ -67,9 +67,9 @@ func (b *Brocketchat) Send(msg config.Message) error {
err := b.mh.Send(matterMessage) err := b.mh.Send(matterMessage)
if err != nil { if err != nil {
flog.Info(err) flog.Info(err)
return err return "", err
} }
return nil return "", nil
} }
func (b *Brocketchat) handleRocketHook() { func (b *Brocketchat) handleRocketHook() {

View File

@ -125,7 +125,7 @@ func (b *Bslack) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Bslack) Send(msg config.Message) error { func (b *Bslack) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
msg.Text = "_" + msg.Text + "_" msg.Text = "_" + msg.Text + "_"
@ -145,13 +145,13 @@ func (b *Bslack) Send(msg config.Message) error {
err := b.mh.Send(matterMessage) err := b.mh.Send(matterMessage)
if err != nil { if err != nil {
flog.Info(err) flog.Info(err)
return err return "", err
} }
return nil return "", nil
} }
schannel, err := b.getChannelByName(channel) schannel, err := b.getChannelByName(channel)
if err != nil { if err != nil {
return err return "", err
} }
np := slack.NewPostMessageParameters() np := slack.NewPostMessageParameters()
if b.Config.PrefixMessagesWithNick { if b.Config.PrefixMessagesWithNick {
@ -170,7 +170,7 @@ func (b *Bslack) Send(msg config.Message) error {
b.rtm.SendMessage(newmsg) b.rtm.SendMessage(newmsg)
*/ */
return nil return "", nil
} }
func (b *Bslack) getAvatar(user string) string { func (b *Bslack) getAvatar(user string) string {

View File

@ -69,13 +69,13 @@ func (b *Bsteam) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Bsteam) Send(msg config.Message) error { func (b *Bsteam) Send(msg config.Message) (string, error) {
id, err := steamid.NewId(msg.Channel) id, err := steamid.NewId(msg.Channel)
if err != nil { if err != nil {
return err return "", err
} }
b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text) b.c.Social.SendMessage(id, steamlang.EChatEntryType_ChatMsg, msg.Username+msg.Text)
return nil return "", nil
} }
func (b *Bsteam) getNick(id steamid.SteamId) string { func (b *Bsteam) getNick(id steamid.SteamId) string {

View File

@ -57,11 +57,11 @@ func (b *Btelegram) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Btelegram) Send(msg config.Message) error { func (b *Btelegram) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
chatid, err := strconv.ParseInt(msg.Channel, 10, 64) chatid, err := strconv.ParseInt(msg.Channel, 10, 64)
if err != nil { if err != nil {
return err return "", err
} }
if b.Config.MessageFormat == "HTML" { if b.Config.MessageFormat == "HTML" {
@ -72,7 +72,7 @@ func (b *Btelegram) Send(msg config.Message) error {
m.ParseMode = tgbotapi.ModeHTML m.ParseMode = tgbotapi.ModeHTML
} }
_, err = b.c.Send(m) _, err = b.c.Send(m)
return err return "", err
} }
func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) { func (b *Btelegram) handleRecv(updates <-chan tgbotapi.Update) {

View File

@ -79,10 +79,10 @@ func (b *Bxmpp) JoinChannel(channel config.ChannelInfo) error {
return nil return nil
} }
func (b *Bxmpp) Send(msg config.Message) error { func (b *Bxmpp) Send(msg config.Message) (string, 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}) b.xc.Send(xmpp.Chat{Type: "groupchat", Remote: msg.Channel + "@" + b.Config.Muc, Text: msg.Username + msg.Text})
return nil return "", nil
} }
func (b *Bxmpp) createXMPP() (*xmpp.Client, error) { func (b *Bxmpp) createXMPP() (*xmpp.Client, error) {

View File

@ -21,6 +21,12 @@ type Gateway struct {
ChannelOptions map[string]config.ChannelOptions ChannelOptions map[string]config.ChannelOptions
Message chan config.Message Message chan config.Message
Name string Name string
Messages map[string][]*BridgeMsg
}
type BridgeMsg struct {
br *bridge.Bridge
ID string
} }
func New(cfg config.Gateway, r *Router) *Gateway { func New(cfg config.Gateway, r *Router) *Gateway {
@ -162,7 +168,7 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) {
if dest.Protocol == "api" { if dest.Protocol == "api" {
msg.Channel = originchannel msg.Channel = originchannel
} }
err := dest.Send(msg) _, err := dest.Send(msg)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }