Rename ValidatorRegexp to CaptureRegexp

This commit is contained in:
nemunaire 2023-05-16 09:59:59 +02:00
parent e472b482d6
commit 78189aab37
5 changed files with 58 additions and 58 deletions

View File

@ -730,7 +730,7 @@ type uploadedFlag struct {
IgnoreCase bool
Multiline bool
NoTrim bool
ValidatorRe *string `json:"validator_regexp"`
CaptureRe *string `json:"validator_regexp"`
SortReGroups bool `json:"sort_re_grps"`
Flag string
Value []byte
@ -752,8 +752,8 @@ func createExerciceFlag(c *gin.Context) {
}
var vre *string = nil
if uk.ValidatorRe != nil && len(*uk.ValidatorRe) > 0 {
vre = uk.ValidatorRe
if uk.CaptureRe != nil && len(*uk.CaptureRe) > 0 {
vre = uk.CaptureRe
}
exercice := c.MustGet("exercice").(*fic.Exercice)
@ -842,10 +842,10 @@ func updateExerciceFlag(c *gin.Context) {
flag.ChoicesCost = uk.ChoicesCost
flag.BonusGain = uk.BonusGain
if uk.ValidatorRe != nil && len(*uk.ValidatorRe) > 0 {
flag.ValidatorRegexp = uk.ValidatorRe
if uk.CaptureRe != nil && len(*uk.CaptureRe) > 0 {
flag.CaptureRegexp = uk.CaptureRe
} else {
flag.ValidatorRegexp = nil
flag.CaptureRegexp = nil
}
if _, err := flag.Update(); err != nil {

View File

@ -46,8 +46,8 @@ type ExerciceFlag struct {
Ordered bool `toml:",omitempty"`
CaseSensitive bool `toml:",omitempty"`
NoTrim bool `toml:",omitempty"`
ValidatorRe string `toml:"validator_regexp,omitempty"`
SortReGroups bool `toml:"sort_validator_regexp_groups,omitempty"`
CaptureRe string `toml:"capture_regexp,omitempty"`
SortReGroups bool `toml:"sort_capture_regexp_groups,omitempty"`
Placeholder string `toml:",omitempty"`
Help string `toml:",omitempty"`
BonusGain int32 `toml:"bonus_gain,omitempty"`
@ -143,7 +143,7 @@ func getExerciceParams(i Importer, exercice *fic.Exercice) (params ExerciceParam
Label: flag.Label,
Type: "ucq",
Raw: flag.Raw,
ValidatorRe: flag.ValidatorRe,
CaptureRe: flag.CaptureRe,
Placeholder: flag.Placeholder,
ChoicesCost: flag.ChoicesCost,
Choice: flag.Choice,

View File

@ -160,7 +160,7 @@ func buildKeyFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int, defau
flag.Label = flag.Label[1:]
}
raw, prep, terrs := getRawKey(flag.Raw, flag.ValidatorRe, flag.Ordered, flag.ShowLines, flag.Separator)
raw, prep, terrs := getRawKey(flag.Raw, flag.CaptureRe, flag.Ordered, flag.ShowLines, flag.Separator)
if len(terrs) > 0 {
for _, terr := range terrs {
@ -175,26 +175,26 @@ func buildKeyFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int, defau
errs = append(errs, NewFlagError(exercice, &flag, flagline, fmt.Errorf("WARNING non-printable characters in flag, is this really expected?")))
}
hashedFlag, err := fic.ComputeHashedFlag([]byte(raw), !flag.CaseSensitive, flag.NoTrim, validatorRegexp(flag.ValidatorRe), flag.SortReGroups)
hashedFlag, err := fic.ComputeHashedFlag([]byte(raw), !flag.CaseSensitive, flag.NoTrim, validatorRegexp(flag.CaptureRe), flag.SortReGroups)
if err != nil {
errs = append(errs, NewFlagError(exercice, &flag, flagline, err))
return
}
fk := &fic.FlagKey{
Type: flag.Type,
IdExercice: exercice.Id,
Order: int8(flagline),
Label: flag.Label,
Placeholder: flag.Placeholder,
Help: flag.Help,
Unit: flag.Unit,
IgnoreCase: !flag.CaseSensitive,
Multiline: flag.Type == "text",
ValidatorRegexp: validatorRegexp(flag.ValidatorRe),
SortReGroups: flag.SortReGroups,
Checksum: hashedFlag[:],
ChoicesCost: flag.ChoicesCost,
BonusGain: flag.BonusGain,
Type: flag.Type,
IdExercice: exercice.Id,
Order: int8(flagline),
Label: flag.Label,
Placeholder: flag.Placeholder,
Help: flag.Help,
Unit: flag.Unit,
IgnoreCase: !flag.CaseSensitive,
Multiline: flag.Type == "text",
CaptureRegexp: validatorRegexp(flag.CaptureRe),
SortReGroups: flag.SortReGroups,
Checksum: hashedFlag[:],
ChoicesCost: flag.ChoicesCost,
BonusGain: flag.BonusGain,
}
// Call checks hooks

View File

@ -36,8 +36,8 @@ type FlagKey struct {
NoTrim bool `json:"notrim,omitempty"`
// Multiline indicates if the flag is stored on multiple lines
Multiline bool `json:"multiline"`
// ValidatorRegexp extracts a subset of the player's answer, that will be checked.
ValidatorRegexp *string `json:"validator_regexp"`
// CaptureRegexp extracts a subset of the player's answer, that will be checked.
CaptureRegexp *string `json:"capture_regexp"`
// SortReGroups indicates if groups resulting of the validator regexp should be sorted.
SortReGroups bool `json:"sort_re_grps"`
// Checksum is the expected hashed flag
@ -110,7 +110,7 @@ func (e *Exercice) GetFlagKeys() ([]*FlagKey, error) {
k := &FlagKey{}
k.IdExercice = e.Id
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain); err != nil {
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain); err != nil {
return nil, err
}
@ -127,26 +127,26 @@ func (e *Exercice) GetFlagKeys() ([]*FlagKey, error) {
// GetFlagKey returns a list of flags comming with the challenge.
func GetFlagKey(id int) (k *FlagKey, err error) {
k = &FlagKey{}
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ?", id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.Multiline, &k.NoTrim, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ?", id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.Multiline, &k.NoTrim, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
return
}
// GetFlagKey returns a flag.
func (e *Exercice) GetFlagKey(id int) (k *FlagKey, err error) {
k = &FlagKey{}
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ? AND id_exercice = ?", id, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_flag = ? AND id_exercice = ?", id, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
return
}
// GetFlagKeyByLabel returns a flag matching the given label.
func (e *Exercice) GetFlagKeyByLabel(label string) (k *FlagKey, err error) {
k = &FlagKey{}
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE label LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.ValidatorRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
err = DBQueryRow("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE label LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Type, &k.Placeholder, &k.Help, &k.Unit, &k.IgnoreCase, &k.NoTrim, &k.Multiline, &k.CaptureRegexp, &k.SortReGroups, &k.Checksum, &k.ChoicesCost, &k.BonusGain)
return
}
// ComputeHashedFlag calculates the expected checksum for the given raw_value.
func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, validator_regexp *string, sortregroups bool) (hash [blake2b.Size]byte, err error) {
func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, capture_regexp *string, sortregroups bool) (hash [blake2b.Size]byte, err error) {
if ignorecase {
raw_value = bytes.ToLower(raw_value)
}
@ -156,8 +156,8 @@ func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, validator
}
// Check that raw value passes through the regexp
if validator_regexp != nil {
if raw_value, err = ExecValidatorRegexp(*validator_regexp, raw_value, ignorecase, sortregroups); err != nil {
if capture_regexp != nil {
if raw_value, err = ExecCaptureRegexp(*capture_regexp, raw_value, ignorecase, sortregroups); err != nil {
return
}
}
@ -171,14 +171,14 @@ func ComputeHashedFlag(raw_value []byte, ignorecase bool, notrim bool, validator
return
}
func ExecValidatorRegexp(vre string, val []byte, ignorecase bool, sortregroups bool) ([]byte, error) {
func ExecCaptureRegexp(vre string, val []byte, ignorecase bool, sortregroups bool) ([]byte, error) {
if ignorecase {
vre = "(?i)" + vre
}
if re, err := regexp.Compile(vre); err != nil {
return val, err
} else if res := re.FindSubmatch(val); res == nil {
return val, errors.New("expected flag doesn't pass through the validator_regexp")
return val, errors.New("expected flag doesn't pass through the capture_regexp")
} else if sortregroups && len(res) > 2 {
var tab []string
for _, v := range res[1:] {
@ -192,24 +192,24 @@ func ExecValidatorRegexp(vre string, val []byte, ignorecase bool, sortregroups b
}
// AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database.
func (e *Exercice) AddRawFlagKey(name string, t string, placeholder string, ignorecase bool, multiline bool, notrim bool, validator_regexp *string, sortregroups bool, raw_value []byte, choicescost int32, bonusgain int32) (*FlagKey, error) {
hash, err := ComputeHashedFlag(raw_value, ignorecase, notrim, validator_regexp, sortregroups)
func (e *Exercice) AddRawFlagKey(name string, t string, placeholder string, ignorecase bool, multiline bool, notrim bool, capture_regexp *string, sortregroups bool, raw_value []byte, choicescost int32, bonusgain int32) (*FlagKey, error) {
hash, err := ComputeHashedFlag(raw_value, ignorecase, notrim, capture_regexp, sortregroups)
if err != nil {
return nil, err
}
f := &FlagKey{
Type: t,
Label: name,
Placeholder: placeholder,
IgnoreCase: ignorecase,
NoTrim: notrim,
Multiline: multiline,
ValidatorRegexp: validator_regexp,
SortReGroups: sortregroups,
Checksum: hash[:],
ChoicesCost: choicescost,
BonusGain: bonusgain,
Type: t,
Label: name,
Placeholder: placeholder,
IgnoreCase: ignorecase,
NoTrim: notrim,
Multiline: multiline,
CaptureRegexp: capture_regexp,
SortReGroups: sortregroups,
Checksum: hash[:],
ChoicesCost: choicescost,
BonusGain: bonusgain,
}
_, err = f.Create(e)
@ -233,13 +233,13 @@ func (k *FlagKey) RecoverId() (Flag, error) {
// AddFlagKey creates and fills a new struct Flag, from a hashed flag, and registers it into the database.
func (k *FlagKey) Create(e *Exercice) (Flag, error) {
// Check the regexp compile
if k.ValidatorRegexp != nil {
if _, err := regexp.Compile(*k.ValidatorRegexp); err != nil {
if k.CaptureRegexp != nil {
if _, err := regexp.Compile(*k.CaptureRegexp); err != nil {
return k, err
}
}
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", e.Id, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.ValidatorRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain); err != nil {
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", e.Id, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.CaptureRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain); err != nil {
return k, err
} else if kid, err := res.LastInsertId(); err != nil {
return k, err
@ -252,19 +252,19 @@ func (k *FlagKey) Create(e *Exercice) (Flag, error) {
// ComputeChecksum calculates the checksum for a given value.
func (k *FlagKey) ComputeChecksum(val []byte) ([]byte, error) {
cksum, err := ComputeHashedFlag(val, k.IgnoreCase, k.NoTrim, k.ValidatorRegexp, k.SortReGroups)
cksum, err := ComputeHashedFlag(val, k.IgnoreCase, k.NoTrim, k.CaptureRegexp, k.SortReGroups)
return cksum[:], err
}
// Update applies modifications back to the database.
func (k *FlagKey) Update() (int64, error) {
if k.ValidatorRegexp != nil {
if _, err := regexp.Compile(*k.ValidatorRegexp); err != nil {
if k.CaptureRegexp != nil {
if _, err := regexp.Compile(*k.CaptureRegexp); err != nil {
return 0, err
}
}
if res, err := DBExec("UPDATE exercice_flags SET id_exercice = ?, ordre = ?, label = ?, type = ?, placeholder = ?, help = ?, unit = ?, ignorecase = ?, notrim = ?, multiline = ?, validator_regexp = ?, sort_re_grps = ?, cksum = ?, choices_cost = ?, bonus_gain = ? WHERE id_flag = ?", k.IdExercice, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.ValidatorRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain, k.Id); err != nil {
if res, err := DBExec("UPDATE exercice_flags SET id_exercice = ?, ordre = ?, label = ?, type = ?, placeholder = ?, help = ?, unit = ?, ignorecase = ?, notrim = ?, multiline = ?, validator_regexp = ?, sort_re_grps = ?, cksum = ?, choices_cost = ?, bonus_gain = ? WHERE id_flag = ?", k.IdExercice, k.Order, k.Label, k.Type, k.Placeholder, k.Help, k.Unit, k.IgnoreCase, k.NoTrim, k.Multiline, k.CaptureRegexp, k.SortReGroups, k.Checksum, k.ChoicesCost, k.BonusGain, k.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err

View File

@ -45,7 +45,7 @@ type myTeamFlag struct {
IgnoreOrder bool `json:"ignore_order,omitempty"`
IgnoreCase bool `json:"ignore_case,omitempty"`
Multiline bool `json:"multiline,omitempty"`
ValidatorRe *string `json:"validator_regexp,omitempty"`
CaptureRe *string `json:"validator_regexp,omitempty"`
Solved *time.Time `json:"found,omitempty"`
PSolved *time.Time `json:"part_solved,omitempty"`
Soluce string `json:"soluce,omitempty"`
@ -272,7 +272,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
// Retrieve solved state or solution for public iface
if t == nil {
flag.IgnoreCase = k.IgnoreCase
flag.ValidatorRe = k.ValidatorRegexp
flag.CaptureRe = k.CaptureRegexp
flag.Soluce = hex.EncodeToString(k.Checksum)
} else if PartialValidation {
flag.Solved = t.HasPartiallySolved(k)