Exemple #1
0
func ProcessCreateGuild(res http.ResponseWriter, req *http.Request, base *BaseController) {
	owner := req.PostFormValue("owner")
	name := req.PostFormValue("name")
	if len(name) > 20 || len(name) < 5 {
		base.Session.SetFlash("Guild name must be between 5 - 20 characters", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z]+$`)
	if err != nil {
		http.Error(res, "Error while compiling regexp at process create guild", 500)
		return
	}
	if !rgxp.MatchString(name) {
		base.Session.SetFlash("Guild name can only contain A - z characters", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	if !models.CheckCharacterNameOfAccount(owner, base.Account.Id) {
		base.Session.SetFlash("Wrong character selected", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	owner_info := models.GetCharacterByName(owner)
	if models.CheckGuildName(name) {
		base.Session.SetFlash("Guild name already in use", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	if models.CheckGuildOwner(owner_info.Id) {
		base.Session.SetFlash("Character already owns a guild", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	if models.CheckCharacterInGuild(owner_info.Id) {
		base.Session.SetFlash("Character already in a guild", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	logo, err := ioutil.ReadFile(config.Parser.Style.Template + "/public/images/logo.gif")
	if err != nil {
		http.Error(res, "Error while creating reading base logo "+err.Error(), 500)
		return
	}
	guild_logo, err := os.Create(config.Parser.Style.Template + "/public/guilds/" + name + ".gif")
	if err != nil {
		http.Error(res, "Error while creating your guild logo "+err.Error(), 500)
		return
	}
	defer guild_logo.Close()
	guild_logo.Write(logo)
	err = models.CreateGuild(name, owner_info.Id)
	if err != nil {
		http.Error(res, "Error while creating your guild "+err.Error(), 500)
		return
	}
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Exemple #2
0
func ProcessRanksUpdate(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	level3 := req.PostFormValue("level3")
	level2 := req.PostFormValue("level2")
	level1 := req.PostFormValue("level1")
	if len(level1) > 15 || len(level2) > 15 || len(level3) > 15 {
		base.Session.SetFlash("Ranks cant have more than 15 characters", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z-0-9]+$`)
	if err != nil {
		http.Error(res, "Error compiling RegExp at guild ranks", 500)
		return
	}
	if !rgxp.MatchString(level3) || !rgxp.MatchString(level2) || !rgxp.MatchString(level1) {
		base.Session.SetFlash("Ranks cant contain invalid characters only A-z and 0-9 are allowed", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	err = models.UpdateGuildRanks(guild_info.Id, level3, level2, level1)
	if err != nil {
		http.Error(res, "Error while updating guild ranks", 500)
		return
	}
	base.Session.SetFlash("Guild ranks updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Exemple #3
0
func ProcessMotdUpdate(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	motd := req.PostFormValue("motd")
	if len(motd) > 255 {
		base.Session.SetFlash("The MOTD cant be longer than 255 characters", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z 0-9]+$`)
	if err != nil {
		http.Error(res, "Error compiling regexp", 500)
		return
	}
	if !rgxp.MatchString(motd) {
		base.Session.SetFlash("The MOTD contains invalid characters you can only use A-Z or 0-9", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	err = models.UpdateGuildMotd(guild_info.Id, motd)
	if err != nil {
		http.Error(res, "Error while updating guild MOTD", 500)
		return
	}
	base.Session.SetFlash("Guild MOTD updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Exemple #4
0
func ProcessGuildInvite(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	player_name := req.PostFormValue("player")
	if !models.CheckCharacterName(player_name) {
		base.Session.SetFlash("Player not found", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	player_info := models.GetCharacterByName(player_name)
	if models.CheckCharacterInGuild(player_info.Id) {
		base.Session.SetFlash("Player is already in a guild", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	err = models.CreateGuildInvite(guild_info.Id, player_info.Id)
	if err != nil {
		log.Println(err)
		http.Error(res, "Error while inviting player", 301)
		return
	}
	base.Session.SetFlash("Player invited to the guild", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
	return
}
Exemple #5
0
func ShowGuild(res http.ResponseWriter, req *http.Request, base *BaseController) {
	// TODO: guild invitations
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while getting guild name", 500)
		return
	}
	if !models.CheckGuildName(name) {
		base.Session.SetFlash("Guild doesnt exists", "error")
		http.Redirect(res, req, "/guilds/list", 301)
		return
	}
	csrf_token := GenerateToken(12)
	base.Session.SetValue("csrf", csrf_token)
	guild_info, err := models.GetGuildByName(name)
	if err != nil {
		http.Error(res, "Error while getting guild information", 500)
		return
	}
	character_list, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting character list", 500)
		return
	}
	owner := true
	if !checkAccountOwner(character_list, guild_info) {
		owner = false
	}
	invited := true
	invited_account_characters := getInvitedPlayers(character_list, guild_info)
	if len(invited_account_characters) == 0 {
		invited = false
	}
	r := &GuildViewResponse{"guilds", csrf_token, guild_info, base.Session.GetFlashes("error"), base.Session.GetFlashes("success"), owner, invited, invited_account_characters}
	views.Parser.ExecuteTemplate(res, "view_guild.html", r)
}
Exemple #6
0
func ProcessLogoUpload(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	file_logo, _, err := req.FormFile("logo")
	if err != nil {
		http.Error(res, "Error while reading logo image", 500)
		return
	}
	defer file_logo.Close()
	logo_image, format, err := image.Decode(file_logo)
	if !checkValidImageFormat(format) {
		base.Session.SetFlash("Guild logo image format is not allowed", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	if err != nil {
		http.Error(res, "Error while decoding logo image", 500)
		return
	}
	logo_out, err := os.Create(config.Parser.Style.Template + "/public/guilds/" + guild_info.Name + ".gif")
	if err != nil {
		http.Error(res, "Error while creating logo stdout", 500)
		return
	}
	defer logo_out.Close()
	resized_logo := resize.Resize(80, 80, logo_image, resize.Lanczos3)
	err = png.Encode(logo_out, resized_logo)
	if err != nil {
		http.Error(res, "Error while encoding logo image", 500)
		return
	}
	base.Session.SetFlash("Guild logo updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}