added input sanitization

This commit is contained in:
marko
2024-10-03 16:19:54 +02:00
parent d928a681f9
commit 97e41eece0
4 changed files with 104 additions and 94 deletions

View File

@@ -6,6 +6,7 @@ import (
"regexp"
"strings"
"time"
"unicode/utf8"
"github.com/xmppo/go-xmpp"
"gopkg.in/ini.v1"
@@ -71,19 +72,30 @@ func (nkbot *NekoUKrovuBot) Listen() {
}
func (nkbot *NekoUKrovuBot) handleChat(ch *xmpp.Chat) {
src := ch.Remote
txt := ch.Text
if src == "sdsads" {
if !nkbot.sanitizeInput(txt) {
return
}
if nkbot.checkForJelNekoUKrovu(txt) {
n := ping.Run()
n := ping.PingLocal255()
nkbot.answer(fmt.Sprintf("%v uredjaja povezano", n))
}
}
func (nkbot *NekoUKrovuBot) sanitizeInput(input string) bool {
if len(input) > 50 {
return false
}
if !utf8.ValidString(input) {
return false
}
return true
}
func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool {
normalizedText := strings.ToLower(txt)
@@ -99,10 +111,10 @@ func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool {
func (nkbot *NekoUKrovuBot) answer(ans string) {
chat := xmpp.Chat{
Remote: "chatbottest@conference.dmz.rs",
Type: "groupchat",
Text: ans,
Stamp: time.Now(),
Remote: "chatbottest@conference.dmz.rs",
Type: "groupchat",
Text: ans,
Stamp: time.Now(),
}
n, err := nkbot.cl.Send(chat)