-
Notifications
You must be signed in to change notification settings - Fork 4
/
github_lines.go
174 lines (146 loc) · 4.55 KB
/
github_lines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package modules
import (
"fmt"
"math"
"regexp"
"strconv"
"strings"
"github.com/Aliucord/Aliucord-backend/common"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/utils/json/option"
"github.com/valyala/fasthttp"
)
const maxContentLength = 5e6 // 5MB
const maxCharCount = 1000
var client *fasthttp.Client
var githubLinesRegex = regexp.MustCompile(`(?:[^!<]|[^!]<|^<?)https?://github\.com/([A-Za-z0-9\-_.]+)/([A-Za-z0-9\-_.]+)/(?:blob|tree)/(\S+?)/(\S+?)(\.\S+)?#L(\d+)[-~]?L?(\d*)`)
func init() {
modules = append(modules, initGithubLines)
client = &fasthttp.Client{}
}
func initGithubLines() {
s.AddHandler(func(msg *gateway.MessageCreateEvent) {
if msg.Author.Bot {
return
}
matches := githubLinesRegex.FindAllStringSubmatch(msg.Content, -1)
if len(matches) != 0 {
var sb strings.Builder
for _, match := range matches {
author, repo, branch, path, ext, start, end := match[1], match[2], match[3], match[4], match[5], match[6], match[7]
rawUrl := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s%s", author, repo, branch, path, ext)
if shouldSkip(rawUrl) {
continue
}
content := getContent(rawUrl, start, end)
if content == "" {
continue
}
content = unindent(content)
content = strings.ReplaceAll(content, "`", "`\u200b")
content = strings.ReplaceAll(content, "@", "@\u200b")
fileNameIdx := strings.LastIndex(path, "/") + 1
fileName := path
if fileNameIdx != 0 {
fileName = path[fileNameIdx:]
}
sb.WriteString("**" + fileName + ext + "#" + start)
if end != "" {
sb.WriteString("-" + end)
}
sb.WriteString("**\n")
sb.WriteString("```")
if len(ext) > 1 {
lang := ext[1:]
if common.IsAlpha(lang) {
sb.WriteString(lang)
}
}
sb.WriteString("\n" + content + "\n```\n")
}
length := sb.Len()
if length > 0 && length < maxCharCount {
_, err := s.SendMessageComplex(msg.ChannelID, api.SendMessageData{
Content: sb.String(),
AllowedMentions: &api.AllowedMentions{RepliedUser: option.False},
Reference: &discord.MessageReference{MessageID: msg.ID},
})
logger.LogWithCtxIfErr("embedding github lines", err)
}
}
})
}
// Check if url is invalid or file larger than maxContentLength
func shouldSkip(url string) bool {
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
req.SetRequestURI(url)
req.Header.SetMethod(fasthttp.MethodHead)
err := client.Do(req, res)
if err != nil {
return true
}
return res.Header.ContentLength() > maxContentLength
}
// Fetch content from url and trim according to startStr and endStr
// Empty string is returned if failed to fetch, startStr is empty or startStr or endStr are not valid integers / out of bounds
func getContent(url, startStr, endStr string) string {
start, err := strconv.Atoi(startStr)
if err != nil || start < 1 {
return ""
}
end := 0
if endStr != "" {
end, err = strconv.Atoi(endStr)
if err != nil || end < 0 {
return ""
}
if start > end {
start, end = end, start
} else if start == end {
end = 0
}
}
req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
req.SetRequestURI(url)
req.Header.SetMethod(fasthttp.MethodGet)
err = client.Do(req, res)
if err != nil || res.StatusCode() != 200 {
return ""
}
content := string(res.Body())
lines := strings.Split(content, "\n")
lineCount := len(lines)
if end == 0 && lineCount >= start {
return lines[start-1]
}
if lineCount >= start && lineCount >= end {
return strings.Join(lines[start-1:end], "\n")
}
return ""
}
// m flag makes ^ operate on the beginning of each line instead of only the beginning of the string
var indentPattern = regexp.MustCompile(`(?m)^ *\S`)
func unindent(code string) string {
// Replace tabs with 4 spaces
code = strings.ReplaceAll(code, "\t", " ")
minIndent := math.MaxInt32
for _, indent := range indentPattern.FindAllStringSubmatch(code, -1) {
// Since go does not support positive lookahead, the pattern also matches the first non whitespace
// (to ignore lines with only whitespace)
l := len(indent[0]) - 1
if l < minIndent {
minIndent = l
}
}
if minIndent == math.MaxInt32 || minIndent == 0 {
return code
}
pat := regexp.MustCompile("(?m)^ {" + strconv.Itoa(minIndent) + "}")
return pat.ReplaceAllString(code, "")
}