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().WriteHeader(http.StatusOK)
greet := config.Message{
Event:config.EVENT_API_CONNECTED,
Timestamp:time.Now(),
Event: config.EVENT_API_CONNECTED,
Timestamp: time.Now(),
}
if err := json.NewEncoder(c.Response()).Encode(greet); err != nil {
return err

View File

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

View File

@ -106,7 +106,10 @@ func (b *Birc) Connect() error {
})
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)

View File

@ -73,8 +73,11 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {
// Make a action /me of the message
if msg.Event == config.EVENT_USER_ACTION {
resp, err := b.mc.SendMessageEvent(channel, "m.room.message",
matrix.TextMessage{"m.emote", msg.Username + msg.Text})
m := matrix.TextMessage{
MsgType: "m.emote",
Body: msg.Username + msg.Text,
}
resp, err := b.mc.SendMessageEvent(channel, "m.room.message", m)
if err != nil {
return "", err
}
@ -158,7 +161,8 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) {
// Text must be a string
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
}

View File

@ -166,15 +166,14 @@ func (b *Bslack) handleMessageEvent(ev *slack.MessageEvent) (*config.Message, er
}
rmsg := config.Message{
Text: ev.Text,
Channel: channelInfo.Name,
Account: b.Account,
ID: "slack " + ev.Timestamp,
Extra: map[string][]interface{}{},
Text: ev.Text,
Channel: channelInfo.Name,
Account: b.Account,
ID: "slack " + ev.Timestamp,
Extra: map[string][]interface{}{},
ParentID: ev.ThreadTimestamp,
}
if b.useChannelID {
rmsg.Channel = "ID:" + channelInfo.ID
}

View File

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

View File

@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"sync"
"time"
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
@ -34,6 +35,11 @@ type Bslack struct {
channelsByID map[string]*slack.Channel
channelsByName map[string]*slack.Channel
channelsMutex sync.RWMutex
refreshInProgress bool
earliestChannelRefresh time.Time
earliestUserRefresh time.Time
refreshMutex sync.Mutex
}
const (
@ -68,12 +74,14 @@ func New(cfg *bridge.Config) bridge.Bridger {
cfg.Log.Fatalf("Could not create LRU cache for Slack bridge: %v", err)
}
b := &Bslack{
Config: cfg,
uuid: xid.New().String(),
cache: newCache,
users: map[string]*slack.User{},
channelsByID: map[string]*slack.Channel{},
channelsByName: map[string]*slack.Channel{},
Config: cfg,
uuid: xid.New().String(),
cache: newCache,
users: map[string]*slack.User{},
channelsByID: map[string]*slack.Channel{},
channelsByName: map[string]*slack.Channel{},
earliestChannelRefresh: time.Now(),
earliestUserRefresh: time.Now(),
}
return b
}

View File

@ -377,7 +377,10 @@ func (b *Btelegram) handleUploadFile(msg *config.Message, chatid int64) (string,
var c tgbotapi.Chattable
for _, f := range msg.Extra["file"] {
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)$")
if re.MatchString(fi.Name) {
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) {
var msgid = ""
var msgreplaceid = ""
// ignore delete messages
if msg.Event == config.EVENT_MSG_DELETE {
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 != "" {
msgid = msg.ID
msgreplaceid = msg.ID

View File

@ -310,9 +310,9 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
msg.Channel = originchannel
}
msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel)
msg.ParentID = gw.getDestMsgID(canonicalParentMsgID, dest, channel)
if msg.ParentID == "" {
msg.ParentID = canonicalParentMsgID
msg.ParentID = canonicalParentMsgID
}
mID, err := dest.Send(msg)

View File

@ -365,7 +365,8 @@ func (m *MMClient) parseActionPost(rmsg *Message) {
data := model.PostFromJson(strings.NewReader(rmsg.Raw.Data["post"].(string)))
// we don't have the user, refresh the userlist
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
}
rmsg.Username = m.GetUserName(data.UserId)
@ -896,7 +897,7 @@ func (m *MMClient) StatusLoop() {
if m.OnWsConnect != nil {
m.OnWsConnect()
}
m.log.Debugf("StatusLoop: %p", m.OnWsConnect)
m.log.Debug("StatusLoop:", m.OnWsConnect != nil)
for {
if m.WsQuit {
return