Skip to content

Commit

Permalink
Remove mattermost 5 from service/userbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
42wim committed Nov 12, 2022
1 parent ccc0bc3 commit da742e9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 330 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Tested on FreeBSD / Linux / Windows

## Compatibility

- Matterircd works with slack and mattermost 5.x, 6.x, 7.x
- Matterircd works with slack and mattermost 6.x, 7.x

Master branch of matterircd should always work against latest STABLE mattermost release.

Expand Down
12 changes: 7 additions & 5 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (m *Mattermost) loginToMattermost(onWsConnect func()) (*matterclient.Client
return mc, nil
}

// nolint:cyclop
//nolint:cyclop
func (m *Mattermost) handleWsMessage(quitChan chan struct{}) {
updateChannelsThrottle := time.NewTicker(time.Second * 60)

Expand Down Expand Up @@ -652,7 +652,7 @@ func (m *Mattermost) createUser(mmuser *model.User) *bridge.UserInfo {
return info
}

// nolint:cyclop
//nolint:cyclop
func isValidNick(s string) bool {
/* IRC RFC ([0] - see below) mentions a limit of 9 chars for
* IRC nicks, but modern clients allow more than that. Let's
Expand Down Expand Up @@ -689,6 +689,7 @@ func isValidNick(s string) bool {
return true
}

//nolint:forcetypeassert
func (m *Mattermost) wsActionPostSkip(rmsg *model.WebSocketEvent) bool {
var data model.Post
if err := json.NewDecoder(strings.NewReader(rmsg.GetData()["post"].(string))).Decode(&data); err != nil {
Expand Down Expand Up @@ -720,7 +721,7 @@ func (m *Mattermost) wsActionPostSkip(rmsg *model.WebSocketEvent) bool {
// maybeShorten returns a prefix of msg that is approximately newLen
// characters long, followed by "...". Words that start with uncounted
// are included in the result but are not reckoned against newLen.
// nolint:cyclop
//nolint:cyclop
func maybeShorten(msg string, newLen int, uncounted string, unicode bool) string {
if newLen == 0 || len(msg) < newLen {
return msg
Expand Down Expand Up @@ -755,7 +756,7 @@ func maybeShorten(msg string, newLen int, uncounted string, unicode bool) string
return fmt.Sprintf("%s %s", newMsg, ellipsis)
}

// nolint:funlen,gocognit,gocyclo,cyclop
//nolint:funlen,gocognit,gocyclo,cyclop,forcetypeassert
func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
var data model.Post
if err := json.NewDecoder(strings.NewReader(rmsg.GetData()["post"].(string))).Decode(&data); err != nil {
Expand All @@ -773,7 +774,7 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
if data.RootId != "" {
parentPost, _, err := m.mc.Client.GetPost(data.RootId, "")
if err != nil {
logger.Errorf("Unable to get parent post for %#v", data) // nolint:govet
logger.Errorf("Unable to get parent post for %#v", data) //nolint:govet
} else {
parentGhost := m.GetUser(parentPost.UserId)

Expand Down Expand Up @@ -1206,6 +1207,7 @@ func (m *Mattermost) handleStatusChangeEvent(rmsg *model.WebSocketEvent) {
m.eventChan <- event
}

//nolint:forcetypeassert
func (m *Mattermost) handleReactionEvent(rmsg *model.WebSocketEvent) {
var reaction model.Reaction
if err := json.NewDecoder(strings.NewReader(rmsg.GetData()["reaction"].(string))).Decode(&reaction); err != nil {
Expand Down
181 changes: 6 additions & 175 deletions mm-go-irckit/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"unicode"

"github.com/42wim/matterircd/bridge"
"github.com/mattermost/mattermost-server/v5/model"
model6 "github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/model"
)

type CommandHandler interface {
Expand Down Expand Up @@ -197,13 +196,8 @@ func login(u *User, toUser *User, args []string, service string) {
u.MsgUser(toUser, "login OK")
}

//nolint:cyclop
func search(u *User, toUser *User, args []string, service string) {
if strings.HasPrefix(u.getMattermostVersion(), "6.") || strings.HasPrefix(u.getMattermostVersion(), "7.") {
search6(u, toUser, args, service)

return
}

if service == "slack" {
u.MsgUser(toUser, "not implemented")
return
Expand All @@ -216,53 +210,7 @@ func search(u *User, toUser *User, args []string, service string) {
return
}

postlist := list.(*model.PostList)

for i := len(postlist.Order) - 1; i >= 0; i-- {
if postlist.Posts[postlist.Order[i]].DeleteAt > postlist.Posts[postlist.Order[i]].CreateAt {
continue
}

timestamp := time.Unix(postlist.Posts[postlist.Order[i]].CreateAt/1000, 0).Format("January 02, 2006 15:04")
channelname := u.br.GetChannelName(postlist.Posts[postlist.Order[i]].ChannelId)

nick := u.br.GetUser(postlist.Posts[postlist.Order[i]].UserId).Nick

u.MsgUser(toUser, "#"+channelname+" <"+nick+"> "+timestamp)
u.MsgUser(toUser, strings.Repeat("=", len("#"+channelname+" <"+nick+"> "+timestamp)))

for _, post := range strings.Split(postlist.Posts[postlist.Order[i]].Message, "\n") {
if post != "" {
u.MsgUser(toUser, post)
}
}

if len(postlist.Posts[postlist.Order[i]].FileIds) > 0 {
for _, fname := range u.br.GetFileLinks(postlist.Posts[postlist.Order[i]].FileIds) {
u.MsgUser(toUser, "download file - "+fname)
}
}

u.MsgUser(toUser, "")
u.MsgUser(toUser, "")
}
}

// nolint:dupl,cyclop
func search6(u *User, toUser *User, args []string, service string) {
if service == "slack" {
u.MsgUser(toUser, "not implemented")
return
}

list := u.br.SearchPosts(strings.Join(args, " "))

if list == nil || list.(*model6.PostList) == nil || len(list.(*model6.PostList).Order) == 0 {
u.MsgUser(toUser, "no results")
return
}

postlist, _ := list.(*model6.PostList)
postlist, _ := list.(*model.PostList)

for i := len(postlist.Order) - 1; i >= 0; i-- {
if postlist.Posts[postlist.Order[i]].DeleteAt > postlist.Posts[postlist.Order[i]].CreateAt {
Expand Down Expand Up @@ -311,14 +259,8 @@ func searchUsers(u *User, toUser *User, args []string, service string) {
}
}

// nolint:dupl,goconst
//nolint:funlen,gocognit,gocyclo,cyclop
func scrollback(u *User, toUser *User, args []string, service string) {
if strings.HasPrefix(u.getMattermostVersion(), "6.") || strings.HasPrefix(u.getMattermostVersion(), "7.") {
scrollback6(u, toUser, args, service)

return
}

if service == "slack" {
u.MsgUser(toUser, "not implemented")
return
Expand Down Expand Up @@ -365,118 +307,7 @@ func scrollback(u *User, toUser *User, args []string, service string) {
return
}

postlist := list.(*model.PostList)

for i := len(postlist.Order) - 1; i >= 0; i-- {
p := postlist.Posts[postlist.Order[i]]
ts := time.Unix(0, p.CreateAt*int64(time.Millisecond))

props := p.GetProps()
botname, override := props["override_username"].(string)
user := u.br.GetUser(p.UserId)
nick := user.Nick
if override {
nick = botname
}

if p.Type == "system_add_to_team" || p.Type == "system_remove_from_team" {
nick = "system"
}

for _, post := range strings.Split(p.Message, "\n") {
switch { // nolint:dupl
case (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && strings.HasPrefix(args[0], "#") && nick != "system":
threadMsgID := u.prefixContext("", p.Id, p.ParentId, "")
scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, post)
spoof(nick, scrollbackMsg)
case strings.HasPrefix(args[0], "#"):
scrollbackMsg := "[" + ts.Format("2006-01-02 15:04") + "] " + post
spoof(nick, scrollbackMsg)
case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post":
threadMsgID := u.prefixContext("", p.Id, p.ParentId, "")
scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, post)
u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg)
default:
scrollbackMsg := "[" + ts.Format("2006-01-02 15:04") + "]" + " <" + nick + "> " + post
u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg)
}
}

if len(p.FileIds) == 0 {
continue
}

for _, fname := range u.br.GetFileLinks(p.FileIds) {
fileMsg := "download file - " + fname
switch { // nolint:dupl
case (u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post") && strings.HasPrefix(args[0], "#"):
threadMsgID := u.prefixContext("", p.Id, p.ParentId, "")
scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, fileMsg)
spoof(nick, scrollbackMsg)
case u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost" || u.v.GetString(u.br.Protocol()+".threadcontext") == "mattermost+post":
threadMsgID := u.prefixContext("", p.Id, p.ParentId, "")
scrollbackMsg := u.formatContextMessage(ts.Format("2006-01-02 15:04"), threadMsgID, fileMsg)
u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg)
case strings.HasPrefix(args[0], "#"):
scrollbackMsg := "[" + ts.Format("2006-01-02 15:04") + "] " + fileMsg
spoof(nick, scrollbackMsg)
default:
scrollbackMsg := "[" + ts.Format("2006-01-02 15:04") + "]" + " <" + nick + "> " + fileMsg
u.MsgSpoofUser(scrollbackUser, nick, scrollbackMsg)
}
}
}
}

// nolint:dupl,funlen,gocognit,gocyclo,cyclop
func scrollback6(u *User, toUser *User, args []string, service string) {
if service == "slack" {
u.MsgUser(toUser, "not implemented")
return
}

if len(args) != 2 {
u.MsgUser(toUser, "need SCROLLBACK (#<channel>|<user>) <lines>")
u.MsgUser(toUser, "e.g. SCROLLBACK #bugs 10 (show last 10 lines from #bugs)")
return
}

limit, err := strconv.Atoi(args[1])
if err != nil {
u.MsgUser(toUser, "need SCROLLBACK (#<channel>|<user>) <lines>")
u.MsgUser(toUser, "e.g. SCROLLBACK #bugs 10 (show last 10 lines from #bugs)")
return
}

var channelID string
var spoof func(string, string, ...int)
scrollbackUser, exists := u.Srv.HasUser(args[0])

switch {
case strings.HasPrefix(args[0], "#"):
channelName := strings.ReplaceAll(args[0], "#", "")
channelID = u.br.GetChannelID(channelName, u.br.GetMe().TeamID)
spoof = u.Srv.Channel(channelID).SpoofMessage
case exists && scrollbackUser.Ghost:
// We need to sort the two user IDs to construct the DM
// channel name.
userIDs := []string{u.User, scrollbackUser.User}
sort.Strings(userIDs)
channelName := userIDs[0] + "__" + userIDs[1]
channelID = u.br.GetChannelID(channelName, u.br.GetMe().TeamID)
default:
u.MsgUser(toUser, "need SCROLLBACK (#<channel>|<user>) <lines>")
u.MsgUser(toUser, "e.g. SCROLLBACK #bugs 10 (show last 10 lines from #bugs)")
return
}

list := u.br.GetPosts(channelID, limit)
if list == nil || list.(*model6.PostList) == nil || len(list.(*model6.PostList).Order) == 0 {
u.MsgUser(toUser, "no results")
return
}

postlist, _ := list.(*model6.PostList)
postlist, _ := list.(*model.PostList)

for i := len(postlist.Order) - 1; i >= 0; i-- {
p := postlist.Posts[postlist.Order[i]]
Expand All @@ -490,7 +321,7 @@ func scrollback6(u *User, toUser *User, args []string, service string) {
nick = botname
}

if p.Type == model6.PostTypeAddToTeam || p.Type == model6.PostTypeRemoveFromTeam {
if p.Type == model.PostTypeAddToTeam || p.Type == model.PostTypeRemoveFromTeam {
nick = "system"
}

Expand Down
Loading

0 comments on commit da742e9

Please sign in to comment.