示例#1
0
// PostJoinHandle implements the handle for the join document (POST).
func PostJoinHandle(newConn net.Conn, roomName, httpPayload string, rooms *config.CherryRooms, preprocessor *html.Preprocessor) {
	//  INFO(Santiago): Here, we need firstly parse the posted fields, check for "nickclash", if this is the case
	//                  flush the page informing it. Otherwise we add the user basic info and flush the room skeleton
	//                  [TOP/BODY/BANNER]. Then we finally close the connection.
	var userData map[string]string
	var replyBuffer []byte
	userData = rawhttp.GetFieldsFromPost(httpPayload)
	if _, posted := userData["user"]; !posted {
		newConn.Close()
		return
	}
	if _, posted := userData["color"]; !posted {
		newConn.Close()
		return
	}
	preprocessor.SetDataValue("{{.nickname}}", userData["user"])
	preprocessor.SetDataValue("{{.session-id}}", "0")
	if rooms.HasUser(roomName, userData["user"]) || userData["user"] == rooms.GetAllUsersAlias(roomName) ||
		strings.Contains(userData["user"], "<") || strings.Contains(userData["user"], ">") ||
		strings.Contains(userData["user"], "&lt") || strings.Contains(userData["user"], "&gt") {
		replyBuffer = rawhttp.MakeReplyBuffer(preprocessor.ExpandData(roomName, rooms.GetNickclashTemplate(roomName)), 200, true)
	} else {
		rooms.AddUser(roomName, userData["user"], userData["color"], true)
		preprocessor.SetDataValue("{{.session-id}}", rooms.GetSessionID(userData["user"], roomName))
		replyBuffer = rawhttp.MakeReplyBuffer(preprocessor.ExpandData(roomName, rooms.GetSkeletonTemplate(roomName)), 200, true)
		rooms.EnqueueMessage(roomName, userData["user"], "", "", "", rooms.GetJoinMessage(roomName), "")
	}
	newConn.Write(replyBuffer)
	newConn.Close()
}
示例#2
0
// RoomMessagePlexer performs all message delivering stuff.
func RoomMessagePlexer(roomName string, rooms *config.CherryRooms) {
	preprocessor := html.NewHTMLPreprocessor(rooms)
	var allUsers = rooms.GetAllUsersAlias(roomName)
	for {
		runtime.Gosched()
		currMessage := rooms.GetNextMessage(roomName)
		if len(currMessage.Say) == 0 && len(currMessage.Image) == 0 /*&& len(currMessage.Sound) == 0*/ {
			continue
		}
		var actionTemplate string
		if rooms.HasAction(roomName, currMessage.Action) {
			actionTemplate = rooms.GetRoomActionTemplate(roomName, currMessage.Action)
		}
		if len(actionTemplate) == 0 {
			actionTemplate = "<p>({{.hour}}:{{.minute}}:{{.second}}) <b>{{.message-colored-user}}</b>: {{.message-says}}" //  INFO(Santiago): A very basic action template.
		}
		message := preprocessor.ExpandData(roomName, actionTemplate)
		if currMessage.Priv != "1" {
			rooms.AddPublicMessage(roomName, message)
		}
		preprocessor.SetDataValue("{{.current-formatted-message}}", message)
		messageHighlighted := preprocessor.ExpandData(roomName, rooms.GetHighlightTemplate(roomName))
		preprocessor.UnsetDataValue("{{.current-formatted-message}}")
		users := rooms.GetRoomUsers(roomName)
		for _, user := range users {
			if currMessage.Priv == "1" &&
				user != currMessage.From &&
				user != currMessage.To &&
				currMessage.To != allUsers {
				continue
			}
			if rooms.IsIgnored(user, currMessage.From, roomName) {
				continue
			}
			var messageBuffer []byte
			if user == currMessage.From ||
				user == currMessage.To {
				messageBuffer = []byte(messageHighlighted)
			} else {
				messageBuffer = []byte(message)
			}
			var conn net.Conn
			conn = rooms.GetUserConnection(roomName, user)
			if conn == nil {
				continue
			}
			_, e := conn.Write(messageBuffer)
			if e != nil {
				rooms.EnqueueMessage(roomName, user, "", "", "", rooms.GetExitMessage(roomName), "")
				rooms.RemoveUser(roomName, user)
			}
		}
		rooms.DequeueMessage(roomName)
	}
}
示例#3
0
// GetExitHandle implements the handle for the exit document (GET).
func GetExitHandle(newConn net.Conn, roomName, httpPayload string, rooms *config.CherryRooms, preprocessor *html.Preprocessor) {
	var userData map[string]string
	var replyBuffer []byte
	userData = rawhttp.GetFieldsFromGet(httpPayload)
	if !rooms.IsValidUserRequest(roomName, userData["user"], userData["id"], newConn) {
		replyBuffer = rawhttp.MakeReplyBuffer(html.GetBadAssErrorData(), 404, true)
	} else {
		preprocessor.SetDataValue("{{.nickname}}", userData["user"])
		preprocessor.SetDataValue("{{.session-id}}", userData["id"])
		replyBuffer = rawhttp.MakeReplyBuffer(preprocessor.ExpandData(roomName, rooms.GetExitTemplate(roomName)), 200, true)
	}
	rooms.EnqueueMessage(roomName, userData["user"], "", "", "", rooms.GetExitMessage(roomName), "")
	newConn.Write(replyBuffer)
	rooms.RemoveUser(roomName, userData["user"])
	newConn.Close()
}
示例#4
0
// PostBannerHandle implements the handle for the banner document (POST).
func PostBannerHandle(newConn net.Conn, roomName, httpPayload string, rooms *config.CherryRooms, preprocessor *html.Preprocessor) {
	var userData map[string]string
	var replyBuffer []byte
	var invalidRequest = false
	userData = rawhttp.GetFieldsFromPost(httpPayload)
	if _, has := userData["user"]; !has {
		invalidRequest = true
	} else if _, has := userData["id"]; !has {
		invalidRequest = true
	} else if _, has := userData["action"]; !has {
		invalidRequest = true
	} else if _, has := userData["whoto"]; !has {
		invalidRequest = true
	} else if _, has := userData["image"]; !has {
		invalidRequest = true
	} else if _, has := userData["says"]; !has {
		invalidRequest = true
	}
	var restoreBanner = true
	if invalidRequest || !rooms.IsValidUserRequest(roomName, userData["user"], userData["id"], newConn) {
		replyBuffer = rawhttp.MakeReplyBuffer(html.GetBadAssErrorData(), 404, true)
	} else if userData["action"] == rooms.GetIgnoreAction(roomName) {
		if userData["user"] != userData["whoto"] && !rooms.IsIgnored(userData["user"], userData["whoto"], roomName) {
			rooms.AddToIgnoreList(userData["user"], userData["whoto"], roomName)
			rooms.EnqueueMessage(roomName, userData["user"], "", "", "", rooms.GetOnIgnoreMessage(roomName)+userData["whoto"], "1")
			restoreBanner = false
		}
	} else if userData["action"] == rooms.GetDeIgnoreAction(roomName) {
		if rooms.IsIgnored(userData["user"], userData["whoto"], roomName) {
			rooms.DelFromIgnoreList(userData["user"], userData["whoto"], roomName)
			rooms.EnqueueMessage(roomName, userData["user"], "", "", "", rooms.GetOnDeIgnoreMessage(roomName)+userData["whoto"], "1")
			restoreBanner = false
		}
	} else {
		var somethingToSay = (len(userData["says"]) > 0 || len(userData["image"]) > 0 || len(userData["sound"]) > 0)
		if somethingToSay {
			//  INFO(Santiago): Any further antiflood control would go from here.
			rooms.EnqueueMessage(roomName, userData["user"], userData["whoto"], userData["action"], userData["image"], userData["says"], userData["priv"])
		}
	}
	preprocessor.SetDataValue("{{.nickname}}", userData["user"])
	preprocessor.SetDataValue("{{.session-id}}", userData["id"])
	if userData["priv"] == "1" {
		preprocessor.SetDataValue("{{.priv}}", "checked")
	}
	tempBanner := preprocessor.ExpandData(roomName, rooms.GetBannerTemplate(roomName))
	if restoreBanner {
		tempBanner = strings.Replace(tempBanner,
			"<option value = \""+userData["whoto"]+"\">",
			"<option value = \""+userData["whoto"]+"\" selected>", -1)
		tempBanner = strings.Replace(tempBanner,
			"<option value = \""+userData["action"]+"\">",
			"<option value = \""+userData["action"]+"\" selected>", -1)
	}
	replyBuffer = rawhttp.MakeReplyBuffer(tempBanner, 200, true)
	newConn.Write(replyBuffer)
	newConn.Close()
}