Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message showing enhancements #230

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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