diff --git a/lorebot/lorebot.go b/lorebot/lorebot.go index e265268..a3096d1 100644 --- a/lorebot/lorebot.go +++ b/lorebot/lorebot.go @@ -1,9 +1,14 @@ package main import ( + "bytes" "errors" "fmt" + "io" "io/ioutil" + "mime/multipart" + "net/http" + "net/textproto" "os" "strings" "time" @@ -144,7 +149,7 @@ func dynamicCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient * switch trimmedPrefix { case "lore": s := strings.SplitN(string(ev.Text), " ", 2) - lorehandler(redisClient, rtm, ev, s[1]) + contenthandler(rtm, ev, s[1], "lore", redisClient) case "newlore": rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) println(content) @@ -182,11 +187,15 @@ func staticCommandHandler(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *r case "version": rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) case "lorelist": - rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) + listhandler(redisClient, rtm, ev, "lore", "added") case "removedlore": - rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) + listhandler(redisClient, rtm, ev, "lore", "removed") + case "commandlist": + listhandler(redisClient, rtm, ev, "command", "added") + case "removedcommands": + listhandler(redisClient, rtm, ev, "command", "removed") case "lorestatus": - rtm.SendMessage(rtm.NewOutgoingMessage("command not yet supported", ev.Channel)) + lorestatus(rtm, ev, redisClient) } } @@ -214,12 +223,12 @@ func advancedhelpmsg(rtm *slack.RTM, ev *slack.MessageEvent) { rtm.SendMessage(rtm.NewOutgoingMessage(help2, ev.Channel)) } -func lorehandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEvent, rawMessage string) { - loreTerm := strings.Replace(rawMessage, ".lore", "", -1) +func contenthandler(rtm *slack.RTM, ev *slack.MessageEvent, message, queryType string, redisClient *redis.Client) { + loreTerm := strings.Replace(message, ".lore", "", -1) println("searching for:", loreTerm) //rtm.SendMessage(rtm.NewOutgoingMessage(fmt.Sprintf("searching for '%s'\n", loreTerm), ev.Channel)) - content, err := checkLore(loreTerm, redisClient) + content, err := loreCheck(loreTerm, queryType, redisClient) if err == nil { // lore found, print it rtm.SendMessage(rtm.NewOutgoingMessage(content, ev.Channel)) @@ -240,12 +249,76 @@ func gnuhandler(redisClient *redis.Client, rtm *slack.RTM, ev *slack.MessageEven rtm.SendMessage(rtm.NewOutgoingMessage(gnuFmt, ev.Channel)) } -func checkLore(searchTerm string, redisClient *redis.Client) (string, error) { - val, err := redisClient.Get(fmt.Sprintf("lore:%s:content", searchTerm)).Result() +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 + 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 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) + //command, err := botfunc.Query(newType, redisClient) + command, err := loreQuery(newType, redisClient) + if err != nil { + glogger.Error.Println(err) + println(err) + } + fCommand := strings.Replace(command, " ", "\n", -1) + fCommand = strings.Replace(fCommand, "\n", "", 1) + + // initialize buffer + bodyBuf := &bytes.Buffer{} + bodyWriter := multipart.NewWriter(bodyBuf) + // create header and body. filename must be set + mh := make(textproto.MIMEHeader) + mh.Set("Content-Type", "application/octet-stream") + mh.Set("Content-Disposition", "form-data; name=\"file\"; filename=\" \"") + // write body with mime header + partWriter, _ := bodyWriter.CreatePart(mh) + io.Copy(partWriter, bytes.NewBufferString(fCommand)) + contentType := bodyWriter.FormDataContentType() + bodyWriter.Close() + // set content type to multipart/form-data, required for bitnuke.io + h := make(textproto.MIMEHeader) + h.Set("Content-Type", "multipart/form-data") + bodyWriter.CreatePart(h) + bodyWriter.Close() + + // POST to bitnuke.io + resp, err := http.Post("https://bitnuke.io/upload", contentType, bodyBuf) + if err != nil { + println(err) + } + defer resp.Body.Close() + resp_body, err := ioutil.ReadAll(resp.Body) + if err != nil { + println(err) + } + // format response to be link + fResponse := fmt.Sprintf("https://bitnuke.io/%s", resp_body[:]) + // print to channel + rtm.SendMessage(rtm.NewOutgoingMessage(fResponse, ev.Channel)) +} + +func lorestatus(rtm *slack.RTM, ev *slack.MessageEvent, redisClient *redis.Client) { + command, _ := loreQuery("index:lore:added", redisClient) + loreammount := strings.Count(command, " ") + status := fmt.Sprintf("the lore db has %d entries", loreammount-1) + rtm.SendMessage(rtm.NewOutgoingMessage(status, ev.Channel)) }