2016-03-06 17:57:08 +00:00
package main
import (
2019-11-25 15:16:27 +00:00
"encoding/base64"
"encoding/binary"
2016-03-06 17:57:08 +00:00
"encoding/json"
"fmt"
2020-01-30 17:59:45 +00:00
"html"
2016-03-06 17:57:08 +00:00
"io/ioutil"
2019-07-11 17:52:13 +00:00
"log"
2019-11-25 15:16:27 +00:00
"math/rand"
2016-03-06 17:57:08 +00:00
"os"
"regexp"
"srs.epita.fr/fic-server/libfic"
)
func validTeamName ( name string ) bool {
match , err := regexp . MatchString ( "^[A-Za-z0-9 àéèêëîïôùûü_-]{1,32}$" , name )
return err == nil && match
}
2021-11-22 14:35:07 +00:00
func treatRename ( pathname string , team * fic . Team ) {
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 renameTeam receive" , pathname )
2016-03-06 17:57:08 +00:00
var keys map [ string ] string
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 )
2016-03-06 17:57:08 +00:00
} else if err := json . Unmarshal ( cnt_raw , & keys ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2016-03-06 17:57:08 +00:00
} else if validTeamName ( keys [ "newName" ] ) {
team . Name = keys [ "newName" ]
if _ , err := team . Update ( ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [WRN] Unable to change team name: %s\n" , id , err )
2016-03-06 17:57:08 +00:00
}
2023-07-10 07:17:02 +00:00
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenTeam , TeamId : team . Id } )
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 )
2016-03-06 17:57:08 +00:00
}
2023-07-10 07:17:02 +00:00
appendGenQueue ( fic . GenStruct { Id : id , Type : fic . GenEvents } )
2016-03-06 17:57:08 +00:00
if err := os . Remove ( pathname ) ; err != nil {
2019-11-25 15:16:27 +00:00
log . Printf ( "%s [ERR] %s\n" , id , err )
2016-03-06 17:57:08 +00:00
}
}
}