package cmd import ( "fmt" "strings" "unicode" "github.com/kf5grd/keybasebot" "github.com/kf5grd/keybasebot/pkg/kvstore" "samhofi.us/x/keybase/v2/types/chat1" ) var NsfwAd = chat1.UserBotCommandInput{ Name: "nsfw", Usage: " ", Description: "Enables or Disables NSFW commands for conversations", } func SetNsfw(m chat1.MsgSummary, b *keybasebot.Bot) (bool, error) { 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 } func addNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool { allowedTeams := make(map[chat1.ConvIDStr]bool) // get the allowed teams data, ok := b.Meta["nsfwAllowed"] if !ok { // the key doesn't exist yet so go ahead and create it allowedTeams[c] = true } else { // the key exists and needs to be updated allowedTeams = data.(map[chat1.ConvIDStr]bool) allowedTeams[c] = true } // write the key to meta b.Meta["nsfwAllowed"] = allowedTeams // write the key to kvStore if enabled data, ok = b.Meta["kvstore"] if ok { team := data.(string) kvKey := kvstore.New("nsfwAllowed", allowedTeams, -1) err := kvstore.Put(b.KB, team, "ssh0le", kvKey) if err != nil { return false } } return true } func delNsfwTeam(b *keybasebot.Bot, c chat1.ConvIDStr) bool { allowedTeams := make(map[chat1.ConvIDStr]bool) // get the allowed teams data, ok := b.Meta["nsfwAllowed"] if !ok { // the key doesn't exist yet so go return return true } else { // the key exists and needs to be updated allowedTeams = data.(map[chat1.ConvIDStr]bool) delete(allowedTeams, c) } // write the key to meta b.Meta["nsfwAllowed"] = allowedTeams // write the key to kvStore if enabled data, ok = b.Meta["kvstore"] if ok { team := data.(string) kvKey := kvstore.New("nsfwAllowed", allowedTeams, -1) err := kvstore.Put(b.KB, team, "ssh0le", kvKey) if err != nil { return false } } 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 }