2016-03-06 17:59:33 +00:00
package main
import (
2019-11-25 15:16:27 +00:00
"encoding/base64"
"encoding/binary"
2016-03-06 17:59:33 +00:00
"encoding/json"
2017-12-21 21:18:18 +00:00
"fmt"
2020-01-30 17:59:45 +00:00
"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
2020-05-16 01:51:36 +00:00
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 ) {
2023-11-22 08:55:09 +00:00
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
2023-11-22 08:55:09 +00:00
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
2023-07-14 14:49:57 +00:00
if err := os . MkdirAll ( path . Join ( TeamsDir , teamDirPath ) , 0751 ) ; err != nil {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
log . Println ( id , "[ERR]" , err )
2019-02-04 17:14:46 +00:00
}
2023-07-10 07:17:02 +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
}
2018-01-21 13:07:44 +00:00
func treatRegistration ( pathname string , team_id string ) {
2019-11-25 15:16:27 +00:00
// 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 {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2019-02-04 17:14:46 +00:00
} else if nTeam . JTeam > 0 {
if ! canJoinTeam {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
registrationProcess ( id , team , nTeam . Members , team_id )
2019-02-04 17:14:46 +00:00
if err := os . Remove ( pathname ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] %s\n" , id , err )
2019-02-04 17:14:46 +00:00
}
}
2020-05-16 01:51:36 +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 ) {
2021-09-04 18:51:30 +00:00
if team , err := fic . CreateTeam ( nTeam . TeamName , fic . HSL { H : rand . Float64 ( ) , L : 1 , S : 0.5 } . ToRGB ( ) , "" ) ; err != nil {
2019-11-25 15:16:27 +00:00
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 {
2019-11-25 15:16:27 +00:00
registrationProcess ( id , team , nTeam . Members , team_id )
2017-12-21 21:18:18 +00:00
if err := os . Remove ( pathname ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] %s\n" , id , err )
2017-12-21 21:18:18 +00:00
}
2020-01-30 17:59:45 +00:00
if _ , err := fic . NewEvent ( fmt . Sprintf ( "Souhaitons bonne chance à l'équipe <strong>%s</strong> qui vient de nous rejoindre !" , html . EscapeString ( team . Name ) ) , "info" ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] Unable to create event: %s\n" , id , err )
2017-12-21 21:18:18 +00:00
}
2023-07-10 07:17:02 +00:00
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenEvents } )
2016-03-06 17:59:33 +00:00
}
}
}