package sync import ( "fmt" "math/rand" "path" "sort" "strings" "unicode" "srs.epita.fr/fic-server/libfic" ) // isFullGraphic detects if some rune are not graphic one. // This function is usefull to display warning when importing key ending with \r. func isFullGraphic(s string) bool { for _, c := range s { if !unicode.IsGraphic(c) { return false } } return true } func validatorRegexp(vre string) (validator_regexp *string) { if len(vre) > 0 { validator_regexp = &vre } else { validator_regexp = nil } return } func getRawKey(input interface{}, validatorRe string, ordered bool) (raw string, prep string, errs []string) { separator := "," // Concatenate array if f, ok := input.([]interface{}); ok { if len(validatorRe) > 0 { errs = append(errs, "ValidatorRe cannot be defined for this kind of flag.") validatorRe = "" } if len(separator) == 0 { separator = "," } else if len(separator) > 1 { separator = string(separator[0]) errs = append(errs, "separator truncated to %q") } var fitems []string for _, v := range f { if g, ok := v.(string); ok { if strings.Index(g, separator) != -1 { errs = append(errs, "flag items cannot contain %q character as it is used as separator. Change the separator attribute for this flag.") return } else { fitems = append(fitems, g) } } else { errs = append(errs, "item %d has an invalid type: can only be string, is %T.") return } } ignord := "f" if !ordered { sort.Strings(fitems) ignord = "t" } raw = strings.Join(fitems, separator) + separator prep = "`" + separator + ignord } else if f, ok := input.(int64); ok { raw = fmt.Sprintf("%d", f) } else if f, ok := input.(string); !ok { errs = append(errs, fmt.Sprintf("has an invalid type: can only be []string or string, not %T", input)) return } else { raw = f } return } func buildKeyFlag(exercice fic.Exercice, flag ExerciceFlag, flagline int, defaultLabel string) (f *fic.Flag, choices []fic.FlagChoice, errs []string) { if len(flag.Label) == 0 { flag.Label = defaultLabel } if flag.Label[0] == '`' { errs = append(errs, fmt.Sprintf("%q: flag #%d: Label should not begin with `.", path.Base(exercice.Path), flagline)) flag.Label = flag.Label[1:] } raw, prep, terrs := getRawKey(flag.Raw, flag.ValidatorRe, flag.Ordered) if len(terrs) > 0 { for _, err := range terrs { errs = append(errs, fmt.Sprintf("%q: flag #%d: %s", path.Base(exercice.Path), flagline, err)) } f = nil return } flag.Label = prep + flag.Label if !isFullGraphic(raw) { errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), flagline)) } hashedFlag := fic.ComputeHashedFlag([]byte(raw)) fl := fic.Flag(fic.FlagKey{ IdExercice: exercice.Id, Label: flag.Label, Help: flag.Help, IgnoreCase: !flag.CaseSensitive, ValidatorRegexp: validatorRegexp(flag.ValidatorRe), Checksum: hashedFlag[:], ChoicesCost: flag.ChoicesCost, }) f = &fl if len(flag.Choice) > 0 || flag.Type == "ucq" { // Import choices hasOne := false if !flag.NoShuffle { rand.Shuffle(len(flag.Choice), func(i, j int) { flag.Choice[i], flag.Choice[j] = flag.Choice[j], flag.Choice[i] }) } for _, choice := range flag.Choice { val, prep, terrs := getRawKey(choice.Value, "", false) if len(terrs) > 0 { for _, err := range terrs { errs = append(errs, fmt.Sprintf("%q: flag #%d: %s", path.Base(exercice.Path), flagline, err)) } continue } if len(choice.Label) == 0 { choice.Label = val } choice.Label = prep + choice.Label choices = append(choices, fic.FlagChoice{ Label: choice.Label, Value: val, }) if val == raw { hasOne = true } } if !hasOne { errs = append(errs, fmt.Sprintf("%q: error in flag #%d: no valid answer defined.", path.Base(exercice.Path), flagline)) } } return } type importFlag struct { Line int Flag fic.Flag Choices []fic.FlagChoice FilesDeps []string FlagsDeps []int64 } // buildExerciceFlags read challenge.txt and extract all flags. func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]importFlag, errs []string) { params, gerrs := getExerciceParams(i, exercice) if len(gerrs) > 0 { return flags, gerrs } flags = map[int64]importFlag{} for nline, flag := range params.Flags { if flag.Id == 0 { // TODO: should be more smart than that. Perhaps search to increment if possible. flag.Id = rand.Int63() } // Ensure flag ID is unique for _, ok := flags[flag.Id]; ok; _, ok = flags[flag.Id] { errs = append(errs, fmt.Sprintf("%q: flag #%d: identifier already used (%d), using a random one.", path.Base(exercice.Path), nline+1, flag.Id)) flag.Id = rand.Int63() } switch strings.ToLower(flag.Type) { case "": flag.Type = "key" case "key": flag.Type = "key" case "vector": flag.Type = "vector" case "ucq": flag.Type = "ucq" case "mcq": flag.Type = "mcq" default: errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'mcq', 'ucq' or 'vector'.", path.Base(exercice.Path), nline+1)) continue } if flag.Type == "key" || flag.Type == "ucq" || flag.Type == "vector" { addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag") if len(berrs) > 0 { errs = append(errs, berrs...) } if addedFlag != nil { flags[flag.Id] = importFlag{ Line: nline + 1, Flag: *addedFlag, Choices: choices, } } } else if flag.Type == "mcq" { addedFlag := fic.MCQ{ IdExercice: exercice.Id, Title: flag.Label, Entries: []fic.MCQ_entry{}, } hasOne := false isJustified := false if !flag.NoShuffle { rand.Shuffle(len(flag.Choice), func(i, j int) { flag.Choice[i], flag.Choice[j] = flag.Choice[j], flag.Choice[i] }) } for cid, choice := range flag.Choice { var val bool if choice.Raw != nil { if hasOne && !isJustified { errs = append(errs, fmt.Sprintf("%q: error MCQ #%d: all true items has to be justified in this MCQ.", path.Base(exercice.Path), nline+1)) continue } val = true isJustified = true } else if p, ok := choice.Value.(bool); ok { val = p if isJustified { errs = append(errs, fmt.Sprintf("%q: error MCQ #%d: all true items has to be justified in this MCQ.", path.Base(exercice.Path), nline+1)) continue } } else if choice.Value == nil { val = false } else { errs = append(errs, fmt.Sprintf("%q: error in MCQ %d choice %d: incorrect type for value: %T is not boolean.", path.Base(exercice.Path), nline+1, cid, choice.Value)) continue } addedFlag.Entries = append(addedFlag.Entries, fic.MCQ_entry{ Label: choice.Label, Response: val, }) if isJustified && choice.Raw != nil { addedFlag, choices, berrs := buildKeyFlag(exercice, choice.ExerciceFlag, nline+1, "Flag correspondant") if len(berrs) > 0 { errs = append(errs, berrs...) } if addedFlag != nil { flags[flag.Id] = importFlag{ Line: nline, Flag: *addedFlag, Choices: choices, } } } } flags[flag.Id] = importFlag{ Line: nline + 1, Flag: addedFlag, } } // Read dependency to flag for _, nf := range flag.NeedFlag { if v, ok := flags[flag.Id]; ok { v.FlagsDeps = append(v.FlagsDeps, nf.Id) flags[flag.Id] = v } } // Read dependency to file for _, lf := range flag.LockedFile { if v, ok := flags[flag.Id]; ok { v.FilesDeps = append(v.FilesDeps, lf.Filename) flags[flag.Id] = v } } } return } // CheckExerciceFlags checks if all flags for the given challenge are correct. func CheckExerciceFlags(i Importer, exercice fic.Exercice, files []fic.EFile) (rf []fic.Flag, errs []string) { flags, berrs := buildExerciceFlags(i, exercice) errs = append(errs, berrs...) for _, flag := range flags { // Check dependency to flag for _, nf := range flag.FlagsDeps { if _, ok := flags[nf]; !ok { errs = append(errs, fmt.Sprintf("%q: error flag #%d dependency to flag id=%d: id not defined", path.Base(exercice.Path), flag.Line, nf)) } } // Check dependency to file for _, lf := range flag.FilesDeps { found := false for _, f := range files { if f.Name == lf { found = true break } } if !found { errs = append(errs, fmt.Sprintf("%q: error flag #%d dependency to %s: No such file", path.Base(exercice.Path), flag.Line, lf)) } } rf = append(rf, flag.Flag) } return } // SyncExerciceFlags imports all kind of flags for the given challenge. func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) { if _, err := exercice.WipeFlags(); err != nil { errs = append(errs, err.Error()) } else if _, err := exercice.WipeMCQs(); err != nil { errs = append(errs, err.Error()) } else { flags, berrs := buildExerciceFlags(i, exercice) errs = append(errs, berrs...) kmap := map[int64]fic.Flag{} // Import flags for flagid, flag := range flags { if addedFlag, err := exercice.AddFlag(flag.Flag); err != nil { errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), flag.Line, err)) } else { if f, ok := addedFlag.(fic.FlagKey); ok { for _, choice := range flag.Choices { if _, err := f.AddChoice(choice); err != nil { errs = append(errs, fmt.Sprintf("%q: error in flag #%d choice #FIXME: %s", path.Base(exercice.Path), flag.Line, err)) } } } kmap[flagid] = addedFlag // Import dependency to flag for _, nf := range flag.FlagsDeps { if rf, ok := kmap[nf]; !ok { errs = append(errs, fmt.Sprintf("%q: error flag #%d dependency to flag id=%d: id not defined, perhaps not available at time of processing", path.Base(exercice.Path), flag.Line, nf)) } else if err := addedFlag.AddDepend(rf); err != nil { errs = append(errs, fmt.Sprintf("%q: error flag #%d dependency to id=%d: %s", path.Base(exercice.Path), flag.Line, nf, err)) } } // Import dependency to file for _, lf := range flag.FilesDeps { if rf, err := exercice.GetFileByFilename(lf); err != nil { errs = append(errs, fmt.Sprintf("%q: error flag #%d dependency to %s: %s", path.Base(exercice.Path), flag.Line, lf, err)) } else if err := rf.AddDepend(addedFlag); err != nil { errs = append(errs, fmt.Sprintf("%q: error flag #%d dependency to %s: %s", path.Base(exercice.Path), flag.Line, lf, err)) } } } } } return }