Browse Source

nsfw commands. WIP

master
David Haukeness 5 years ago
parent
commit
493d179ed4
  1. 73
      cmd/nsfw.go

73
cmd/nsfw.go

@ -1,6 +1,10 @@
package cmd package cmd
import ( import (
"fmt"
"strings"
"unicode"
"github.com/kf5grd/keybasebot" "github.com/kf5grd/keybasebot"
"github.com/kf5grd/keybasebot/pkg/kvstore" "github.com/kf5grd/keybasebot/pkg/kvstore"
"samhofi.us/x/keybase/v2/types/chat1" "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) { func SetNsfw(m chat1.MsgSummary, b *keybasebot.Bot) (bool, error) {
b.KB.SendMessageByConvID(m.ConvID, "Pong!") fields := strings.Fields(strings.TrimSpace(strings.Replace(m.Content.Text.Body, "!nsfw", "", 1)))
b.Logger.Info("owner command in convid %s", m.ConvID)
// 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 return true, nil
} }
@ -27,7 +80,7 @@ func addNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool {
allowedTeams[c] = true allowedTeams[c] = true
} else { } else {
// the key exists and needs to be updated // the key exists and needs to be updated
allowedTeams := data.(map[chat1.ConvIDStr]bool) allowedTeams = data.(map[chat1.ConvIDStr]bool)
allowedTeams[c] = true allowedTeams[c] = true
} }
// write the key to meta // write the key to meta
@ -54,7 +107,7 @@ func delNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool {
return true return true
} else { } else {
// the key exists and needs to be updated // the key exists and needs to be updated
allowedTeams := data.(map[chat1.ConvIDStr]bool) allowedTeams = data.(map[chat1.ConvIDStr]bool)
delete(allowedTeams, c) delete(allowedTeams, c)
} }
// write the key to meta // write the key to meta
@ -71,3 +124,15 @@ func delNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool {
} }
return true 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
}

Loading…
Cancel
Save