package chatbot import ( "fmt" "log" "regexp" "strings" "time" "unicode/utf8" "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 if !nkbot.sanitizeInput(txt) { return } if nkbot.checkForJelNekoUKrovu(txt) { n := ping.PingLocal255() nkbot.answer(fmt.Sprintf("%v uredjaja povezano", n)) } } func (nkbot *NekoUKrovuBot) sanitizeInput(input string) bool { if len(input) > 50 { return true } if !utf8.ValidString(input) { return true } return true } 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{ Remote: "chatbottest@conference.dmz.rs", Type: "groupchat", Text: ans, Stamp: time.Now(), } n, err := nkbot.cl.Send(chat) if err != nil { log.Print(err) } _ = n }