From cb39de09838ad9e75bc03d44178f74ec1f9cde71 Mon Sep 17 00:00:00 2001 From: Matthew Faltys Date: Thu, 7 Sep 2017 08:52:39 -0500 Subject: [PATCH] Break out lorebot into seperate files Pull auth out of config for portability --- .gitignore | 1 + Makefile | 4 +- config.gcfg | 2 +- lorebot/botfunc.go | 106 +++++++++++++++++++++++++++++++++++++++++++ lorebot/lorebot.go | 109 +++------------------------------------------ 5 files changed, 118 insertions(+), 104 deletions(-) create mode 100644 lorebot/botfunc.go diff --git a/.gitignore b/.gitignore index 25db753..d1544bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp bin/ stage.tmp/ +.auth diff --git a/Makefile b/Makefile index 3109bcc..77c3668 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,9 @@ lorebot: $(GOC) lorebot/lorebot.go run: - go run lorebot/lorebot.go + go run \ + lorebot/lorebot.go \ + lorebot/botfunc.go stat: mkdir -p bin/ diff --git a/config.gcfg b/config.gcfg index 4c668af..ae6434d 100644 --- a/config.gcfg +++ b/config.gcfg @@ -1,7 +1,7 @@ [lorebot] loglevel = "debug" bootstrapdelay = 1 - APIToken = "xoxb-189788161987-7FK5NKnBtrFGGtv4yszr2URb" + AuthFile = ".auth" [redis] host = "127.0.0.1:6379" diff --git a/lorebot/botfunc.go b/lorebot/botfunc.go new file mode 100644 index 0000000..d3e594f --- /dev/null +++ b/lorebot/botfunc.go @@ -0,0 +1,106 @@ +package main + +import ( + "errors" + "fmt" + "strings" + "time" + + "gopkg.in/redis.v5" +) + +func loreQuery(searchTerm string, redisClient *redis.Client) (string, error) { + val, err := redisClient.Get(searchTerm).Result() + if err != nil { + return "", errors.New("String not found.") + } else { + return val, nil + } +} + +func loreCheck(searchTerm, searchType string, redisClient *redis.Client) (string, error) { + searchString := fmt.Sprintf("%s:%s:%s", searchType, searchTerm, "content") + val, err := redisClient.Get(searchString).Result() + if err != nil { + return "", errors.New("String not found.") + } else { + return val, nil + } +} + +func loreNewString(nick, newString, newType string, redisClient *redis.Client) error { + s := strings.SplitN(newString, " ", 2) + command, content := s[0], []byte(s[1]) + + t := time.Now() + + // set created date + attribute := "cdate" + date := t.Format("2006-01-02 15:04:05") + setstring := fmt.Sprintf("%s:%s:%s", newType, command, attribute) + redisClient.Set(setstring, date, 0) + + // set owner + attribute = "owner" + setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute) + redisClient.Set(setstring, nick, 0) + + // set content + attribute = "content" + setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute) + redisClient.Set(setstring, content, 0) + + // append to index + appendIndex := fmt.Sprintf("index:%s:added", newType) + appendString := fmt.Sprintf(" %s", command) + redisClient.Append(appendIndex, appendString) + + return nil +} + +func loreRmString(nick, rmString, rmType string, redisClient *redis.Client) error { + t := time.Now() + + // remove content + keyString := fmt.Sprintf("%s:%s:%s", rmType, rmString, "content") + redisClient.Del(keyString) + //redisClient.Set(keyString, "--LORE HAS BEEN REMOVED--", 0) + + // set "removed by" + attribute := "rmby" + setstring := fmt.Sprintf("%s:%s:%s", rmType, rmString, attribute) + redisClient.Set(setstring, nick, 0) + + // set removed date + attribute = "rmdate" + date := t.Format("2006-01-02 15:04:05") + setstring = fmt.Sprintf("%s:%s:%s", rmType, rmString, attribute) + redisClient.Set(setstring, date, 0) + + // append to deleted index + appendIndex := fmt.Sprintf("index:%s:removed", rmType) + appendString := fmt.Sprintf(" %s", rmString) + redisClient.Append(appendIndex, appendString) + + // remove from added index + queryString := fmt.Sprintf("index:%s:added", rmType) + tmpIndex, _ := loreQuery(queryString, redisClient) + // add a space to the beginning of string to replace + formattedString := fmt.Sprintf(" %s ", rmString) + // find and replace + newIndex := strings.Replace(tmpIndex, formattedString, " ", -1) + redisClient.Set(queryString, newIndex, 0) + + return nil +} + +func loreAttrQuery(queryString, queryType, queryAttr string, redisClient *redis.Client) (string, error) { + searchString := fmt.Sprintf("%s:%s:%s", queryType, queryAttr, queryString) + + val, err := redisClient.Get(searchString).Result() + if err != nil { + return "", errors.New("String not found.") + } else { + return val, nil + } +} diff --git a/lorebot/lorebot.go b/lorebot/lorebot.go index d161e8e..1271a5a 100644 --- a/lorebot/lorebot.go +++ b/lorebot/lorebot.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "errors" "fmt" "io" "io/ioutil" @@ -23,7 +22,7 @@ type Config struct { Lorebot struct { Loglevel string BootstrapDelay time.Duration - APIToken string + AuthFile string } Redis struct { Host string @@ -56,7 +55,12 @@ func main() { glogger.Debug.Println("connection to redis succeeded.") glogger.Info.Println("link to redis on", config.Redis.Host) - api := slack.New(config.Lorebot.APIToken) + // read in creds + credentials, _ := ioutil.ReadFile(config.Lorebot.AuthFile) + lines := strings.Split(string(credentials), "\n") + auth := lines[0] + + api := slack.New(auth) //api.SetDebug(true) rtm := api.NewRTM() @@ -262,105 +266,6 @@ func gnuhandler(rtm *slack.RTM, ev *slack.MessageEvent, target string) { rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel)) } -func loreQuery(searchTerm string, redisClient *redis.Client) (string, error) { - val, err := redisClient.Get(searchTerm).Result() - if err != nil { - return "", errors.New("String not found.") - } else { - return val, nil - } - return "", nil -} - -func loreCheck(searchTerm, searchType string, redisClient *redis.Client) (string, error) { - searchString := fmt.Sprintf("%s:%s:%s", searchType, searchTerm, "content") - val, err := redisClient.Get(searchString).Result() - if err != nil { - return "", errors.New("String not found.") - } else { - return val, nil - } - return "", nil -} - -func loreNewString(nick, newString, newType string, redisClient *redis.Client) error { - s := strings.SplitN(newString, " ", 2) - command, content := s[0], []byte(s[1]) - - t := time.Now() - - // set created date - attribute := "cdate" - date := t.Format("2006-01-02 15:04:05") - setstring := fmt.Sprintf("%s:%s:%s", newType, command, attribute) - redisClient.Set(setstring, date, 0) - - // set owner - attribute = "owner" - setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute) - redisClient.Set(setstring, nick, 0) - - // set content - attribute = "content" - setstring = fmt.Sprintf("%s:%s:%s", newType, command, attribute) - redisClient.Set(setstring, content, 0) - - // append to index - appendIndex := fmt.Sprintf("index:%s:added", newType) - appendString := fmt.Sprintf(" %s", command) - redisClient.Append(appendIndex, appendString) - - return nil -} - -func loreRmString(nick, rmString, rmType string, redisClient *redis.Client) error { - t := time.Now() - - // remove content - keyString := fmt.Sprintf("%s:%s:%s", rmType, rmString, "content") - redisClient.Del(keyString) - //redisClient.Set(keyString, "--LORE HAS BEEN REMOVED--", 0) - - // set "removed by" - attribute := "rmby" - setstring := fmt.Sprintf("%s:%s:%s", rmType, rmString, attribute) - redisClient.Set(setstring, nick, 0) - - // set removed date - attribute = "rmdate" - date := t.Format("2006-01-02 15:04:05") - setstring = fmt.Sprintf("%s:%s:%s", rmType, rmString, attribute) - redisClient.Set(setstring, date, 0) - - // append to deleted index - appendIndex := fmt.Sprintf("index:%s:removed", rmType) - appendString := fmt.Sprintf(" %s", rmString) - redisClient.Append(appendIndex, appendString) - - // remove from added index - queryString := fmt.Sprintf("index:%s:added", rmType) - tmpIndex, _ := loreQuery(queryString, redisClient) - // add a space to the beginning of string to replace - formattedString := fmt.Sprintf(" %s ", rmString) - // find and replace - newIndex := strings.Replace(tmpIndex, formattedString, " ", -1) - redisClient.Set(queryString, newIndex, 0) - - return nil -} - -func loreAttrQuery(queryString, queryType, queryAttr string, redisClient *redis.Client) (string, error) { - searchString := fmt.Sprintf("%s:%s:%s", queryType, queryAttr, queryString) - - val, err := redisClient.Get(searchString).Result() - if err != nil { - return "", errors.New("String not found.") - } else { - return val, nil - } - return "", nil -} - func listhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEvent, listType, listAttr string) { // aka index:command:added newType := fmt.Sprintf("%s:%s:%s", "index", listType, listAttr)