Creating your own chat types

If your AddOn is outputting a lot of text in the chat windows, it's a good idea to let the player control which windows it will show in, and probably also the color of the text. You can either implement configuration for this yourself, or you can just hook into the standard chat setting menus, where people are used to go to change these settings anyway!
== Hooks for creating custom chat types ==
Here's how to create your own chat types, and make it configurable via the standard chat setting menus:
 local function FireChatEvent(evt, a1,a2,a3,a4,a5,a6,a7,a8,a9)
 
 local bIsChat = (strsub(evt, 1,9)=="CHAT_MSG_");
 local chattype = strsub(evt, 10); -- might just be garbage if bIsChat==false, but in that case we don't use it
 
 local b1,b2,b3,b4,b5,b6,b7,b8,b9 = arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9;
 local oldevent = event;
 local oldthis = this;
 
 for i=1,NUM_CHAT_WINDOWS do
 if(not bIsChat or MyAddOn_UserVars.Chat[chattype][i]) then
 this = getglobal("ChatFrame"..i);
 event=evt;
 arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9 = a1,a2,a3,a4,a5,a6,a7,a8,a9;
 ChatFrame_OnEvent(evt);
 end
 end
 
 arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9 = b1,b2,b3,b4,b5,b6,b7,b8,b9;
 event=oldevent;
 this=oldthis;		
 end
 
 local ORIG_GetChatWindowMessages = GetChatWindowMessages;
 function GetChatWindowMessages(n)
 ret = { ORIG_GetChatWindowMessages(n) };
 for chattype,settings in pairs(MyAddOn_UserVars.Chat) do
 if(settings[n]) then
 tinsert(ret, chattype);
 end
 end
 return unpack(ret);
 end
 
 
 local ORIG_AddChatWindowMessages = AddChatWindowMessages;
 function AddChatWindowMessages(n, chattype)
 if(MyAddOn_UserVars.Chat[chattype]) then
 MyAddOn_UserVars.Chat[chattype][n]=true;
 else
 ORIG_AddChatWindowMessages(n,chattype);
 end
 end
 
 
 local ORIG_RemoveChatWindowMessages = RemoveChatWindowMessages;
 function RemoveChatWindowMessages(n, chattype)
 if(MyAddOn_UserVars.Chat[chattype]) then
 MyAddOn_UserVars.Chat[chattype][n]=false;
 else
 ORIG_RemoveChatWindowMessages(n,chattype);
 end
 end
 
 
 local ORIG_ChangeChatColor = ChangeChatColor;
 function ChangeChatColor(chattype, r,g,b)
 if(MyAddOn_UserVars.Chat[chattype]) then
 MyAddOn_UserVars.Chat[chattype].r=r;
 MyAddOn_UserVars.Chat[chattype].g=g;
 MyAddOn_UserVars.Chat[chattype].b=b;
 FireChatEvent("UPDATE_CHAT_COLOR", chattype, r,g,b);
 else
 ORIG_ChangeChatColor(chattype,r,g,b);
 end
 end
 
 
 function MyAddOn_Msg(chattype, txt)
 assert(MyAddOn_UserVars.Chat[chattype]);
 FireChatEvent("CHAT_MSG_"..chattype, txt, "", "", "", "", "", "", "", "");
 end
The reason we're hooking all the functions to get and set chattype settings is because the game client refuses to store chat settings for chat types it does not recognize.
== Adding logic ==
The above code assumes that there is a MyAddOn_UserVars.Chat table that contains your chat configuration settings. It would normally be a saved variable, and initialized on VARIABLES_LOADED. We also need to insert a bit more information in ChatFrame's tables:
 function MyAddOn_OnEvent()
 if(event=="VARIABLES_LOADED") then
 if(not MyAddOn_UserVars) then MyAddOn_UserVars = {}; end
 if(not MyAddOn_UserVars.Chat) then
 MyAddOn_UserVars.Chat={
 ["MYADDONMSG"] = { [1]=true; r=1; g=0.7; b=0.3; }
 }
 end
 
 
 -- Insert our chat types in the ChatFrame system
 CHAT_MSG_MYADDONMSG="MyAddOn"; -- Displayed in menu
 CHAT_MYADDONMSG_GET = ""; -- ChatFrame needs it. Never mind it.
 
 for chattype,info in MyAddOn_UserVars.Chat do
 ChatTypeGroup[chattype] = {
 "CHAT_MSG_"..chattype
 };
 ChatTypeInfo[chattype] = info;
 tinsert(OtherMenuChatTypeGroups, chattype);
 end
 end
 end
This defines the chat type "MYADDONMSG" (which gets delivered as "CHAT_MSG_MYADDONMSG" events). It defaults to being shown only in window 1 with a light orange color. In the chat settings menu, it is listed as "MyAddOn".
== Using the result ==
To send output from your AddOn, all you do now is:
* MyAddOn_Msg("MYADDONMSG", "And then any text you want displayed!");
If you need more than one chat type, adding more is easy:
# Create a new MyAddOn_UserVars.Chat["type"] entry
# Add a CHAT_MSG_type = "displayname"
# Add a CHAT_type_GET = "";
== See Also ==
* The small Satellite function library, which contains this functionality.