5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-25 20:11:36 +00:00

Clean up various stuff (#508)

* various cleanups
This commit is contained in:
David Hill 2018-11-07 14:36:50 -05:00 committed by Wim
parent 141a42a75b
commit 0e2522279e
11 changed files with 77 additions and 54 deletions

View File

@ -110,8 +110,8 @@ func (b *Api) handleStream(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON) c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c.Response().WriteHeader(http.StatusOK) c.Response().WriteHeader(http.StatusOK)
greet := config.Message{ greet := config.Message{
Event:config.EVENT_API_CONNECTED, Event: config.EVENT_API_CONNECTED,
Timestamp:time.Now(), Timestamp: time.Now(),
} }
if err := json.NewEncoder(c.Response()).Encode(greet); err != nil { if err := json.NewEncoder(c.Response()).Encode(greet); err != nil {
return err return err

View File

@ -91,6 +91,7 @@ func (b *Bdiscord) Connect() error {
} }
// obtaining guild members and initializing nickname mapping // obtaining guild members and initializing nickname mapping
b.Lock() b.Lock()
defer b.Unlock()
members, err := b.c.GuildMembers(b.guildID, "", 1000) members, err := b.c.GuildMembers(b.guildID, "", 1000)
if err != nil { if err != nil {
b.Log.Error("Error obtaining guild members", err) b.Log.Error("Error obtaining guild members", err)
@ -103,7 +104,6 @@ func (b *Bdiscord) Connect() error {
b.nickMemberMap[member.Nick] = member b.nickMemberMap[member.Nick] = member
} }
} }
b.Unlock()
return nil return nil
} }
@ -488,9 +488,16 @@ func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (stri
var err error var err error
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
files := []*discordgo.File{} file := discordgo.File{
files = append(files, &discordgo.File{fi.Name, "", bytes.NewReader(*fi.Data)}) Name: fi.Name,
_, err = b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{Content: msg.Username + fi.Comment, Files: files}) ContentType: "",
Reader: bytes.NewReader(*fi.Data),
}
m := discordgo.MessageSend{
Content: msg.Username + fi.Comment,
Files: []*discordgo.File{&file},
}
_, err = b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil { if err != nil {
return "", fmt.Errorf("file upload failed: %#v", err) return "", fmt.Errorf("file upload failed: %#v", err)
} }

View File

@ -106,7 +106,10 @@ func (b *Birc) Connect() error {
}) })
if b.GetBool("UseSASL") { if b.GetBool("UseSASL") {
i.Config.SASL = &girc.SASLPlain{b.GetString("NickServNick"), b.GetString("NickServPassword")} i.Config.SASL = &girc.SASLPlain{
User: b.GetString("NickServNick"),
Pass: b.GetString("NickServPassword"),
}
} }
i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection) i.Handlers.Add(girc.RPL_WELCOME, b.handleNewConnection)

View File

