PageRenderTime 122ms CodeModel.GetById 60ms app.highlight 4ms RepoModel.GetById 57ms app.codeStats 0ms

/Tukui/modules/chat/copyurl.lua

http://github.com/Asphyxia/Tukui
Lua | 66 lines | 59 code | 6 blank | 1 comment | 12 complexity | dc8092274c51a12cda379963a061bc59 MD5 | raw file
 1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
 2if C["chat"].enable ~= true then return end
 3
 4local SetItemRef_orig = SetItemRef
 5function ReURL_SetItemRef(link, text, button, chatFrame)
 6	if (strsub(link, 1, 3) == "url") then
 7		local ChatFrameEditBox = ChatEdit_ChooseBoxForSend()
 8		local url = strsub(link, 5);
 9		if (not ChatFrameEditBox:IsShown()) then
10			ChatEdit_ActivateChat(ChatFrameEditBox)
11		end
12		ChatFrameEditBox:Insert(url)
13		ChatFrameEditBox:HighlightText()
14
15	else
16		SetItemRef_orig(link, text, button, chatFrame)
17	end
18end
19SetItemRef = ReURL_SetItemRef
20
21function ReURL_AddLinkSyntax(chatstring)
22	if (type(chatstring) == "string") then
23		local extraspace;
24		if (not strfind(chatstring, "^ ")) then
25			extraspace = true;
26			chatstring = " "..chatstring;
27		end
28		chatstring = gsub (chatstring, " www%.([_A-Za-z0-9-]+)%.(%S+)%s?", ReURL_Link("www.%1.%2"))
29		chatstring = gsub (chatstring, " (%a+)://(%S+)%s?", ReURL_Link("%1://%2"))
30		chatstring = gsub (chatstring, " ([_A-Za-z0-9-%.]+)@([_A-Za-z0-9-]+)(%.+)([_A-Za-z0-9-%.]+)%s?", ReURL_Link("%1@%2%3%4"))
31		chatstring = gsub (chatstring, " (%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?):(%d%d?%d?%d?%d?)%s?", ReURL_Link("%1.%2.%3.%4:%5"))
32		chatstring = gsub (chatstring, " (%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%s?", ReURL_Link("%1.%2.%3.%4"))
33		if (extraspace) then
34			chatstring = strsub(chatstring, 2);
35		end
36	end
37	return chatstring
38end
39
40REURL_COLOR = "16FF5D"
41ReURL_Brackets = false
42ReUR_CustomColor = true
43
44function ReURL_Link(url)
45	if (ReUR_CustomColor) then
46		if (ReURL_Brackets) then
47			url = " |cff"..REURL_COLOR.."|Hurl:"..url.."|h["..url.."]|h|r "
48		else
49			url = " |cff"..REURL_COLOR.."|Hurl:"..url.."|h"..url.."|h|r "
50		end
51	else
52		if (ReURL_Brackets) then
53			url = " |Hurl:"..url.."|h["..url.."]|h "
54		else
55			url = " |Hurl:"..url.."|h"..url.."|h "
56		end
57	end
58	return url
59end
60
61--Hook all the AddMessage funcs
62for i=1, NUM_CHAT_WINDOWS do
63	local frame = getglobal("ChatFrame"..i)
64	local addmessage = frame.AddMessage
65	frame.AddMessage = function(self, text, ...) addmessage(self, ReURL_AddLinkSyntax(text), ...) end
66end