From 493d179ed45542fde4a2b346bc040ef579322d79 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Sat, 16 Jan 2021 14:03:17 -0700 Subject: [PATCH] nsfw commands. WIP --- cmd/nsfw.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/cmd/nsfw.go b/cmd/nsfw.go index 1273997..c611be2 100644 --- a/cmd/nsfw.go +++ b/cmd/nsfw.go @@ -1,6 +1,10 @@ package cmd import ( + "fmt" + "strings" + "unicode" + "github.com/kf5grd/keybasebot" "github.com/kf5grd/keybasebot/pkg/kvstore" "samhofi.us/x/keybase/v2/types/chat1" @@ -13,8 +17,57 @@ var NsfwAd = chat1.UserBotCommandInput{ } func SetNsfw(m chat1.MsgSummary, b *keybasebot.Bot) (bool, error) { - b.KB.SendMessageByConvID(m.ConvID, "Pong!") - b.Logger.Info("owner command in convid %s", m.ConvID) + fields := strings.Fields(strings.TrimSpace(strings.Replace(m.Content.Text.Body, "!nsfw", "", 1))) + + // first pass filters + if fields == nil || len(fields) > 2 { + return true, fmt.Errorf("@%s - Invalid Request.", m.Sender.Username) + } + + // validate the arguments + if fields[0] != "add" && fields[0] != "del" && fields[0] != "ls" { + return true, fmt.Errorf("@%s - Invalid Request.", m.Sender.Username) + } + + // if its list, list and exit + if fields[0] == "ls" { + // get the allowed teams + data, ok := b.Meta["nsfwAllowed"] + if !ok { + return true, fmt.Errorf("@%s - Unable to fetch allowed teams.", m.Sender.Username) + } else { + // the key exists and needs to be updated + allowedTeams := data.(map[chat1.ConvIDStr]bool) + message := fmt.Sprintf("NSFW: %+v\n", allowedTeams) + b.KB.SendMessageByConvID(m.ConvID, message) + } + return true, nil + } + + if fields[1] != "here" && !isConversationId(fields[1]) { + return true, fmt.Errorf("@%s - Unable to parse conversation id.", m.Sender.Username) + } + var convid chat1.ConvIDStr + // resolve the conversation id + if fields[1] == "here" { + convid = m.ConvID + } else { + convid = chat1.ConvIDStr(fields[1]) + } + + // then do the action + if fields[0] == "add" { + ok := addNsfwTeam(b, convid) + if !ok { + return true, fmt.Errorf("@%s - Unable to add conversation id.", m.Sender.Username) + } + } else if fields[0] == "del" { + ok := delNsfwTeam(b, convid) + if !ok { + return true, fmt.Errorf("@%s - Unable to delete conversation id.", m.Sender.Username) + } + } + b.KB.ReactByConvID(m.ConvID, m.Id, ":white_check_mark:") return true, nil } @@ -27,7 +80,7 @@ func addNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool { allowedTeams[c] = true } else { // the key exists and needs to be updated - allowedTeams := data.(map[chat1.ConvIDStr]bool) + allowedTeams = data.(map[chat1.ConvIDStr]bool) allowedTeams[c] = true } // write the key to meta @@ -54,7 +107,7 @@ func delNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool { return true } else { // the key exists and needs to be updated - allowedTeams := data.(map[chat1.ConvIDStr]bool) + allowedTeams = data.(map[chat1.ConvIDStr]bool) delete(allowedTeams, c) } // write the key to meta @@ -71,3 +124,15 @@ func delNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool { } return true } + +func isConversationId(s string) bool { + if len(s) != 64 { + return false + } + for _, c := range s { + if !unicode.IsLetter(c) && !unicode.IsNumber(c) { + return false + } + } + return true +}