Ejemplo n.º 1
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)
}
Ejemplo n.º 2
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)
}
Ejemplo n.º 3
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
}
Ejemplo n.º 4
0
func ProcessGuildInviteJoin(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
	}
	player, err := url.QueryUnescape(mux.Vars(req)["player"])
	if err != nil {
		http.Error(res, "Error while getting player name", 500)
		return
	}
	player_info := models.GetCharacterByName(player)
	if player_info.Name == "" {
		http.Error(res, "Unknown player", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if guild_info.Name == "" {
		http.Error(res, "Unknown guild", 500)
		return
	}
	if !models.CheckCharacterNameOfAccount(player_info.Name, base.Account.Id) {
		http.Error(res, "Unkown character", 500)
		return
	}
	if !models.CheckGuildInvitation(player_info.Id, guild_info.Id) {
		http.Error(res, "Unkown guild invitation", 500)
		return
	}
	err, _ = models.DeleteGuildInvite(player_info.Id, guild_info.Id)
	if err != nil {
		http.Error(res, "Error while deleting guild invitation", 500)
		return
	}
	err = models.JoinGuild(player_info.Id, guild_info.Id)
	if err != nil {
		http.Error(res, "Error while joining guild: "+err.Error(), 500)
		return
	}
	base.Session.SetFlash("You joined the guild successfully", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Ejemplo n.º 5
0
func ProcessGuildInviteRemove(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
	}
	player, err := url.QueryUnescape(mux.Vars(req)["player"])
	if err != nil {
		http.Error(res, "Error while getting player name", 500)
		return
	}
	player_info := models.GetCharacterByName(player)
	if player_info.Name == "" {
		http.Error(res, "Unknown player", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if guild_info.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
	}
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	err, _ = models.DeleteGuildInvite(player_info.Id, guild_info.Id)
	if err != nil {
		http.Error(res, "Error while deleting guild invitation", 500)
		return
	}
	base.Session.SetFlash("Removed guild invitation successfully!", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Ejemplo n.º 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)
}