Skip to content

Commit

Permalink
Add message showing enhancements (42wim#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aketzu authored and 42wim committed Feb 25, 2019
1 parent ee54e66 commit 6fe31cf
Showing 1 changed file with 60 additions and 13 deletions.
73 changes: 60 additions & 13 deletions mm-go-irckit/slackuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package irckit
import (
"encoding/json"
"errors"
"fmt"
"html"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -329,6 +330,8 @@ func (u *User) handleSlackActionPost(rmsg *slack.MessageEvent) {
}
}

msghandled := false

// handle bot messages
botname := ""
if rmsg.User == "" && rmsg.BotID != "" {
Expand Down Expand Up @@ -361,7 +364,13 @@ func (u *User) handleSlackActionPost(rmsg *slack.MessageEvent) {
spoofUsername = strings.TrimSpace(botname)
}

msgs := strings.Split(rmsg.Text, "\n")
msgs := []string{}

if rmsg.Text != "" {
msgs = append(msgs, strings.Split(rmsg.Text, "\n")...)
msghandled = true
}

// direct message

ch = u.Srv.Channel(rmsg.Channel)
Expand All @@ -381,20 +390,45 @@ func (u *User) handleSlackActionPost(rmsg *slack.MessageEvent) {
}
}

// look in attachments if we have no text
if rmsg.Text == "" {
for _, attach := range rmsg.Attachments {
if attach.Text != "" {
msgs = append(msgs, strings.Split(attach.Text, "\n")...)
} else {
msgs = append(msgs, strings.Split(attach.Fallback, "\n")...)
// look in attachments
for _, attach := range rmsg.Attachments {
if attach.Pretext != "" {
msgs = append(msgs, strings.Split(attach.Pretext, "\n")...)
}

if attach.Text != "" {
for i, row := range strings.Split(attach.Text, "\n") {
msgs = append(msgs, "> "+row)
if i > 4 {
msgs = append(msgs, "> ...")
break
}
}
}
msghandled = true
}

// List files
for _, file := range rmsg.Files {
msgs = append(msgs, "Uploaded "+file.Mode+" "+
file.Name+" / "+file.Title+" ("+file.Filetype+"): "+file.URLPrivate)
msghandled = true
}

if len(rmsg.Files) > 0 {
for _, f := range rmsg.Files {
msgs = append(msgs, f.URLPrivate)
if msghandled {
if rmsg.ThreadTimestamp != "" {
var threadts, threadus int64
fmt.Sscanf(rmsg.ThreadTimestamp, "%d.%d", &threadts, &threadus)
ts := time.Unix(threadts, threadus*1000)

threadtime := ""
if ts.YearDay() != time.Now().YearDay() {
threadtime = ts.Format("2.1. 15:04:05")
} else {
threadtime = ts.Format("15:04:05")
}

msgs[0] = "[T " + threadtime + "] " + msgs[0]
}
}

Expand All @@ -408,8 +442,9 @@ func (u *User) handleSlackActionPost(rmsg *slack.MessageEvent) {
m = html.UnescapeString(m)

// still no text, ignore this message
if m == "" {
continue
if !msghandled {
//continue
m = fmt.Sprintf("Empty: %#v", rmsg)
}

if strings.HasPrefix(rmsg.Channel, "D") {
Expand Down Expand Up @@ -474,6 +509,12 @@ func (u *User) syncSlackChannel(id string, name string) {
}
}

//Add slackbot to all channels
slackuser := u.getSlackUser("USLACKBOT")
if slackuser != nil {
u.addSlackUserToChannel(slackuser, "#"+name, id)
}

ch := srv.Channel(id)
svc, _ := srv.HasUser("slack")
ch.Topic(svc, info.Topic.Value)
Expand Down Expand Up @@ -505,6 +546,12 @@ func (u *User) syncSlackGroup(id string, name string) {
}
}

//Add slackbot to all channels
slackuser := u.getSlackUser("USLACKBOT")
if slackuser != nil {
u.addSlackUserToChannel(slackuser, "#"+name, id)
}

ch := srv.Channel(id)
svc, _ := srv.HasUser("slack")
ch.Topic(svc, info.Topic.Value)
Expand Down

0 comments on commit 6fe31cf

Please sign in to comment.