mirror of https://github.com/velour/catbase.git
Merge 0925976fe7
into 4c0b8f4fad
This commit is contained in:
commit
54e2840661
|
@ -0,0 +1,38 @@
|
||||||
|
# Use this file to configure the Overcommit hooks you wish to use. This will
|
||||||
|
# extend the default configuration defined in:
|
||||||
|
# https://github.com/brigade/overcommit/blob/master/config/default.yml
|
||||||
|
#
|
||||||
|
# At the topmost level of this YAML file is a key representing type of hook
|
||||||
|
# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
|
||||||
|
# customize each hook, such as whether to only run it on certain files (via
|
||||||
|
# `include`), whether to only display output if it fails (via `quiet`), etc.
|
||||||
|
#
|
||||||
|
# For a complete list of hooks, see:
|
||||||
|
# https://github.com/brigade/overcommit/tree/master/lib/overcommit/hook
|
||||||
|
#
|
||||||
|
# For a complete list of options that you can use to customize hooks, see:
|
||||||
|
# https://github.com/brigade/overcommit#configuration
|
||||||
|
#
|
||||||
|
# Uncomment the following lines to make the configuration take effect.
|
||||||
|
|
||||||
|
PreCommit:
|
||||||
|
Vet:
|
||||||
|
enabled: true
|
||||||
|
command: ['./vet.sh']
|
||||||
|
|
||||||
|
#PreCommit:
|
||||||
|
# RuboCop:
|
||||||
|
# enabled: true
|
||||||
|
# on_warn: fail # Treat all warnings as failures
|
||||||
|
#
|
||||||
|
# TrailingWhitespace:
|
||||||
|
# enabled: true
|
||||||
|
# exclude:
|
||||||
|
# - '**/db/structure.sql' # Ignore trailing whitespace in generated files
|
||||||
|
#
|
||||||
|
#PostCheckout:
|
||||||
|
# ALL: # Special hook name that customizes all hooks of this type
|
||||||
|
# quiet: true # Change all post-checkout hooks to only display output on failure
|
||||||
|
#
|
||||||
|
# IndexTags:
|
||||||
|
# enabled: true # Generate a tags file with `ctags` each time HEAD changes
|
|
@ -35,7 +35,7 @@ func New(bot bot.Bot) *EmojifyMePlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Emoji struct {
|
type Emoji struct {
|
||||||
Aliases []string `json:aliases`
|
Aliases []string `json:"aliases"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var emoji []Emoji
|
var emoji []Emoji
|
||||||
|
|
|
@ -49,7 +49,7 @@ func (p *ZorkPlugin) runZork(ch string) error {
|
||||||
var w io.WriteCloser
|
var w io.WriteCloser
|
||||||
cmd.Stdin, w = io.Pipe()
|
cmd.Stdin, w = io.Pipe()
|
||||||
|
|
||||||
log.Printf("zork running %s\n", cmd)
|
log.Printf("zork running %v\n", cmd)
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
w.Close()
|
w.Close()
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
package slack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fixText strips all of the Slack-specific annotations from message text,
|
||||||
|
// replacing it with the equivalent display form.
|
||||||
|
// Currently it:
|
||||||
|
// • Replaces user mentions like <@U124356> with @ followed by the user's nick.
|
||||||
|
// This uses the lookupUser function, which must map U1243456 to the nick.
|
||||||
|
// • Replaces user mentions like <U123456|nick> with the user's nick.
|
||||||
|
// • Strips < and > surrounding links.
|
||||||
|
//
|
||||||
|
// This was directly bogarted from velour/chat with emoji conversion removed.
|
||||||
|
func fixText(findUser func(id string) (string, bool), text string) string {
|
||||||
|
var output []rune
|
||||||
|
for len(text) > 0 {
|
||||||
|
r, i := utf8.DecodeRuneInString(text)
|
||||||
|
text = text[i:]
|
||||||
|
switch {
|
||||||
|
case r == '<':
|
||||||
|
var tag []rune
|
||||||
|
for {
|
||||||
|
r, i := utf8.DecodeRuneInString(text)
|
||||||
|
text = text[i:]
|
||||||
|
switch {
|
||||||
|
case r == '>':
|
||||||
|
if t, ok := fixTag(findUser, tag); ok {
|
||||||
|
output = append(output, t...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fallthrough
|
||||||
|
case len(text) == 0:
|
||||||
|
output = append(output, '<')
|
||||||
|
output = append(output, tag...)
|
||||||
|
output = append(output, r)
|
||||||
|
default:
|
||||||
|
tag = append(tag, r)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
output = append(output, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func fixTag(findUser func(string) (string, bool), tag []rune) ([]rune, bool) {
|
||||||
|
switch {
|
||||||
|
case hasPrefix(tag, "@U"):
|
||||||
|
if i := indexRune(tag, '|'); i >= 0 {
|
||||||
|
return tag[i+1:], true
|
||||||
|
}
|
||||||
|
if findUser != nil {
|
||||||
|
if u, ok := findUser(string(tag[1:])); ok {
|
||||||
|
return []rune("@" + u), true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tag, true
|
||||||
|
|
||||||
|
case hasPrefix(tag, "#C"):
|
||||||
|
if i := indexRune(tag, '|'); i >= 0 {
|
||||||
|
return append([]rune{'#'}, tag[i+1:]...), true
|
||||||
|
}
|
||||||
|
|
||||||
|
case hasPrefix(tag, "http"):
|
||||||
|
if i := indexRune(tag, '|'); i >= 0 {
|
||||||
|
tag = tag[:i]
|
||||||
|
}
|
||||||
|
return tag, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasPrefix(text []rune, prefix string) bool {
|
||||||
|
for _, r := range prefix {
|
||||||
|
if len(text) == 0 || text[0] != r {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
text = text[1:]
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexRune(text []rune, find rune) int {
|
||||||
|
for i, r := range text {
|
||||||
|
if r == find {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
|
@ -228,8 +228,8 @@ func (s *Slack) populateEmojiList() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type EmojiListResponse struct {
|
type EmojiListResponse struct {
|
||||||
OK bool `json:ok`
|
OK bool `json:"ok"`
|
||||||
Emoji map[string]string `json:emoji`
|
Emoji map[string]string `json:"emoji"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var list EmojiListResponse
|
var list EmojiListResponse
|
||||||
|
@ -306,14 +306,13 @@ var urlDetector = regexp.MustCompile(`<(.+)://([^|^>]+).*>`)
|
||||||
func (s *Slack) buildMessage(m slackMessage) msg.Message {
|
func (s *Slack) buildMessage(m slackMessage) msg.Message {
|
||||||
text := html.UnescapeString(m.Text)
|
text := html.UnescapeString(m.Text)
|
||||||
|
|
||||||
// remove <> from URLs, URLs may also be <url|description>
|
text = fixText(s.getUser, text)
|
||||||
text = urlDetector.ReplaceAllString(text, "${1}://${2}")
|
|
||||||
|
|
||||||
isCmd, text := bot.IsCmd(s.config, text)
|
isCmd, text := bot.IsCmd(s.config, text)
|
||||||
|
|
||||||
isAction := m.SubType == "me_message"
|
isAction := m.SubType == "me_message"
|
||||||
|
|
||||||
u := s.getUser(m.User)
|
u, _ := s.getUser(m.User)
|
||||||
if m.Username != "" {
|
if m.Username != "" {
|
||||||
u = m.Username
|
u = m.Username
|
||||||
}
|
}
|
||||||
|
@ -460,9 +459,9 @@ func (s *Slack) connect() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get username for Slack user ID
|
// Get username for Slack user ID
|
||||||
func (s *Slack) getUser(id string) string {
|
func (s *Slack) getUser(id string) (string, bool) {
|
||||||
if name, ok := s.users[id]; ok {
|
if name, ok := s.users[id]; ok {
|
||||||
return name
|
return name, true
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("User %s not already found, requesting info", id)
|
log.Printf("User %s not already found, requesting info", id)
|
||||||
|
@ -472,17 +471,17 @@ func (s *Slack) getUser(id string) string {
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
log.Printf("Error posting user info request: %d %s",
|
log.Printf("Error posting user info request: %d %s",
|
||||||
resp.StatusCode, err)
|
resp.StatusCode, err)
|
||||||
return "UNKNOWN"
|
return "UNKNOWN", false
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
var userInfo slackUserInfoResp
|
var userInfo slackUserInfoResp
|
||||||
err = json.NewDecoder(resp.Body).Decode(&userInfo)
|
err = json.NewDecoder(resp.Body).Decode(&userInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error decoding response: ", err)
|
log.Println("Error decoding response: ", err)
|
||||||
return "UNKNOWN"
|
return "UNKNOWN", false
|
||||||
}
|
}
|
||||||
s.users[id] = userInfo.User.Name
|
s.users[id] = userInfo.User.Name
|
||||||
return s.users[id]
|
return s.users[id], true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Who gets usernames out of a channel
|
// Who gets usernames out of a channel
|
||||||
|
@ -513,7 +512,8 @@ func (s *Slack) Who(id string) []string {
|
||||||
|
|
||||||
handles := []string{}
|
handles := []string{}
|
||||||
for _, member := range chanInfo.Channel.Members {
|
for _, member := range chanInfo.Channel.Members {
|
||||||
handles = append(handles, s.getUser(member))
|
u, _ := s.getUser(member)
|
||||||
|
handles = append(handles, u)
|
||||||
}
|
}
|
||||||
log.Printf("Returning %d handles", len(handles))
|
log.Printf("Returning %d handles", len(handles))
|
||||||
return handles
|
return handles
|
||||||
|
|
Loading…
Reference in New Issue