server/checker/registration.go

101 lines
3.2 KiB
Go
Raw Normal View History

2016-03-06 17:59:33 +00:00
package main
import (
"encoding/base64"
"encoding/binary"
2016-03-06 17:59:33 +00:00
"encoding/json"
2017-12-21 21:18:18 +00:00
"fmt"
"html"
2016-03-06 17:59:33 +00:00
"io/ioutil"
"log"
"math/rand"
"os"
"path"
"srs.epita.fr/fic-server/libfic"
)
2019-02-04 17:14:46 +00:00
var (
allowRegistration = false
2019-07-11 17:52:13 +00:00
canJoinTeam = false
denyTeamCreation = false
2019-02-04 17:14:46 +00:00
)
2017-12-21 21:18:18 +00:00
type uTeamRegistration struct {
TeamName string
2019-02-04 17:14:46 +00:00
JTeam int64
2017-12-21 21:18:18 +00:00
Members []fic.Member
}
2021-11-22 14:35:07 +00:00
func registrationProcess(id string, team *fic.Team, members []fic.Member, team_id string) {
for i, m := range members {
2019-02-04 17:14:46 +00:00
// Force Id to 0, as it shouldn't have been defined yet
m.Id = 0
if err := team.GainMember(&members[i]); err != nil {
2019-02-04 17:14:46 +00:00
log.Println("[WRN] Unable to add member (", m, ") to team (", team, "):", err)
}
}
teamDirPath := fmt.Sprintf("%d", team.Id)
// Create team directories into TEAMS
if err := os.MkdirAll(path.Join(TeamsDir, teamDirPath), 0751); err != nil {
log.Println(id, "[ERR]", err)
2019-02-04 17:14:46 +00:00
}
if err := os.Symlink(teamDirPath, path.Join(TeamsDir, team_id)); err != nil {
log.Println(id, "[ERR]", err)
2019-02-04 17:14:46 +00:00
}
appendGenQueue(fic.GenStruct{Id: id, Type: fic.GenTeam, TeamId: team.Id})
appendGenQueue(fic.GenStruct{Id: id, Type: fic.GenTeams})
2019-02-04 17:14:46 +00:00
}
func treatRegistration(pathname string, team_id string) {
// Generate a unique identifier to follow the request in logs
bid := make([]byte, 5)
binary.LittleEndian.PutUint32(bid, rand.Uint32())
id := "[" + base64.StdEncoding.EncodeToString(bid) + "]"
log.Println(id, "New registration receive", pathname)
2017-12-21 21:18:18 +00:00
var nTeam uTeamRegistration
2016-03-06 17:59:33 +00:00
2019-02-04 17:14:46 +00:00
if !allowRegistration {
log.Printf("%s [ERR] Registration received, whereas disabled. Skipped.\n", id)
2019-02-04 17:14:46 +00:00
} else if cnt_raw, err := ioutil.ReadFile(pathname); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2017-12-21 21:18:18 +00:00
} else if err := json.Unmarshal(cnt_raw, &nTeam); err != nil {
log.Printf("%s [ERR] %s\n", id, err)
2019-02-04 17:14:46 +00:00
} else if nTeam.JTeam > 0 {
if !canJoinTeam {
log.Printf("%s [ERR] Join team received, whereas disabled. Skipped.\n", id)
2019-02-04 17:14:46 +00:00
} else if len(nTeam.Members) != 1 {
log.Printf("%s [ERR] Join team received, with incorrect member length: %d. Skipped.\n", id, len(nTeam.Members))
2019-02-04 17:14:46 +00:00
} else if team, err := fic.GetTeam(nTeam.JTeam); err != nil {
log.Printf("%s [ERR] Unable to join registered team %d: %s\n", id, nTeam.JTeam, err)
2019-02-04 17:14:46 +00:00
} else {
registrationProcess(id, team, nTeam.Members, team_id)
2019-02-04 17:14:46 +00:00
if err := os.Remove(pathname); err != nil {
log.Printf("%s [WRN] %s\n", id, err)
2019-02-04 17:14:46 +00:00
}
}
} else if denyTeamCreation {
log.Printf("%s [ERR] Registration received, whereas team creation denied. Skipped.\n", id)
2017-12-21 21:18:18 +00:00
} else if validTeamName(nTeam.TeamName) {
if team, err := fic.CreateTeam(nTeam.TeamName, fic.HSL{H: rand.Float64(), L: 1, S: 0.5}.ToRGB(), ""); err != nil {
log.Printf("%s [ERR] Unable to register new team %s: %s\n", id, nTeam.TeamName, err)
2016-03-06 17:59:33 +00:00
} else {
registrationProcess(id, team, nTeam.Members, team_id)
2017-12-21 21:18:18 +00:00
if err := os.Remove(pathname); err != nil {
log.Printf("%s [WRN] %s\n", id, err)
2017-12-21 21:18:18 +00:00
}
if _, err := fic.NewEvent(fmt.Sprintf("Souhaitons bonne chance à l'équipe <strong>%s</strong> qui vient de nous rejoindre&#160;!", html.EscapeString(team.Name)), "info"); err != nil {
log.Printf("%s [WRN] Unable to create event: %s\n", id, err)
2017-12-21 21:18:18 +00:00
}
appendGenQueue(fic.GenStruct{Id: id, Type: fic.GenEvents})
2016-03-06 17:59:33 +00:00
}
}
}