Example #1
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)
	}
}
Example #2
0
func processNewConnection(newConn net.Conn, roomName string, rooms *config.CherryRooms) {
	buf := make([]byte, 4096)
	bufLen, err := newConn.Read(buf)
	if err == nil {
		preprocessor := html.NewHTMLPreprocessor(rooms)
		httpPayload := string(buf[:bufLen])
		var trap reqtraps.RequestTrap
		trap = reqtraps.GetRequestTrap(httpPayload)
		trap().Handle(newConn, roomName, httpPayload, rooms, preprocessor)
	} else {
		newConn.Close()
	}
}
Example #3
0
func PreprocessorBasicTest(t *testing.T) {
	preprocessor := html.NewHTMLPreprocessor(nil)
	if preprocessor.ExpandData("land-of-competition", "{{.FoD}} Zzz...") != "{{.FoD}} Zzz..." {
		t.Fail()
	}
	var cherry_rooms *config.CherryRooms
	cwd, _ := os.Getwd()
	os.Chdir("../../sample")
	var error *parser.CherryFileError
	cherry_rooms, error = parser.ParseCherryFile("conf/sample.cherry")
	os.Chdir(cwd)
	if error != nil {
		t.Fail()
	}
	preprocessor.Init(cherry_rooms)
	preprocessor.SetDataValue("{{.foo}}", "bar")
	preprocessor.SetDataValue("{{.bar}}", "foo")
	if preprocessor.ExpandData("aliens-on-earth", "{{.foo}}{{.bar}}") != "barfoo" {
		t.Fail()
	}
	if preprocessor.ExpandData("aliens-on-earth", "{{.greeting-message}}") != "Take meeeeee to your leader!!!" {
		t.Fail()
	}
}