2024-10-03 13:52:06 +00:00
|
|
|
package chatbot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-10-03 14:19:54 +00:00
|
|
|
"unicode/utf8"
|
2024-10-03 13:52:06 +00:00
|
|
|
|
|
|
|
"github.com/xmppo/go-xmpp"
|
|
|
|
"gopkg.in/ini.v1"
|
|
|
|
|
|
|
|
"gitea.dmz.rs/bauljamic123arlijam/neko-u-krovu-bot/ping"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NekoUKrovuBot struct {
|
|
|
|
cl *xmpp.Client
|
|
|
|
host string
|
|
|
|
user string
|
|
|
|
mcuJid string
|
|
|
|
nick string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNekoUKrovuBot() (*NekoUKrovuBot, error) {
|
|
|
|
cfg, err := ini.Load("config.ini")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load INI file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
host := cfg.Section("credentials").Key("HOST").String()
|
|
|
|
user := cfg.Section("credentials").Key("JID").String()
|
|
|
|
passwd := cfg.Section("credentials").Key("PASSWORD").String()
|
|
|
|
mcuJid := cfg.Section("credentials").Key("ROOM").String()
|
|
|
|
nick := cfg.Section("credentials").Key("NICK").String()
|
|
|
|
|
|
|
|
cl, err := xmpp.NewClientNoTLS(host, user, passwd, false)
|
|
|
|
if err != nil {
|
|
|
|
return &NekoUKrovuBot{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := cl.JoinMUCNoHistory(mcuJid, nick)
|
|
|
|
if err != nil {
|
|
|
|
return &NekoUKrovuBot{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = n
|
|
|
|
|
|
|
|
return &NekoUKrovuBot{cl: cl,
|
|
|
|
host: host,
|
|
|
|
user: user,
|
|
|
|
mcuJid: mcuJid,
|
|
|
|
nick: nick}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nkbot *NekoUKrovuBot) Listen() {
|
|
|
|
for {
|
|
|
|
msg, err := nkbot.cl.Recv()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m := msg.(type) {
|
|
|
|
case xmpp.Chat:
|
|
|
|
nkbot.handleChat(&m)
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nkbot *NekoUKrovuBot) handleChat(ch *xmpp.Chat) {
|
|
|
|
txt := ch.Text
|
|
|
|
|
2024-10-03 14:19:54 +00:00
|
|
|
if !nkbot.sanitizeInput(txt) {
|
2024-10-03 13:52:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if nkbot.checkForJelNekoUKrovu(txt) {
|
2024-10-03 14:19:54 +00:00
|
|
|
n := ping.PingLocal255()
|
2024-10-03 13:52:06 +00:00
|
|
|
nkbot.answer(fmt.Sprintf("%v uredjaja povezano", n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-03 14:19:54 +00:00
|
|
|
func (nkbot *NekoUKrovuBot) sanitizeInput(input string) bool {
|
|
|
|
if len(input) > 50 {
|
2024-10-03 15:46:43 +00:00
|
|
|
return true
|
2024-10-03 14:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !utf8.ValidString(input) {
|
2024-10-03 15:46:43 +00:00
|
|
|
return true
|
2024-10-03 14:19:54 +00:00
|
|
|
}
|
2024-10-03 15:46:43 +00:00
|
|
|
|
2024-10-03 14:19:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-10-03 13:52:06 +00:00
|
|
|
func (nkbot *NekoUKrovuBot) checkForJelNekoUKrovu(txt string) bool {
|
|
|
|
normalizedText := strings.ToLower(txt)
|
|
|
|
|
|
|
|
pattern := `(?i)^(?:\S+\s+){2,}\b(jel|el|ima li|ima|neko|koga|nekoga|je l)?\s*(neko|koga|nekoga)?\s*(u)?\s*(krovu|krov|vkro)\b`
|
|
|
|
matched, err := regexp.MatchString(pattern, normalizedText)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error compiling regex:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return matched
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nkbot *NekoUKrovuBot) answer(ans string) {
|
|
|
|
chat := xmpp.Chat{
|
2024-10-03 14:19:54 +00:00
|
|
|
Remote: "chatbottest@conference.dmz.rs",
|
|
|
|
Type: "groupchat",
|
|
|
|
Text: ans,
|
|
|
|
Stamp: time.Now(),
|
2024-10-03 13:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n, err := nkbot.cl.Send(chat)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = n
|
|
|
|
}
|