@@ -2,6 +2,7 @@ package components
22
33import (
44 "fmt"
5+ "html/template"
56 "io/ioutil"
67 "math"
78 "os"
@@ -18,10 +19,10 @@ func preParseHTML(html string) string {
1819}
1920
2021func (theme Theme ) parseComponent (component string ) string {
21- comp , err := ioutil .ReadFile (filepath .Join (theme .ThemeDir , "components" , component + ".html" ))
22+ comp , err := ioutil .ReadFile (filepath .Join (theme .ThemeDir , "components" , component + ".html" ))
2223
2324 if os .IsNotExist (err ) {
24- baseComp , err := ioutil .ReadFile (filepath .Join (theme .BaseCss , "components" , component + ".html" ))
25+ baseComp , err := ioutil .ReadFile (filepath .Join (theme .BaseCss , "components" , component + ".html" ))
2526 tools .PanicIfErr (err )
2627 comp = baseComp
2728 } else {
@@ -37,18 +38,17 @@ func (theme *Theme) LoadTheme(themeName string, DW_MEDIA bool) {
3738 } else {
3839 tools .PanicIfErr (err )
3940 }
40-
41+
4142 themeDir := filepath .Join ("themes" , themeName )
42-
43-
43+
4444 themeLoc , err := os .Stat (themeDir )
45-
45+
4646 if os .IsNotExist (err ) || ! themeLoc .IsDir () {
4747 panic ("Cannot find theme!" )
4848 } else {
4949 tools .PanicIfErr (err )
5050 }
51-
51+
5252 theme .ThemeDir = themeDir
5353 theme .BaseCss = filepath .Join ("themes" , "base" )
5454
@@ -67,6 +67,7 @@ func (theme *Theme) LoadTheme(themeName string, DW_MEDIA bool) {
6767 theme .REPLY = theme .parseComponent ("reply" )
6868 theme .GIF = theme .parseComponent ("gifs" )
6969 theme .OTHER_MIME = theme .parseComponent ("otherMime" )
70+ theme .MENTION = theme .parseComponent ("mention" )
7071
7172 theme .DownloadMedia = DW_MEDIA
7273}
@@ -77,10 +78,13 @@ var tabReg = regexp.MustCompile(`\t`)
7778
7879func (theme Theme ) MessageComponent (msg discord.Message , previousMsg discord.Message , firstMsg bool ) string {
7980 content := msg .Content
80- content = newLineReg .ReplaceAllString (content , "<br />" )
8181
8282 attachContent := ""
8383
84+ content = template .HTMLEscapeString (content )
85+
86+ content = newLineReg .ReplaceAllString (content , "<br />" )
87+
8488 for _ , attach := range msg .Attachments {
8589 if attach .ContentType [:5 ] == "image" {
8690 mediaUrl := attach .Url
@@ -89,24 +93,24 @@ func (theme Theme) MessageComponent(msg discord.Message, previousMsg discord.Mes
8993 }
9094 attachContent += tools .ParseTemplate (theme .IMG , map [string ]string {
9195 "IMG_URL" : mediaUrl ,
92- "WIDTH" : fmt .Sprint (math .Floor (0.8 * float64 (attach .Width ))),
93- "HEIGHT" : fmt .Sprint (math .Floor (0.8 * float64 (attach .Height ))),
96+ "WIDTH" : fmt .Sprint (math .Floor (0.8 * float64 (attach .Width ))),
97+ "HEIGHT" : fmt .Sprint (math .Floor (0.8 * float64 (attach .Height ))),
9498 })
9599 } else {
96100 attachContent += tools .ParseTemplate (theme .OTHER_MIME , map [string ]string {
97- "FILENAME" : attach .MediaName () ,
101+ "FILENAME" : attach .Name ,
98102 })
99103 }
100104 }
101-
105+
102106 gifContents := ""
103107
104108 for _ , embed := range msg .Embeds {
105109 if embed .Type == discord .EMBED_GIF {
106110 gifContents += tools .ParseTemplate (theme .GIF , map [string ]string {
107111 "VIDEO_URL" : embed .Url ,
108- "WIDTH" : fmt .Sprint (float64 (embed .Thumbnail .Width )* 0.7 ),
109- "HEIGHT" : fmt .Sprint (float64 (embed .Thumbnail .Height )* 0.7 ),
112+ "WIDTH" : fmt .Sprint (float64 (embed .Thumbnail .Width ) * 0.7 ),
113+ "HEIGHT" : fmt .Sprint (float64 (embed .Thumbnail .Height ) * 0.7 ),
110114 })
111115 if msg .Content == embed .GifContentUrl {
112116 content = ""
@@ -125,35 +129,44 @@ func (theme Theme) MessageComponent(msg discord.Message, previousMsg discord.Mes
125129 if msg .IsSystemType {
126130 return "TODO:"
127131 } else {
132+ for _ , mention := range msg .Mentions {
133+ idReg := regexp .MustCompile (fmt .Sprintf (`<@!?%v>` , mention .ID ))
134+ content = idReg .ReplaceAllString (content , tools .ParseTemplate (theme .MENTION , map [string ]string {
135+ "MENTION_NAME" : mention .Name ,
136+ "MENTION_PREFIX" : "@" ,
137+ }))
138+ }
139+
128140 if firstMsg {
129141 replyContent := ""
142+
130143 if msg .IsReply {
131144 replyContent = tools .ParseTemplate (theme .REPLY , map [string ]string {
132- "PFP" : msg .ReplyTo .Author .URL (16 ),
133- "NAME" : msg .ReplyTo .Author .Name ,
145+ "PFP" : msg .ReplyTo .Author .URL (16 ),
146+ "NAME" : msg .ReplyTo .Author .Name ,
134147 "CONTENT" : msg .ReplyTo .Content ,
135148 })
136149 }
137-
150+
138151 return tools .ParseTemplate (theme .MSG_WITH_PFP , map [string ]string {
139- "PFP" : msg .Author .URL (256 ),
140- "USERNAME" : msg .Author .Name ,
141- "DATE" : discord .TimestampToTime (msg .Timestamp ).Format ("Mon 02/01/2006 03:04:05 PM" ),
142- "CONTENT" : content ,
143- "ATTACH_CONTENT" : attachContent ,
144- "ID" : msg .ID ,
145- "REPLY_CONTENT" : replyContent ,
152+ "PFP" : msg .Author .URL (256 ),
153+ "USERNAME" : msg .Author .Name ,
154+ "DATE" : discord .TimestampToTime (msg .Timestamp ).Format ("Mon 02/01/2006 03:04:05 PM" ),
155+ "CONTENT" : content ,
156+ "ATTACH_CONTENT" : attachContent ,
157+ "ID" : msg .ID ,
158+ "REPLY_CONTENT" : replyContent ,
146159 "STICKER_CONTENT" : stickerContent ,
147- "GIFS" : gifContents ,
160+ "GIFS" : gifContents ,
148161 })
149162 } else {
150163 return tools .ParseTemplate (theme .MSG , map [string ]string {
151- "DATE" : discord .TimestampToTime (msg .Timestamp ).Format ("15:04" ),
152- "CONTENT" : content ,
153- "ATTACH_CONTENT" : attachContent ,
154- "ID" : msg .ID ,
164+ "DATE" : discord .TimestampToTime (msg .Timestamp ).Format ("15:04" ),
165+ "CONTENT" : content ,
166+ "ATTACH_CONTENT" : attachContent ,
167+ "ID" : msg .ID ,
155168 "STICKER_CONTENT" : stickerContent ,
156- "GIFS" : gifContents ,
169+ "GIFS" : gifContents ,
157170 })
158171 }
159172 }
@@ -175,37 +188,37 @@ func (theme Theme) TopBar(title string, channelType discord.ChannelType) string
175188 icon := ""
176189
177190 switch channelType {
178- case discord .CHANNEL_TYPE_DM :
179- icon = theme .SVG_DM
180- case discord .CHANNEL_TYPE_GROUP_DM :
181- icon = theme .SVG_GROUP_DM
182- default :
183- icon = theme .SVG_CHANNEL
191+ case discord .CHANNEL_TYPE_DM :
192+ icon = theme .SVG_DM
193+ case discord .CHANNEL_TYPE_GROUP_DM :
194+ icon = theme .SVG_GROUP_DM
195+ default :
196+ icon = theme .SVG_CHANNEL
184197 }
185-
198+
186199 return tools .ParseTemplate (theme .TOP_BAR , map [string ]string {
187- "ICON" : icon ,
200+ "ICON" : icon ,
188201 "TITLE" : title ,
189202 })
190203}
191204
192205func (theme Theme ) StartDM (author discord.Author ) string {
193206 return tools .ParseTemplate (theme .START_CHAN , map [string ]string {
194207 "TITLE" : author .Name ,
195- "ICON" : author .URL (512 ),
208+ "ICON" : author .URL (512 ),
196209 })
197210}
198211
199212func (theme Theme ) StartChannel (channel discord.Channel ) string {
200213 return tools .ParseTemplate (theme .START_CHAN , map [string ]string {
201214 "TITLE" : "Welcome to #" + channel .Name ,
202- "ICON" : "assets/channelStart.png" ,
215+ "ICON" : "assets/channelStart.png" ,
203216 })
204217}
205218
206219func (theme Theme ) StartGroupDM (groupDM discord.Channel ) string {
207220 return tools .ParseTemplate (theme .START_CHAN , map [string ]string {
208221 "TITLE" : groupDM .Name ,
209- "ICON" : groupDM .Icon ,
222+ "ICON" : groupDM .Icon ,
210223 })
211224}
0 commit comments