If you need to know the actual message type (rather than just what color the message was printed in) you will need to either (a) use chat filters or (b) hook ChatFrame_MessageEventHandler. Both suffer from the same problem, in that both will get you every single chat message, whether or not it's set to show in a specific frame, or any frame, so you'll have to figure that out yourself.
Code:
local eventToCategory = {}
local function SaveLastMessageType(frame, event, ...)
local category = eventToCategory[event]
-- This maps eg. "CHAT_MSG_PARTY_LEADER" to "PARTY"
-- which you can look up in ChatTypeInfo etc.
-- Insert code here to figure out whether this frame
-- should display this category, and return out if not.
frame.lastMessageType = category
-- Now you can reference this value in your other scripts.
end
for category, events in pairs(CHAT_CATEGORY_LIST) do
for i = 1, #events do
local event = "CHAT_MSG_" .. events[i]
eventToCategory[event] = category
ChatFrame_RegisterMessageEventFilter(event, SaveLastMessageType)
end
end