Skip to content

Commit

Permalink
Merge pull request #42 from JonasDev99/enhancement-1
Browse files Browse the repository at this point in the history
Regex patterns
  • Loading branch information
parkervcp authored May 27, 2022
2 parents cd51278 + c4d0826 commit d1790e5
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
24 changes: 19 additions & 5 deletions config_examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,27 @@ func createExampleDiscordConfig(confDir string) (err error) {
Response: []string{"I have responded to seeing the word example."},
},
},
Regex: []pattern{
{
Pattern: ".*example.*",
Reaction: []string{""},
Response: []string{"I have found the word example somewhere in there."},
},
},
Parsing: parsing{
Image: parsingImageConfig{
FileTypes: []string{
"png",
"jpg"},
Sites: []parsingConfig{
Sites: []parsingConfig{
{
Name: "pastebin",
URL: "'https://pastebin.com/'",
Name: "pastebin",
URL: "'https://pastebin.com/'",
Format: "'https://pastebin.com/raw/&filename&'",
},
{
Name: "hastebin",
URL: "'https://hastebin.com/'",
Name: "hastebin",
URL: "'https://hastebin.com/'",
Format: "'https://hastebin.com/raw/&filename&'",
},
},
Expand Down Expand Up @@ -257,6 +264,13 @@ func createExampleIRCConfig(confDir string) (err error) {
Response: []string{"I have responded to seeing the word example."},
},
},
Regex: []pattern{
{
Pattern: ".*example.*",
Reaction: []string{""},
Response: []string{"I have found the word example somewhere in there."},
},
},
Parsing: parsing{
Image: parsingImageConfig{
FileTypes: []string{},
Expand Down
15 changes: 15 additions & 0 deletions config_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ func getKeywords(inService, botName, inServer, inChannel string) (keywords []key
return
}

func getRegexPatterns(inService, botName, inServer, inChannel string) (patterns []pattern) {
// prep stuff for passing to the parser
for _, group := range getChannelGroups(inService, botName, inServer, inChannel) {
for _, channel := range group.ChannelIDs {
if inChannel == channel {
for _, pat := range group.Regex {
patterns = append(patterns, pat)
}
}
}
}

return
}

func getMentions(inService, botName, inServer, inChannel string) (ping, mention responseArray) {
switch inService {
case "discord":
Expand Down
6 changes: 6 additions & 0 deletions configs/discord/example-bot/example-server/example-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ channel_groups:
- ""
response:
- I have responded to seeing the word example.
regex:
- pattern: ".*example.*"
reaction:
- ""
response:
- I have responded to matching the example regex pattern.
parsing:
image:
filetypes:
Expand Down
8 changes: 7 additions & 1 deletion discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func discordMessageHandler(dg *discordgo.Session, m *discordgo.MessageCreate, bo
prefix := getPrefix("discord", botName, channel.GuildID)
channelCommands := getCommands("discord", botName, channel.GuildID, m.ChannelID)
channelKeywords := getKeywords("discord", botName, channel.GuildID, m.ChannelID)
channelPatterns := getRegexPatterns("discord", botName, channel.GuildID, m.ChannelID)
channelParsing := getParsing("discord", botName, channel.GuildID, m.ChannelID)
serverFilter := getFilter("discord", botName, channel.GuildID)

Expand Down Expand Up @@ -222,8 +223,13 @@ func discordMessageHandler(dg *discordgo.Session, m *discordgo.MessageCreate, bo

}
} else {
// regex -- priority over keywords
response, reaction = parseRegex(m.Content, botName, channelPatterns, channelParsing)

// keyword
response, reaction = parseKeyword(m.Content, botName, channelKeywords, channelParsing)
if response == nil {
response, reaction = parseKeyword(m.Content, botName, channelKeywords, channelParsing)
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -121,7 +122,7 @@ func parseURL(url string, parseConf parsing) (parsedText string) {
//Catch domains and route to the proper controllers (image, binsite parsers)
Log.Debugf("checking for pastes and images on %s\n", url)
// if a url ends with a / remove it. Stupid chrome adds them.
if strings.HasSuffix(url,"/") {
if strings.HasSuffix(url, "/") {
url = strings.TrimSuffix(url, "/")
}
if len(parseConf.Image.Sites) != 0 {
Expand Down Expand Up @@ -214,6 +215,29 @@ func parseKeyword(message, botName string, channelKeywords []keyword, parseConf
return
}

// returns response and reaction for patterns
func parseRegex(message, botName string, channelPatterns []pattern, parseConf parsing) (response, reaction []string) {
Log.Debugf("Parsing inbound chat for %s", botName)

message = strings.ToLower(message)

//regex match search
Log.Debug("Testing regex patterns")

for _, pat := range channelPatterns {
Log.Infof("Pattern is %s", pat.Pattern)
if match, err := regexp.MatchString(pat.Pattern, message); err != nil {
Log.Error(err)
} else if match {
// if the pattern was a match
Log.Debugf("Response is %v", pat.Response)
Log.Debugf("Reaction is %v", pat.Reaction)
return pat.Response, pat.Reaction
}
}
return
}

// __
// _______ __ _ __ _ ___ ____ ___/ /
// / __/ _ \/ ' \/ ' \/ _ `/ _ \/ _ /
Expand Down
9 changes: 8 additions & 1 deletion parsing_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ type keyword struct {
Exact bool `json:"exact,omitempty"`
}

type pattern struct {
Pattern string `json:"pattern,omitempty"`
Reaction []string `json:"reaction,omitempty"`
Response []string `json:"response,omitempty"`
}

type mentions struct {
Ping responseArray `json:"ping,omitempty"`
Mention responseArray `json:"mention,omitempty"`
}

type filter struct {
Term string `json:"term,omitempty"`
Term string `json:"term,omitempty"`
Reason []string `json:"reason,omitempty"`
}

Expand Down Expand Up @@ -63,6 +69,7 @@ type channelGroup struct {
Mentions mentions `json:"mentions,omitempty"`
Commands []command `json:"commands,omitempty"`
Keywords []keyword `json:"keywords,omitempty"`
Regex []pattern `json:"regex,omitempty"`
Parsing parsing `json:"parsing,omitempty"`
Permissions []permission `json:"permissions,omitempty"`
KOM discordKickOnMention `json:"kick_on_mention,omitempty"`
Expand Down

0 comments on commit d1790e5

Please sign in to comment.