@ -73,8 +73,11 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
// Make a action /me of the message // Make a action /me of the message
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
resp, err := b.mc.SendMessageEvent(channel, "m.room.message", m := matrix.TextMessage{
matrix.TextMessage{"m.emote", msg.Username + msg.Text}) MsgType: "m.emote",
Body: msg.Username + msg.Text,
}
resp, err := b.mc.SendMessageEvent(channel, "m.room.message", m)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -158,7 +161,8 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) {
// Text must be a string // Text must be a string
if rmsg.Text, ok = ev.Content["body"].(string); !ok { if rmsg.Text, ok = ev.Content["body"].(string); !ok {
b.Log.Errorf("Content[body] wasn't a %T ?", rmsg.Text) b.Log.Errorf("Content[body] is not a string: %T\n%#v",
ev.Content["body"], ev.Content)
return return
} }

View File

@ -174,7 +174,6 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er
ParentID: ev.ThreadTimestamp, ParentID: ev.ThreadTimestamp,
} }
if b.useChannelID { if b.useChannelID {
rmsg.Channel = "ID:" + channelInfo.ID rmsg.Channel = "ID:" + channelInfo.ID
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "time"
"github.com/nlopes/slack" "github.com/nlopes/slack"
@ -61,22 +60,17 @@ func (b *Bslack) getChannelByID(ID string) (*slack.Channel, error) {
const minimumRefreshInterval = 10 * time.Second const minimumRefreshInterval = 10 * time.Second
var (
refreshMutex sync.Mutex
refreshInProgress bool
earliestChannelRefresh = time.Now()
earliestUserRefresh = time.Now()
)
func (b *Bslack) populateUsers() { func (b *Bslack) populateUsers() {
refreshMutex.Lock() b.refreshMutex.Lock()
if time.Now().Before(earliestUserRefresh) || refreshInProgress { if time.Now().Before(b.earliestUserRefresh) || b.refreshInProgress {
b.Log.Debugf("Not refreshing user list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds())) b.Log.Debugf("Not refreshing user list as it was done less than %v ago.",
refreshMutex.Unlock() minimumRefreshInterval)
b.refreshMutex.Unlock()
return return
} }
refreshInProgress = true b.refreshInProgress = true
refreshMutex.Unlock() b.refreshMutex.Unlock()
users, err := b.sc.GetUsers() users, err := b.sc.GetUsers()
if err != nil { if err != nil {
@ -95,19 +89,22 @@ func (b *Bslack) populateUsers() {
defer b.usersMutex.Unlock() defer b.usersMutex.Unlock()
b.users = newUsers b.users = newUsers
earliestUserRefresh = time.Now().Add(minimumRefreshInterval) b.refreshMutex.Lock()
refreshInProgress = false defer b.refreshMutex.Unlock()
b.earliestUserRefresh = time.Now().Add(minimumRefreshInterval)
b.refreshInProgress = false
} }
func (b *Bslack) populateChannels() { func (b *Bslack) populateChannels() {
refreshMutex.Lock() b.refreshMutex.Lock()
if time.Now().Before(earliestChannelRefresh) || refreshInProgress { if time.Now().Before(b.earliestChannelRefresh) || b.refreshInProgress {
b.Log.Debugf("Not refreshing channel list as it was done less than %d seconds ago.", int(minimumRefreshInterval.Seconds())) b.Log.Debugf("Not refreshing channel list as it was done less than %v seconds ago.",
refreshMutex.Unlock() minimumRefreshInterval)
b.refreshMutex.Unlock()
return return
} }
refreshInProgress = true b.refreshInProgress = true
refreshMutex.Unlock() b.refreshMutex.Unlock()
newChannelsByID := map[string]*slack.Channel{} newChannelsByID := map[string]*slack.Channel{}
newChannelsByName := map[string]*slack.Channel{} newChannelsByName := map[string]*slack.Channel{}
@ -139,8 +136,10 @@ func (b *Bslack) populateChannels() {
b.channelsByID = newChannelsByID b.channelsByID = newChannelsByID
b.channelsByName = newChannelsByName b.channelsByName = newChannelsByName
earliestChannelRefresh = time.Now().Add(minimumRefreshInterval) b.refreshMutex.Lock()
refreshInProgress = false defer b.refreshMutex.Unlock()
b.earliestChannelRefresh = time.Now().Add(minimumRefreshInterval)
b.refreshInProgress = false
} }
var ( var (

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"sync" "sync"
"time"
"github.com/42wim/matterbridge/bridge" "github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
@ -34,6 +35,11 @@ type Bslack struct {
channelsByID map[string]*slack.Channel channelsByID map[string]*slack.Channel
channelsByName map[string]*slack.Channel channelsByName map[string]*slack.Channel
channelsMutex sync.RWMutex channelsMutex sync.RWMutex
refreshInProgress bool
earliestChannelRefresh time.Time
earliestUserRefresh time.Time
refreshMutex sync.Mutex
} }
const ( const (
@ -74,6 +80,8 @@ func New(cfg *bridge.Config) bridge.Bridger {
users: map[string]*slack.User{}, users: map[string]*slack.User{},
channelsByID: map[string]*slack.Channel{}, channelsByID: map[string]*slack.Channel{},
channelsByName: map[string]*slack.Channel{}, channelsByName: map[string]*slack.Channel{},
earliestChannelRefresh: time.Now(),
earliestUserRefresh: time.Now(),
} }
return b return b
} }

View File

@ -377,7 +377,10 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string,
var c tgbotapi.Chattable var c tgbotapi.Chattable
for _, f := range msg.Extra["file"] { for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) fi := f.(config.FileInfo)
file := tgbotapi.FileBytes{fi.Name, *fi.Data} file := tgbotapi.FileBytes{
Name: fi.Name,
Bytes: *fi.Data,
}
re := regexp.MustCompile(".(jpg|png)$") re := regexp.MustCompile(".(jpg|png)$")
if re.MatchString(fi.Name) { if re.MatchString(fi.Name) {
c = tgbotapi.NewPhotoUpload(chatid, file) c = tgbotapi.NewPhotoUpload(chatid, file)

View File

@ -75,8 +75,6 @@ func (b *Bxmpp) JoinChannel(channel config.ChannelInfo) error {
} }
func (b *Bxmpp) Send(msg config.Message) (string, error) { func (b *Bxmpp) Send(msg config.Message) (string, error) {
var msgid = ""
var msgreplaceid = ""
// ignore delete messages // ignore delete messages
if msg.Event == config.EVENT_MSG_DELETE { if msg.Event == config.EVENT_MSG_DELETE {
return "", nil return "", nil
@ -93,7 +91,8 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
} }
} }
msgid = xid.New().String() var msgreplaceid string
msgid := xid.New().String()
if msg.ID != "" { if msg.ID != "" {
msgid = msg.ID msgid = msg.ID
msgreplaceid = msg.ID msgreplaceid = msg.ID

View File

@ -365,7 +365,8 @@ func (m *MMClient) parseActionPost(rmsg *Message) {
data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string))) data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))
// we don't have the user, refresh the userlist // we don't have the user, refresh the userlist
if m.GetUser(data.UserId) == nil { if m.GetUser(data.UserId) == nil {
m.log.Infof("User %s is not known, ignoring message %s", data.UserId, data.Message) m.log.Infof("User '%v' is not known, ignoring message '%#v'",
data.UserId, data)
return return
} }
rmsg.Username = m.GetUserName(data.UserId) rmsg.Username = m.GetUserName(data.UserId)
@ -896,7 +897,7 @@ func (m *MMClient) StatusLoop() {
if m.OnWsConnect != nil { if m.OnWsConnect != nil {
m.OnWsConnect() m.OnWsConnect()
} }
m.log.Debugf("StatusLoop: %p", m.OnWsConnect) m.log.Debug("StatusLoop:", m.OnWsConnect != nil)
for { for {
if m.WsQuit { if m.WsQuit {
return return