server/admin/sync/exercice_keys.go

90 lines
2.9 KiB
Go
Raw Normal View History

package sync
import (
"fmt"
"path"
2017-12-17 15:10:59 +00:00
"unicode"
"srs.epita.fr/fic-server/libfic"
)
2018-03-09 18:07:08 +00:00
// isFullGraphic detects if some rune are not graphic one.
// This function is usefull to display warning when importing key ending with \r.
2017-12-17 15:10:59 +00:00
func isFullGraphic(s string) bool {
for _, c := range s {
if !unicode.IsGraphic(c) {
return false
}
}
return true
}
2018-05-12 00:01:49 +00:00
// SyncExerciceKeys reads the content of challenge.txt and import "classic" flags as Key for the given challenge.
func SyncExerciceKeys(i Importer, exercice fic.Exercice) (errs []string) {
if _, err := exercice.WipeKeys(); err != nil {
errs = append(errs, err.Error())
2018-05-12 00:01:49 +00:00
} else if _, err := exercice.WipeMCQs(); err != nil {
errs = append(errs, err.Error())
} else if params, err := parseExerciceParams(i, exercice.Path); err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
} else if len(params.Flags) == 0 && len(params.FlagsUCQ) == 0 && len(params.FlagsMCQ) == 0 {
errs = append(errs, fmt.Sprintf("%q: has no flag", path.Base(exercice.Path)))
} else {
2018-05-12 00:01:49 +00:00
// Import normal flags
for nline, flag := range params.Flags {
if len(flag.Label) == 0 {
flag.Label = "Flag"
}
2018-05-12 00:01:49 +00:00
if !isFullGraphic(flag.Raw) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
}
2018-05-12 00:01:49 +00:00
if _, err := exercice.AddRawKey(flag.Label, flag.Raw); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
}
}
2017-12-16 02:39:57 +00:00
2018-05-12 00:01:49 +00:00
// Import UCQ flags
for nline, flag := range params.FlagsUCQ {
if len(flag.Label) == 0 {
flag.Label = "Flag"
2017-12-16 02:39:57 +00:00
}
2018-05-12 00:01:49 +00:00
if !isFullGraphic(flag.Raw) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag UCQ #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
2017-12-16 02:39:57 +00:00
}
2018-05-12 00:01:49 +00:00
if _, err := exercice.AddRawKey(flag.Label, flag.Raw); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag UCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
2017-12-16 02:39:57 +00:00
continue
}
}
2018-05-12 00:01:49 +00:00
// Import MCQ flags
for nline, quest := range params.FlagsMCQ {
if flag, err := exercice.AddMCQ(quest.Label); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag MCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
2017-12-16 02:39:57 +00:00
continue
} else {
hasOne := false
2018-05-12 00:01:49 +00:00
for cid, choice := range quest.Choice {
if _, err := flag.AddEntry(choice.Label, choice.Value); err != nil {
errs = append(errs, fmt.Sprintf("%q: error in MCQ %d choice %d: %s", path.Base(exercice.Path), nline + 1, cid, err))
2017-12-17 15:10:59 +00:00
continue
}
2018-05-12 00:01:49 +00:00
if choice.Value {
hasOne = true
2017-12-16 02:39:57 +00:00
}
}
if !hasOne {
2018-05-12 00:01:49 +00:00
errs = append(errs, fmt.Sprintf("%q: warning MCQ %d: no valid answer defined, is this really expected?", path.Base(exercice.Path), nline + 1))
2017-12-16 02:39:57 +00:00
}
}
}
}
2018-05-12 00:01:49 +00:00
return
2017-12-16 02:39:57 +00:00
}