package ai import ( "encoding/json" "io/ioutil" "os" "regexp" "strings" "git.postblue.info/keygoller/utils" "github.com/google/uuid" "github.com/sirupsen/logrus" ) // ReadFromStream is the main AI function, it reads from the stream of the keys // and then answers if some conditions are met. func ReadFromStream(inputChan chan utils.InputEvent) { var b strings.Builder input := make([]utils.InputEvent, 0) for { data := <-inputChan b.WriteString(data.KeyString()) input = append(input, data) switch b.String() { case b.String(): if findPassword(b.String()) { utils.ClippySays("I LIKE YOUR PASSWORD") saveData(b.String(), input) b.Reset() } if findLove(b.String()) { utils.ClippySays("I love you too...") b.Reset() } default: continue } } } type Node struct { ID uuid.UUID Word string `json:"word"` } type Edge struct { ID uuid.UUID NodeID uuid.UUID `json:"NodeID"` Keys []utils.InputEvent `json:"keys"` } func saveData(word string, keysPressed []utils.InputEvent) { err := fileExists("nodes.json") err = fileExists("edges.json") if err != nil { logrus.Error(err) } nodeFile, err := ioutil.ReadFile("nodes.json") edgeFile, err := ioutil.ReadFile("edges.json") if err != nil { logrus.Error(err) } nodeDatas := []Node{} edgeDatas := []Edge{} json.Unmarshal(nodeFile, &nodeDatas) json.Unmarshal(edgeFile, &edgeDatas) node := &Node{ ID: uuid.New(), Word: word, } edge := &Edge{ ID: uuid.New(), NodeID: node.ID, Keys: keysPressed, } nodeDatas = append(nodeDatas, *node) edgeDatas = append(edgeDatas, *edge) nodeBytes, err := json.Marshal(nodeDatas) edgeBytes, err := json.Marshal(edgeDatas) ioutil.WriteFile("nodes.json", nodeBytes, 0644) ioutil.WriteFile("edges.json", edgeBytes, 0644) } func fileExists(filename string) error { _, err := os.Stat(filename) if os.IsNotExist(err) { _, err := os.Create(filename) if err != nil { return err } } return nil } func findPassword(input string) bool { r, _ := regexp.Compile("PASSWORD") if ok := r.MatchString(input); ok { return true } return false } func findLove(input string) bool { r, _ := regexp.Compile("ILOVEYOU") if ok := r.MatchString(input); ok { return true } return false }